diff --git a/src/app/admin/catalog/actions.ts b/src/app/admin/catalog/actions.ts new file mode 100644 index 0000000..0a8a5fc --- /dev/null +++ b/src/app/admin/catalog/actions.ts @@ -0,0 +1,64 @@ +"use server"; + +import { db } from "@/db"; +import { service_catalog } from "@/db/schema"; +import { revalidatePath } from "next/cache"; +import { eq } from "drizzle-orm"; +import { z } from "zod"; +import { getServerSession } from "next-auth"; +import { authOptions } from "@/lib/auth"; + +const serviceSchema = z.object({ + name: z.string().min(1, "Nome richiesto"), + description: z.string().optional(), + unit_price: z.coerce.number().min(0.01, "Prezzo deve essere maggiore di 0"), +}); + +async function requireAdmin() { + const session = await getServerSession(authOptions); + if (!session) throw new Error("Non autorizzato"); +} + +export async function createService(formData: FormData) { + await requireAdmin(); + const parsed = serviceSchema.safeParse({ + name: formData.get("name"), + description: formData.get("description") ?? "", + unit_price: formData.get("unit_price"), + }); + if (!parsed.success) throw new Error(parsed.error.issues[0].message); + await db.insert(service_catalog).values({ + name: parsed.data.name, + description: parsed.data.description ?? null, + unit_price: parsed.data.unit_price.toFixed(2), + }); + revalidatePath("/admin/catalog"); +} + +export async function updateService(serviceId: string, formData: FormData) { + await requireAdmin(); + const parsed = serviceSchema.safeParse({ + name: formData.get("name"), + description: formData.get("description") ?? "", + unit_price: formData.get("unit_price"), + }); + if (!parsed.success) throw new Error(parsed.error.issues[0].message); + await db + .update(service_catalog) + .set({ + name: parsed.data.name, + description: parsed.data.description ?? null, + unit_price: parsed.data.unit_price.toFixed(2), + }) + .where(eq(service_catalog.id, serviceId)); + revalidatePath("/admin/catalog"); +} + +export async function toggleServiceActive(serviceId: string, active: boolean) { + await requireAdmin(); + await db + .update(service_catalog) + .set({ active }) + .where(eq(service_catalog.id, serviceId)); + revalidatePath("/admin/catalog"); +} diff --git a/src/lib/admin-queries.ts b/src/lib/admin-queries.ts index ab50a5c..669f61f 100644 --- a/src/lib/admin-queries.ts +++ b/src/lib/admin-queries.ts @@ -9,6 +9,7 @@ import { documents, notes, time_entries, + service_catalog, } from "@/db/schema"; import { eq, inArray, asc, isNull, sql } from "drizzle-orm"; import type { @@ -20,6 +21,7 @@ import type { Document, Note, Comment, + ServiceCatalog, } from "@/db/schema"; export type ClientWithPayments = { @@ -188,4 +190,11 @@ export async function getClientFullDetail(id: string): Promise { + return db + .select() + .from(service_catalog) + .orderBy(asc(service_catalog.name)); } \ No newline at end of file