80 lines
2.8 KiB
TypeScript
80 lines
2.8 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
import {
|
|
contentMutationService,
|
|
createContentMutationInvocation,
|
|
} from "@/features/housekeeping/domains/content/services/mutations";
|
|
import { createCorrelationId } from "@/features/housekeeping/foundation/contracts";
|
|
import { requirePermission } from "@/lib/admin/guard";
|
|
import { canonicalize, sanitizeField } from "@/lib/foundation/security";
|
|
import { PERMS } from "@/lib/permissions";
|
|
|
|
function helpInput(formData: FormData) {
|
|
return {
|
|
name: sanitizeField(formData.get("name")),
|
|
content: canonicalize(String(formData.get("content") ?? "")),
|
|
position:
|
|
Number(formData.get("position")) > 0
|
|
? Math.floor(Number(formData.get("position")))
|
|
: 1,
|
|
imageUrl: sanitizeField(formData.get("imageUrl")),
|
|
buttonText: sanitizeField(formData.get("buttonText")),
|
|
buttonUrl: sanitizeField(formData.get("buttonUrl")),
|
|
buttonColor: sanitizeField(formData.get("buttonColor"), 16) || "#eeb425",
|
|
buttonBorderColor:
|
|
sanitizeField(formData.get("buttonBorderColor"), 16) || "#facc15",
|
|
smallBox: formData.get("smallBox") != null,
|
|
};
|
|
}
|
|
|
|
export async function createHelpQuestion(formData: FormData): Promise<void> {
|
|
const staff = await requirePermission(PERMS.PAGES_EDIT);
|
|
const input = helpInput(formData);
|
|
if (!input.name || !input.content) return;
|
|
const result = await contentMutationService.execute(
|
|
createContentMutationInvocation(staff, createCorrelationId()),
|
|
"help-question.change",
|
|
{ action: "create", ...input },
|
|
);
|
|
if (!result.ok) {
|
|
revalidatePath("/ase/content/help/questions");
|
|
redirect(
|
|
"/ase/content/help/questions/new?error=Unique name collision or database error. Please try again.",
|
|
);
|
|
}
|
|
revalidatePath("/ase/content/help/questions");
|
|
redirect("/ase/content/help/questions");
|
|
}
|
|
|
|
export async function updateHelpQuestion(formData: FormData): Promise<void> {
|
|
const staff = await requirePermission(PERMS.PAGES_EDIT);
|
|
const id = String(formData.get("id") ?? "").trim();
|
|
if (!/^[1-9]\d*$/u.test(id)) return;
|
|
const input = helpInput(formData);
|
|
if (!input.name || !input.content) return;
|
|
const result = await contentMutationService.execute(
|
|
createContentMutationInvocation(staff, createCorrelationId()),
|
|
"help-question.change",
|
|
{ action: "update", id, ...input },
|
|
);
|
|
if (!result.ok) {
|
|
revalidatePath(`/ase/content/help/questions/${id}`);
|
|
return;
|
|
}
|
|
redirect("/ase/content/help/questions");
|
|
}
|
|
|
|
export async function deleteHelpQuestion(formData: FormData): Promise<void> {
|
|
const staff = await requirePermission(PERMS.PAGES_EDIT);
|
|
const id = String(formData.get("id") ?? "").trim();
|
|
if (!/^[1-9]\d*$/u.test(id)) return;
|
|
await contentMutationService.execute(
|
|
createContentMutationInvocation(staff, createCorrelationId()),
|
|
"help-question.change",
|
|
{ action: "delete", id },
|
|
);
|
|
redirect("/ase/content/help/questions");
|
|
}
|