C2 — Volunteer profile sliders (self_comfort; verified_level untouched)

Adds the user_skills record and slider save path so a volunteer can rate
their own comfort without ever moving the verified level, which stays
C3's to write.
This commit is contained in:
Tiel Coder
2026-11-18 04:14:06 +00:00
parent d74232de60
commit a77f47d5bd
8 changed files with 2287 additions and 5 deletions
+100
View File
@@ -0,0 +1,100 @@
import { NextRequest } from "next/server";
import { z } from "zod";
import { getUserSkills, setSelfComfort } from "@/db/userSkills";
import { validateSelfComfort } from "@/domain/userSkills";
const JSON_HEADERS = { "content-type": "application/json" };
const idSchema = z.string().min(1);
/** A slider save: a volunteer rates their own comfort for one skill. */
const selfComfortCreateSchema = z.object({
userId: z.string().min(1),
skillId: z.string().min(1),
/** 0..100, or null to clear a rating. Validated in the domain layer. */
selfComfort: z.number().int().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 = (row: unknown) =>
new Response(JSON.stringify({ row }), {
status: 200,
headers: JSON_HEADERS,
});
/**
* Map Postgres constraint violations to client errors (see catalog route):
* 23505 = duplicate (user_id, skill_id); 23503 = dangling user/skill 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 competency 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,
});
};
/** GET /api/dev/user-skills?userId=<id> — a volunteer's competency records. */
export const GET = async (req: NextRequest) => {
const userId = idSchema.safeParse(req.nextUrl.searchParams.get("userId"));
if (!userId.success) return bad("invalid userId");
const rows = await getUserSkills(userId.data);
return new Response(JSON.stringify({ rows }), { status: 200, headers: JSON_HEADERS });
};
/**
* POST /api/dev/user-skills { userId, skillId, selfComfort } — set a
* volunteer's self-reported comfort (the profile slider). Only
* self_comfort / self_rated_at change; verified_level is never touched.
*/
export const POST = async (req: NextRequest) => {
const body = await readBody(req);
if (!body || typeof body !== "object") return bad("invalid body");
const parsed = selfComfortCreateSchema.safeParse(body);
if (!parsed.success) return bad(parsed.error.message);
const selfComfort = parsed.data.selfComfort ?? null;
const problems = validateSelfComfort(selfComfort);
if (problems.length > 0) return bad(problems[0]);
try {
const row = await setSelfComfort(parsed.data.userId, parsed.data.skillId, selfComfort);
return ok(row);
} catch (err) {
return dbError(err);
}
};