import { NextRequest } from "next/server"; import { z } from "zod"; import { listPositions, createPosition, updatePosition, deactivatePosition, } from "@/db/positions"; const JSON_HEADERS = { "content-type": "application/json" }; const idSchema = z.string().min(1); const positionCreateSchema = z.object({ name: z.string().min(1), description: z.string().nullable().optional(), colorHex: z.string().nullable().optional(), callTimeOffsetMinutes: z.number().int().nullable().optional(), requiresCertification: z.boolean().nullable().optional(), defaultHeadcount: z.number().int().nullable().optional(), sortOrder: z.number().int().optional(), }); const positionUpdateSchema = z.object({ name: z.string().min(1).optional(), description: z.string().nullable().optional(), colorHex: z.string().nullable().optional(), callTimeOffsetMinutes: z.number().int().nullable().optional(), requiresCertification: z.boolean().nullable().optional(), defaultHeadcount: z.number().int().nullable().optional(), sortOrder: z.number().int().optional(), }); 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). */ const dbError = (err: unknown) => { 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 position with this slug already exists" }), { status: 409, headers: JSON_HEADERS, }); } return new Response(JSON.stringify({ error: "internal error" }), { status: 500, headers: JSON_HEADERS, }); }; /** GET /api/dev/positions?active=true — list picker rows. */ export const GET = async (req: NextRequest) => { const active = req.nextUrl.searchParams.get("active") !== "false"; const rows = await listPositions({ active }); return new Response(JSON.stringify({ rows }), { status: 200, headers: JSON_HEADERS, }); }; /** POST /api/dev/positions { ...createFields } — create (slug generated). */ export const POST = async (req: NextRequest) => { const body = await readBody(req); if (!body || typeof body !== "object") { return bad("invalid body"); } const parsed = positionCreateSchema.safeParse(body); if (!parsed.success) return bad(parsed.error.message); try { return created(await createPosition(parsed.data)); } catch (err) { return dbError(err); } }; /** * PATCH /api/dev/positions { id, ...updateFields } — rename / edit. * PATCH { id, active: false } — deactivate (soft delete). * `slug` 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 deactivatePosition(id.data); return ok(row); } const parsed = positionUpdateSchema.safeParse(body); if (!parsed.success) return bad(parsed.error.message); try { return ok(await updatePosition(id.data, parsed.data)); } catch (err) { return dbError(err); } };