'use server'; import { revalidatePath } from 'next/cache'; import { diffOf, recordAudit } from '@/lib/audit'; import { requireAdmin } from '@/lib/auth/actions'; import { prisma } from '@/lib/db'; import { syncHolidays } from '@/lib/holidays/sync'; export type ActionResult = { ok: true; value: T } | { ok: false; error: string }; /** Whether the shop closes for a given public holiday. Some are trading days. */ export async function setHolidayClosure(id: string, isAutoClosed: boolean): Promise { const gate = await requireAdmin(); if (!gate.ok) { return { ok: false, error: gate.error }; } const existing = await prisma.publicHoliday.findUnique({ where: { id } }); if (!existing) { return { ok: false, error: 'Ce jour férié n’est plus dans le calendrier.' }; } await prisma.publicHoliday.update({ where: { id }, data: { isAutoClosed } }); await recordAudit({ userEmail: gate.value.email, action: 'holiday.set_closure', entity: 'PublicHoliday', entityId: id, diff: diffOf( { férié: existing.nameFr, fermé: existing.isAutoClosed }, { férié: existing.nameFr, fermé: isAutoClosed }, ), }); revalidatePath('/admin'); revalidatePath('/admin/feries'); return { ok: true, value: undefined }; } export async function runHolidaySync(): Promise> { const gate = await requireAdmin(); if (!gate.ok) { return { ok: false, error: gate.error }; } const result = await syncHolidays(); if (!result.ok) { return { ok: false, error: `Synchronisation impossible : ${result.error}` }; } await recordAudit({ userEmail: gate.value.email, action: 'holiday.sync', entity: 'PublicHoliday', diff: { ajoutés: { before: null, after: result.created }, corrigés: { before: null, after: result.updated }, retirés: { before: null, after: result.removed }, }, }); revalidatePath('/admin'); revalidatePath('/admin/feries'); return { ok: true, value: { summary: `${result.created} ajouté(s), ${result.updated} corrigé(s), ${result.removed} retiré(s).`, }, }; }