Define Supabase schema for post-session reports
epic-structured-post-session-report-foundation-task-001 — Create the Supabase database migration for the post_session_reports table including columns for report_id, activity_id (foreign key), peer_mentor_id, org_id, status, field_values (JSONB), created_at, updated_at. Add RLS policies to restrict read/write access by org_id and role. Ensure activity_id foreign key constraint is enforced.
Acceptance Criteria
Technical Requirements
Implementation Notes
Write the migration as a standard Supabase migration file in supabase/migrations/
For the role-based RLS, check if the project uses a roles table join or a JWT claim — use whichever pattern is established in existing tables. Add a SQL comment on the field_values column documenting its expected JSON shape (fieldId: value pairs matching the report schema). The migration should also create a corresponding report_drafts table or reuse this table with status='draft' — confirm with the team which draft storage strategy is used (separate table vs. status column) before writing the migration, as this affects the repository implementation in subsequent tasks.
Testing Requirements
Database-level tests using Supabase local development environment (supabase start). Test suite: (1) migration applies cleanly on a fresh schema; (2) inserting a valid report succeeds; (3) inserting with a non-existent activity_id fails with FK violation; (4) inserting with an invalid status value (e.g., 'pending') fails with check constraint violation; (5) RLS: authenticated peer_mentor user can only read their own org's reports; (6) RLS: peer_mentor cannot read another org's reports even with a valid report_id; (7) RLS: coordinator can read all reports within their org; (8) RLS: no unauthenticated request can read any report; (9) updated_at is automatically updated on row modification. Use pgTAP or Supabase's built-in SQL test runner for database-level tests. Flutter integration tests should also mock the Supabase client for above-database tests.
Supabase RLS policies for multi-org report access may be more complex than anticipated — coordinators need cross-peer-mentor access within their org but not across orgs, and draft reports should be invisible to coordinators until submitted. Misconfigured RLS could expose sensitive health data or block legitimate access.
Mitigation & Contingency
Mitigation: Define and test RLS policies in isolation before writing repository code. Create a dedicated SQL migration file with policy definitions and an automated integration test suite that verifies each role's access boundaries using real Supabase auth tokens.
Contingency: If RLS proves too complex to express declaratively, implement application-level access control in the repository layer with explicit org and role checks, and add a security audit task before the feature goes to production.
The org field config JSON stored in Supabase may lack a stable, versioned schema contract. If different organisations have drifted to different field-definition formats, org-field-config-loader will fail silently or crash, breaking form rendering for those orgs.
Mitigation & Contingency
Mitigation: Define a canonical JSON Schema for field config and validate all existing org configs against it before implementation begins. Store a schema version field in every config record and handle version migrations explicitly in the loader.
Contingency: If existing configs are too heterogeneous, implement a config normalisation pass in org-field-config-loader that coerces known variants to the canonical format, logging warnings for fields that cannot be normalised so operations can fix them in the admin console.
TTL-based schema cache invalidation may cause peer mentors to use stale field definitions for up to the TTL window after an admin updates the org config, potentially collecting data against outdated field structures.
Mitigation & Contingency
Mitigation: Set a conservative TTL (e.g. 15 minutes) and expose a manual cache-bust mechanism triggered on app foreground-resume. Document the maximum staleness window in the admin console so org admins know to plan config changes outside active reporting windows.
Contingency: If stale schema causes a data quality incident, add a Supabase Realtime subscription to the org config table that invalidates the cache immediately on any config update.