diff --git a/drizzle/0006_modern_shiver_man.sql b/drizzle/0006_modern_shiver_man.sql new file mode 100644 index 0000000..3d49ffe --- /dev/null +++ b/drizzle/0006_modern_shiver_man.sql @@ -0,0 +1,18 @@ +CREATE TABLE "skill_assessments" ( + "id" text PRIMARY KEY NOT NULL, + "user_id" text NOT NULL, + "skill_id" text NOT NULL, + "assessor_id" text, + "kind" text DEFAULT 'admin' NOT NULL, + "level_from" text NOT NULL, + "level_to" text NOT NULL, + "comfort_at_time" integer, + "notes" text, + "created_at" timestamp with time zone DEFAULT now() NOT NULL +); +--> statement-breakpoint +ALTER TABLE "skill_assessments" ADD CONSTRAINT "skill_assessments_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "skill_assessments" ADD CONSTRAINT "skill_assessments_skill_id_skills_id_fk" FOREIGN KEY ("skill_id") REFERENCES "public"."skills"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "skill_assessments" ADD CONSTRAINT "skill_assessments_assessor_id_users_id_fk" FOREIGN KEY ("assessor_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint +CREATE INDEX "skill_assessments_user_created_idx" ON "skill_assessments" USING btree ("user_id","created_at");--> statement-breakpoint +CREATE INDEX "skill_assessments_skill_idx" ON "skill_assessments" USING btree ("skill_id"); \ No newline at end of file diff --git a/drizzle/meta/0006_snapshot.json b/drizzle/meta/0006_snapshot.json new file mode 100644 index 0000000..16a7ecd --- /dev/null +++ b/drizzle/meta/0006_snapshot.json @@ -0,0 +1,2150 @@ +{ + "id": "c4ccc044-4f5d-4977-b199-df91479e1116", + "prevId": "a4a9d88a-c0d8-4c41-83f7-101cbc566cf1", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.assignments": { + "name": "assignments", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "slot_id": { + "name": "slot_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'proposed'" + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'manual'" + }, + "assigned_by": { + "name": "assigned_by", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "assigned_at": { + "name": "assigned_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "responded_at": { + "name": "responded_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "decline_reason": { + "name": "decline_reason", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "confirmed_via": { + "name": "confirmed_via", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "reminder_count": { + "name": "reminder_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + } + }, + "indexes": { + "uq_active_assignment": { + "name": "uq_active_assignment", + "columns": [ + { + "expression": "slot_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "where": "\"assignments\".\"status\" IN ('proposed','confirmed','completed')", + "concurrently": false, + "method": "btree", + "with": {} + }, + "assignments_user_idx": { + "name": "assignments_user_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "assignments_slot_id_slots_id_fk": { + "name": "assignments_slot_id_slots_id_fk", + "tableFrom": "assignments", + "tableTo": "slots", + "columnsFrom": [ + "slot_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "assignments_user_id_users_id_fk": { + "name": "assignments_user_id_users_id_fk", + "tableFrom": "assignments", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.functions": { + "name": "functions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "slug": { + "name": "slug", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "category_id": { + "name": "category_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "sort_order": { + "name": "sort_order", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "active": { + "name": "active", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "functions_slug_idx": { + "name": "functions_slug_idx", + "columns": [ + { + "expression": "slug", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "functions_active_idx": { + "name": "functions_active_idx", + "columns": [ + { + "expression": "active", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "functions_category_idx": { + "name": "functions_category_idx", + "columns": [ + { + "expression": "category_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "functions_category_id_skill_categories_id_fk": { + "name": "functions_category_id_skill_categories_id_fk", + "tableFrom": "functions", + "tableTo": "skill_categories", + "columnsFrom": [ + "category_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "functions_slug_unique": { + "name": "functions_slug_unique", + "nullsNotDistinct": false, + "columns": [ + "slug" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.notification_preferences": { + "name": "notification_preferences", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "email_enabled": { + "name": "email_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "sms_enabled": { + "name": "sms_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "sms_consent_at": { + "name": "sms_consent_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "sms_consent_source": { + "name": "sms_consent_source", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "sms_consent_ip": { + "name": "sms_consent_ip", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "reminder_lead_days": { + "name": "reminder_lead_days", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 7 + }, + "quiet_hours_start": { + "name": "quiet_hours_start", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'21:00'" + }, + "quiet_hours_end": { + "name": "quiet_hours_end", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'08:00'" + }, + "digest_only": { + "name": "digest_only", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + } + }, + "indexes": {}, + "foreignKeys": { + "notification_preferences_user_id_users_id_fk": { + "name": "notification_preferences_user_id_users_id_fk", + "tableFrom": "notification_preferences", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.position_skills": { + "name": "position_skills", + "schema": "", + "columns": { + "position_id": { + "name": "position_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "skill_id": { + "name": "skill_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "weight": { + "name": "weight", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 1 + }, + "required_level": { + "name": "required_level", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "is_core": { + "name": "is_core", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + } + }, + "indexes": { + "position_skills_skill_idx": { + "name": "position_skills_skill_idx", + "columns": [ + { + "expression": "skill_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "position_skills_position_id_roles_id_fk": { + "name": "position_skills_position_id_roles_id_fk", + "tableFrom": "position_skills", + "tableTo": "roles", + "columnsFrom": [ + "position_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + }, + "position_skills_skill_id_skills_id_fk": { + "name": "position_skills_skill_id_skills_id_fk", + "tableFrom": "position_skills", + "tableTo": "skills", + "columnsFrom": [ + "skill_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "position_skills_pk": { + "name": "position_skills_pk", + "columns": [ + "position_id", + "skill_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.roles": { + "name": "roles", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "slug": { + "name": "slug", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "color_hex": { + "name": "color_hex", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "call_time_offset_minutes": { + "name": "call_time_offset_minutes", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "requires_certification": { + "name": "requires_certification", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "default_headcount": { + "name": "default_headcount", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 1 + }, + "sort_order": { + "name": "sort_order", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "active": { + "name": "active", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "roles_slug_idx": { + "name": "roles_slug_idx", + "columns": [ + { + "expression": "slug", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "roles_active_idx": { + "name": "roles_active_idx", + "columns": [ + { + "expression": "active", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "roles_slug_unique": { + "name": "roles_slug_unique", + "nullsNotDistinct": false, + "columns": [ + "slug" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.service_instances": { + "name": "service_instances", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "service_id": { + "name": "service_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "occurs_on": { + "name": "occurs_on", + "type": "date", + "primaryKey": false, + "notNull": false + }, + "starts_at": { + "name": "starts_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "ends_at": { + "name": "ends_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "is_special": { + "name": "is_special", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "title_override": { + "name": "title_override", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'draft'" + }, + "published_at": { + "name": "published_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "published_by": { + "name": "published_by", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "si_service_starts_at_idx": { + "name": "si_service_starts_at_idx", + "columns": [ + { + "expression": "service_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "starts_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "si_occurs_on_idx": { + "name": "si_occurs_on_idx", + "columns": [ + { + "expression": "occurs_on", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "si_status_idx": { + "name": "si_status_idx", + "columns": [ + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "service_instances_service_id_services_id_fk": { + "name": "service_instances_service_id_services_id_fk", + "tableFrom": "service_instances", + "tableTo": "services", + "columnsFrom": [ + "service_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.services": { + "name": "services", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "location": { + "name": "location", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "timezone": { + "name": "timezone", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'America/New_York'" + }, + "rrule": { + "name": "rrule", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "start_time_local": { + "name": "start_time_local", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "duration_minutes": { + "name": "duration_minutes", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 75 + }, + "doors_open_minutes_before": { + "name": "doors_open_minutes_before", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "active": { + "name": "active", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "services_active_idx": { + "name": "services_active_idx", + "columns": [ + { + "expression": "active", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "services_name_unique": { + "name": "services_name_unique", + "nullsNotDistinct": false, + "columns": [ + "name" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.skill_assessments": { + "name": "skill_assessments", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "skill_id": { + "name": "skill_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "assessor_id": { + "name": "assessor_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "kind": { + "name": "kind", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'admin'" + }, + "level_from": { + "name": "level_from", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "level_to": { + "name": "level_to", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "comfort_at_time": { + "name": "comfort_at_time", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "skill_assessments_user_created_idx": { + "name": "skill_assessments_user_created_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "skill_assessments_skill_idx": { + "name": "skill_assessments_skill_idx", + "columns": [ + { + "expression": "skill_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "skill_assessments_user_id_users_id_fk": { + "name": "skill_assessments_user_id_users_id_fk", + "tableFrom": "skill_assessments", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "skill_assessments_skill_id_skills_id_fk": { + "name": "skill_assessments_skill_id_skills_id_fk", + "tableFrom": "skill_assessments", + "tableTo": "skills", + "columnsFrom": [ + "skill_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "skill_assessments_assessor_id_users_id_fk": { + "name": "skill_assessments_assessor_id_users_id_fk", + "tableFrom": "skill_assessments", + "tableTo": "users", + "columnsFrom": [ + "assessor_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.skill_categories": { + "name": "skill_categories", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "slug": { + "name": "slug", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "color_hex": { + "name": "color_hex", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "icon": { + "name": "icon", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "sort_order": { + "name": "sort_order", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "active": { + "name": "active", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "skill_categories_slug_idx": { + "name": "skill_categories_slug_idx", + "columns": [ + { + "expression": "slug", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "skill_categories_active_idx": { + "name": "skill_categories_active_idx", + "columns": [ + { + "expression": "active", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "skill_categories_slug_unique": { + "name": "skill_categories_slug_unique", + "nullsNotDistinct": false, + "columns": [ + "slug" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.skills": { + "name": "skills", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "slug": { + "name": "slug", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "category_id": { + "name": "category_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "technology_id": { + "name": "technology_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "function_id": { + "name": "function_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "dedupe_key": { + "name": "dedupe_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "mentor_min_level": { + "name": "mentor_min_level", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'expert'" + }, + "stale_after_days": { + "name": "stale_after_days", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 120 + }, + "recert_required": { + "name": "recert_required", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "sort_order": { + "name": "sort_order", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "active": { + "name": "active", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "skills_slug_idx": { + "name": "skills_slug_idx", + "columns": [ + { + "expression": "slug", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "skills_active_idx": { + "name": "skills_active_idx", + "columns": [ + { + "expression": "active", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "skills_category_idx": { + "name": "skills_category_idx", + "columns": [ + { + "expression": "category_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "skills_category_id_skill_categories_id_fk": { + "name": "skills_category_id_skill_categories_id_fk", + "tableFrom": "skills", + "tableTo": "skill_categories", + "columnsFrom": [ + "category_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + }, + "skills_technology_id_technologies_id_fk": { + "name": "skills_technology_id_technologies_id_fk", + "tableFrom": "skills", + "tableTo": "technologies", + "columnsFrom": [ + "technology_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + }, + "skills_function_id_functions_id_fk": { + "name": "skills_function_id_functions_id_fk", + "tableFrom": "skills", + "tableTo": "functions", + "columnsFrom": [ + "function_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "skills_slug_unique": { + "name": "skills_slug_unique", + "nullsNotDistinct": false, + "columns": [ + "slug" + ] + }, + "skills_dedupe_key_unique": { + "name": "skills_dedupe_key_unique", + "nullsNotDistinct": false, + "columns": [ + "dedupe_key" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.slots": { + "name": "slots", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "service_instance_id": { + "name": "service_instance_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "event_id": { + "name": "event_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "role_id": { + "name": "role_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "slot_index": { + "name": "slot_index", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "call_time": { + "name": "call_time", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'open'" + }, + "slot_note": { + "name": "slot_note", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "is_trainee_slot": { + "name": "is_trainee_slot", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "paired_with_user_id": { + "name": "paired_with_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "slots_instance_role_idx": { + "name": "slots_instance_role_idx", + "columns": [ + { + "expression": "service_instance_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "role_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "slot_index", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "slots_status_idx": { + "name": "slots_status_idx", + "columns": [ + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "slots_service_instance_id_service_instances_id_fk": { + "name": "slots_service_instance_id_service_instances_id_fk", + "tableFrom": "slots", + "tableTo": "service_instances", + "columnsFrom": [ + "service_instance_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "slots_role_id_roles_id_fk": { + "name": "slots_role_id_roles_id_fk", + "tableFrom": "slots", + "tableTo": "roles", + "columnsFrom": [ + "role_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + }, + "slots_paired_with_user_id_users_id_fk": { + "name": "slots_paired_with_user_id_users_id_fk", + "tableFrom": "slots", + "tableTo": "users", + "columnsFrom": [ + "paired_with_user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": { + "chk_slots_single_parent": { + "name": "chk_slots_single_parent", + "value": "(service_instance_id IS NOT NULL) <> (event_id IS NOT NULL)" + } + }, + "isRLSEnabled": false + }, + "public.technologies": { + "name": "technologies", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "slug": { + "name": "slug", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "vendor_id": { + "name": "vendor_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "category_id": { + "name": "category_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "model_number": { + "name": "model_number", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "version_label": { + "name": "version_label", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "docs_url": { + "name": "docs_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "purchased_on": { + "name": "purchased_on", + "type": "date", + "primaryKey": false, + "notNull": false + }, + "serial_number": { + "name": "serial_number", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "retired_at": { + "name": "retired_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "successor_technology_id": { + "name": "successor_technology_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "sort_order": { + "name": "sort_order", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "active": { + "name": "active", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "technologies_slug_idx": { + "name": "technologies_slug_idx", + "columns": [ + { + "expression": "slug", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "technologies_active_idx": { + "name": "technologies_active_idx", + "columns": [ + { + "expression": "active", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "technologies_vendor_idx": { + "name": "technologies_vendor_idx", + "columns": [ + { + "expression": "vendor_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "technologies_category_idx": { + "name": "technologies_category_idx", + "columns": [ + { + "expression": "category_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "technologies_vendor_id_vendors_id_fk": { + "name": "technologies_vendor_id_vendors_id_fk", + "tableFrom": "technologies", + "tableTo": "vendors", + "columnsFrom": [ + "vendor_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + }, + "technologies_category_id_skill_categories_id_fk": { + "name": "technologies_category_id_skill_categories_id_fk", + "tableFrom": "technologies", + "tableTo": "skill_categories", + "columnsFrom": [ + "category_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "restrict", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "technologies_slug_unique": { + "name": "technologies_slug_unique", + "nullsNotDistinct": false, + "columns": [ + "slug" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.user_skills": { + "name": "user_skills", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "skill_id": { + "name": "skill_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "self_comfort": { + "name": "self_comfort", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "self_rated_at": { + "name": "self_rated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "verified_level": { + "name": "verified_level", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'none'" + }, + "verified_by": { + "name": "verified_by", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "verified_at": { + "name": "verified_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "mentor_opt_in": { + "name": "mentor_opt_in", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "last_served_on": { + "name": "last_served_on", + "type": "date", + "primaryKey": false, + "notNull": false + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "user_skills_user_skill_idx": { + "name": "user_skills_user_skill_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "skill_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "user_skills_skill_idx": { + "name": "user_skills_skill_idx", + "columns": [ + { + "expression": "skill_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "user_skills_user_id_users_id_fk": { + "name": "user_skills_user_id_users_id_fk", + "tableFrom": "user_skills", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "user_skills_skill_id_skills_id_fk": { + "name": "user_skills_skill_id_skills_id_fk", + "tableFrom": "user_skills", + "tableTo": "skills", + "columnsFrom": [ + "skill_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "phone_e164": { + "name": "phone_e164", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "first_name": { + "name": "first_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "last_name": { + "name": "last_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "display_name": { + "name": "display_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "system_role": { + "name": "system_role", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'volunteer'" + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'pending_approval'" + }, + "avatar_url": { + "name": "avatar_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "timezone": { + "name": "timezone", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'America/New_York'" + }, + "emergency_contact": { + "name": "emergency_contact", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "approved_by": { + "name": "approved_by", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "approved_at": { + "name": "approved_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "last_served_at": { + "name": "last_served_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "reliability_score": { + "name": "reliability_score", + "type": "numeric(5, 2)", + "primaryKey": false, + "notNull": false + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "deactivated_at": { + "name": "deactivated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "users_email_lower_idx": { + "name": "users_email_lower_idx", + "columns": [ + { + "expression": "lower(\"email\")", + "asc": true, + "isExpression": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "users_status_idx": { + "name": "users_status_idx", + "columns": [ + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "users_system_role_idx": { + "name": "users_system_role_idx", + "columns": [ + { + "expression": "system_role", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "users_email_unique": { + "name": "users_email_unique", + "nullsNotDistinct": false, + "columns": [ + "email" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.vendors": { + "name": "vendors", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "slug": { + "name": "slug", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "support_url": { + "name": "support_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "support_phone": { + "name": "support_phone", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "sort_order": { + "name": "sort_order", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "active": { + "name": "active", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "vendors_slug_idx": { + "name": "vendors_slug_idx", + "columns": [ + { + "expression": "slug", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "vendors_active_idx": { + "name": "vendors_active_idx", + "columns": [ + { + "expression": "active", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "vendors_slug_unique": { + "name": "vendors_slug_unique", + "nullsNotDistinct": false, + "columns": [ + "slug" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index 6e5d92e..78e568d 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -43,6 +43,13 @@ "when": 1794973120521, "tag": "0005_keen_lenny_balinger", "breakpoints": true + }, + { + "idx": 6, + "version": "7", + "when": 1794975448394, + "tag": "0006_modern_shiver_man", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/app/api/dev/skills/assessments/route.ts b/src/app/api/dev/skills/assessments/route.ts new file mode 100644 index 0000000..6f0942c --- /dev/null +++ b/src/app/api/dev/skills/assessments/route.ts @@ -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 }); +}; diff --git a/src/app/api/dev/skills/matrix/route.ts b/src/app/api/dev/skills/matrix/route.ts new file mode 100644 index 0000000..c7952db --- /dev/null +++ b/src/app/api/dev/skills/matrix/route.ts @@ -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 }); +}; diff --git a/src/app/api/dev/skills/verify/route.ts b/src/app/api/dev/skills/verify/route.ts new file mode 100644 index 0000000..ad71cf6 --- /dev/null +++ b/src/app/api/dev/skills/verify/route.ts @@ -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); + } +}; diff --git a/src/db/assessments.ts b/src/db/assessments.ts new file mode 100644 index 0000000..5c0da8d --- /dev/null +++ b/src/db/assessments.ts @@ -0,0 +1,229 @@ +/** + * Skill assessment data layer — unit C3 (ARCHITECTURE §4.3). + * + * Two responsibilities: + * 1. `verifySkillLevel` — the one write path that moves a volunteer's + * `verified_level`. It appends a `skill_assessments` row and updates + * `user_skills` in a single transaction, so a level can never move without + * its audit row. It deliberately does NOT touch `self_comfort` / + * `self_rated_at` — those are the volunteer's own signal (C2). + * 2. The read models behind the admin matrix (grid + filters) and the + * assessment history beside the verify modal. + * + * Append-only: there is no update or delete function for `skill_assessments`. + */ + +import { and, asc, desc, eq, inArray, sql, type SQL } from "drizzle-orm"; +import { db } from "@/db"; +import { + skillAssessments, + skillCategories, + skills, + userSkills, + users, + type AssessmentKind, + type SkillLevel, +} from "@/db/schema"; +import { buildAssessment } from "@/domain/assessments"; + +/** Validation failure from a verify request (route maps this to 400). */ +export class AssessmentError extends Error { + constructor(readonly problems: string[]) { + super(problems[0] ?? "invalid assessment"); + this.name = "AssessmentError"; + } +} + +/** Display name expression shared by the read models. */ +const userName = sql`coalesce(${users.displayName}, ${users.firstName} || ' ' || ${users.lastName})`; + +export type VerifyInput = { + userId: string; + skillId: string; + /** Requested new verified level (validated by the domain rule). */ + level: string; + /** Mentor/admin signing off; null for self/quiz assessments. */ + assessorId?: string | null; + kind?: AssessmentKind; + notes?: string | null; +}; + +/** + * Verify a volunteer's level: append the assessment, move the gate. Both rows + * are written in one transaction; if either fails nothing changes. Verifying a + * skill the volunteer has never rated is allowed — the `user_skills` record is + * created with `self_comfort` null (verification is not self-report). + */ +export async function verifySkillLevel(input: VerifyInput) { + return db.transaction(async (tx) => { + const [existing] = await tx + .select() + .from(userSkills) + .where( + and(eq(userSkills.userId, input.userId), eq(userSkills.skillId, input.skillId)), + ) + .limit(1); + + const result = buildAssessment({ + currentLevel: existing?.verifiedLevel ?? "none", + newLevel: input.level, + selfComfort: existing?.selfComfort ?? null, + }); + if (!result.ok) throw new AssessmentError(result.problems); + + const now = new Date(); + const [assessment] = await tx + .insert(skillAssessments) + .values({ + id: crypto.randomUUID(), + userId: input.userId, + skillId: input.skillId, + assessorId: input.assessorId ?? null, + kind: input.kind ?? "admin", + levelFrom: result.draft.levelFrom, + levelTo: result.draft.levelTo, + comfortAtTime: result.draft.comfortAtTime, + notes: input.notes ?? null, + createdAt: now, + }) + .returning(); + + let userSkill; + if (existing) { + [userSkill] = await tx + .update(userSkills) + .set({ + verifiedLevel: result.draft.levelTo, + verifiedBy: input.assessorId ?? null, + verifiedAt: now, + // self_comfort / self_rated_at intentionally untouched (C2 rule). + }) + .where(eq(userSkills.id, existing.id)) + .returning(); + } else { + [userSkill] = await tx + .insert(userSkills) + .values({ + id: crypto.randomUUID(), + userId: input.userId, + skillId: input.skillId, + selfComfort: null, + selfRatedAt: null, + verifiedLevel: result.draft.levelTo, + verifiedBy: input.assessorId ?? null, + verifiedAt: now, + }) + .returning(); + } + + return { assessment, userSkill }; + }); +} + +export type MatrixFilters = { + userId?: string; + skillId?: string; + categoryId?: string; + /** Only rows currently at this verified level. */ + level?: SkillLevel; +}; + +/** + * The admin skills matrix: one row per competency record, joined to the person + * and the skill, grouped by category → skill → person. Filters compose (all + * are ANDed). Rows without a slider or a verification simply do not exist yet; + * the grid is not a cross-product of every user and every skill. + */ +export async function listSkillsMatrix(filters: MatrixFilters = {}) { + const conditions: SQL[] = []; + if (filters.userId) conditions.push(eq(userSkills.userId, filters.userId)); + if (filters.skillId) conditions.push(eq(userSkills.skillId, filters.skillId)); + if (filters.categoryId) conditions.push(eq(skills.categoryId, filters.categoryId)); + if (filters.level) conditions.push(eq(userSkills.verifiedLevel, filters.level)); + + return db + .select({ + userId: userSkills.userId, + userName, + email: users.email, + skillId: userSkills.skillId, + skillName: skills.name, + categoryId: skills.categoryId, + categoryName: skillCategories.name, + selfComfort: userSkills.selfComfort, + selfRatedAt: userSkills.selfRatedAt, + verifiedLevel: userSkills.verifiedLevel, + verifiedBy: userSkills.verifiedBy, + verifiedAt: userSkills.verifiedAt, + lastServedOn: userSkills.lastServedOn, + }) + .from(userSkills) + .innerJoin(users, eq(userSkills.userId, users.id)) + .innerJoin(skills, eq(userSkills.skillId, skills.id)) + .innerJoin(skillCategories, eq(skills.categoryId, skillCategories.id)) + .where(conditions.length > 0 ? and(...conditions) : undefined) + .orderBy( + asc(skillCategories.name), + asc(skills.name), + asc(users.firstName), + asc(users.lastName), + ); +} + +export type AssessmentFilters = { + userId?: string; + skillId?: string; + limit?: number; +}; + +/** + * Assessment history, newest first — the rows the verify modal shows after a + * save. Assessor names are resolved in JS (same approach as the position + * requirements join) to avoid a second self-join alias. + */ +export async function listAssessments(filters: AssessmentFilters = {}) { + const conditions: SQL[] = []; + if (filters.userId) conditions.push(eq(skillAssessments.userId, filters.userId)); + if (filters.skillId) conditions.push(eq(skillAssessments.skillId, filters.skillId)); + + const limit = Math.min(Math.max(filters.limit ?? 50, 1), 200); + const rows = await db + .select({ + id: skillAssessments.id, + userId: skillAssessments.userId, + userName, + skillId: skillAssessments.skillId, + skillName: skills.name, + assessorId: skillAssessments.assessorId, + kind: skillAssessments.kind, + levelFrom: skillAssessments.levelFrom, + levelTo: skillAssessments.levelTo, + comfortAtTime: skillAssessments.comfortAtTime, + notes: skillAssessments.notes, + createdAt: skillAssessments.createdAt, + }) + .from(skillAssessments) + .innerJoin(users, eq(skillAssessments.userId, users.id)) + .innerJoin(skills, eq(skillAssessments.skillId, skills.id)) + .where(conditions.length > 0 ? and(...conditions) : undefined) + .orderBy(desc(skillAssessments.createdAt)) + .limit(limit); + + const assessorIds = [...new Set(rows.map((row) => row.assessorId).filter(Boolean))] as string[]; + const assessors = + assessorIds.length > 0 + ? await db + .select({ + id: users.id, + name: userName, + }) + .from(users) + .where(inArray(users.id, assessorIds)) + : []; + const nameById = new Map(assessors.map((a) => [a.id, a.name])); + + return rows.map((row) => ({ + ...row, + assessorName: row.assessorId ? (nameById.get(row.assessorId) ?? null) : null, + })); +} diff --git a/src/db/schema.ts b/src/db/schema.ts index 33a90ba..422d9e1 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -54,6 +54,10 @@ export type SmsConsentSource = (typeof SMS_CONSENT_SOURCES)[number]; export const SKILL_LEVELS = ["none", "observer", "assisted", "qualified", "expert"] as const; export type SkillLevel = (typeof SKILL_LEVELS)[number]; +/** Who produced an assessment row — durable evidence for the audit trail. */ +export const ASSESSMENT_KINDS = ["self", "mentor", "admin", "quiz"] as const; +export type AssessmentKind = (typeof ASSESSMENT_KINDS)[number]; + const DEFAULT_TIMEZONE = "America/New_York"; /* ------------------------------------------------------------------ users */ @@ -502,14 +506,54 @@ export const userSkills = pgTable( ], ); +/* ------------------------------------------- skill_assessments (C3) */ + +/** + * Append-only progression history (ARCHITECTURE §4.3). Every movement of a + * volunteer's verified level — up or down — writes exactly one row here, and + * there is deliberately no update or delete path: this table is the audit trail + * that answers "why was this person cleared?". `comfort_at_time` snapshots the + * volunteer's self-reported comfort at the moment of the decision, so a later + * slider move cannot rewrite the context the assessor saw. + */ +export const skillAssessments = pgTable( + "skill_assessments", + { + id: text("id").primaryKey(), + userId: text("user_id") + .notNull() + .references(() => users.id, { onDelete: "cascade" }), + skillId: text("skill_id") + .notNull() + .references(() => skills.id, { onDelete: "cascade" }), + /** Mentor/admin who signed off; null for self or quiz assessments. */ + assessorId: text("assessor_id").references(() => users.id, { onDelete: "set null" }), + kind: text("kind", { enum: ASSESSMENT_KINDS }).notNull().default("admin"), + /** The verified level before this assessment (server-owned, never client-set). */ + levelFrom: text("level_from", { enum: SKILL_LEVELS }).notNull(), + levelTo: text("level_to", { enum: SKILL_LEVELS }).notNull(), + /** Snapshot of `user_skills.self_comfort` when the assessment was recorded. */ + comfortAtTime: integer("comfort_at_time"), + notes: text("notes"), + createdAt: timestamp("created_at", { mode: "date", withTimezone: true }) + .notNull() + .defaultNow(), + }, + (table) => [ + index("skill_assessments_user_created_idx").on(table.userId, table.createdAt), + index("skill_assessments_skill_idx").on(table.skillId), + ], +); + /* ------------------------------------------------------------- relations */ /** * Skills compose from the four catalog tables (B3). `skills`, - * `position_skills` and `user_skills` are intentionally excluded from `relations`: - * their FK columns are themselves part of composite/unique keys, which trips - * Drizzle 0.45.4's `one()` typing (see the B2 note). Queried directly until a - * working eager-load pattern exists — the grid render in I2 pre-resolves the joins. + * `position_skills`, `user_skills` and `skill_assessments` are intentionally + * excluded from `relations`: their FK columns are themselves part of + * composite/unique keys, which trips Drizzle 0.45.4's `one()` typing (see the + * B2 note). Queried directly until a working eager-load pattern exists — the + * grid render in I2 pre-resolves the joins. */ export const usersRelations = relations(users, ({ many }) => ({ assignments: many(assignments), @@ -588,6 +632,7 @@ export const schema = { skills, positionSkills, userSkills, + skillAssessments, }; export type Schema = typeof schema; diff --git a/src/domain/assessments.test.ts b/src/domain/assessments.test.ts new file mode 100644 index 0000000..c595b30 --- /dev/null +++ b/src/domain/assessments.test.ts @@ -0,0 +1,101 @@ +import { describe, expect, it } from "vitest"; +import { + buildAssessment, + isSkillLevel, + levelRank, + SKILL_LEVELS, +} from "./assessments"; +import { SKILL_LEVELS as DB_SKILL_LEVELS } from "@/db/schema"; + +describe("skill level ladder", () => { + it("orders none < observer < assisted < qualified < expert", () => { + expect(["none", "observer", "assisted", "qualified", "expert"].map(levelRank)).toEqual([ + 0, 1, 2, 3, 4, + ]); + }); + + it("keeps the domain ladder in sync with the database enum", () => { + expect([...SKILL_LEVELS]).toEqual([...DB_SKILL_LEVELS]); + }); + + it("recognizes only real levels", () => { + expect(isSkillLevel("qualified")).toBe(true); + expect(isSkillLevel("grandmaster")).toBe(false); + expect(isSkillLevel(null)).toBe(false); + }); +}); + +describe("buildAssessment", () => { + it("records an upward move with level_from taken from the stored level", () => { + const result = buildAssessment({ + currentLevel: "none", + newLevel: "qualified", + selfComfort: 55, + }); + expect(result.ok).toBe(true); + if (!result.ok) return; + expect(result.draft).toEqual({ + levelFrom: "none", + levelTo: "qualified", + comfortAtTime: 55, + direction: "up", + }); + }); + + it("records a downward move too — demotions are audited", () => { + const result = buildAssessment({ + currentLevel: "expert", + newLevel: "assisted", + selfComfort: 40, + }); + expect(result.ok).toBe(true); + if (!result.ok) return; + expect(result.draft.direction).toBe("down"); + expect(result.draft.levelFrom).toBe("expert"); + expect(result.draft.levelTo).toBe("assisted"); + }); + + it("rejects an unknown new level instead of storing it", () => { + const result = buildAssessment({ + currentLevel: "none", + newLevel: "grandmaster", + selfComfort: null, + }); + expect(result.ok).toBe(false); + if (result.ok) return; + expect(result.problems[0]).toContain("observer"); + }); + + it("rejects an unknown current level", () => { + const result = buildAssessment({ + currentLevel: "", + newLevel: "qualified", + selfComfort: null, + }); + expect(result.ok).toBe(false); + if (result.ok) return; + expect(result.problems[0]).toContain("unknown current level"); + }); + + it("refuses an unchanged level — no silent no-op row", () => { + const result = buildAssessment({ + currentLevel: "assisted", + newLevel: "assisted", + selfComfort: 70, + }); + expect(result.ok).toBe(false); + if (result.ok) return; + expect(result.problems[0]).toContain("unchanged"); + }); + + it("snapshots a null comfort when the volunteer never rated the skill", () => { + const result = buildAssessment({ + currentLevel: "none", + newLevel: "observer", + selfComfort: null, + }); + expect(result.ok).toBe(true); + if (!result.ok) return; + expect(result.draft.comfortAtTime).toBeNull(); + }); +}); diff --git a/src/domain/assessments.ts b/src/domain/assessments.ts new file mode 100644 index 0000000..c268307 --- /dev/null +++ b/src/domain/assessments.ts @@ -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", + }, + }; +}