"use server"; import { revalidatePath } from "next/cache"; import { createPeopleMutationInvocation, peopleMutationService, } from "@/features/housekeeping/domains/people/services/mutations"; import { createCorrelationId } from "@/features/housekeeping/foundation/contracts"; import { requirePermission } from "@/lib/admin/guard"; import { formPositiveBigInt } from "@/lib/form-data"; import { PERMS } from "@/lib/permissions"; function text(formData: FormData, key: string): string { return String(formData.get(key) ?? "") .normalize("NFC") .trim(); } export async function createTeam(formData: FormData): Promise { const staff = await requirePermission(PERMS.USERS_EDIT); const rankName = text(formData, "rankName"); if (!rankName) return; const result = await peopleMutationService.execute( createPeopleMutationInvocation(staff, createCorrelationId()), "team.change", { action: "create", rankName, badge: text(formData, "badge"), jobDescription: text(formData, "jobDescription"), staffColor: text(formData, "staffColor") || "#327fa8", hiddenRank: formData.get("hiddenRank") === "on", }, ); if (!result.ok) throw new Error("Could not create team"); revalidatePath("/admin/teams"); } export async function deleteTeam(formData: FormData): Promise { const staff = await requirePermission(PERMS.USERS_EDIT); const rawTeamId = formPositiveBigInt(formData, "id"); if (!rawTeamId) return; const teamId = rawTeamId.toString(); const result = await peopleMutationService.execute( createPeopleMutationInvocation(staff, createCorrelationId()), "team.change", { action: "delete", teamId }, ); if (!result.ok && result.error.code !== "NOT_FOUND") { throw new Error("Could not delete team"); } revalidatePath("/admin/teams"); }