unit(C6): growth goals + shadow pairing validation

Adds the growth_goals table (ARCHITECTURE §4.3, §5.8.6) with its migration and a
pure nextGoalStatus() state machine (requested -> mentor_assigned -> in_progress
-> achieved, with pause/resume and decline). Adds validateShadowPairing()
(src/domain/shadows.ts): a shadow/trainee slot requires a paired mentor in the
same instance who is mentor-ready for that exact skill (C5) — rejects a shadow
with no paired mentor, enforced by the pure rule and the chk_shadow_paired CHECK
constraint on slots. Verified the migration applies and the CHECK rejects a
trainee slot without a paired mentor. 22 tests named after each rule.
This commit is contained in:
Tiel Coder
2026-11-18 04:43:07 +00:00
parent 371524b33e
commit 676751c489
8 changed files with 2696 additions and 1 deletions
+55 -1
View File
@@ -6,7 +6,8 @@
*
* Deliberately NOT here (added in later units, do not add "while I'm in the schema"):
* - notification_templates/rules/outbox (later)
* - user_skills, skill_assessments, growth_goals, skill_equivalencies (C1-C6)
* - user_skills (C2), skill_assessments (C3), growth_goals (C6),
* skill_equivalencies (later)
* - service_role_requirements, unavailability, events, notifications outbox
*
* Conventions (ARCHITECTURE §4.2):
@@ -58,6 +59,17 @@ export type SkillLevel = (typeof SKILL_LEVELS)[number];
export const ASSESSMENT_KINDS = ["self", "mentor", "admin", "quiz"] as const;
export type AssessmentKind = (typeof ASSESSMENT_KINDS)[number];
/** Growth-goal lifecycle (ARCHITECTURE §5.8.6). */
export const GOAL_STATUSES = [
"requested",
"mentor_assigned",
"in_progress",
"achieved",
"paused",
"declined",
] as const;
export type GoalStatus = (typeof GOAL_STATUSES)[number];
const DEFAULT_TIMEZONE = "America/New_York";
/* ------------------------------------------------------------------ users */
@@ -235,6 +247,13 @@ export const slots = pgTable(
"chk_slots_single_parent",
sql`(service_instance_id IS NOT NULL) <> (event_id IS NOT NULL)`,
),
// A shadow/trainee slot only exists alongside a mentor in the same instance
// (ARCHITECTURE §5.8.5). The pure validation in src/domain/shadows.ts is the
// first line; this CHECK is the data-layer backstop.
check(
"chk_shadow_paired",
sql`not is_trainee_slot or paired_with_user_id is not null`,
),
],
);
@@ -545,6 +564,40 @@ export const skillAssessments = pgTable(
],
);
/* --------------------------------------------- growth_goals (C6) */
/**
* "I want to skill up" (ARCHITECTURE §4.3, §5.8.6). One active goal per
* (user_id, skill_id) — a volunteer can hold many skills at different levels but
* only one open goal per skill. Drives the mentor queue and the growth report.
*/
export const growthGoals = pgTable(
"growth_goals",
{
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
skillId: text("skill_id")
.notNull()
.references(() => skills.id, { onDelete: "cascade" }),
targetLevel: text("target_level", { enum: SKILL_LEVELS }).notNull(),
targetDate: date("target_date", { mode: "date" }),
status: text("status", { enum: GOAL_STATUSES }).notNull().default("requested"),
mentorUserId: text("mentor_user_id").references(() => users.id, { onDelete: "set null" }),
shadowCount: integer("shadow_count").notNull().default(0),
createdBy: text("created_by"),
createdAt: timestamp("created_at", { mode: "date", withTimezone: true })
.notNull()
.defaultNow(),
},
(table) => [
// One active goal per (user, skill).
uniqueIndex("growth_goals_user_skill_idx").on(table.userId, table.skillId),
index("growth_goals_status_idx").on(table.status),
],
);
/* ------------------------------------------------------------- relations */
/**
@@ -633,6 +686,7 @@ export const schema = {
positionSkills,
userSkills,
skillAssessments,
growthGoals,
};
export type Schema = typeof schema;