fix: harden delivery accounting and history
This commit is contained in:
1 parent
8871e4794a
commit
510fde7afa
13 files changed
+728
-88
No files matched your search
@@ -20,6 +20,7 @@ test("renders every terminal attempt with its immutable usage snapshot", () => {
|
||||
reference: "Ausgabe 8/26",
|
||||
createdAt: new Date("2026-08-09T10:00:00.000Z"),
|
||||
billingStatus: "open",
|
||||
priceCents: 1_500,
|
||||
billableCents: 1_500,
|
||||
attempts: [
|
||||
{
|
||||
@@ -55,6 +56,9 @@ test("renders every terminal attempt with its immutable usage snapshot", () => {
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
hasNext: true,
|
||||
attemptPageSize: 25,
|
||||
attemptCursor: "1786314600000.current",
|
||||
nextAttemptCursor: "1786311000000.next",
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
@@ -65,20 +69,93 @@ test("renders every terminal attempt with its immutable usage snapshot", () => {
|
||||
expect(screen.getByText("Abgeschlossen")).not.toBeNull();
|
||||
expect(screen.getByText("Unterbrochen")).not.toBeNull();
|
||||
expect(screen.getByText("Vor Auslieferung fehlgeschlagen")).not.toBeNull();
|
||||
expect(screen.getByText(/15,00\s*€/)).not.toBeNull();
|
||||
expect(screen.getAllByText(/15,00\s*€/)).toHaveLength(2);
|
||||
expect(screen.getAllByText(/0,00\s*€/)).toHaveLength(2);
|
||||
expect(screen.getByRole("link", { name: /nächste seite/i }).getAttribute("href")).toBe(
|
||||
expect(screen.getByRole("link", { name: /nächste nutzungsseite/i }).getAttribute("href")).toBe(
|
||||
"/redaktion/nutzungen?page=2",
|
||||
);
|
||||
expect(
|
||||
screen.getByRole("link", { name: /neueste auslieferungen/i }).getAttribute("href"),
|
||||
).toBe("/redaktion/nutzungen?page=1");
|
||||
expect(
|
||||
screen.getByRole("link", { name: /ältere auslieferungen/i }).getAttribute("href"),
|
||||
).toBe(
|
||||
"/redaktion/nutzungen?page=1&attemptCursor=1786311000000.next",
|
||||
);
|
||||
});
|
||||
|
||||
test("renders a directional empty state", () => {
|
||||
render(
|
||||
<UsageHistoryContent
|
||||
history={{ items: [], page: 1, pageSize: 20, hasNext: false }}
|
||||
history={{
|
||||
items: [],
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
hasNext: false,
|
||||
attemptPageSize: 25,
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByText(/noch keine nutzungen/i)).not.toBeNull();
|
||||
expect(screen.getByRole("link", { name: /zu den alben/i })).not.toBeNull();
|
||||
});
|
||||
|
||||
test("shows the usage-level price and billing status before any attempt is terminal", () => {
|
||||
render(
|
||||
<UsageHistoryContent
|
||||
history={{
|
||||
items: [
|
||||
{
|
||||
usageId: "usage-open",
|
||||
filename: "noch-im-abruf.jpg",
|
||||
albumName: "Pressebilder",
|
||||
purpose: "Online-Meldung",
|
||||
createdAt: new Date("2026-08-10T10:00:00.000Z"),
|
||||
billingStatus: "open",
|
||||
priceCents: 1_500,
|
||||
billableCents: 1_500,
|
||||
attempts: [],
|
||||
},
|
||||
],
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
hasNext: false,
|
||||
attemptPageSize: 25,
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByText("Abrechnungsstatus")).not.toBeNull();
|
||||
expect(screen.getByText("Offen")).not.toBeNull();
|
||||
expect(screen.getByText(/15,00\s*€/)).not.toBeNull();
|
||||
});
|
||||
|
||||
test("keeps the immutable usage price visible when billing was voided", () => {
|
||||
render(
|
||||
<UsageHistoryContent
|
||||
history={{
|
||||
items: [
|
||||
{
|
||||
usageId: "usage-voided",
|
||||
filename: "fehlgeschlagen.jpg",
|
||||
albumName: "Pressebilder",
|
||||
purpose: "Online-Meldung",
|
||||
createdAt: new Date("2026-08-10T10:00:00.000Z"),
|
||||
billingStatus: "voided",
|
||||
priceCents: 1_500,
|
||||
billableCents: 0,
|
||||
attempts: [],
|
||||
},
|
||||
],
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
hasNext: false,
|
||||
attemptPageSize: 25,
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByText("Storniert")).not.toBeNull();
|
||||
expect(screen.getByText(/15,00\s*€/)).not.toBeNull();
|
||||
});
|
||||
@@ -4,20 +4,28 @@ import { requireEditorial } from "../access";
|
||||
import { UsageHistoryContent } from "./usage-history-content";
|
||||
|
||||
type UsageHistoryPageProps = Readonly<{
|
||||
searchParams: Promise<{ page?: string | string[] }>;
|
||||
searchParams: Promise<{ page?: string | string[]; attemptCursor?: string | string[] }>;
|
||||
}>;
|
||||
|
||||
export default async function UsagesPage({ searchParams }: UsageHistoryPageProps) {
|
||||
const [principal, parameters] = await Promise.all([requireEditorial(), searchParams]);
|
||||
const result = await loadHistory(principal.editorialId, singleValue(parameters.page));
|
||||
const result = await loadHistory(
|
||||
principal.editorialId,
|
||||
singleValue(parameters.page),
|
||||
singleValue(parameters.attemptCursor),
|
||||
);
|
||||
if (result.unavailable) return <HistoryUnavailable />;
|
||||
return <UsageHistoryContent history={result.history} />;
|
||||
}
|
||||
|
||||
async function loadHistory(editorialId: string, page: string | undefined) {
|
||||
async function loadHistory(
|
||||
editorialId: string,
|
||||
page: string | undefined,
|
||||
attemptCursor: string | undefined,
|
||||
) {
|
||||
try {
|
||||
return {
|
||||
history: await listEditorialUsageHistory(editorialId, page),
|
||||
history: await listEditorialUsageHistory(editorialId, page, attemptCursor),
|
||||
unavailable: false as const,
|
||||
};
|
||||
} catch {
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import Link from "next/link";
|
||||
|
||||
import { formatBerlinDateTime } from "@/modules/downloads/format";
|
||||
import type { UsageHistoryAttempt, UsageHistoryPage } from "@/modules/downloads/stream";
|
||||
|
||||
const dateTimeFormatter = new Intl.DateTimeFormat("de-DE", {
|
||||
dateStyle: "medium",
|
||||
timeStyle: "short",
|
||||
});
|
||||
const priceFormatter = new Intl.NumberFormat("de-DE", {
|
||||
style: "currency",
|
||||
currency: "EUR",
|
||||
@@ -39,7 +36,7 @@ export function UsageHistoryContent({ history }: Readonly<{ history: UsageHistor
|
||||
<li key={usage.usageId}>
|
||||
<article className="usage-ledger-entry">
|
||||
<header>
|
||||
<p>{formatDateTime(usage.createdAt)}</p>
|
||||
<p>{formatBerlinDateTime(usage.createdAt)}</p>
|
||||
<h2>{usage.filename}</h2>
|
||||
</header>
|
||||
<dl className="usage-snapshot">
|
||||
@@ -51,6 +48,14 @@ export function UsageHistoryContent({ history }: Readonly<{ history: UsageHistor
|
||||
<dt>Verwendungszweck</dt>
|
||||
<dd>{usage.purpose}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>Nutzungspreis</dt>
|
||||
<dd>{priceFormatter.format(usage.priceCents / 100)}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>Abrechnungsstatus</dt>
|
||||
<dd>{billingStatusLabel(usage.billingStatus)}</dd>
|
||||
</div>
|
||||
{usage.reference ? (
|
||||
<div>
|
||||
<dt>Interne Referenz</dt>
|
||||
@@ -59,7 +64,9 @@ export function UsageHistoryContent({ history }: Readonly<{ history: UsageHistor
|
||||
) : null}
|
||||
</dl>
|
||||
{usage.attempts.length === 0 ? (
|
||||
<p className="usage-attempt-empty">Noch kein abgeschlossener Auslieferungsversuch.</p>
|
||||
<p className="usage-attempt-empty">
|
||||
Keine Auslieferungsversuche auf dieser Auslieferungsseite.
|
||||
</p>
|
||||
) : (
|
||||
<ol className="usage-attempts" aria-label={`Auslieferungen für ${usage.filename}`}>
|
||||
{usage.attempts.map((attempt) => (
|
||||
@@ -74,6 +81,7 @@ export function UsageHistoryContent({ history }: Readonly<{ history: UsageHistor
|
||||
)}
|
||||
|
||||
<HistoryPagination history={history} />
|
||||
<AttemptPagination history={history} />
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -81,7 +89,9 @@ export function UsageHistoryContent({ history }: Readonly<{ history: UsageHistor
|
||||
function AttemptRow({ attempt }: Readonly<{ attempt: UsageHistoryAttempt }>) {
|
||||
return (
|
||||
<li>
|
||||
<time dateTime={attempt.finishedAt.toISOString()}>{formatDateTime(attempt.finishedAt)}</time>
|
||||
<time dateTime={attempt.finishedAt.toISOString()}>
|
||||
{formatBerlinDateTime(attempt.finishedAt)}
|
||||
</time>
|
||||
<strong>{statusLabel(attempt.status)}</strong>
|
||||
<span>{attempt.repeat ? "Wiederholter Abruf" : "Neue Nutzung"}</span>
|
||||
<span>{byteFormatter.format(attempt.transferredBytes)}</span>
|
||||
@@ -95,12 +105,36 @@ function HistoryPagination({ history }: Readonly<{ history: UsageHistoryPage }>)
|
||||
return (
|
||||
<nav aria-label="Seiten der Nutzungshistorie" className="photo-pagination">
|
||||
{history.page > 1 ? (
|
||||
<Link href={`/redaktion/nutzungen?page=${history.page - 1}`}>Vorherige Seite</Link>
|
||||
<Link href={`/redaktion/nutzungen?page=${history.page - 1}`}>
|
||||
Vorherige Nutzungsseite
|
||||
</Link>
|
||||
) : (
|
||||
<span />
|
||||
)}
|
||||
{history.hasNext ? (
|
||||
<Link href={`/redaktion/nutzungen?page=${history.page + 1}`}>Nächste Seite</Link>
|
||||
<Link href={`/redaktion/nutzungen?page=${history.page + 1}`}>
|
||||
Nächste Nutzungsseite
|
||||
</Link>
|
||||
) : null}
|
||||
</nav>
|
||||
);
|
||||
}
|
||||
|
||||
function AttemptPagination({ history }: Readonly<{ history: UsageHistoryPage }>) {
|
||||
if (!history.attemptCursor && !history.nextAttemptCursor) return null;
|
||||
return (
|
||||
<nav aria-label="Seiten der Auslieferungsversuche" className="photo-pagination">
|
||||
{history.attemptCursor ? (
|
||||
<Link href={`/redaktion/nutzungen?page=${history.page}`}>Neueste Auslieferungen</Link>
|
||||
) : (
|
||||
<span />
|
||||
)}
|
||||
{history.nextAttemptCursor ? (
|
||||
<Link
|
||||
href={`/redaktion/nutzungen?page=${history.page}&attemptCursor=${encodeURIComponent(history.nextAttemptCursor)}`}
|
||||
>
|
||||
Ältere Auslieferungen
|
||||
</Link>
|
||||
) : null}
|
||||
</nav>
|
||||
);
|
||||
@@ -115,6 +149,12 @@ function statusLabel(status: UsageHistoryAttempt["status"]): string {
|
||||
return labels[status];
|
||||
}
|
||||
|
||||
function formatDateTime(value: Date): string {
|
||||
return dateTimeFormatter.format(value);
|
||||
function billingStatusLabel(status: UsageHistoryPage["items"][number]["billingStatus"]): string {
|
||||
const labels: Record<UsageHistoryPage["items"][number]["billingStatus"], string> = {
|
||||
open: "Offen",
|
||||
billed: "Abgerechnet",
|
||||
reopened: "Wieder geöffnet",
|
||||
voided: "Storniert",
|
||||
};
|
||||
return labels[status];
|
||||
}
|
||||
Reference in new issue
Block a user