139 lines
3.6 KiB
TypeScript
139 lines
3.6 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import {
|
|
type EconomyMutationSnapshot,
|
|
executeLegacyEconomyMutation,
|
|
} from "@/features/housekeeping/domains/economy/services/mutations";
|
|
import { requirePermission } from "@/lib/admin/guard";
|
|
import { PERMS } from "@/lib/permissions";
|
|
|
|
function revalidateCatalog(): void {
|
|
revalidatePath("/admin/catalog");
|
|
}
|
|
|
|
export async function createCatalogItem(data: {
|
|
pageId: number;
|
|
itemIds: string;
|
|
catalogName: string;
|
|
costCredits: number;
|
|
costPoints: number;
|
|
pointsType: number;
|
|
amount: number;
|
|
orderNumber: number;
|
|
offerId: number;
|
|
limitedSells: number;
|
|
limitedStack: number;
|
|
extradata: string;
|
|
songId: number;
|
|
haveOffer: "0" | "1";
|
|
clubOnly: "0" | "1";
|
|
}) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
const snapshot = await executeLegacyEconomyMutation(
|
|
staff,
|
|
"catalog-item.change",
|
|
{ action: "create", ...data },
|
|
);
|
|
revalidateCatalog();
|
|
return { ok: true as const, data: { id: Number(snapshot.output?.id) } };
|
|
}
|
|
|
|
export async function bulkCreateCatalogItems(input: {
|
|
pageId: number;
|
|
rows: Array<{
|
|
baseId: number;
|
|
credits?: number;
|
|
points?: number;
|
|
pointsType?: number;
|
|
}>;
|
|
}): Promise<
|
|
| { ok: true; data: { created: number; failed: number } }
|
|
| { ok: false; error: string }
|
|
> {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
let snapshot: EconomyMutationSnapshot;
|
|
try {
|
|
snapshot = await executeLegacyEconomyMutation(
|
|
staff,
|
|
"catalog-item.bulk-create",
|
|
input,
|
|
);
|
|
} catch {
|
|
return { ok: false, error: "Bulk import failed" };
|
|
}
|
|
revalidateCatalog();
|
|
return {
|
|
ok: true as const,
|
|
data: {
|
|
created: Number(snapshot.output?.created ?? 0),
|
|
failed: Number(snapshot.output?.failed ?? 0),
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function deleteCatalogItems({ ids }: { ids: number[] }) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
await executeLegacyEconomyMutation(staff, "catalog-item.change", {
|
|
action: "delete",
|
|
ids,
|
|
});
|
|
revalidateCatalog();
|
|
return { ok: true as const, data: {} };
|
|
}
|
|
|
|
export async function moveCatalogItems(input: {
|
|
ids: number[];
|
|
targetPageId: number;
|
|
}) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
await executeLegacyEconomyMutation(staff, "catalog-item.move", input);
|
|
revalidateCatalog();
|
|
return { ok: true as const, data: {} };
|
|
}
|
|
|
|
export async function reorderCatalogItems(input: {
|
|
orders: Array<{ id: number; orderNumber: number }>;
|
|
}) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
await executeLegacyEconomyMutation(staff, "catalog-item.reorder", input);
|
|
revalidateCatalog();
|
|
return { ok: true as const, data: {} };
|
|
}
|
|
|
|
export async function updateCatalogItem(input: {
|
|
id: number;
|
|
catalogFields: Record<string, unknown>;
|
|
baseItem?: { id: number; fields: Record<string, unknown> };
|
|
}) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
await executeLegacyEconomyMutation(staff, "catalog-item.change", {
|
|
action: "update",
|
|
...input,
|
|
});
|
|
revalidateCatalog();
|
|
return { ok: true as const, data: {} };
|
|
}
|
|
|
|
export async function translateCatalogItems(input: {
|
|
items: Array<{ id: number; publicName: string; description?: string }>;
|
|
}) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
const snapshot = await executeLegacyEconomyMutation(
|
|
staff,
|
|
"catalog-item.translate",
|
|
input,
|
|
);
|
|
revalidateCatalog();
|
|
return {
|
|
ok: true as const,
|
|
data: {
|
|
namesUpdated: Number(snapshot.output?.namesUpdated ?? 0),
|
|
descriptionsUpdated: Number(snapshot.output?.descriptionsUpdated ?? 0),
|
|
furniDataUpdated: Number(snapshot.output?.furniDataUpdated ?? 0),
|
|
furniDataInserted: Number(snapshot.output?.furniDataInserted ?? 0),
|
|
updated: input.items.length,
|
|
},
|
|
};
|
|
}
|