import { NextRequest } from "next/server"; import { z } from "zod"; import { listServices, createService, updateService, deactivateService, } from "@/db/services"; const JSON_HEADERS = { "content-type": "application/json" }; const idSchema = z.string().min(1); const serviceCreateSchema = z.object({ name: z.string().min(1), location: z.string().nullable().optional(), timezone: z.string().optional(), rrule: z.string().nullable().optional(), startTimeLocal: z.string().min(1), durationMinutes: z.number().int().min(1).optional(), doorsOpenMinutesBefore: z.number().int().min(0).optional(), notes: z.string().nullable().optional(), active: z.boolean().optional(), }); const serviceUpdateSchema = serviceCreateSchema.partial(); async function readBody(req: NextRequest) { try { return await req.json(); } catch { return null; } } const bad = (message: string) => new Response(JSON.stringify({ error: message }), { status: 400, headers: JSON_HEADERS, }); const ok = (row: unknown) => new Response(JSON.stringify({ row }), { status: 200, headers: JSON_HEADERS, }); const created = (row: unknown) => new Response(JSON.stringify({ row }), { status: 201, headers: JSON_HEADERS, }); /** * Map Postgres constraint violations to client errors (see catalog route). * Returns null for non-constraint errors so callers can apply their own message. */ const constraintError = (err: unknown): Response | null => { let code: string | undefined; let node: { code?: string; cause?: unknown } | null = err as { code?: string; cause?: unknown; } | null; while (node) { if (node.code) { code = node.code; break; } node = (node.cause as { code?: string; cause?: unknown } | null) ?? null; } if (code === "23505") { return new Response( JSON.stringify({ error: "a service with this name already exists" }), { status: 409, headers: JSON_HEADERS }, ); } if (code === "23503" || code === "23514") { return new Response(JSON.stringify({ error: "referenced row does not exist" }), { status: 400, headers: JSON_HEADERS, }); } return null; }; /** GET /api/dev/services?active=true — list service templates. */ export const GET = async (req: NextRequest) => { const active = req.nextUrl.searchParams.get("active") !== "false"; const rows = await listServices({ active }); return new Response(JSON.stringify({ rows }), { status: 200, headers: JSON_HEADERS, }); }; /** POST /api/dev/services { ...fields } — create a service template. */ export const POST = async (req: NextRequest) => { const body = await readBody(req); if (!body || typeof body !== "object") { return bad("invalid body"); } const parsed = serviceCreateSchema.safeParse(body); if (!parsed.success) return bad(parsed.error.message); try { return created(await createService(parsed.data)); } catch (err) { const constraint = constraintError(err); if (constraint) return constraint; return bad(err instanceof Error ? err.message : "internal error"); } }; /** * PATCH /api/dev/services { id, ...fields } — edit. * PATCH { id, active: false } — deactivate (soft delete). * `name` is never accepted here: it freezes after creation. */ export const PATCH = async (req: NextRequest) => { const body = await readBody(req); if (!body || typeof body !== "object") { return bad("invalid body"); } const id = idSchema.safeParse((body as { id?: unknown }).id); if (!id.success) return bad("invalid id"); // Deactivate path: explicit active=false, no editable fields applied. if ((body as { active?: unknown }).active === false) { const row = await deactivateService(id.data); return ok(row); } const parsed = serviceUpdateSchema.safeParse(body); if (!parsed.success) return bad(parsed.error.message); try { return ok(await updateService(id.data, parsed.data)); } catch (err) { const constraint = constraintError(err); if (constraint) return constraint; return bad(err instanceof Error ? err.message : "internal error"); } };