feat: Add flatrate billing model for editorial accounts
This commit is contained in:
1 parent
699580a6a0
commit
0b1ffdfdfc
39 files changed
+4900
-57
No files matched your search
@@ -10,9 +10,10 @@ export default async function AdminLayout({ children }: Readonly<{ children: Rea
|
||||
|
||||
return (
|
||||
<PortalShell
|
||||
brandHref="/admin/redaktionen"
|
||||
brandHref="/admin"
|
||||
sectionLabel="Administration"
|
||||
nav={[
|
||||
{ href: "/admin", label: "Dashboard" },
|
||||
{ href: "/admin/redaktionen", label: "Redaktionen" },
|
||||
{ href: "/admin/abrechnungen", label: "Abrechnungen" },
|
||||
{ href: "/admin/audit", label: "Audit" },
|
||||
|
||||
+132
-5
@@ -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>
|
||||
);
|
||||
}
|
||||
@@ -3,6 +3,9 @@
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { headers } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "@/db/client";
|
||||
import { editorialAccounts } from "@/db/schema";
|
||||
|
||||
import { requireAdmin } from "@/modules/auth/principal";
|
||||
import { editorialService, EditorialServiceError } from "@/modules/editorials/service";
|
||||
@@ -74,6 +77,42 @@ export async function reactivateEditorialAction(editorialId: string): Promise<vo
|
||||
revalidatePath(`/admin/redaktionen/${editorialId}`);
|
||||
}
|
||||
|
||||
export async function updateFlatrateAction(editorialId: string, formData: FormData): Promise<void> {
|
||||
const admin = await requireAdmin(new Headers(await headers()));
|
||||
const safeEditorialId = requiredId(editorialId);
|
||||
|
||||
const flatrateActive = formData.get("flatrateActive") === "on";
|
||||
|
||||
let flatratePriceCents: number | null = null;
|
||||
const rawPrice = formData.get("flatratePriceEuros");
|
||||
if (rawPrice && typeof rawPrice === "string" && rawPrice.trim() !== "") {
|
||||
const price = parsePriceCents(rawPrice);
|
||||
if (price.ok) {
|
||||
flatratePriceCents = price.cents;
|
||||
}
|
||||
}
|
||||
|
||||
let flatrateAssetLimit: number | null = null;
|
||||
const rawLimit = formData.get("flatrateAssetLimit");
|
||||
if (rawLimit && typeof rawLimit === "string" && rawLimit.trim() !== "") {
|
||||
const limit = parseInt(rawLimit, 10);
|
||||
if (Number.isFinite(limit) && limit >= 1) {
|
||||
flatrateAssetLimit = limit;
|
||||
}
|
||||
}
|
||||
|
||||
await db
|
||||
.update(editorialAccounts)
|
||||
.set({
|
||||
flatrateActive,
|
||||
flatratePriceCents,
|
||||
flatrateAssetLimit,
|
||||
})
|
||||
.where(eq(editorialAccounts.id, safeEditorialId));
|
||||
|
||||
revalidatePath(`/admin/redaktionen/${safeEditorialId}`);
|
||||
}
|
||||
|
||||
function parsePriceCents(
|
||||
value: unknown,
|
||||
): { ok: true; cents: number } | { ok: false; message: string } {
|
||||
|
||||
@@ -49,40 +49,48 @@ function AlbumCard({
|
||||
};
|
||||
|
||||
return (
|
||||
<li className="album-card" data-granted={album.isGranted}>
|
||||
<div className="album-card-image-container">
|
||||
<li className={`flex flex-col bg-white border ${album.isGranted ? 'border-green-500 shadow-md ring-1 ring-green-500' : 'border-gray-200'} rounded-xl overflow-hidden transition-all duration-300 hover:shadow-lg`} data-granted={album.isGranted}>
|
||||
<div className="relative aspect-[3/2] bg-gray-100 overflow-hidden">
|
||||
{album.albumThumbnailAssetId ? (
|
||||
<Image
|
||||
className="album-card-image"
|
||||
className="w-full h-full object-cover transition-transform duration-500 hover:scale-105"
|
||||
alt={`Titelbild von ${album.albumName}`}
|
||||
height={120}
|
||||
height={200}
|
||||
src={`/api/assets/${encodeURIComponent(album.albumThumbnailAssetId)}/thumbnail?albumId=${encodeURIComponent(album.id)}`}
|
||||
width={160}
|
||||
width={300}
|
||||
/>
|
||||
) : (
|
||||
<div aria-label="Kein Albumcover verfügbar" className="album-card-placeholder" role="img">
|
||||
<div aria-label="Kein Albumcover verfügbar" className="w-full h-full flex items-center justify-center text-gray-400 text-xs uppercase tracking-widest" role="img">
|
||||
Kein Cover
|
||||
</div>
|
||||
)}
|
||||
{album.isGranted && (
|
||||
<div className="absolute top-2 right-2 bg-green-500 text-white text-xs font-bold px-2 py-1 rounded-full shadow-sm">
|
||||
Zugewiesen
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="album-card-body">
|
||||
<h2 className="album-card-name" id={`album-${album.id}`} title={album.albumName}>
|
||||
<div className="p-4 flex flex-col flex-grow">
|
||||
<h2 className="text-base font-bold text-gray-900 mb-1 truncate" id={`album-${album.id}`} title={album.albumName}>
|
||||
{album.albumName}
|
||||
</h2>
|
||||
<p className="album-card-count">{album.assetCount} Fotos</p>
|
||||
<p className={`album-card-status ${album.isGranted ? "album-card-status--granted" : ""}`}>
|
||||
{album.isGranted ? "Zugewiesen" : "Nicht zugewiesen"}
|
||||
</p>
|
||||
<form action={handleAction}>
|
||||
<input name="immichAlbumId" type="hidden" value={album.id} />
|
||||
<button
|
||||
type="submit"
|
||||
className={`album-card-action ${album.isGranted ? "album-card-action--revoke" : "album-card-action--grant"}`}
|
||||
disabled={isPending}
|
||||
>
|
||||
{isPending ? "Lädt..." : album.isGranted ? "Zuweisung aufheben" : "Album zuweisen"}
|
||||
</button>
|
||||
</form>
|
||||
<p className="text-sm text-gray-500 mb-4">{album.assetCount} Fotos</p>
|
||||
<div className="mt-auto">
|
||||
<form action={handleAction}>
|
||||
<input name="immichAlbumId" type="hidden" value={album.id} />
|
||||
<button
|
||||
type="submit"
|
||||
className={`w-full py-2 px-4 rounded-lg font-semibold text-sm transition-colors duration-200 focus:outline-none focus:ring-2 focus:ring-offset-1 ${
|
||||
album.isGranted
|
||||
? "bg-red-50 text-red-600 hover:bg-red-100 focus:ring-red-500 border border-red-200"
|
||||
: "bg-gray-900 text-white hover:bg-gray-800 focus:ring-gray-900"
|
||||
}`}
|
||||
disabled={isPending}
|
||||
>
|
||||
{isPending ? "Wird verarbeitet..." : album.isGranted ? "Zuweisung aufheben" : "Album zuweisen"}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
@@ -109,7 +117,7 @@ export function AlbumGrid({ albums, editorialId, grantAction, revokeAction }: Al
|
||||
{filteredAlbums.length === 0 ? (
|
||||
<p>Keine Alben gefunden.</p>
|
||||
) : (
|
||||
<ul className="album-grid" aria-label="Zuweisbare Immich-Alben">
|
||||
<ul className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-6" aria-label="Zuweisbare Immich-Alben">
|
||||
{filteredAlbums.map((album) => (
|
||||
<AlbumCard
|
||||
key={album.id}
|
||||
|
||||
@@ -18,6 +18,9 @@ export default async function EditorialDetailPage({
|
||||
status: editorialAccounts.status,
|
||||
emailVerifiedAt: editorialAccounts.emailVerifiedAt,
|
||||
currentPriceCents: editorialAccounts.currentPriceCents,
|
||||
flatrateActive: editorialAccounts.flatrateActive,
|
||||
flatratePriceCents: editorialAccounts.flatratePriceCents,
|
||||
flatrateAssetLimit: editorialAccounts.flatrateAssetLimit,
|
||||
archivedAt: editorialAccounts.archivedAt,
|
||||
})
|
||||
.from(editorialAccounts)
|
||||
|
||||
Reference in new issue
Block a user