115 lines
2.7 KiB
TypeScript
115 lines
2.7 KiB
TypeScript
"use server";
|
|
import { revalidatePath } from "next/cache";
|
|
import type { BulkOfferInput } from "@/features/catalog/domain/bulk-offers";
|
|
import {
|
|
applyBulkOffersCommand,
|
|
listBulkOfferDestinationsCommand,
|
|
previewBulkOffersCommand,
|
|
undoBulkOffersCommand,
|
|
} from "@/features/catalog/server/bulk-offers";
|
|
import { catalogFailure } from "@/features/catalog/server/errors";
|
|
import { OperationConflict } from "@/features/operations/model";
|
|
import { requirePermission } from "@/lib/admin/guard";
|
|
import { logger } from "@/lib/logger";
|
|
import { PERMS } from "@/lib/permissions";
|
|
import { withCatalogExport } from "@/lib/services/catalog-git-queue";
|
|
export async function previewBulkOffers(input: BulkOfferInput) {
|
|
await requirePermission(PERMS.CATALOG_VIEW);
|
|
try {
|
|
return { ok: true as const, data: await previewBulkOffersCommand(input) };
|
|
} catch (error) {
|
|
return {
|
|
ok: false as const,
|
|
error:
|
|
error instanceof OperationConflict
|
|
? error.message
|
|
: catalogFailure(error).message,
|
|
};
|
|
}
|
|
}
|
|
export async function applyBulkOffers(
|
|
input: BulkOfferInput,
|
|
fingerprint: string,
|
|
requestKey?: string,
|
|
) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
try {
|
|
return await withCatalogExport(async () => {
|
|
const data = await applyBulkOffersCommand(
|
|
input,
|
|
fingerprint,
|
|
staff.id,
|
|
requestKey,
|
|
);
|
|
if (data.changedCount > 0) {
|
|
refreshCatalog();
|
|
}
|
|
return { ok: true as const, data };
|
|
});
|
|
} catch (error) {
|
|
return {
|
|
ok: false as const,
|
|
error:
|
|
error instanceof OperationConflict
|
|
? error.message
|
|
: catalogFailure(error).message,
|
|
};
|
|
}
|
|
}
|
|
|
|
export async function getBulkOfferDestinations() {
|
|
await requirePermission(PERMS.CATALOG_VIEW);
|
|
try {
|
|
return {
|
|
ok: true as const,
|
|
data: await listBulkOfferDestinationsCommand(),
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
ok: false as const,
|
|
error:
|
|
error instanceof OperationConflict
|
|
? error.message
|
|
: catalogFailure(error).message,
|
|
};
|
|
}
|
|
}
|
|
|
|
export async function undoBulkOffers(
|
|
historyIds: number[],
|
|
requestKey?: string,
|
|
) {
|
|
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
|
try {
|
|
return await withCatalogExport(async () => {
|
|
const data = await undoBulkOffersCommand(
|
|
historyIds,
|
|
staff.id,
|
|
requestKey,
|
|
);
|
|
refreshCatalog();
|
|
return { ok: true as const, data };
|
|
});
|
|
} catch (error) {
|
|
return {
|
|
ok: false as const,
|
|
error:
|
|
error instanceof OperationConflict
|
|
? error.message
|
|
: catalogFailure(error).message,
|
|
};
|
|
}
|
|
}
|
|
|
|
function refreshCatalog() {
|
|
try {
|
|
revalidatePath("/admin/catalog", "layout");
|
|
revalidatePath("/admin/logs/audit");
|
|
} catch (error) {
|
|
logger.error("Catalog committed; page invalidation failed", {
|
|
module: "catalog",
|
|
error,
|
|
});
|
|
}
|
|
}
|