a77f47d5bd
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.
74 lines
2.5 KiB
TypeScript
74 lines
2.5 KiB
TypeScript
/**
|
|
* Volunteer competency records — `user_skills` (ARCHITECTURE §4.3). C2 owns the
|
|
* self-report path: a volunteer rates their own comfort (`self_comfort`, 0-100).
|
|
*
|
|
* Integrity rules (enforced here, not just the UI):
|
|
* - Two numbers, one truth. `self_comfort` is the signal; `verified_level`
|
|
* is the gate. Setting a volunteer's comfort NEVER touches `verified_level`
|
|
* or its audit columns (`verified_by` / `verified_at`) — those are written
|
|
* only by C3's verify flow (skill_assessments).
|
|
* - One record per (user, skill) — the unique index enforces it.
|
|
*
|
|
* `user_skills` is excluded from schema relations (FK columns sit on a unique
|
|
* key, which trips Drizzle 0.45.4's `one()` typing), so this queries directly.
|
|
*/
|
|
|
|
import { eq } from "drizzle-orm";
|
|
import { db } from "@/db";
|
|
import { userSkills, skills } from "@/db/schema";
|
|
|
|
/**
|
|
* Set a volunteer's self-reported comfort for one skill (the profile slider).
|
|
* Upserts on (user_id, skill_id). Creates the record if absent (verified_level
|
|
* defaults to "none"); otherwise updates ONLY the self-report fields. The
|
|
* verified level and its audit columns are never touched here.
|
|
*/
|
|
export async function setSelfComfort(
|
|
userId: string,
|
|
skillId: string,
|
|
selfComfort: number | null,
|
|
) {
|
|
const now = new Date();
|
|
const selfRatedAt = selfComfort === null ? null : now;
|
|
const [row] = await db
|
|
.insert(userSkills)
|
|
.values({
|
|
id: crypto.randomUUID(),
|
|
userId,
|
|
skillId,
|
|
selfComfort,
|
|
selfRatedAt,
|
|
verifiedLevel: "none",
|
|
})
|
|
.onConflictDoUpdate({
|
|
target: [userSkills.userId, userSkills.skillId],
|
|
set: { selfComfort, selfRatedAt },
|
|
})
|
|
.returning();
|
|
return row;
|
|
}
|
|
|
|
/** A user's competency records, joined to the skill for display. */
|
|
export async function getUserSkills(userId: string) {
|
|
return db
|
|
.select({
|
|
id: userSkills.id,
|
|
userId: userSkills.userId,
|
|
skillId: userSkills.skillId,
|
|
selfComfort: userSkills.selfComfort,
|
|
selfRatedAt: userSkills.selfRatedAt,
|
|
verifiedLevel: userSkills.verifiedLevel,
|
|
verifiedBy: userSkills.verifiedBy,
|
|
verifiedAt: userSkills.verifiedAt,
|
|
mentorOptIn: userSkills.mentorOptIn,
|
|
lastServedOn: userSkills.lastServedOn,
|
|
notes: userSkills.notes,
|
|
name: skills.name,
|
|
slug: skills.slug,
|
|
})
|
|
.from(userSkills)
|
|
.innerJoin(skills, eq(userSkills.skillId, skills.id))
|
|
.where(eq(userSkills.userId, userId))
|
|
.orderBy(skills.name);
|
|
}
|