B4 — Positions + skills + requirements editor
Positions (schedulable seats) CRUD + requirements editor. Governing rule:
a position with no is_core skill refuses to save as publishable.
Schema (src/db/schema.ts):
- skills table (B4 prerequisite of position_skills): id, slug (unique),
name, category_id/technology_id/function_id FKs, dedupe_key
(category|technology|function, NULLs -> ''), mentor_min_level (default
'expert'), stale_after_days (default 120), recert_required, sort_order,
active, created_at, updated_at. All FKs ON DELETE RESTRICT.
- position_skills join: position_id, skill_id, weight, required_level,
is_core; PK (position_id, skill_id).
- roles (positions) gained active (default true) + updated_at for
soft-delete/pickers/audit.
Data layer (src/db/positions.ts): list/create/update/deactivate for
positions and skills; getPositionSkills (skills merged into
position_skills in JS); setPositionSkills (replaces the whole set);
dedupeKey() helper.
Domain (src/domain/positions.ts + .test.ts): pure publishability rule —
isPublishable(skills) = has >=1 is_core skill. 8 tests named after the
rule.
API:
- src/app/api/dev/skills/route.ts — GET/POST/PATCH (slug + dedupe_key
generated at create; slug frozen on update).
- src/app/api/dev/positions/route.ts — GET/POST/PATCH (rename/edit or
deactivate via active:false).
- src/app/api/dev/positions/requirements/route.ts — GET (list a
position's requirements) + POST (set requirements; enforces the
publishability gate: published:true with no core skill -> 400).
Migration drizzle/0004_hesitant_ezekiel.sql.
Verification: typecheck ✓ lint ✓ test ✓ (44, +8) build ✓ db:migrate ✓;
runtime smoke /tmp/b4.sh (24 checks) — publish-with-core 200,
publish-without-core 400, weight-out-of-range 400, rename slug frozen,
deactivate hides from picker but row persists.
Decisions: skills table is a B4 prerequisite; requirements editor takes
positionId in the body (Next 15.5.29 rejects an explicitly-typed
{ params: { id } } route-handler second arg); publishability gate lives
in the domain and is enforced in the route; join resolved in JS
(drizzle 0.45.4 exports no join).
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { getPositionSkills, setPositionSkills } from "@/db/positions";
|
||||
import { validateRequirements } from "@/domain/positions";
|
||||
|
||||
const JSON_HEADERS = { "content-type": "application/json" };
|
||||
|
||||
const positionSkillSchema = z.object({
|
||||
skillId: z.string().min(1),
|
||||
weight: z.number().int().min(1).max(5).optional(),
|
||||
requiredLevel: z.string().nullable().optional(),
|
||||
isCore: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const setRequirementsSchema = z.object({
|
||||
positionId: z.string().min(1),
|
||||
skills: z.array(positionSkillSchema).min(1),
|
||||
published: z.boolean().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,
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/dev/positions/requirements?positionId=<id> — a position's
|
||||
* requirements editor state (each skill with its weight, required level, and
|
||||
* core flag).
|
||||
*/
|
||||
export const GET = async (req: NextRequest) => {
|
||||
const positionId = req.nextUrl.searchParams.get("positionId");
|
||||
if (!positionId) return bad("missing positionId");
|
||||
const row = await getPositionSkills(positionId);
|
||||
return ok(row);
|
||||
};
|
||||
|
||||
/**
|
||||
* POST /api/dev/positions/requirements — save the requirements editor.
|
||||
*
|
||||
* The payload is the full skill set (each with weight, required level, core
|
||||
* flag) plus a `published` flag. A position with no core skill refuses to save
|
||||
* as publishable (ARCHITECTURE §4.3 rule 4):
|
||||
*
|
||||
* { "positionId": "<id>", "skills": [ { "skillId": "<id>", "isCore": true,
|
||||
* "weight": 3, "requiredLevel": "qualified" } ], "published": true }
|
||||
*/
|
||||
export const POST = async (req: NextRequest) => {
|
||||
const body = await readBody(req);
|
||||
if (!body || typeof body !== "object") return bad("invalid body");
|
||||
|
||||
const parsed = setRequirementsSchema.safeParse(body);
|
||||
if (!parsed.success) return bad(parsed.error.message);
|
||||
|
||||
// Publishability gate: a published position needs at least one core skill.
|
||||
const problems = validateRequirements({
|
||||
skills: parsed.data.skills,
|
||||
published: parsed.data.published,
|
||||
});
|
||||
if (problems.length > 0) {
|
||||
return bad(problems.join("; "));
|
||||
}
|
||||
|
||||
try {
|
||||
const row = await setPositionSkills(parsed.data.positionId, parsed.data.skills);
|
||||
return ok(row);
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : "internal error";
|
||||
return bad(message);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,137 @@
|
||||
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);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,142 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
listSkills,
|
||||
createSkill,
|
||||
updateSkill,
|
||||
deactivateSkill,
|
||||
} from "@/db/positions";
|
||||
|
||||
const JSON_HEADERS = { "content-type": "application/json" };
|
||||
|
||||
const idSchema = z.string().min(1);
|
||||
|
||||
const skillCreateSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
categoryId: z.string().min(1),
|
||||
technologyId: z.string().nullable().optional(),
|
||||
functionId: z.string().nullable().optional(),
|
||||
description: z.string().nullable().optional(),
|
||||
mentorMinLevel: z.string().nullable().optional(),
|
||||
staleAfterDays: z.number().int().nullable().optional(),
|
||||
recertRequired: z.boolean().nullable().optional(),
|
||||
sortOrder: z.number().int().optional(),
|
||||
});
|
||||
|
||||
const skillUpdateSchema = z.object({
|
||||
name: z.string().min(1).optional(),
|
||||
description: z.string().nullable().optional(),
|
||||
mentorMinLevel: z.string().nullable().optional(),
|
||||
staleAfterDays: z.number().int().nullable().optional(),
|
||||
recertRequired: z.boolean().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):
|
||||
* 23505 = duplicate slug / dedupe_key, 23503 = dangling category/tech/function FK.
|
||||
*/
|
||||
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 skill with this slug or dedupe key already exists" }), {
|
||||
status: 409,
|
||||
headers: JSON_HEADERS,
|
||||
});
|
||||
}
|
||||
if (code === "23503") {
|
||||
return new Response(JSON.stringify({ error: "referenced row does not exist" }), {
|
||||
status: 400,
|
||||
headers: JSON_HEADERS,
|
||||
});
|
||||
}
|
||||
return new Response(JSON.stringify({ error: "internal error" }), {
|
||||
status: 500,
|
||||
headers: JSON_HEADERS,
|
||||
});
|
||||
};
|
||||
|
||||
/** GET /api/dev/skills?active=true — list picker rows. */
|
||||
export const GET = async (req: NextRequest) => {
|
||||
const active = req.nextUrl.searchParams.get("active") !== "false";
|
||||
const rows = await listSkills({ active });
|
||||
return new Response(JSON.stringify({ rows }), {
|
||||
status: 200,
|
||||
headers: JSON_HEADERS,
|
||||
});
|
||||
};
|
||||
|
||||
/** POST /api/dev/skills { ...createFields } — create (slug + dedupe_key generated). */
|
||||
export const POST = async (req: NextRequest) => {
|
||||
const body = await readBody(req);
|
||||
if (!body || typeof body !== "object") return bad("invalid body");
|
||||
const parsed = skillCreateSchema.safeParse(body);
|
||||
if (!parsed.success) return bad(parsed.error.message);
|
||||
try {
|
||||
return created(await createSkill(parsed.data));
|
||||
} catch (err) {
|
||||
return dbError(err);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* PATCH /api/dev/skills { id, ...updateFields } — rename / edit.
|
||||
* PATCH { id, active: false } — deactivate (soft delete).
|
||||
* `slug` and the composing FKs are never accepted here: they freeze 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");
|
||||
|
||||
if ((body as { active?: unknown }).active === false) {
|
||||
const row = await deactivateSkill(id.data);
|
||||
return ok(row);
|
||||
}
|
||||
|
||||
const parsed = skillUpdateSchema.safeParse(body);
|
||||
if (!parsed.success) return bad(parsed.error.message);
|
||||
try {
|
||||
return ok(await updateSkill(id.data, parsed.data));
|
||||
} catch (err) {
|
||||
return dbError(err);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user