C1 — Skills composer (name composition + dedupe_key preview)

Skills composer: category (required) + optional technology + optional
function -> composed name prefill + dedupe_key. Governing rule: a null
technology yields a generic (technology-agnostic) skill; a duplicate
(category, tech, function) is rejected.
Delivered:
- src/domain/skills.ts — pure composeSkillName (no DB, no src/db imports).
  Rule: function+technology -> '<fn> on <tech>'; function only -> '<fn>';
  technology only -> '<tech>'; neither -> category. Null technology ->
  generic skill (name never names a specific piece of gear).
- src/domain/skills.test.ts — 6 tests named after the composition
  behaviors.
- src/app/api/dev/skills/compose/route.ts — POST /api/dev/skills/compose
  returns { name, dedupeKey } for prefilling the form; writes no row.
  The admin edits the name, then submits to POST /api/dev/skills, which
  generates slug + dedupe_key and enforces the unique constraint (409 on
  duplicate).
No schema change, no migration: the skills table, data-layer CRUD, and
dedupeKey were delivered in B4; C1 adds only the composition rule +
composer endpoint (matching the isPublishable/validateRequirements
domain pattern).
Verification: typecheck lint test (50, +6) build all green; runtime
smoke /tmp/c1.sh (13 checks): compose fn+tech -> '<fn> on <tech>',
compose function-only -> generic, create composed skill (slug frozen,
dedupe_key persisted), duplicate -> 409, null-tech generic skill,
dangling FK -> 400.
This commit is contained in:
Tiel Coder
2026-11-18 03:08:25 +00:00
parent 5c789f63cc
commit e9168def6c
4 changed files with 183 additions and 1 deletions
+58
View File
@@ -0,0 +1,58 @@
import { NextRequest } from "next/server";
import { z } from "zod";
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 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 },
);
};