import { desc, inArray, sql } from "drizzle-orm"; import Link from "next/link"; import { db } from "@/db/client"; import { editorialAccounts, usages } from "@/db/schema"; import { PageHeader } from "@/components/ui/page-header"; import { IconChevronRight } from "@sakaniui/react/icons"; import { Badge, Card, EmptyState } from "@/components/ui/sakani"; export default async function EditorialsPage() { const [editorials, openUsageTotals] = await Promise.all([ db .select({ id: editorialAccounts.id, name: editorialAccounts.name, contactName: editorialAccounts.contactName, loginEmail: editorialAccounts.loginEmail, status: editorialAccounts.status, currentPriceCents: editorialAccounts.currentPriceCents, createdAt: editorialAccounts.createdAt, archivedAt: editorialAccounts.archivedAt, }) .from(editorialAccounts) .orderBy(desc(editorialAccounts.createdAt)), // Nicht abgerechnete Nutzungen je Redaktion — dieselben Status, die auch // in der Abrechnung wählbar sind (voided zählt nicht). db .select({ editorialId: usages.editorialAccountId, openCents: sql`COALESCE(SUM(${usages.priceCents}), 0)::int`, }) .from(usages) .where(inArray(usages.billingStatus, ["open", "reopened"])) .groupBy(usages.editorialAccountId), ]); const openCentsByEditorial = new Map( openUsageTotals.map((total) => [total.editorialId, total.openCents]), ); const activeEditorials = editorials.filter((e) => !e.archivedAt); const archivedEditorials = editorials.filter((e) => e.archivedAt); return (
{activeEditorials.length === 0 ? ( ) : (
    {activeEditorials.map((editorial) => { const openCents = openCentsByEditorial.get(editorial.id) ?? 0; return (
  • {editorial.name}

    {STATUS[editorial.status]?.label ?? "Gesperrt"}
    Kontakt
    {editorial.contactName}
    E-Mail
    {editorial.loginEmail}

    {editorial.currentPriceCents === null ? "Preis noch nicht festgelegt" : `Netto ${formatCents(editorial.currentPriceCents)}`} 0 ? "text-[var(--color-warning-fg)]" : "text-[var(--color-fg-muted)]"}`} title="Summe der noch nicht abgerechneten Bildnutzungen" > · Offen {formatCents(openCents)}

  • ); })}
)} {archivedEditorials.length > 0 && (
    {archivedEditorials.map((editorial) => (
  • {editorial.name}

    Archiviert

    {editorial.contactName} · {editorial.loginEmail}

    Archiviert am: {editorial.archivedAt ? new Date(editorial.archivedAt).toLocaleDateString("de-DE") : ""}

  • ))}
)}
); } const CARD_LINK = "block h-full rounded-[var(--radius-xl)] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--color-border-focus)]"; const STATUS: Record = { active: { label: "Aktiv", variant: "success" }, pending_approval: { label: "Ausstehend", variant: "warning" }, }; function formatCents(cents: number): string { return new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(cents / 100); }