diff --git a/src/app/admin/redaktionen/[id]/actions.ts b/src/app/admin/redaktionen/[id]/actions.ts index 397bcfd..4c95df9 100644 --- a/src/app/admin/redaktionen/[id]/actions.ts +++ b/src/app/admin/redaktionen/[id]/actions.ts @@ -3,8 +3,43 @@ import { revalidatePath } from "next/cache"; import { headers } from "next/headers"; import { redirect } from "next/navigation"; + import { requireAdmin } from "@/modules/auth/principal"; -import { editorialService } from "@/modules/editorials/service"; +import { editorialService, EditorialServiceError } from "@/modules/editorials/service"; + +// Returned to the client form so failures surface as inline German text +// instead of an opaque Next.js error-digest page (a thrown server action +// becomes a 500 with a digest the user can't read). +export type ApproveActionResult = { ok: boolean; message: string }; + +const INITIAL_RESULT: ApproveActionResult = { ok: true, message: "" }; + +export async function approveEditorialAction( + editorialId: string, + _prevState: ApproveActionResult, + formData: FormData, +): Promise { + const principal = await requireAdmin(new Headers(await headers())); + const safeEditorialId = requiredId(editorialId); + const price = parsePriceCents(formData.get("priceEuros")); + if (!price.ok) { + return { ok: false, message: price.message }; + } + + try { + await editorialService.approveEditorial({ + editorialId: safeEditorialId, + priceCents: price.cents, + adminUserId: principal.userId, + }); + } catch (error) { + return { ok: false, message: approveErrorMessage(error) }; + } + + revalidatePath(`/admin/redaktionen/${safeEditorialId}`); + revalidatePath(`/admin/redaktionen`); + return INITIAL_RESULT; +} export async function archiveEditorialAction(editorialId: string): Promise { const admin = await requireAdmin(new Headers(await headers())); @@ -13,16 +48,16 @@ export async function archiveEditorialAction(editorialId: string): Promise adminUserId: admin.userId, }); revalidatePath(`/admin/redaktionen/${editorialId}`); - revalidatePath('/admin/redaktionen'); - redirect('/admin/redaktionen'); + revalidatePath("/admin/redaktionen"); + redirect("/admin/redaktionen"); } export async function suspendEditorialAction(editorialId: string): Promise { const admin = await requireAdmin(new Headers(await headers())); await editorialService.transitionEditorialStatus({ editorialId, - from: 'active', - to: 'suspended', + from: "active", + to: "suspended", adminUserId: admin.userId, }); revalidatePath(`/admin/redaktionen/${editorialId}`); @@ -32,9 +67,47 @@ export async function reactivateEditorialAction(editorialId: string): Promise = { + INVALID_PRICE: "Bitte geben Sie einen gültigen Nettopreis an.", + EDITORIAL_NOT_FOUND: "Redaktion nicht gefunden.", + EMAIL_NOT_VERIFIED: "Die E-Mail-Adresse der Redaktion ist noch nicht bestätigt.", + ALBUM_GRANT_REQUIRED: "Bitte weisen Sie zuerst mindestens ein Album zu.", + INVALID_ACCOUNT_TRANSITION: + "Die Redaktion kann in ihrem aktuellen Status nicht freigegeben werden.", + APPROVAL_NOTICE_DELIVERY_UNKNOWN: + "Der Freigabe-Hinweis konnte nicht eindeutig zugeordnet werden.", + }; + return messages[error.code] ?? "Die Freigabe ist fehlgeschlagen."; + } + return "Die Freigabe ist fehlgeschlagen."; +} + +function requiredId(value: unknown): string { + if ( + typeof value !== "string" || + !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value) + ) { + throw new Error("INVALID_ID"); + } + return value; +} diff --git a/src/app/admin/redaktionen/[id]/alben/AlbumGrid.tsx b/src/app/admin/redaktionen/[id]/alben/AlbumGrid.tsx index 6b70aa2..95251e9 100644 --- a/src/app/admin/redaktionen/[id]/alben/AlbumGrid.tsx +++ b/src/app/admin/redaktionen/[id]/alben/AlbumGrid.tsx @@ -3,6 +3,14 @@ import { useState, useTransition } from "react"; import Image from "next/image"; +import type { AlbumActionResult } from "./actions"; + +type AlbumGridAction = ( + editorialId: string, + _prevState: AlbumActionResult, + formData: FormData, +) => Promise; + type AlbumGridProps = { albums: Array<{ id: string; @@ -12,8 +20,8 @@ type AlbumGridProps = { isGranted: boolean; }>; editorialId: string; - grantAction: (editorialId: string, formData: FormData) => Promise; - revokeAction: (editorialId: string, formData: FormData) => Promise; + grantAction: AlbumGridAction; + revokeAction: AlbumGridAction; }; function AlbumCard({ @@ -24,17 +32,18 @@ function AlbumCard({ }: { album: AlbumGridProps["albums"][0]; editorialId: string; - grantAction: AlbumGridProps["grantAction"]; - revokeAction: AlbumGridProps["revokeAction"]; + grantAction: AlbumGridAction; + revokeAction: AlbumGridAction; }) { const [isPending, startTransition] = useTransition(); const handleAction = (formData: FormData) => { startTransition(async () => { + const initial: AlbumActionResult = { ok: true, message: "" }; if (album.isGranted) { - await revokeAction(editorialId, formData); + await revokeAction(editorialId, initial, formData); } else { - await grantAction(editorialId, formData); + await grantAction(editorialId, initial, formData); } }); }; @@ -83,7 +92,7 @@ export function AlbumGrid({ albums, editorialId, grantAction, revokeAction }: Al const [search, setSearch] = useState(""); const filteredAlbums = albums.filter((album) => - album.albumName.toLowerCase().includes(search.toLowerCase()) + album.albumName.toLowerCase().includes(search.toLowerCase()), ); return ( @@ -96,7 +105,7 @@ export function AlbumGrid({ albums, editorialId, grantAction, revokeAction }: Al onChange={(e) => setSearch(e.target.value)} aria-label="Alben durchsuchen" /> - + {filteredAlbums.length === 0 ? (

Keine Alben gefunden.

) : (