From efa6c38ee467683523f2233b04c90a3a67ea8f33 Mon Sep 17 00:00:00 2001 From: Cato <41178744+catomean@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:21:02 +0200 Subject: [PATCH] feat(plan): "Did it help?" under every step, and the next plan learns from it (#65) Each step in "Your steps" can be marked "I did this" or "Not for me", then answered: did it help? Yes, a little, no. Answers are kept per person and step (step_outcomes), so the same step in two plans is one experience, and "Change" takes one back. The next assessment reads them: a step that helped counts like something the person already does (the worry reads as covered), and one that did not help or was not wanted is set aside, listed as such, with the next option recommended and a line saying why. With nothing said, a plan is exactly as before (tested). One form per step with a button per answer: works without JavaScript and the choices sit as equal cells. The roadmap item moves to the changelog. Claude-Session: https://claude.ai/code/session_01TTH5cy8bKmEqzdTyAsbbT8 Co-authored-by: Claude --- drizzle/0016_step_outcomes.sql | 8 + drizzle/meta/0016_snapshot.json | 2017 +++++++++++++++++++++++ drizzle/meta/_journal.json | 7 + src/app/(app)/assessments/[id]/page.tsx | 5 +- src/app/(app)/assessments/actions.ts | 22 + src/components/safety-plan.tsx | 21 +- src/components/step-outcome.tsx | 57 + src/config/development.ts | 13 +- src/db/schema/index.ts | 1 + src/db/schema/outcomes.ts | 17 + src/domain/safety-plan.ts | 25 +- src/domain/step-outcomes.test.ts | 57 + src/domain/step-outcomes.ts | 46 + src/server/assessments.ts | 6 +- src/server/step-outcomes.test.ts | 32 + src/server/step-outcomes.ts | 38 + 16 files changed, 2352 insertions(+), 20 deletions(-) create mode 100644 drizzle/0016_step_outcomes.sql create mode 100644 drizzle/meta/0016_snapshot.json create mode 100644 src/components/step-outcome.tsx create mode 100644 src/db/schema/outcomes.ts create mode 100644 src/domain/step-outcomes.test.ts create mode 100644 src/domain/step-outcomes.ts create mode 100644 src/server/step-outcomes.test.ts create mode 100644 src/server/step-outcomes.ts diff --git a/drizzle/0016_step_outcomes.sql b/drizzle/0016_step_outcomes.sql new file mode 100644 index 0000000..17df18e --- /dev/null +++ b/drizzle/0016_step_outcomes.sql @@ -0,0 +1,8 @@ +CREATE TABLE "step_outcomes" ( + "sub" text NOT NULL, + "intervention_key" text NOT NULL, + "status" text NOT NULL, + "helped" text, + "updated_at" timestamp with time zone DEFAULT now() NOT NULL, + CONSTRAINT "step_outcomes_sub_intervention_key_pk" PRIMARY KEY("sub","intervention_key") +); diff --git a/drizzle/meta/0016_snapshot.json b/drizzle/meta/0016_snapshot.json new file mode 100644 index 0000000..faa2855 --- /dev/null +++ b/drizzle/meta/0016_snapshot.json @@ -0,0 +1,2017 @@ +{ + "id": "a7554ccd-b4f3-444b-94db-96e06f59b53c", + "prevId": "bdfc3ac3-0c0d-4ca9-bf41-79ad58aab937", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.course_interests": { + "name": "course_interests", + "schema": "", + "columns": { + "sub": { + "name": "sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "course_key": { + "name": "course_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": { + "course_interests_sub_course_key_pk": { + "name": "course_interests_sub_course_key_pk", + "columns": [ + "sub", + "course_key" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.assessments": { + "name": "assessments", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "customer_sub": { + "name": "customer_sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "environment_id": { + "name": "environment_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "place_name": { + "name": "place_name", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "''" + }, + "protecting": { + "name": "protecting", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "concerns": { + "name": "concerns", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "exposures": { + "name": "exposures", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "threats": { + "name": "threats", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "measures": { + "name": "measures", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "budget": { + "name": "budget", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "upcoming": { + "name": "upcoming", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "''" + }, + "plan": { + "name": "plan", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "assessments_customer_idx": { + "name": "assessments_customer_idx", + "columns": [ + { + "expression": "customer_sub", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "assessments_environment_id_environments_id_fk": { + "name": "assessments_environment_id_environments_id_fk", + "tableFrom": "assessments", + "tableTo": "environments", + "columnsFrom": [ + "environment_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.environments": { + "name": "environments", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "owner_sub": { + "name": "owner_sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "environment_type", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "area": { + "name": "area", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "environments_owner_idx": { + "name": "environments_owner_idx", + "columns": [ + { + "expression": "owner_sub", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.audit_events": { + "name": "audit_events", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "actor_sub": { + "name": "actor_sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "action": { + "name": "action", + "type": "audit_action", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "subject_type": { + "name": "subject_type", + "type": "audit_subject", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "subject_id": { + "name": "subject_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "detail": { + "name": "detail", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "at": { + "name": "at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "audit_events_at_idx": { + "name": "audit_events_at_idx", + "columns": [ + { + "expression": "at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "audit_events_subject_idx": { + "name": "audit_events_subject_idx", + "columns": [ + { + "expression": "subject_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "subject_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.booking_events": { + "name": "booking_events", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "booking_id": { + "name": "booking_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "action": { + "name": "action", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "from_status": { + "name": "from_status", + "type": "booking_status", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "to_status": { + "name": "to_status", + "type": "booking_status", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "actor_role": { + "name": "actor_role", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "actor_sub": { + "name": "actor_sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "protector_id": { + "name": "protector_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "match_reasons": { + "name": "match_reasons", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "note": { + "name": "note", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "at": { + "name": "at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "booking_events_booking_idx": { + "name": "booking_events_booking_idx", + "columns": [ + { + "expression": "booking_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "booking_events_booking_id_bookings_id_fk": { + "name": "booking_events_booking_id_bookings_id_fk", + "tableFrom": "booking_events", + "tableTo": "bookings", + "columnsFrom": [ + "booking_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "booking_events_protector_id_protectors_id_fk": { + "name": "booking_events_protector_id_protectors_id_fk", + "tableFrom": "booking_events", + "tableTo": "protectors", + "columnsFrom": [ + "protector_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.bookings": { + "name": "bookings", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "customer_sub": { + "name": "customer_sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "service": { + "name": "service", + "type": "service", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "booking_status", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'REQUESTED'" + }, + "starts_at": { + "name": "starts_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "hours": { + "name": "hours", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "area": { + "name": "area", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "meeting_point": { + "name": "meeting_point", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "''" + }, + "languages": { + "name": "languages", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "presence_style": { + "name": "presence_style", + "type": "presence_style", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "required_capabilities": { + "name": "required_capabilities", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "hard_constraints": { + "name": "hard_constraints", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "protector_id": { + "name": "protector_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "payment_status": { + "name": "payment_status", + "type": "payment_status", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "bookings_customer_idx": { + "name": "bookings_customer_idx", + "columns": [ + { + "expression": "customer_sub", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "bookings_protector_idx": { + "name": "bookings_protector_idx", + "columns": [ + { + "expression": "protector_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "bookings_status_idx": { + "name": "bookings_status_idx", + "columns": [ + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "bookings_protector_id_protectors_id_fk": { + "name": "bookings_protector_id_protectors_id_fk", + "tableFrom": "bookings", + "tableTo": "protectors", + "columnsFrom": [ + "protector_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": { + "bookings_hours_positive": { + "name": "bookings_hours_positive", + "value": "\"bookings\".\"hours\" > 0" + } + }, + "isRLSEnabled": false + }, + "public.ratings": { + "name": "ratings", + "schema": "", + "columns": { + "booking_id": { + "name": "booking_id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "respect": { + "name": "respect", + "type": "smallint", + "primaryKey": false, + "notNull": true + }, + "discretion": { + "name": "discretion", + "type": "smallint", + "primaryKey": false, + "notNull": true + }, + "felt_safe": { + "name": "felt_safe", + "type": "smallint", + "primaryKey": false, + "notNull": true + }, + "professionalism": { + "name": "professionalism", + "type": "smallint", + "primaryKey": false, + "notNull": false + }, + "communication": { + "name": "communication", + "type": "smallint", + "primaryKey": false, + "notNull": false + }, + "punctuality": { + "name": "punctuality", + "type": "smallint", + "primaryKey": false, + "notNull": false + }, + "judgment": { + "name": "judgment", + "type": "smallint", + "primaryKey": false, + "notNull": false + }, + "comment": { + "name": "comment", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "''" + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "ratings_booking_id_bookings_id_fk": { + "name": "ratings_booking_id_bookings_id_fk", + "tableFrom": "ratings", + "tableTo": "bookings", + "columnsFrom": [ + "booking_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": { + "ratings_respect_range": { + "name": "ratings_respect_range", + "value": "\"ratings\".\"respect\" BETWEEN 1 AND 5" + }, + "ratings_discretion_range": { + "name": "ratings_discretion_range", + "value": "\"ratings\".\"discretion\" BETWEEN 1 AND 5" + }, + "ratings_felt_safe_range": { + "name": "ratings_felt_safe_range", + "value": "\"ratings\".\"felt_safe\" BETWEEN 1 AND 5" + }, + "ratings_professionalism_range": { + "name": "ratings_professionalism_range", + "value": "\"ratings\".\"professionalism\" BETWEEN 1 AND 5" + }, + "ratings_communication_range": { + "name": "ratings_communication_range", + "value": "\"ratings\".\"communication\" BETWEEN 1 AND 5" + }, + "ratings_punctuality_range": { + "name": "ratings_punctuality_range", + "value": "\"ratings\".\"punctuality\" BETWEEN 1 AND 5" + }, + "ratings_judgment_range": { + "name": "ratings_judgment_range", + "value": "\"ratings\".\"judgment\" BETWEEN 1 AND 5" + } + }, + "isRLSEnabled": false + }, + "public.reports": { + "name": "reports", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "booking_id": { + "name": "booking_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "protector_id": { + "name": "protector_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "kind": { + "name": "kind", + "type": "report_kind", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "summary": { + "name": "summary", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "police_involved": { + "name": "police_involved", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "observations": { + "name": "observations", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "severity": { + "name": "severity", + "type": "incident_severity", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "review": { + "name": "review", + "type": "incident_review", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "review_note": { + "name": "review_note", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "reviewed_by": { + "name": "reviewed_by", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "reviewed_at": { + "name": "reviewed_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "reports_booking_idx": { + "name": "reports_booking_idx", + "columns": [ + { + "expression": "booking_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "reports_review_idx": { + "name": "reports_review_idx", + "columns": [ + { + "expression": "review", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "reports_booking_id_bookings_id_fk": { + "name": "reports_booking_id_bookings_id_fk", + "tableFrom": "reports", + "tableTo": "bookings", + "columnsFrom": [ + "booking_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "reports_protector_id_protectors_id_fk": { + "name": "reports_protector_id_protectors_id_fk", + "tableFrom": "reports", + "tableTo": "protectors", + "columnsFrom": [ + "protector_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": { + "reports_incident_review": { + "name": "reports_incident_review", + "value": "(\"reports\".\"kind\" = 'INCIDENT') = (\"reports\".\"review\" IS NOT NULL)" + }, + "reports_incident_severity": { + "name": "reports_incident_severity", + "value": "\"reports\".\"severity\" IS NULL OR \"reports\".\"kind\" = 'INCIDENT'" + } + }, + "isRLSEnabled": false + }, + "public.complaints": { + "name": "complaints", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "booking_id": { + "name": "booking_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "customer_sub": { + "name": "customer_sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "protector_id": { + "name": "protector_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "category": { + "name": "category", + "type": "complaint_category", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "body": { + "name": "body", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "complaint_status", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'RECEIVED'" + }, + "summary_for_protector": { + "name": "summary_for_protector", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "protector_response": { + "name": "protector_response", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "appeal": { + "name": "appeal", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "decision_note": { + "name": "decision_note", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "decided_by": { + "name": "decided_by", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "decided_at": { + "name": "decided_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "complaints_status_idx": { + "name": "complaints_status_idx", + "columns": [ + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "complaints_protector_idx": { + "name": "complaints_protector_idx", + "columns": [ + { + "expression": "protector_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "complaints_booking_idx": { + "name": "complaints_booking_idx", + "columns": [ + { + "expression": "booking_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "complaints_booking_id_bookings_id_fk": { + "name": "complaints_booking_id_bookings_id_fk", + "tableFrom": "complaints", + "tableTo": "bookings", + "columnsFrom": [ + "booking_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "complaints_protector_id_protectors_id_fk": { + "name": "complaints_protector_id_protectors_id_fk", + "tableFrom": "complaints", + "tableTo": "protectors", + "columnsFrom": [ + "protector_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.inbound_mail": { + "name": "inbound_mail", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "message_id": { + "name": "message_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "from_address": { + "name": "from_address", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "from_name": { + "name": "from_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "subject": { + "name": "subject", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "body": { + "name": "body", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "received_at": { + "name": "received_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "suspect": { + "name": "suspect", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "kind": { + "name": "kind", + "type": "mail_kind", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "urgency": { + "name": "urgency", + "type": "mail_urgency", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "summary": { + "name": "summary", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "language": { + "name": "language", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "needs_human": { + "name": "needs_human", + "type": "boolean", + "primaryKey": false, + "notNull": true + }, + "triaged_by": { + "name": "triaged_by", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "replied_at": { + "name": "replied_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "handled_at": { + "name": "handled_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "handled_by": { + "name": "handled_by", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "inbound_mail_received_idx": { + "name": "inbound_mail_received_idx", + "columns": [ + { + "expression": "received_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.step_outcomes": { + "name": "step_outcomes", + "schema": "", + "columns": { + "sub": { + "name": "sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "intervention_key": { + "name": "intervention_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "helped": { + "name": "helped", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": { + "step_outcomes_sub_intervention_key_pk": { + "name": "step_outcomes_sub_intervention_key_pk", + "columns": [ + "sub", + "intervention_key" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.customer_profiles": { + "name": "customer_profiles", + "schema": "", + "columns": { + "sub": { + "name": "sub", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "preferred_name": { + "name": "preferred_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.preference_profiles": { + "name": "preference_profiles", + "schema": "", + "columns": { + "sub": { + "name": "sub", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "hard_constraints": { + "name": "hard_constraints", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "presence_style": { + "name": "presence_style", + "type": "presence_style", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "languages": { + "name": "languages", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "axes": { + "name": "axes", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "values_note": { + "name": "values_note", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "''" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.protector_availability": { + "name": "protector_availability", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "protector_id": { + "name": "protector_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "weekday": { + "name": "weekday", + "type": "smallint", + "primaryKey": false, + "notNull": true + }, + "start_minute": { + "name": "start_minute", + "type": "smallint", + "primaryKey": false, + "notNull": true + }, + "duration_minutes": { + "name": "duration_minutes", + "type": "smallint", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "protector_availability_protector_id_protectors_id_fk": { + "name": "protector_availability_protector_id_protectors_id_fk", + "tableFrom": "protector_availability", + "tableTo": "protectors", + "columnsFrom": [ + "protector_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "protector_availability_day": { + "name": "protector_availability_day", + "nullsNotDistinct": false, + "columns": [ + "protector_id", + "weekday" + ] + } + }, + "policies": {}, + "checkConstraints": { + "availability_weekday": { + "name": "availability_weekday", + "value": "\"protector_availability\".\"weekday\" BETWEEN 1 AND 7" + }, + "availability_start": { + "name": "availability_start", + "value": "\"protector_availability\".\"start_minute\" BETWEEN 0 AND 1439" + }, + "availability_duration": { + "name": "availability_duration", + "value": "\"protector_availability\".\"duration_minutes\" BETWEEN 1 AND 1440" + } + }, + "isRLSEnabled": false + }, + "public.protector_capabilities": { + "name": "protector_capabilities", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "protector_id": { + "name": "protector_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "capability": { + "name": "capability", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "level": { + "name": "level", + "type": "capability_level", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "verification": { + "name": "verification", + "type": "verification_status", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'SELF_DECLARED'" + }, + "certification": { + "name": "certification", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "''" + }, + "evidence": { + "name": "evidence", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "''" + }, + "expires_on": { + "name": "expires_on", + "type": "date", + "primaryKey": false, + "notNull": false + }, + "assessed_by": { + "name": "assessed_by", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "assessed_at": { + "name": "assessed_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "protector_capabilities_protector_id_protectors_id_fk": { + "name": "protector_capabilities_protector_id_protectors_id_fk", + "tableFrom": "protector_capabilities", + "tableTo": "protectors", + "columnsFrom": [ + "protector_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "protector_capabilities_unique": { + "name": "protector_capabilities_unique", + "nullsNotDistinct": false, + "columns": [ + "protector_id", + "capability" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.protectors": { + "name": "protectors", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "sub": { + "name": "sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "display_name": { + "name": "display_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "bio": { + "name": "bio", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "languages": { + "name": "languages", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "skills": { + "name": "skills", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "experience_years": { + "name": "experience_years", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "services": { + "name": "services", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "presence_styles": { + "name": "presence_styles", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "status": { + "name": "status", + "type": "protector_status", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'APPLIED'" + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "approved_at": { + "name": "approved_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "protectors_sub_unique": { + "name": "protectors_sub_unique", + "nullsNotDistinct": false, + "columns": [ + "sub" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.booking_messages": { + "name": "booking_messages", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "booking_id": { + "name": "booking_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "author_id": { + "name": "author_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "author_sub": { + "name": "author_sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "body": { + "name": "body", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "booking_messages_booking_idx": { + "name": "booking_messages_booking_idx", + "columns": [ + { + "expression": "booking_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "booking_messages_booking_id_bookings_id_fk": { + "name": "booking_messages_booking_id_bookings_id_fk", + "tableFrom": "booking_messages", + "tableTo": "bookings", + "columnsFrom": [ + "booking_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.booking_thread_reads": { + "name": "booking_thread_reads", + "schema": "", + "columns": { + "booking_id": { + "name": "booking_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "actor_id": { + "name": "actor_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "last_read_at": { + "name": "last_read_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "booking_thread_reads_booking_id_bookings_id_fk": { + "name": "booking_thread_reads_booking_id_bookings_id_fk", + "tableFrom": "booking_thread_reads", + "tableTo": "bookings", + "columnsFrom": [ + "booking_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "booking_thread_reads_booking_id_actor_id_pk": { + "name": "booking_thread_reads_booking_id_actor_id_pk", + "columns": [ + "booking_id", + "actor_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": { + "public.audit_action": { + "name": "audit_action", + "schema": "public", + "values": [ + "VIEW_BOOKING", + "VIEW_COMPLAINT", + "VIEW_PROTECTOR", + "CHANGE_PROTECTOR_STATUS", + "ASSESS_CAPABILITY", + "OVERRIDE_MATCH", + "VIEW_MAIL", + "REPLY_MAIL" + ] + }, + "public.audit_subject": { + "name": "audit_subject", + "schema": "public", + "values": [ + "BOOKING", + "COMPLAINT", + "PROTECTOR", + "CAPABILITY", + "MAIL" + ] + }, + "public.booking_status": { + "name": "booking_status", + "schema": "public", + "values": [ + "REQUESTED", + "ASSIGNED", + "ACCEPTED", + "CHECKED_IN", + "IN_PROGRESS", + "COMPLETED", + "CANCELLED", + "EXPIRED" + ] + }, + "public.capability_level": { + "name": "capability_level", + "schema": "public", + "values": [ + "BASIC", + "PROFICIENT", + "ADVANCED" + ] + }, + "public.complaint_category": { + "name": "complaint_category", + "schema": "public", + "values": [ + "DISRESPECT", + "UNPROFESSIONAL", + "FELT_UNSAFE", + "PRIVACY", + "SERVICE", + "OTHER" + ] + }, + "public.complaint_status": { + "name": "complaint_status", + "schema": "public", + "values": [ + "RECEIVED", + "AWAITING_RESPONSE", + "UNDER_REVIEW", + "UPHELD", + "NOT_UPHELD", + "APPEALED" + ] + }, + "public.environment_type": { + "name": "environment_type", + "schema": "public", + "values": [ + "PERSON", + "HOME", + "VENUE", + "WORKPLACE", + "VEHICLE", + "JOURNEY", + "EVENT" + ] + }, + "public.incident_review": { + "name": "incident_review", + "schema": "public", + "values": [ + "OPEN", + "UNDER_REVIEW", + "RESOLVED" + ] + }, + "public.incident_severity": { + "name": "incident_severity", + "schema": "public", + "values": [ + "LOW", + "MEDIUM", + "HIGH" + ] + }, + "public.mail_kind": { + "name": "mail_kind", + "schema": "public", + "values": [ + "question", + "request", + "complaint", + "booking", + "billing", + "partnership", + "press", + "spam", + "other" + ] + }, + "public.mail_urgency": { + "name": "mail_urgency", + "schema": "public", + "values": [ + "urgent", + "normal", + "low" + ] + }, + "public.payment_status": { + "name": "payment_status", + "schema": "public", + "values": [ + "PAYMENT_PENDING", + "PAID" + ] + }, + "public.presence_style": { + "name": "presence_style", + "schema": "public", + "values": [ + "DISCREET", + "VISIBLE" + ] + }, + "public.protector_status": { + "name": "protector_status", + "schema": "public", + "values": [ + "APPLIED", + "APPROVED", + "SUSPENDED", + "REJECTED" + ] + }, + "public.report_kind": { + "name": "report_kind", + "schema": "public", + "values": [ + "REPORT", + "INCIDENT" + ] + }, + "public.service": { + "name": "service", + "schema": "public", + "values": [ + "NIGHT_OUT", + "GET_HOME", + "HIGH_EXPOSURE", + "FAMILY", + "VENUE_STAFF", + "PERSONAL_PROTECTION", + "PROTECTOR_DRIVER", + "SPECIALIST_SUPPORT" + ] + }, + "public.verification_status": { + "name": "verification_status", + "schema": "public", + "values": [ + "SELF_DECLARED", + "VERIFIED", + "REJECTED" + ] + } + }, + "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 e955d1b..a34ad1e 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -113,6 +113,13 @@ "when": 1792660409041, "tag": "0015_inbound_mail", "breakpoints": true + }, + { + "idx": 16, + "version": "7", + "when": 1792685665478, + "tag": "0016_step_outcomes", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/app/(app)/assessments/[id]/page.tsx b/src/app/(app)/assessments/[id]/page.tsx index 997a078..e1d88b6 100644 --- a/src/app/(app)/assessments/[id]/page.tsx +++ b/src/app/(app)/assessments/[id]/page.tsx @@ -11,7 +11,9 @@ import { idInput } from "@/domain/inputs"; import { readPlan } from "@/domain/plan-versions"; import { describeLeans } from "@/domain/preference-fit"; import { getAssessment } from "@/server/assessments"; +import { outcomesOf } from "@/server/step-outcomes"; import { requireViewer } from "@/server/viewer"; +import { stepOutcomeAction } from "../actions"; export const metadata: Metadata = { title: "Safety Plan" }; @@ -24,6 +26,7 @@ export default async function AssessmentPage({ params }: { params: Promise<{ id: const assessment = await getAssessment(getDb(), viewer.sub, id.data); if (!assessment) notFound(); const plan = readPlan(assessment.plan); + const outcomes = await outcomesOf(getDb(), viewer.sub); return ( <> @@ -32,7 +35,7 @@ export default async function AssessmentPage({ params }: { params: Promise<{ id: lead={`${environmentTypeLabel(assessment.environment.type)} · ${formatWhen(assessment.createdAt)}`} /> - + {/* What the plan rests on: there to check, not to act on, so folded. */} { @@ -32,3 +35,22 @@ export async function assessLifeAction(): Promise { const life = await ensureLifeEnvironment(getDb(), viewer.sub); redirect(`/assessments/new?place=${life.id}`); } + +const ANSWERS: Record = { + DONE: { status: "DONE" }, + NOT_FOR_ME: { status: "NOT_FOR_ME" }, + YES: { status: "DONE", helped: "YES" }, + SOMEWHAT: { status: "DONE", helped: "SOMEWHAT" }, + NO: { status: "DONE", helped: "NO" }, + UNDO: null, +}; + +/** "I did this", "Not for me", "Did it help?" under a step of a Safety Plan. */ +export async function stepOutcomeAction(form: FormData): Promise { + const viewer = await requireViewer(); + const step = form.get("step"); + const answer = String(form.get("answer") ?? ""); + if (typeof step !== "string" || !(answer in ANSWERS) || !findIntervention(step)) return; + await setStepOutcome(getDb(), viewer.sub, step, ANSWERS[answer]!); + revalidatePath("/assessments", "layout"); +} diff --git a/src/components/safety-plan.tsx b/src/components/safety-plan.tsx index 1f73cb0..e2a464e 100644 --- a/src/components/safety-plan.tsx +++ b/src/components/safety-plan.tsx @@ -15,6 +15,8 @@ import { } from "@/config/interventions"; import { PRIORITY_LABELS } from "@/domain/findings"; import { planSteps, planVerdict, type FindingPlan, type PlanVerdict, type SafetyPlan } from "@/domain/safety-plan"; +import type { StepOutcome } from "@/domain/step-outcomes"; +import { StepOutcomeControls } from "./step-outcome"; import { Badge, Card } from "./ui"; export function InterventionCard({ id, why, emphasis = false }: { id: string; why?: string[]; emphasis?: boolean }) { @@ -159,6 +161,7 @@ export function FindingSection({ finding, shownAbove = false }: { finding: Findi )} + ; + answer?: (form: FormData) => Promise; +}) { const steps = planSteps(plan); if (steps.length === 0) return null; return ( @@ -259,13 +271,14 @@ export function PlanSteps({ plan }: { plan: SafetyPlan }) { return (
  • {n + 1}. - +
    {i.title} {needsPurchase(i.kind) ? {costLabel(i.cost)} : Nothing to buy} For: {step.concerns.map(concernLabel).join(" · ")} - + {answer && } +
  • ); })} diff --git a/src/components/step-outcome.tsx b/src/components/step-outcome.tsx new file mode 100644 index 0000000..1fce54f --- /dev/null +++ b/src/components/step-outcome.tsx @@ -0,0 +1,57 @@ +import { HELPED_LABELS, outcomeSentence, STEP_HELPED, type StepOutcome } from "@/domain/step-outcomes"; +import { buttonClass } from "./ui"; + +const cell = `${buttonClass.secondary} w-full`; + +/** + * Under one step of a Safety Plan: "I did this" / "Not for me", then "Did it + * help?". One form, one button per answer, so it works without JavaScript and + * the choices sit as equal cells. What is said here shapes the next plan. + */ +export function StepOutcomeControls({ + stepKey, + outcome, + action, +}: { + stepKey: string; + outcome: StepOutcome | undefined; + action: (form: FormData) => Promise; +}) { + const sentence = outcomeSentence(outcome); + const asking = outcome?.status === "DONE" && !outcome.helped; + return ( +
    + + {!outcome && ( +
    + + +
    + )} + {asking && ( +
    + Did it help? +
    + {STEP_HELPED.map((h) => ( + + ))} +
    +
    + )} + {outcome && !asking && sentence && ( +

    + {sentence} + +

    + )} +
    + ); +} diff --git a/src/config/development.ts b/src/config/development.ts index ccf42e8..a414182 100644 --- a/src/config/development.ts +++ b/src/config/development.ts @@ -27,15 +27,6 @@ export const PUBLIC_DEVELOPMENT: DevelopmentProfile = { "Next: named, vetted people you can book inside Skif, each shown with what they fix and what they cost you in privacy.", ], }, - { - title: "“Did it help?” after every step", - status: "Next", - progress: null, - targetDate: null, - milestones: [ - "A short follow-up on each step you took, so your next plan is built on what worked for you.", - ], - }, { title: "Pay for a booking inside Skif", status: "Planned", @@ -61,6 +52,10 @@ export const PUBLIC_DEVELOPMENT: DevelopmentProfile = { }, ], changelog: [ + { + date: "2026-10-22", + done: "“Did it help?” under every step of your Safety Plan: say you did it, or that it is not for you, and then whether it helped. Your next plan learns from it: a step that helped counts as something you already have, and one that did not is set aside, with the next option in its place and a line saying why.", + }, { date: "2026-10-22", done: "Cards look the same everywhere now: your bookings, preferences and training are drawn exactly like the cards on the public pages, from one definition, and a test refuses a second look.", diff --git a/src/db/schema/index.ts b/src/db/schema/index.ts index cbe4b8c..b20f542 100644 --- a/src/db/schema/index.ts +++ b/src/db/schema/index.ts @@ -9,6 +9,7 @@ export * from "./audit"; export * from "./bookings"; export * from "./complaints"; export * from "./mail"; +export * from "./outcomes"; export * from "./people"; export * from "./shared"; export * from "./threads"; diff --git a/src/db/schema/outcomes.ts b/src/db/schema/outcomes.ts new file mode 100644 index 0000000..3817985 --- /dev/null +++ b/src/db/schema/outcomes.ts @@ -0,0 +1,17 @@ +/** "Did it help?": what a person said about a step their Safety Plan gave + * them (src/domain/step-outcomes.ts). One row per person and step. */ +import { pgTable, primaryKey, text, timestamp } from "drizzle-orm/pg-core"; +import type { StepHelped, StepStatus } from "@/domain/step-outcomes"; + +export const stepOutcomes = pgTable( + "step_outcomes", + { + sub: text("sub").notNull(), + /** An intervention key from src/config/interventions.ts. */ + interventionKey: text("intervention_key").notNull(), + status: text("status").$type().notNull(), + helped: text("helped").$type(), + updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(), + }, + (t) => [primaryKey({ columns: [t.sub, t.interventionKey] })], +); diff --git a/src/domain/safety-plan.ts b/src/domain/safety-plan.ts index bcdbf2e..2b3d743 100644 --- a/src/domain/safety-plan.ts +++ b/src/domain/safety-plan.ts @@ -11,7 +11,9 @@ * - for a high-priority finding (a known threat), the *most effective* one, * because proportionality cuts both ways; * - nothing new when what the person already does covers it — unless the - * finding is high priority. + * finding is high priority; + * - learning from "Did it help?": a step that helped counts as covered, and + * one that did not (or was "not for me") is set aside for the next option. * "Nothing to buy" is a first-class outcome, not a fallback. */ import type { HardConstraintKey } from "@/config/constraints"; @@ -26,6 +28,7 @@ import { type Level, } from "@/config/interventions"; import { preferenceFit } from "./preference-fit"; +import type { PlanLessons } from "./step-outcomes"; import { deriveFindings, type Finding, type FindingInput } from "./findings"; export type PlanInput = FindingInput & { @@ -34,6 +37,8 @@ export type PlanInput = FindingInput & { budget: CostTier; /** The person's trade-off leans; DISCRETION comes from their presence style. */ leans: Leans; + /** What the person said about earlier steps. Absent: nothing said yet. */ + lessons?: PlanLessons; }; export type ExcludedIntervention = { key: string; constraints: HardConstraintKey[] }; @@ -49,6 +54,8 @@ export type FindingPlan = Finding & { alternatives: string[]; overBudget: string[]; excluded: ExcludedIntervention[]; + /** Tried and did not help, or "not for me". Absent on older plans. */ + setAside?: string[]; }; export type SafetyPlan = { @@ -88,11 +95,15 @@ function ordering(high: boolean, leans: Leans) { high ? benefit(a, b) || byLeans(a, b) || leastIntrusive(a, b) : byLeans(a, b) || leastIntrusive(a, b); } -function whyRecommended(i: Intervention, finding: Finding, covered: boolean, leans: Leans): string[] { +function whyRecommended(i: Intervention, finding: Finding, covered: boolean, leans: Leans, setAside: string[]): string[] { const why = finding.priority === "HIGH" ? ["The most effective option within your limits and budget — this is a high priority."] : ["The least intrusive option within your limits and budget that addresses this."]; + if (setAside.length) { + const titles = setAside.map((k) => findIntervention(k)?.title ?? k).join(", "); + why.push(`You said ${titles} didn't help or wasn't for you, so this is the next option.`); + } if (covered) why.push("What you already do helps, but a known threat justifies more."); const fit = preferenceFit(i, leans); if (fit.with.length) why.push(`Fits your lean towards ${fit.with.join(", ")}.`); @@ -112,17 +123,21 @@ function planFinding(finding: Finding, input: PlanInput): FindingPlan { const excluded: ExcludedIntervention[] = []; const alreadyCovered: string[] = []; const overBudget: string[] = []; + const setAside: string[] = []; const eligible: Intervention[] = []; + const worked = input.lessons?.worked ?? []; + const notAgain = input.lessons?.setAside ?? []; for (const i of candidates) { const broken = i.conflictsWith.filter((c) => input.constraints.includes(c)); if (broken.length > 0) excluded.push({ key: i.key, constraints: broken }); - else if (i.coveredBy && input.measures.includes(i.coveredBy)) alreadyCovered.push(i.key); + else if ((i.coveredBy && input.measures.includes(i.coveredBy)) || worked.includes(i.key)) alreadyCovered.push(i.key); + else if (notAgain.includes(i.key)) setAside.push(i.key); else if (costRank(i.cost) > costRank(input.budget)) overBudget.push(i.key); else eligible.push(i); } const covered = alreadyCovered.length > 0; - const base = { ...finding, alreadyCovered, overBudget, excluded }; + const base = { ...finding, alreadyCovered, overBudget, excluded, ...(setAside.length ? { setAside } : {}) }; if (covered && !high) { return { ...base, outcome: "COVERED", recommended: null, alternatives: eligible.map((i) => i.key) }; } @@ -131,7 +146,7 @@ function planFinding(finding: Finding, input: PlanInput): FindingPlan { return { ...base, outcome: "RECOMMENDED", - recommended: { key: best.key, why: whyRecommended(best, finding, covered, input.leans) }, + recommended: { key: best.key, why: whyRecommended(best, finding, covered, input.leans, setAside) }, alternatives: rest.map((i) => i.key), }; } diff --git a/src/domain/step-outcomes.test.ts b/src/domain/step-outcomes.test.ts new file mode 100644 index 0000000..18fda33 --- /dev/null +++ b/src/domain/step-outcomes.test.ts @@ -0,0 +1,57 @@ +import { describe, expect, it } from "vitest"; +import { buildSafetyPlan, type PlanInput } from "./safety-plan"; +import { lessonsFrom, outcomeSentence, type StepOutcome } from "./step-outcomes"; + +const input = (overrides: Partial = {}): PlanInput => ({ + environment: "HOME", + concerns: ["BURGLARY"], + exposures: [], + threats: [], + upcoming: "", + measures: [], + constraints: [], + budget: "HIGH", + leans: {}, + ...overrides, +}); + +describe("what a plan learns from Did it help?", () => { + it("counts a step that helped as had, and sets aside one that did not or was not wanted", () => { + const outcomes = new Map([ + ["A", { status: "DONE", helped: "YES" }], + ["B", { status: "DONE", helped: "SOMEWHAT" }], + ["C", { status: "DONE", helped: "NO" }], + ["D", { status: "NOT_FOR_ME", helped: null }], + ["E", { status: "DONE", helped: null }], + ]); + expect(lessonsFrom(outcomes)).toEqual({ worked: ["A", "B"], setAside: ["C", "D"] }); + }); + + it("recommends the next option, and says why, when the first did not help", () => { + const first = buildSafetyPlan(input()).findings[0]!; + const tried = first.recommended!.key; + expect(first.alternatives.length).toBeGreaterThan(0); + const next = buildSafetyPlan(input({ lessons: { worked: [], setAside: [tried] } })).findings[0]!; + expect(next.recommended!.key).toBe(first.alternatives[0]); + expect(next.setAside).toEqual([tried]); + expect(next.recommended!.why.join(" ")).toMatch(/didn't help or wasn't for you/); + }); + + it("treats a step that helped like something the person already does", () => { + const first = buildSafetyPlan(input()).findings[0]!; + const next = buildSafetyPlan(input({ lessons: { worked: [first.recommended!.key], setAside: [] } })).findings[0]!; + expect(next.outcome).toBe("COVERED"); + expect(next.alreadyCovered).toContain(first.recommended!.key); + }); + + it("leaves a plan exactly as before when nothing has been said", () => { + expect(buildSafetyPlan(input({ lessons: { worked: [], setAside: [] } }))).toEqual(buildSafetyPlan(input())); + }); + + it("says what each answer means for the next plan", () => { + expect(outcomeSentence(undefined)).toBeNull(); + expect(outcomeSentence({ status: "DONE", helped: null })).toBe("Done."); + expect(outcomeSentence({ status: "DONE", helped: "NO" })).toMatch(/suggests something else/); + expect(outcomeSentence({ status: "DONE", helped: "YES" })).toMatch(/counts it/); + }); +}); diff --git a/src/domain/step-outcomes.ts b/src/domain/step-outcomes.ts new file mode 100644 index 0000000..5076c2a --- /dev/null +++ b/src/domain/step-outcomes.ts @@ -0,0 +1,46 @@ +/** + * "Did it help?" — what a person says about each step a Safety Plan gave + * them, and what their next plan learns from it. Keyed by person and step, + * not by plan: the same step in two plans is one experience. + */ +export const STEP_STATUSES = ["DONE", "NOT_FOR_ME"] as const; +export type StepStatus = (typeof STEP_STATUSES)[number]; + +export const STEP_HELPED = ["YES", "SOMEWHAT", "NO"] as const; +export type StepHelped = (typeof STEP_HELPED)[number]; + +export type StepOutcome = { status: StepStatus; helped: StepHelped | null }; + +export const HELPED_LABELS: Record = { + YES: "Yes", + SOMEWHAT: "A little", + NO: "No", +}; + +/** What the next plan takes from the answers so far. */ +export type PlanLessons = { + /** Done, and it helped: counts like a measure the person already has. */ + worked: string[]; + /** Done and it did not help, or "not for me": recommended no more. */ + setAside: string[]; +}; + +export function lessonsFrom(outcomes: ReadonlyMap): PlanLessons { + const worked: string[] = []; + const setAside: string[] = []; + for (const [key, o] of outcomes) { + if (o.status === "NOT_FOR_ME" || o.helped === "NO") setAside.push(key); + else if (o.helped === "YES" || o.helped === "SOMEWHAT") worked.push(key); + } + return { worked: worked.sort(), setAside: setAside.sort() }; +} + +/** One line for a step's row, once the person has said something about it. */ +export function outcomeSentence(o: StepOutcome | undefined): string | null { + if (!o) return null; + if (o.status === "NOT_FOR_ME") return "Not for you. Your next plan suggests something else."; + if (o.helped === "YES") return "Done, and it helped. Your next plan counts it as something you have."; + if (o.helped === "SOMEWHAT") return "Done, and it helped a little. Your next plan counts it as something you have."; + if (o.helped === "NO") return "Done, but it didn't help. Your next plan suggests something else."; + return "Done."; +} diff --git a/src/server/assessments.ts b/src/server/assessments.ts index 5137d7d..5c9e2fa 100644 --- a/src/server/assessments.ts +++ b/src/server/assessments.ts @@ -6,13 +6,16 @@ import type { AssessmentInput } from "@/domain/inputs"; import { fail, ok, type Result } from "@/domain/result"; import { leansOf } from "@/domain/preference-fit"; import { buildSafetyPlan } from "@/domain/safety-plan"; +import { lessonsFrom } from "@/domain/step-outcomes"; import { getEnvironment, type Environment } from "./environments"; import { getPreferences } from "./preferences"; +import { outcomesOf } from "./step-outcomes"; export type Assessment = typeof assessments.$inferSelect; /** Run an assessment against the person's current hard constraints and save - * the answers and the resulting Safety Plan, constraints included, as decided. */ + * the answers and the resulting Safety Plan, constraints included, as decided. + * What the person said about earlier steps ("Did it help?") shapes it too. */ export async function createAssessment( db: Db, customerSub: string, @@ -28,6 +31,7 @@ export async function createAssessment( environment: environment.type, constraints: prefs.hardConstraints, leans: leansOf(prefs.axes, prefs.presenceStyle), + lessons: lessonsFrom(await outcomesOf(db, customerSub)), }); const [row] = await db .insert(assessments) diff --git a/src/server/step-outcomes.test.ts b/src/server/step-outcomes.test.ts new file mode 100644 index 0000000..8d17863 --- /dev/null +++ b/src/server/step-outcomes.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from "vitest"; +import { INTERVENTIONS } from "@/config/interventions"; +import { createTestDb } from "@/test/db"; +import { outcomesOf, setStepOutcome } from "./step-outcomes"; + +const KEY = INTERVENTIONS[0]!.key; + +describe("step outcomes", () => { + it("records done, then whether it helped, keeps the answer on a repeat, and forgets on undo", async () => { + const db = await createTestDb(); + await setStepOutcome(db, "a", KEY, { status: "DONE" }); + expect((await outcomesOf(db, "a")).get(KEY)).toEqual({ status: "DONE", helped: null }); + await setStepOutcome(db, "a", KEY, { status: "DONE", helped: "YES" }); + await setStepOutcome(db, "a", KEY, { status: "DONE" }); + expect((await outcomesOf(db, "a")).get(KEY)).toEqual({ status: "DONE", helped: "YES" }); + expect((await outcomesOf(db, "b")).size).toBe(0); + await setStepOutcome(db, "a", KEY, null); + expect((await outcomesOf(db, "a")).size).toBe(0); + }); + + it("clears 'did it help' when a step becomes not for me", async () => { + const db = await createTestDb(); + await setStepOutcome(db, "a", KEY, { status: "DONE", helped: "NO" }); + await setStepOutcome(db, "a", KEY, { status: "NOT_FOR_ME" }); + expect((await outcomesOf(db, "a")).get(KEY)).toEqual({ status: "NOT_FOR_ME", helped: null }); + }); + + it("refuses a step that does not exist", async () => { + const db = await createTestDb(); + await expect(setStepOutcome(db, "a", "TELEPORT", { status: "DONE" })).rejects.toThrow(/Unknown step/); + }); +}); diff --git a/src/server/step-outcomes.ts b/src/server/step-outcomes.ts new file mode 100644 index 0000000..9133eed --- /dev/null +++ b/src/server/step-outcomes.ts @@ -0,0 +1,38 @@ +import "server-only"; +import { and, eq } from "drizzle-orm"; +import { findIntervention } from "@/config/interventions"; +import { stepOutcomes } from "@/db/schema"; +import type { Db } from "@/db/types"; +import { STEP_HELPED, STEP_STATUSES, type StepHelped, type StepOutcome, type StepStatus } from "@/domain/step-outcomes"; + +/** Everything this person has said about their steps, by intervention key. */ +export async function outcomesOf(db: Db, sub: string): Promise> { + const rows = await db.select().from(stepOutcomes).where(eq(stepOutcomes.sub, sub)); + return new Map(rows.map((r) => [r.interventionKey, { status: r.status, helped: r.helped }])); +} + +export type StepAnswer = { status: StepStatus; helped?: StepHelped | null } | null; + +/** + * Record what the person said about a step, or forget it (`null`). Saying a + * step was done keeps an earlier "did it help?" answer unless a new one is + * given; "not for me" clears it. + */ +export async function setStepOutcome(db: Db, sub: string, interventionKey: string, answer: StepAnswer): Promise { + if (!findIntervention(interventionKey)) throw new Error(`Unknown step: ${interventionKey}`); + const where = and(eq(stepOutcomes.sub, sub), eq(stepOutcomes.interventionKey, interventionKey)); + if (!answer) { + await db.delete(stepOutcomes).where(where); + return; + } + if (!STEP_STATUSES.includes(answer.status)) throw new Error(`Unknown status: ${answer.status}`); + if (answer.helped && !STEP_HELPED.includes(answer.helped)) throw new Error(`Unknown answer: ${answer.helped}`); + const helped = answer.status === "NOT_FOR_ME" ? null : answer.helped; + await db + .insert(stepOutcomes) + .values({ sub, interventionKey, status: answer.status, helped: helped ?? null }) + .onConflictDoUpdate({ + target: [stepOutcomes.sub, stepOutcomes.interventionKey], + set: { status: answer.status, ...(helped !== undefined ? { helped } : {}), updatedAt: new Date() }, + }); +}