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,33 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { listAssessments } from "@/db/assessments";
|
||||
|
||||
const JSON_HEADERS = { "content-type": "application/json" };
|
||||
|
||||
const bad = (message: string) =>
|
||||
new Response(JSON.stringify({ error: message }), {
|
||||
status: 400,
|
||||
headers: JSON_HEADERS,
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/dev/skills/assessments?userId=&skillId=&limit=
|
||||
*
|
||||
* Append-only assessment history, newest first — the audit trail shown beside
|
||||
* the verify modal. There is intentionally no update or delete endpoint.
|
||||
*/
|
||||
export const GET = async (req: NextRequest) => {
|
||||
const params = req.nextUrl.searchParams;
|
||||
const rawLimit = params.get("limit");
|
||||
let limit: number | undefined;
|
||||
if (rawLimit !== null) {
|
||||
limit = Number(rawLimit);
|
||||
if (!Number.isInteger(limit) || limit < 1) return bad("limit must be a positive integer");
|
||||
}
|
||||
|
||||
const rows = await listAssessments({
|
||||
userId: params.get("userId") ?? undefined,
|
||||
skillId: params.get("skillId") ?? undefined,
|
||||
limit,
|
||||
});
|
||||
return new Response(JSON.stringify({ rows }), { status: 200, headers: JSON_HEADERS });
|
||||
};
|
||||
@@ -0,0 +1,35 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { listSkillsMatrix } from "@/db/assessments";
|
||||
import { isSkillLevel, SKILL_LEVELS, type SkillLevel } from "@/domain/assessments";
|
||||
|
||||
const JSON_HEADERS = { "content-type": "application/json" };
|
||||
|
||||
const bad = (message: string) =>
|
||||
new Response(JSON.stringify({ error: message }), {
|
||||
status: 400,
|
||||
headers: JSON_HEADERS,
|
||||
});
|
||||
|
||||
/**
|
||||
* GET /api/dev/skills/matrix
|
||||
* ?userId= &skillId= &categoryId= &level=
|
||||
*
|
||||
* The admin skills matrix grid: one row per competency record (person × skill)
|
||||
* with the volunteer's self-comfort and the current verified level. Filters
|
||||
* compose, so `?level=qualified` answers "who is currently signed off?".
|
||||
*/
|
||||
export const GET = async (req: NextRequest) => {
|
||||
const params = req.nextUrl.searchParams;
|
||||
const level = params.get("level");
|
||||
if (level !== null && !isSkillLevel(level)) {
|
||||
return bad(`level must be one of: ${SKILL_LEVELS.join(", ")}`);
|
||||
}
|
||||
|
||||
const rows = await listSkillsMatrix({
|
||||
userId: params.get("userId") ?? undefined,
|
||||
skillId: params.get("skillId") ?? undefined,
|
||||
categoryId: params.get("categoryId") ?? undefined,
|
||||
level: (level ?? undefined) as SkillLevel | undefined,
|
||||
});
|
||||
return new Response(JSON.stringify({ rows }), { status: 200, headers: JSON_HEADERS });
|
||||
};
|
||||
@@ -0,0 +1,88 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { AssessmentError, verifySkillLevel } from "@/db/assessments";
|
||||
import { ASSESSMENT_KINDS } from "@/db/schema";
|
||||
import { SKILL_LEVELS } from "@/domain/assessments";
|
||||
|
||||
const JSON_HEADERS = { "content-type": "application/json" };
|
||||
|
||||
const verifySchema = z.object({
|
||||
userId: z.string().min(1),
|
||||
skillId: z.string().min(1),
|
||||
/** The level being signed off — validated again by the domain rule. */
|
||||
level: z.enum(SKILL_LEVELS),
|
||||
assessorId: z.string().min(1).nullable().optional(),
|
||||
kind: z.enum(ASSESSMENT_KINDS).optional(),
|
||||
notes: 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,
|
||||
});
|
||||
|
||||
const ok = (payload: unknown) =>
|
||||
new Response(JSON.stringify(payload), { status: 200, headers: JSON_HEADERS });
|
||||
|
||||
/** Postgres constraint violations → client errors (same mapping as C2). */
|
||||
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 record already exists for this user and skill" }),
|
||||
{ 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,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* POST /api/dev/skills/verify { userId, skillId, level, assessorId?, kind?, notes? }
|
||||
*
|
||||
* The verify-level modal's save: appends a `skill_assessments` row and moves
|
||||
* `user_skills.verified_level` in one transaction. The volunteer's
|
||||
* `self_comfort` is untouched (C2's signal is not the gate).
|
||||
*/
|
||||
export const POST = async (req: NextRequest) => {
|
||||
const body = await readBody(req);
|
||||
if (!body || typeof body !== "object") return bad("invalid body");
|
||||
const parsed = verifySchema.safeParse(body);
|
||||
if (!parsed.success) return bad(parsed.error.message);
|
||||
|
||||
try {
|
||||
const result = await verifySkillLevel(parsed.data);
|
||||
return ok(result);
|
||||
} catch (err) {
|
||||
if (err instanceof AssessmentError) return bad(err.problems.join("; "));
|
||||
return dbError(err);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user