"use server"; import { z } from "zod"; import { contentMutationService } from "@/features/housekeeping/domains/content/services/mutations"; import { PERMS } from "@/lib/permissions"; import { adminAction } from "@/lib/safe-action"; import { ActionError, actionOk } from "@/lib/safe-action-shared"; const bannerSchema = z.object({ title: z.string().min(1).max(255), subtitle: z.string().max(500).optional().default(""), image: z.string().max(500), link: z.string().max(500).optional().default(""), color: z.string().max(20).optional().default(""), isActive: z.coerce.number().int().min(0).max(1).default(1), sortOrder: z.coerce.number().int().min(0).default(0), startDate: z.string().max(50).nullable().optional(), endDate: z.string().max(50).nullable().optional(), }); async function runBanner( ctx: { data: Record; requestId: unknown; session: { user: { id: number } }; }, action: "create" | "update" | "delete", ) { return contentMutationService.execute( { correlationId: String(ctx.requestId), expectedActorId: Number(ctx.session.user.id), legacy: true, }, "banner.change", { action, ...ctx.data }, ); } export const createBanner = adminAction( { permission: PERMS.BANNERS_EDIT, schema: bannerSchema }, async (ctx) => { const result = await runBanner(ctx, "create"); if (!result.ok) throw new ActionError("Banner creation failed"); return actionOk({ id: Number(result.data.output?.id) }); }, ); const updateBannerInput = bannerSchema .partial() .extend({ id: z.coerce.number().int().positive() }); export const updateBanner = adminAction( { permission: PERMS.BANNERS_EDIT, schema: updateBannerInput }, async (ctx) => { const result = await runBanner(ctx, "update"); if (!result.ok) throw new ActionError("Banner not found"); return actionOk({ id: ctx.data.id }); }, ); const deleteBannerInput = z.object({ id: z.coerce.number().int().positive() }); export const deleteBanner = adminAction( { permission: PERMS.BANNERS_EDIT, schema: deleteBannerInput }, async (ctx) => { const result = await runBanner(ctx, "delete"); if (!result.ok) throw new ActionError("Banner not found"); return actionOk(); }, );