diff --git a/.agent/PROGRESS.md b/.agent/PROGRESS.md index daeaf5a..4d4a904 100644 --- a/.agent/PROGRESS.md +++ b/.agent/PROGRESS.md @@ -267,4 +267,34 @@ Build log for Media Team Hub. One unit per commit, top to bottom. Append after e - **`join` resolved in JS.** Drizzle 0.45.4 exports no `join` from `drizzle-orm` or `drizzle-orm/pg-core`. `getPositionSkills` fetches `position_skills` then the referenced `skills` and merges in JS. - **No skill/position seed yet.** Per ARCHITECTURE the only seeded artifact is `seed/catalog.json` (later unit). Tables start empty; this unit is the data layer + API other units consume. -**Next: B5 — user_skills + verified levels (who has what, and at what level).**** +**Next: B5 — user_skills + verified levels (who has what, and at what level).** + +--- + +## C1 — Skills composer — DONE 2026-11-18 + +**Scope (from BUILD-PLAN.md):** Skills composer — category (required) + optional technology + optional function → `dedupe_key`, name prefill. ARCHITECTURE §4.3: a skill is "function applied to a technology (either may be generic)"; the name is auto-composed on create ("Switch cameras on ATEM Mini Pro") then freely editable. Done when: a duplicate (category, tech, function) is rejected; a null-tech skill is generic. + +**Delivered:** +- `src/domain/skills.ts` — pure `composeSkillName({ categoryName, technologyName, functionName })` (no DB, no `src/db` imports). Rule: function + technology → " on "; function only → ""; technology only → ""; neither → category. A null technology yields a generic (technology-agnostic) skill — the name never names a specific piece of gear. +- `src/domain/skills.test.ts` — 6 tests named after the composition behaviors (compose fn+tech, generic on null tech, tech-only, category fallback, null-tech never names gear, trims whitespace). +- `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. + +**Verification (all exit 0):** +- `pnpm typecheck` ✓ · `pnpm lint` ✓ · `pnpm test` ✓ (**50** total, +6 for compose) · `pnpm build` ✓ +- Runtime smoke against live app + Postgres on :5433 (`/tmp/c1.sh`, **13 checks**): + - compose function + technology → "Switch cameras on ATEM C1 Pro", dedupe_key = `category|tech|func` ✓ + - compose function only (null tech) → "Run FOH audio", dedupe_key = `category||func` (empty tech segment) ✓ + - create composed skill → 201, slug frozen from composed name, dedupe_key persisted ✓ + - duplicate (category, tech, function) → **409** (the C1 rule) ✓ + - create null-tech (generic) skill → 201, `technology_id` null ✓ + - dangling category FK → 400 ✓ + +**Decisions:** +- **C1 is the composition rule, not new tables.** The `skills` table + data-layer CRUD + dedupe_key already exist from B4. C1's distinguishing logic is the name-composition (prefill) rule, so it lives as a pure function in `src/domain/skills.ts` and is exercised by a composer endpoint — matching the `isPublishable`/`validateRequirements` domain pattern from B4. +- **Composer endpoint returns a preview, writes nothing.** `POST /api/dev/skills/compose` returns `{ name, dedupeKey }` so the UI prefills the form; the edit-then-submit flow keeps slug + dedupe_key generation server-authoritative in `POST /api/dev/skills`. +- **No skill/position seed yet.** Per ARCHITECTURE the only seeded artifact is `seed/catalog.json` (later unit). Tables start empty; this unit is the composition layer other units (C2 sliders, C3 matrix) consume. + +**Next: C2 — volunteer `/profile/skills` sliders (self_comfort) with verified chip.**** diff --git a/src/app/api/dev/skills/compose/route.ts b/src/app/api/dev/skills/compose/route.ts new file mode 100644 index 0000000..c6dcbcd --- /dev/null +++ b/src/app/api/dev/skills/compose/route.ts @@ -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 }, + ); +}; diff --git a/src/domain/skills.test.ts b/src/domain/skills.test.ts new file mode 100644 index 0000000..0577d94 --- /dev/null +++ b/src/domain/skills.test.ts @@ -0,0 +1,64 @@ +import { describe, expect, it } from "vitest"; +import { composeSkillName } from "./skills"; + +describe("composeSkillName", () => { + it("composes function + technology as ' on '", () => { + expect( + composeSkillName({ + categoryName: "Audio", + functionName: "Switch cameras", + technologyName: "ATEM Mini Pro", + }), + ).toBe("Switch cameras on ATEM Mini Pro"); + }); + + it("uses just the function when technology is null (generic skill)", () => { + expect( + composeSkillName({ + categoryName: "Audio", + functionName: "Run FOH audio", + technologyName: null, + }), + ).toBe("Run FOH audio"); + }); + + it("uses just the technology when no function is selected", () => { + expect( + composeSkillName({ + categoryName: "Livestream", + functionName: null, + technologyName: "ATEM Mini Pro", + }), + ).toBe("ATEM Mini Pro"); + }); + + it("falls back to the category when neither function nor technology (generic skill)", () => { + expect( + composeSkillName({ + categoryName: "Audio", + functionName: null, + technologyName: null, + }), + ).toBe("Audio"); + }); + + it("never names a specific gear when technology is null", () => { + const name = composeSkillName({ + categoryName: "Audio", + functionName: "Gain staging", + technologyName: null, + }); + expect(name).not.toMatch(/console|mixer|gate/i); + expect(name).toBe("Gain staging"); + }); + + it("trims surrounding whitespace from each selection", () => { + expect( + composeSkillName({ + categoryName: " Audio ", + functionName: " Switch cameras ", + technologyName: " ATEM Mini Pro ", + }), + ).toBe("Switch cameras on ATEM Mini Pro"); + }); +}); diff --git a/src/domain/skills.ts b/src/domain/skills.ts new file mode 100644 index 0000000..fbd22ad --- /dev/null +++ b/src/domain/skills.ts @@ -0,0 +1,30 @@ +/** + * Skill name composition (ARCHITECTURE §4.3, unit C1 — Skills composer). + * + * A skill is composed from a category (required) + optional technology + + * optional function. The composer prefills the display name from these + * selections; the admin may then edit it before saving. The rule: + * + * function + technology -> " on " e.g. "Switch cameras on ATEM Mini Pro" + * function only -> "" e.g. "Run FOH audio" (technology-agnostic) + * technology only -> "" e.g. "ATEM Mini Pro" + * neither (category) -> "" e.g. "Audio" (generic) + * + * A null technology yields a generic (technology-agnostic) skill: the composed + * name never names a specific piece of gear. Pure — no DB, no imports from + * `src/db`. + */ +export function composeSkillName(params: { + categoryName: string; + technologyName?: string | null; + functionName?: string | null; +}): string { + const categoryName = params.categoryName.trim(); + const technologyName = params.technologyName?.trim() ?? ""; + const functionName = params.functionName?.trim() ?? ""; + + if (functionName && technologyName) return `${functionName} on ${technologyName}`; + if (functionName) return functionName; + if (technologyName) return technologyName; + return categoryName; +}