C3 — Admin skills matrix + verify-level modal
Adds the append-only skill_assessments history and the verify path, so a level can only move together with the row recording who moved it, from what level, and the comfort they saw.
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Skill level assessment rules — unit C3 (ARCHITECTURE §4.3, §5.8.2).
|
||||
*
|
||||
* Pure: no DB, no imports from `src/db`, so the rule can be tested without a
|
||||
* running server. The route and the data layer both call it; neither is allowed
|
||||
* to invent a level move on its own.
|
||||
*
|
||||
* The rules:
|
||||
* - Levels are an ordered ladder: none < observer < assisted < qualified < expert.
|
||||
* - A move may go up or down — demotions are audited just like promotions.
|
||||
* - `level_from` is always the volunteer's *current verified level*, read from
|
||||
* the database. A caller cannot rewrite history by passing it in.
|
||||
* - An unchanged level records nothing; a verify that does not verify is a
|
||||
* mistake, not a silent no-op row.
|
||||
* - `comfort_at_time` snapshots the volunteer's current self_comfort — the
|
||||
* signal the assessor saw — and is null when they never rated it.
|
||||
*/
|
||||
|
||||
/** Ordered lowest → highest. Keep in sync with the DB enum (tested). */
|
||||
export const SKILL_LEVELS = ["none", "observer", "assisted", "qualified", "expert"] as const;
|
||||
|
||||
export type SkillLevel = (typeof SKILL_LEVELS)[number];
|
||||
|
||||
export function isSkillLevel(value: unknown): value is SkillLevel {
|
||||
return typeof value === "string" && (SKILL_LEVELS as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
/** Ladder position: 0 = none, 4 = expert. -1 for a value outside the ladder. */
|
||||
export function levelRank(level: string): number {
|
||||
return (SKILL_LEVELS as readonly string[]).indexOf(level);
|
||||
}
|
||||
|
||||
export interface AssessmentDraft {
|
||||
/** Always the current verified level at assessment time. */
|
||||
levelFrom: SkillLevel;
|
||||
levelTo: SkillLevel;
|
||||
/** The volunteer's self_comfort at assessment time (null = never rated). */
|
||||
comfortAtTime: number | null;
|
||||
direction: "up" | "down";
|
||||
}
|
||||
|
||||
export type AssessmentResult =
|
||||
| { ok: true; draft: AssessmentDraft }
|
||||
| { ok: false; problems: string[] };
|
||||
|
||||
/**
|
||||
* Build the row to append for a level verification. `currentLevel` and
|
||||
* `newLevel` are typed as strings on purpose: both arrive from untrusted input
|
||||
* (the client for `newLevel`, the DB for `currentLevel`) and are validated here.
|
||||
*/
|
||||
export function buildAssessment(params: {
|
||||
currentLevel: string;
|
||||
newLevel: string;
|
||||
selfComfort: number | null;
|
||||
}): AssessmentResult {
|
||||
const problems: string[] = [];
|
||||
|
||||
if (!isSkillLevel(params.currentLevel)) {
|
||||
problems.push(`unknown current level "${params.currentLevel}"`);
|
||||
}
|
||||
if (!isSkillLevel(params.newLevel)) {
|
||||
problems.push(`level must be one of: ${SKILL_LEVELS.join(", ")}`);
|
||||
}
|
||||
if (problems.length > 0) return { ok: false, problems };
|
||||
|
||||
const currentLevel = params.currentLevel as SkillLevel;
|
||||
const newLevel = params.newLevel as SkillLevel;
|
||||
|
||||
if (currentLevel === newLevel) {
|
||||
return { ok: false, problems: ["level is unchanged — nothing to assess"] };
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
draft: {
|
||||
// Server-owned: the from-level comes from the stored record, never input.
|
||||
levelFrom: currentLevel,
|
||||
levelTo: newLevel,
|
||||
comfortAtTime: params.selfComfort,
|
||||
direction: levelRank(newLevel) > levelRank(currentLevel) ? "up" : "down",
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user