feat: Add flatrate billing model for editorial accounts

This commit is contained in:
Leon Tonhäuser committed 2026-08-11 03:27:10 +02:00
1 parent 699580a6a0
commit 0b1ffdfdfc
39 files changed
+4900 -57

No files matched your search

+132 -5
View File
@@ -1,7 +1,134 @@
import { redirect } from "next/navigation";
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";
// /admin has no dashboard of its own; the admin layout's wordmark links here,
// so route authenticated admins to the editorials list instead of 404ing.
export default function AdminIndexPage() {
redirect("/admin/redaktionen");
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<number>`COALESCE(SUM(${usages.priceCents}), 0)::int`,
count: sql<number>`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<number>`COUNT(${usages.id})::int`,
revenueCents: sql<number>`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<number>`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 (
<section className="content-section">
<h1>Admin Dashboard</h1>
<p className="text-gray-600 mb-8">Willkommen zurück! Hier ist ein Überblick über die aktuelle Nutzung des Portals.</p>
<div className="grid grid-cols-1 md:grid-cols-3 gap-6 mb-12">
<div className="bg-white p-6 rounded-xl border border-gray-200 shadow-sm">
<h2 className="text-sm font-semibold text-gray-500 uppercase tracking-wide">Umsatz diesen Monat</h2>
<p className="text-4xl font-bold text-green-600 mt-2">
{formatter.format((revenueStats?.totalCents ?? 0) / 100)}
</p>
<p className="text-sm text-gray-500 mt-2">
{revenueStats?.count ?? 0} lizenzierte Bilder im {now.toLocaleString("de-DE", { month: "long" })}
</p>
</div>
<div className="bg-white p-6 rounded-xl border border-gray-200 shadow-sm flex flex-col justify-center">
<Link href="/admin/redaktionen" className="primary-action text-center m-0">
Redaktionen verwalten
</Link>
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-8">
<div>
<h3 className="text-xl font-bold mb-4">Aktivste Redaktionen</h3>
<div className="bg-white rounded-xl border border-gray-200 overflow-hidden shadow-sm">
{topEditorials.length === 0 ? (
<p className="p-4 text-gray-500">Noch keine Daten vorhanden.</p>
) : (
<table className="w-full text-left border-collapse">
<thead className="bg-gray-50">
<tr>
<th className="py-3 px-4 font-semibold text-gray-700">Redaktion</th>
<th className="py-3 px-4 font-semibold text-gray-700 text-right">Downloads</th>
<th className="py-3 px-4 font-semibold text-gray-700 text-right">Umsatz</th>
</tr>
</thead>
<tbody>
{topEditorials.map(ed => (
<tr key={ed.editorialId} className="border-t border-gray-100">
<td className="py-3 px-4">
<Link href={`/admin/redaktionen/${ed.editorialId}`} className="text-blue-600 hover:underline">
{ed.name}
</Link>
</td>
<td className="py-3 px-4 text-right">{ed.downloadCount}</td>
<td className="py-3 px-4 text-right">{formatter.format(ed.revenueCents / 100)}</td>
</tr>
))}
</tbody>
</table>
)}
</div>
</div>
<div>
<h3 className="text-xl font-bold mb-4">Beliebteste Alben</h3>
<div className="bg-white rounded-xl border border-gray-200 overflow-hidden shadow-sm">
{topAlbums.length === 0 ? (
<p className="p-4 text-gray-500">Noch keine Daten vorhanden.</p>
) : (
<table className="w-full text-left border-collapse">
<thead className="bg-gray-50">
<tr>
<th className="py-3 px-4 font-semibold text-gray-700">Album</th>
<th className="py-3 px-4 font-semibold text-gray-700 text-right">Downloads</th>
</tr>
</thead>
<tbody>
{topAlbums.map(album => (
<tr key={album.albumName} className="border-t border-gray-100">
<td className="py-3 px-4 font-medium text-gray-800">{album.albumName}</td>
<td className="py-3 px-4 text-right text-gray-600">{album.downloadCount}</td>
</tr>
))}
</tbody>
</table>
)}
</div>
</div>
</div>
</section>
);
}