import { eq, sql, and, desc, gte } from "drizzle-orm"; import { db } from "@/db/client"; import { usages, editorialAccounts } from "@/db/schema"; import { requireAdmin } from "@/modules/auth/principal"; import { headers } from "next/headers"; import Link from "next/link"; export default async function AdminDashboardPage() { await requireAdmin(new Headers(await headers())); const now = new Date(); const firstDayOfMonth = new Date(Date.UTC(now.getFullYear(), now.getMonth(), 1)); // 1. Umsatz diesen Monat (Offen + Abgerechnet, aber nur usages aus diesem Monat) const [revenueStats] = await db .select({ totalCents: sql`COALESCE(SUM(${usages.priceCents}), 0)::int`, count: sql`COUNT(${usages.id})::int`, }) .from(usages) .where(gte(usages.createdAt, firstDayOfMonth)); // 2. Aktivste Redaktionen (nach Anzahl an Downloads insgesamt) const topEditorials = await db .select({ editorialId: usages.editorialAccountId, name: editorialAccounts.name, downloadCount: sql`COUNT(${usages.id})::int`, revenueCents: sql`COALESCE(SUM(${usages.priceCents}), 0)::int`, }) .from(usages) .innerJoin(editorialAccounts, eq(usages.editorialAccountId, editorialAccounts.id)) .groupBy(usages.editorialAccountId, editorialAccounts.name) .orderBy(desc(sql`COUNT(${usages.id})`)) .limit(5); // 3. Beliebteste Alben const topAlbums = await db .select({ albumName: usages.albumNameSnapshot, downloadCount: sql`COUNT(${usages.id})::int`, }) .from(usages) .groupBy(usages.albumNameSnapshot) .orderBy(desc(sql`COUNT(${usages.id})`)) .limit(5); const formatter = new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }); return (

Admin Dashboard

Willkommen zurück! Hier ist ein Überblick über die aktuelle Nutzung des Portals.

Umsatz diesen Monat

{formatter.format((revenueStats?.totalCents ?? 0) / 100)}

{revenueStats?.count ?? 0} lizenzierte Bilder im {now.toLocaleString("de-DE", { month: "long" })}

Redaktionen verwalten

Aktivste Redaktionen

{topEditorials.length === 0 ? (

Noch keine Daten vorhanden.

) : ( {topEditorials.map(ed => ( ))}
Redaktion Downloads Umsatz
{ed.name} {ed.downloadCount} {formatter.format(ed.revenueCents / 100)}
)}

Beliebteste Alben

{topAlbums.length === 0 ? (

Noch keine Daten vorhanden.

) : ( {topAlbums.map(album => ( ))}
Album Downloads
{album.albumName} {album.downloadCount}
)}
); }