Bulk import resolves names from items_base and refreshes RCON once. Page/item updates only accept an allowlisted field set. Co-authored-by: Cursor <[email protected]>
193 lines
4.8 KiB
TypeScript
193 lines
4.8 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { requirePermission } from "@/lib/admin/guard";
|
|
import { PERMS } from "@/lib/permissions";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { rcon } from "@/lib/services/rcon";
|
|
import { logStaffActivity } from "@/lib/services/staff-activity";
|
|
|
|
const BC_PAGE_FIELDS = [
|
|
"caption",
|
|
"parentId",
|
|
"pageLayout",
|
|
"enabled",
|
|
"visible",
|
|
"orderNum",
|
|
"iconImage",
|
|
"iconColor",
|
|
"pageHeadline",
|
|
"pageTeaser",
|
|
"pageSpecial",
|
|
"pageText1",
|
|
"pageText2",
|
|
"pageTextDetails",
|
|
"pageTextTeaser",
|
|
] as const;
|
|
|
|
const BC_ITEM_FIELDS = [
|
|
"itemIds",
|
|
"catalogName",
|
|
"orderNumber",
|
|
"extradata",
|
|
"pageId",
|
|
] as const;
|
|
|
|
function pickAllowed(
|
|
fields: Record<string, unknown>,
|
|
allowed: readonly string[],
|
|
) {
|
|
const out: Record<string, unknown> = {};
|
|
for (const key of allowed) {
|
|
if (Object.hasOwn(fields, key) && fields[key] !== undefined) {
|
|
out[key] = fields[key];
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export async function updateBcPage({
|
|
id,
|
|
...fields
|
|
}: { id: number } & Record<string, unknown>) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
const data = pickAllowed(fields, BC_PAGE_FIELDS);
|
|
if (Object.keys(data).length === 0) {
|
|
return { ok: false as const, error: "No valid fields to update" };
|
|
}
|
|
await prisma.catalogPagesBc.update({ where: { id }, data: data as any });
|
|
await rcon.updateCatalog();
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "bc_page_update",
|
|
description: `Updated BC catalog page #${id}`,
|
|
targetType: "catalog_page_bc",
|
|
targetId: id,
|
|
});
|
|
revalidatePath("/admin/catalog/builder-club");
|
|
return { ok: true as const };
|
|
}
|
|
|
|
export async function deleteBcItem({ id }: { id: number }) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
await prisma.catalogItemsBc.delete({ where: { id } });
|
|
await rcon.updateCatalog();
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "bc_item_delete",
|
|
description: `Deleted BC catalog item #${id}`,
|
|
targetType: "catalog_item_bc",
|
|
targetId: id,
|
|
});
|
|
revalidatePath("/admin/catalog/builder-club");
|
|
return { ok: true as const };
|
|
}
|
|
|
|
export async function updateBcItem({
|
|
id,
|
|
...data
|
|
}: {
|
|
id: number;
|
|
itemIds?: string;
|
|
catalogName?: string;
|
|
orderNumber?: number;
|
|
extradata?: string;
|
|
}) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
const safe = pickAllowed(data as Record<string, unknown>, BC_ITEM_FIELDS);
|
|
if (Object.keys(safe).length === 0) {
|
|
return { ok: false as const, error: "No valid fields to update" };
|
|
}
|
|
await prisma.catalogItemsBc.update({ where: { id }, data: safe as any });
|
|
await rcon.updateCatalog();
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "bc_item_update",
|
|
description: `Updated BC catalog item #${id}`,
|
|
targetType: "catalog_item_bc",
|
|
targetId: id,
|
|
});
|
|
revalidatePath("/admin/catalog/builder-club");
|
|
return { ok: true as const };
|
|
}
|
|
|
|
export async function createBcItem({
|
|
pageId,
|
|
...data
|
|
}: {
|
|
pageId: number;
|
|
itemIds: string;
|
|
catalogName: string;
|
|
orderNumber: number;
|
|
extradata: string;
|
|
}) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
const created = await prisma.catalogItemsBc.create({
|
|
data: { pageId, ...data },
|
|
});
|
|
await rcon.updateCatalog();
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "bc_item_create",
|
|
description: `Created BC catalog item #${created.id}`,
|
|
targetType: "catalog_item_bc",
|
|
targetId: created.id,
|
|
});
|
|
revalidatePath("/admin/catalog/builder-club");
|
|
return { ok: true as const, data: { id: created.id } };
|
|
}
|
|
|
|
export async function toggleBcPage({
|
|
id,
|
|
field,
|
|
}: {
|
|
id: number;
|
|
field: "enabled" | "visible";
|
|
}) {
|
|
await requirePermission(PERMS.CATALOG_EDIT);
|
|
const page = await prisma.catalogPagesBc.findUnique({
|
|
where: { id },
|
|
select: { enabled: true, visible: true },
|
|
});
|
|
if (!page) return { ok: false as const, error: "Page not found" };
|
|
await prisma.catalogPagesBc.update({
|
|
where: { id },
|
|
data: { [field]: page[field] === "1" ? "0" : "1" },
|
|
});
|
|
await rcon.updateCatalog();
|
|
revalidatePath("/admin/catalog/builder-club");
|
|
return { ok: true as const };
|
|
}
|
|
|
|
export async function createBcPage(input: {
|
|
caption: string;
|
|
parentId: number;
|
|
pageLayout: string;
|
|
}) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
const created = await prisma.catalogPagesBc.create({
|
|
data: {
|
|
caption: input.caption,
|
|
parentId: input.parentId,
|
|
pageLayout: input.pageLayout,
|
|
iconColor: 0,
|
|
iconImage: 0,
|
|
orderNum: 0,
|
|
visible: "1",
|
|
enabled: "1",
|
|
pageHeadline: "",
|
|
pageTeaser: "",
|
|
},
|
|
});
|
|
await rcon.updateCatalog();
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "bc_page_create",
|
|
description: `Created BC catalog page "${input.caption}"`,
|
|
targetType: "catalog_page_bc",
|
|
targetId: created.id,
|
|
});
|
|
revalidatePath("/admin/catalog/builder-club");
|
|
return { ok: true as const, data: { id: created.id } };
|
|
}
|