53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { executeLegacyEconomyMutation } from "@/features/housekeeping/domains/economy/services/mutations";
|
|
import { requirePermission } from "@/lib/admin/guard";
|
|
import { PERMS } from "@/lib/permissions";
|
|
import {
|
|
type ActionResult,
|
|
actionError,
|
|
actionOk,
|
|
} from "@/lib/safe-action-shared";
|
|
import { logServerError } from "@/lib/server-log";
|
|
|
|
export async function createVoucher(input: {
|
|
code: string;
|
|
amount: number;
|
|
maxUses: number;
|
|
expiresAt?: string;
|
|
}): Promise<ActionResult<{ id: string }>> {
|
|
const staff = await requirePermission(PERMS.SHOP_EDIT);
|
|
try {
|
|
const snapshot = await executeLegacyEconomyMutation(
|
|
staff,
|
|
"voucher.change",
|
|
{ action: "create", ...input },
|
|
);
|
|
revalidatePath("/admin/vouchers");
|
|
return actionOk({ id: String(snapshot.output?.id) });
|
|
} catch (error) {
|
|
logServerError("admin.voucher_create_failed", error);
|
|
return actionError("Could not create voucher (code may already exist)");
|
|
}
|
|
}
|
|
|
|
export async function deleteVoucher(input: {
|
|
id: string;
|
|
}): Promise<ActionResult> {
|
|
const staff = await requirePermission(PERMS.SHOP_EDIT);
|
|
try {
|
|
await executeLegacyEconomyMutation(staff, "voucher.change", {
|
|
action: "delete",
|
|
id: input.id,
|
|
});
|
|
revalidatePath("/admin/vouchers");
|
|
return actionOk();
|
|
} catch (error) {
|
|
logServerError("admin.voucher_delete_failed", error, {
|
|
voucherId: input.id,
|
|
});
|
|
return actionError("Could not delete voucher");
|
|
}
|
|
}
|