Apply RLS policies and RPC functions for pause status
epic-peer-mentor-pause-management-foundation-task-005 — Define RLS policies on peer_mentor_status and peer_mentor_status_log tables. Implement RPC functions for atomic status transitions (activate_pause, deactivate_pause, get_active_pauses_for_chapter) to ensure safe concurrent updates and audit logging.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Use a single migration file (e.g., 20260330_pause_rls_and_rpcs.sql). Define RLS policies before granting EXECUTE on RPC functions to avoid a window where functions are callable without policies. For the atomic status transition, use a CTE pattern: WITH updated AS (UPDATE peer_mentor_status SET status='paused', ... WHERE id=...
RETURNING *) INSERT INTO peer_mentor_status_log SELECT ... FROM updated — this guarantees log insertion only if the update succeeds. Use pg_advisory_xact_lock(hashtext(peer_mentor_id::text)) inside activate_pause/deactivate_pause to serialize concurrent calls without table-level locks. Extract organization_id from (auth.jwt()->'user_metadata'->>'organization_id') consistently — document which claim key is used so the Dart repository layer matches.
Avoid SECURITY DEFINER unless cross-schema access is needed; SECURITY INVOKER with RLS is safer and auditable.
Testing Requirements
Write pgTAP tests covering: (1) peer mentor can SELECT their own status row, (2) peer mentor cannot SELECT another mentor's status, (3) coordinator can SELECT all statuses in their org, (4) cross-org SELECT is rejected, (5) activate_pause succeeds and inserts log entry atomically, (6) deactivate_pause succeeds and inserts log entry, (7) concurrent activate_pause calls for the same mentor serialize correctly, (8) mismatched organization_id raises exception. Integration tests in Dart using a Supabase test project should verify that calling activate_pause via supabase.rpc() with a valid JWT returns the expected status row and that an unauthorized JWT returns a Postgres error. Aim for 100% branch coverage on all RPC conditional paths.
Supabase RLS policies for coordinator-scoped status queries may be difficult to express correctly, especially for peer mentors assigned to multiple coordinators or chapters, leading to data leakage or overly restrictive access blocking valid queries.
Mitigation & Contingency
Mitigation: Design RLS policies using security-definer RPCs rather than table-level policies for complex multi-coordinator scenarios. Write a comprehensive RLS test matrix covering all role and assignment permutations before marking complete.
Contingency: Fall back to application-level filtering in the repository layer with explicit coordinator_id parameter checks if RLS proves intractable, and document the trade-off for security review.
The HLF Dynamics portal API contract may be undocumented or subject to change, causing the DynamicsPortalClient to break during development or production rollout.
Mitigation & Contingency
Mitigation: Obtain the full Dynamics portal API specification and credentials early in the sprint. Build the client behind a well-defined interface so the HLF-specific implementation can be swapped without affecting upstream services.
Contingency: If the Dynamics API is unavailable or unstable, stub the client with a feature-flag-guarded no-op implementation so all other epics can proceed to completion independently.
Supabase Edge Functions used as the nightly scheduler host may have cold-start latency or execution time limits that prevent reliable nightly certification checks on large mentor rosters.
Mitigation & Contingency
Mitigation: Benchmark Edge Function execution time against the expected roster size. Design the expiry check to process in paginated batches to stay within execution limits. Use pg_cron with a direct database function as an alternative trigger if Edge Functions prove unreliable.
Contingency: Migrate the scheduler trigger to pg_cron invoking a Postgres function directly, removing the Edge Function dependency entirely for the scheduling layer.