"use server"; import { revalidatePath } from "next/cache"; import { prisma } from "@/lib/prisma"; import { logAdminAction } from "@/lib/audit-log"; import { MAX_SEPA_HOLD_DAYS, SEPA_HOLD_DAYS_KEY, getSepaHoldDays } from "@/lib/sponsorship/settings"; import { requireAdmin, fail, succeed, str } from "./_shared"; import { intOrNull } from "./sponsorship-shared"; const SUBSCRIPTIONS_PATH = "/admin/patenschaften/abos"; /** * Hebt die SEPA-Sperre eines Kontos auf (gesetzt nach einer Rueckbuchung). * Danach kann das Konto neue Abos wieder per Lastschrift abschliessen. */ export async function liftSepaBlockAction(formData: FormData): Promise { const admin = await requireAdmin(); const customerId = str(formData, "customerId"); const customer = await prisma.customer.findUnique({ where: { id: customerId }, select: { email: true, sepaBlockedAt: true } }); if (!customer) return fail(SUBSCRIPTIONS_PATH, "Das Konto wurde nicht gefunden."); if (!customer.sepaBlockedAt) return succeed(SUBSCRIPTIONS_PATH, "Für dieses Konto besteht keine SEPA-Sperre."); await prisma.customer.update({ where: { id: customerId }, data: { sepaBlockedAt: null } }); await logAdminAction(admin, { action: "customer.sepa_unblock", entityType: "Customer", entityId: customerId, detail: `${customer.email} (gesperrt seit ${customer.sepaBlockedAt.toISOString().slice(0, 10)})`, }); revalidatePath(SUBSCRIPTIONS_PATH); return succeed(SUBSCRIPTIONS_PATH, `SEPA-Sperre für ${customer.email} aufgehoben.`); } export async function updateSepaHoldDaysAction(formData: FormData): Promise { const admin = await requireAdmin(); const days = intOrNull(str(formData, "sepaHoldDays")); if (days === null || days > MAX_SEPA_HOLD_DAYS) { return fail(SUBSCRIPTIONS_PATH, `Bitte eine ganze Zahl von 0 bis ${MAX_SEPA_HOLD_DAYS} Tagen angeben.`); } const previous = await getSepaHoldDays(); await prisma.siteContent.upsert({ where: { key: SEPA_HOLD_DAYS_KEY }, create: { key: SEPA_HOLD_DAYS_KEY, valueDe: String(days) }, update: { valueDe: String(days) }, }); await logAdminAction(admin, { action: "sponsorship.sepa_hold_days", entityType: "SiteContent", entityId: SEPA_HOLD_DAYS_KEY, detail: `${previous} → ${days} Tage`, }); revalidatePath(SUBSCRIPTIONS_PATH); return succeed(SUBSCRIPTIONS_PATH, days === 0 ? "SEPA-Neukunden-Frist abgeschaltet." : `SEPA-Neukunden-Frist auf ${days} Tage gesetzt.`); }