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 { EmptyState } from "@/components/ui/sakani"; import { requireAdminPage } from "../access"; import { EditorialsTable } from "./EditorialsTable"; export default async function EditorialsPage() { await requireAdminPage(); 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]), ); return (
{editorials.length === 0 ? ( ) : ( ({ id: editorial.id, name: editorial.name, contactName: editorial.contactName, loginEmail: editorial.loginEmail, status: editorial.status, archived: Boolean(editorial.archivedAt), priceCents: editorial.currentPriceCents, openCents: openCentsByEditorial.get(editorial.id) ?? 0, createdAt: editorial.createdAt.toISOString(), }))} /> )}
); }