Implement getMentorsByStatus query for assignment pool filtering
epic-peer-mentor-pause-core-logic-task-006 — Add a getMentorsByStatus(status: MentorStatus, chapterId?: string) method to MentorStatusService that returns the list of mentors with the given status. This method is consumed by the assignment pool filtering logic to exclude paused and inactive mentors from matching. Implement efficient Supabase query with RLS-aware filtering to scope results to the calling user's chapter.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 3 - 413 tasks
Can start after Tier 2 completes
Implementation Notes
Define a MentorSummary value object (Dart class with copyWith and equality) containing only the fields needed for assignment pool logic. Avoid fetching the full contact or assignment record. Use Supabase's select('mentor_id, display_name, chapter_id, status, certification_expiry_date') to minimize payload size. The chapterId parameter should be typed as String?
and handled with a conditional .eq() call rather than a ternary on the query builder — this keeps the query construction readable. Consider adding a convenience method getMentorsAvailableForAssignment() that internally calls getMentorsByStatus(MentorStatus.active) — this makes the intent explicit at the call site in the assignment pool logic.
Testing Requirements
Unit tests with mocked Supabase client verifying: correct .eq filter applied for each status value, chapterId filter is conditionally applied when provided and omitted when null, empty list returned when no results, error propagation for network failures. Integration test (or widget test with supabase_flutter mock) confirming query shape. Verify that the RLS correctly scopes results by running queries as different test users belonging to different chapters.
The status state machine must handle race conditions where two concurrent callers (e.g., a mentor self-pausing and a coordinator force-pausing simultaneously) attempt to update the same mentor's status. Without a concurrency guard, both writes could succeed, leaving the audit log in an inconsistent state.
Mitigation & Contingency
Mitigation: Use a Supabase RPC with a row-level lock (SELECT FOR UPDATE) inside a transaction so only one transition wins. Return a clear error to the losing caller. Test with concurrent requests in the integration test suite.
Contingency: If row-level locking proves unreliable in the Supabase environment, add an optimistic-locking version field to peer_mentors and have the service retry up to three times on version conflict before surfacing an error to the caller.
If the CertificationExpiryJob Edge Function fails silently (network timeout, Supabase cold start), HLF mentors with expired certifications could remain in active status and continue appearing on the chapter website, creating a compliance breach.
Mitigation & Contingency
Mitigation: Implement structured error logging inside the Edge Function, write a monitoring query that checks for mentors with expired certifications still in active status, and set up an alert if any are detected 30 minutes after the scheduled nightly run.
Contingency: Provide a coordinator-accessible manual trigger for the expiry check that can be invoked via the admin interface if the scheduled job is known to have failed. Document the manual recovery procedure for HLF coordinators.
pg_cron registration in Supabase requires superuser-level access that may not be available in all environments (local dev, staging, CI). If the cron job cannot be registered automatically, the Edge Function will never execute on schedule, breaking the HLF certification expiry workflow.
Mitigation & Contingency
Mitigation: Use Supabase's recommended pg_cron setup via the SQL editor migration script and document the exact commands. Validate cron registration in the staging environment as part of the epic's deployment checklist.
Contingency: If pg_cron is unavailable, switch to a Supabase scheduled Edge Function invocation via an external cron service (e.g., a GitHub Actions scheduled workflow calling the Edge Function endpoint with a service-role key) until the pg_cron approach is resolved.