feat: reset editorial usage data; fix hard delete FK order

Admins can now wipe an editorial's usage footprint (usages, download
events, billing reports incl. items and related audit rows) so test
accounts no longer pollute dashboard statistics. Grants, price, status
and login stay untouched; the action is confirmed by typing the
editorial name.

Hard delete previously failed with FK violations as soon as a usage had
ever been billed or an address saved:
- billing_report_items (restrict -> usages) were never deleted
- billing_address_versions were deleted before the referencing
  current_billing_address_version_id and their self-referencing
  superseded_by_id, both restrict

Both are now handled in correct FK order inside the transaction; the
audit trail of the deleted editorial is purged and verification tokens
are removed.
This commit is contained in:
Leon Tonhäuser committed 2026-08-25 01:28:34 +02:00
1 parent c6b69eb88d
commit c9d3f3d1dc
5 files changed
+262 -5

No files matched your search

+28
View File
@@ -62,6 +62,34 @@ export async function hardDeleteEditorialAction(editorialId: string): Promise<vo
redirect("/admin/redaktionen");
}
export type ResetUsageResult = { ok: boolean; message: string };
export async function resetUsageDataAction(editorialId: string): Promise<ResetUsageResult> {
const admin = await requireAdmin(new Headers(await headers()));
const safeEditorialId = requiredId(editorialId);
try {
const result = await editorialService.resetUsageData({
editorialId: safeEditorialId,
adminUserId: admin.userId,
});
revalidatePath(`/admin/redaktionen/${safeEditorialId}`);
revalidatePath("/admin/redaktionen");
revalidatePath("/admin");
revalidatePath("/admin/abrechnungen");
return {
ok: true,
message: `${result.deletedUsages} Nutzung(en) und ${result.deletedReports} Abrechnung(en) gelöscht.`,
};
} catch (error) {
if (error instanceof EditorialServiceError && error.code === "EDITORIAL_NOT_FOUND") {
return { ok: false, message: "Redaktion nicht gefunden." };
}
console.error("Failed to reset usage data:", error);
return { ok: false, message: "Das Zurücksetzen ist fehlgeschlagen." };
}
}
export async function suspendEditorialAction(editorialId: string): Promise<void> {
const admin = await requireAdmin(new Headers(await headers()));
await editorialService.transitionEditorialStatus({