e80c95f838
Single persistent option pool per taxonomy (7 fields: offer categoria/ticket/tipo/obiettivo + catalog fase/offerta/pacchetto), stored as JSON in the settings table (no DB migration). - src/lib/taxonomy.ts: pools with lazy seed from in-use values, add/remove(cascade)/rename helpers - Inline creation anywhere registers into the pool (save offer, addServiceOption, updateServiceField fase, quickAdd, create macro) - Deselecting on a row never touches the pool; global delete only from settings (cascade-strips tags / nulls columns) - getOfferFieldOptions + getCatalogFieldOptions read from pools - Settings: TaxonomyManager (offer + catalog groups), confirm on delete Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
"use server";
|
|
|
|
import { getServerSession } from "next-auth";
|
|
import { authOptions } from "@/lib/auth";
|
|
import { revalidatePath } from "next/cache";
|
|
import {
|
|
addPoolValue,
|
|
removePoolValue,
|
|
TAXONOMY_FIELDS,
|
|
type TaxonomyFieldId,
|
|
} from "@/lib/taxonomy";
|
|
|
|
async function requireAdmin() {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session) throw new Error("Non autorizzato");
|
|
}
|
|
|
|
function assertField(fieldId: string): asserts fieldId is TaxonomyFieldId {
|
|
if (!(fieldId in TAXONOMY_FIELDS)) throw new Error(`Tassonomia non valida: ${fieldId}`);
|
|
}
|
|
|
|
function revalidateAll() {
|
|
revalidatePath("/admin/impostazioni");
|
|
revalidatePath("/admin/offers");
|
|
revalidatePath("/admin/catalog");
|
|
}
|
|
|
|
export async function addTaxonomyValue(fieldId: string, value: string): Promise<void> {
|
|
await requireAdmin();
|
|
assertField(fieldId);
|
|
await addPoolValue(fieldId, value);
|
|
revalidateAll();
|
|
}
|
|
|
|
// Global delete: removes the value from the pool AND cascade-strips it from every
|
|
// offer/service using it.
|
|
export async function removeTaxonomyValue(fieldId: string, value: string): Promise<void> {
|
|
await requireAdmin();
|
|
assertField(fieldId);
|
|
await removePoolValue(fieldId, value);
|
|
revalidateAll();
|
|
}
|