sanamare/src/app/(shop)/konto/bestellung/[number]/page.tsx
2026-10-19 20:25:40 +02:00

80 lines
4.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* eslint-disable @next/next/no-img-element */
import Link from "next/link";
import { notFound, redirect } from "next/navigation";
import { db } from "@/lib/db";
import { getCurrentUser } from "@/lib/auth";
import { formatPrice } from "@/lib/money";
import { PAYMENT_STATUS, dayFmt } from "@/lib/status";
import type { AddressData } from "@/lib/checkout";
import { AddressLines } from "@/components/shop/AddressLines";
import { DhlTracking } from "@/components/shop/DhlTracking";
import { AccountShell } from "@/components/shop/account/AccountShell";
import { StatusBadge } from "@/components/shop/account/OrderCard";
import { fetchTracking, getDhlConfig, syncDelivered } from "@/lib/dhl";
export default async function OrderDetail({ params }: { params: Promise<{ number: string }> }) {
const { number } = await params;
const user = await getCurrentUser();
if (!user) redirect(`/konto/anmelden?weiter=/konto/bestellung/${encodeURIComponent(number)}`);
const order = await db.order.findUnique({ where: { number }, include: { items: { include: { product: { select: { images: true, slug: true, active: true } } } }, invoices: { orderBy: { createdAt: "asc" } } } });
if (!order || order.userId !== user.id) notFound();
const dhl = await getDhlConfig();
const tracking = order.trackingNumber ? await fetchTracking(dhl, order.trackingNumber) : null;
if (await syncDelivered(dhl, order, tracking)) order.status = "COMPLETED";
return (
<AccountShell
user={user}
title={`Bestellung ${order.number}`}
lead={<>vom {dayFmt.format(order.createdAt)}</>}
>
<div className="sm-account__badges">
<StatusBadge status={order.status} />
<span className={`sm-badge sm-status sm-status--${PAYMENT_STATUS[order.paymentStatus].tone}`}><span className="sm-dot" />{PAYMENT_STATUS[order.paymentStatus].label}</span>
</div>
{order.trackingNumber && <DhlTracking trackingNumber={order.trackingNumber} tracking={tracking} />}
<section className="sm-box sm-account__panel">
<h2 className="display-sm">Artikel</h2>
<ul className="sm-orderitems">
{order.items.map((i) => {
const href = i.product?.active && i.product.slug ? `/produkt/${i.product.slug}` : null;
const img = i.product?.images[0];
return (
<li key={i.id}>
<span className="sm-ordercard__thumb">{img ? <img src={img} alt="" loading="lazy" /> : <span>{i.name.charAt(0)}</span>}</span>
<div>
{href ? <Link href={href}>{i.name}</Link> : <span>{i.name}</span>}
<small>{i.variantName ? `${i.variantName} · ` : ""}{i.quantity} × {formatPrice(i.unitPrice)}</small>
</div>
<strong>{formatPrice(i.total)}</strong>
</li>
);
})}
</ul>
<dl className="sm-ordersum">
<div><dt>Zwischensumme</dt><dd>{formatPrice(order.subtotal)}</dd></div>
<div><dt>Versand</dt><dd>{order.shippingCost === 0 ? "Kostenlos" : formatPrice(order.shippingCost)}</dd></div>
{order.discount > 0 && <div><dt>Gutschein</dt><dd>−{formatPrice(order.discount)}</dd></div>}
<div className="sm-ordersum__total"><dt>Gesamt</dt><dd>{formatPrice(order.total)}</dd></div>
</dl>
</section>
<div className="sm-review" style={{ marginTop: "var(--space-5)" }}>
<div className="sm-review__box"><h3>Lieferadresse</h3><AddressLines a={order.shippingAddress as AddressData} /></div>
<div className="sm-review__box"><h3>Rechnungsadresse</h3>{order.billingAddress ? <AddressLines a={order.billingAddress as AddressData} /> : <p className="body">Wie Lieferadresse</p>}</div>
</div>
{order.invoices.length > 0 && (
<section className="sm-box sm-account__panel" style={{ marginTop: "var(--space-5)" }}>
<h2 className="display-sm">Rechnungen</h2>
<ul className="sm-invoices">
{order.invoices.map((inv) => (
<li key={inv.id}>
<a href={`/rechnung/${inv.id}`} target="_blank" rel="noopener">{inv.kind === "credit" ? "Stornorechnung" : "Rechnung"} {inv.number}</a>
<small>{dayFmt.format(inv.createdAt)} · {formatPrice(inv.total)}</small>
</li>
))}
</ul>
</section>
)}
<p className="body-sm" style={{ marginTop: "var(--space-6)" }}><Link href="/konto/bestellungen">← Alle Bestellungen</Link></p>
</AccountShell>
);
}