Provision nightly scheduler via Supabase Edge Function and pg_cron
epic-peer-mentor-pause-management-foundation-task-007 — Create a Supabase Edge Function that acts as the nightly job scheduler entry point. Configure pg_cron to trigger the function on a nightly schedule. Implement job dispatch logic that routes to registered job handlers (certification expiry, HLF sync). Include error capture and execution logging.
Acceptance Criteria
Technical Requirements
Implementation Notes
Structure the Edge Function as: (1) validate Authorization header, (2) parse job_name from request body, (3) look up handler in registry, (4) record start time, (5) execute handler inside try/catch, (6) write execution log. Use Supabase's supabaseAdmin client (service role) inside the function for database writes — never pass the service role key to job handlers that call external APIs. For pg_cron setup, use: SELECT cron.schedule('nightly-scheduler', '0 2 * * *', $$SELECT net.http_post(url:='https://
Consider using a Deno KV or Postgres advisory lock to prevent duplicate concurrent runs if the function is ever invoked twice in quick succession.
Testing Requirements
Write Deno unit tests (Deno.test) covering: (1) known job_name dispatches to correct handler, (2) unknown job_name returns a structured error and logs failure, (3) handler that throws is caught and logged without propagating, (4) missing or invalid Authorization header returns 401 before any job runs, (5) execution log entry contains correct started_at/finished_at timestamps. Integration test: deploy to a Supabase local dev stack (supabase start), invoke the function via curl with a valid secret, and verify the scheduler_execution_log row is inserted. Verify pg_cron job fires correctly by checking cron.job_run_details in the local Postgres instance after manual trigger. Document a manual smoke-test checklist for production deployment validation.
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.