import { NextRequest } from "next/server"; import { z } from "zod"; import { gateOrUser } from "@/auth/api"; import { composeSkillName } from "@/domain/skills"; import { dedupeKey } from "@/db/positions"; const JSON_HEADERS = { "content-type": "application/json" }; /** * The composer: pick a category (required) + optional technology + optional * function, and get back the prefilled display name and the dedupe_key the * create path will persist. The UI fills the form with `name` (editable) and * submits to POST /api/dev/skills. This endpoint never writes a row. */ const composeSchema = z.object({ categoryId: z.string().min(1), categoryName: z.string().min(1), technologyId: z.string().nullable().optional(), technologyName: z.string().nullable().optional(), functionId: z.string().nullable().optional(), functionName: z.string().nullable().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, }); /** * POST /api/dev/skills/compose { categoryId, categoryName, technologyId?, * technologyName?, functionId?, functionName? } -> { name, dedupeKey }. */ export const POST = async (req: NextRequest) => { const gate = await gateOrUser("training.author"); if (gate instanceof Response) return gate; const body = await readBody(req); if (!body || typeof body !== "object") return bad("invalid body"); const parsed = composeSchema.safeParse(body); if (!parsed.success) return bad(parsed.error.message); const d = parsed.data; return new Response( JSON.stringify({ name: composeSkillName({ categoryName: d.categoryName, technologyName: d.technologyName, functionName: d.functionName, }), dedupeKey: dedupeKey(d.categoryId, d.technologyId, d.functionId), }), { status: 200, headers: JSON_HEADERS }, ); };