Files
glenloch-media/src/domain/positions.ts
T
Tiel Coder 5c789f63cc 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).
2026-11-18 01:34:24 +00:00

54 lines
1.7 KiB
TypeScript

/**
* Position publishability — the pure rule behind B4's requirements editor
* (ARCHITECTURE §4.3 rule 4).
*
* A position with no `is_core` skill cannot be published: if every skill on a
* position is non-core, "anyone can fill it" becomes the rule by accident. The
* gate lives here — a single auditable predicate the route calls — so the rule
* and the code cannot drift.
*/
export interface PositionSkillInput {
isCore?: boolean;
weight?: number;
}
/**
* A position is publishable when at least one of its skills is core. Core
* skills hard-gate a solo assignment; without one there is nothing that
* distinguishes a qualified operator from anyone.
*/
export function isPublishable(skills: readonly PositionSkillInput[]): boolean {
return skills.some((skill) => skill.isCore === true);
}
/**
* Validate a requirements payload before it hits the DB. Returns a list of
* human-readable problems (empty when valid). Keeps the route thin and the
* rules testable in isolation.
*
* - weight must be an integer in 1..5
* - a requested `published: true` requires at least one core skill
*/
export function validateRequirements(params: {
skills: readonly PositionSkillInput[];
published?: boolean;
}): string[] {
const problems: string[] = [];
const { skills, published } = params;
skills.forEach((skill, i) => {
const weight = skill.weight as number | undefined;
if (weight === undefined) return;
if (!Number.isInteger(weight) || weight < 1 || weight > 5) {
problems.push(`skill[${i}].weight must be an integer from 1 to 5`);
}
});
if (published === true && !isPublishable(skills)) {
problems.push("a published position needs at least one core skill");
}
return problems;
}