Scaffold CertificationExpiryCron Edge Function
epic-certification-management-automation-task-001 — Create the Supabase Edge Function skeleton for the nightly certification expiry cron job. Set up the Deno runtime entry point, configure the cron schedule trigger, establish the function's module structure, and wire up the Supabase client with service-role credentials. Ensure the function can be deployed and invoked manually for testing.
Acceptance Criteria
Technical Requirements
Implementation Notes
Use the Supabase CLI scaffold command (supabase functions new certification-expiry-cron) as a starting point, then extend it. Register the cron trigger in supabase/config.toml under [functions.certification-expiry-cron] with schedule = '0 2 * * *'. Import createClient from https://esm.sh/@supabase/supabase-js@2 — pin to a specific version to prevent unexpected breakage. Structure the handler to be an async function and use top-level await only for initialisation that must complete before serving.
Keep index.ts thin — it should only parse the request, call an orchestrateExpiryCheck() function (to be implemented in later tasks), and return the response. This makes unit-testing the business logic possible without spinning up the full Deno serve runtime. Document environment variable names exactly as they will be set in CI/CD secrets to avoid deployment mismatches.
Testing Requirements
Verify the scaffold with a manual invocation test: run supabase start locally, deploy the function with supabase functions deploy, invoke with supabase functions invoke certification-expiry-cron --no-verify-jwt, and assert the response body equals { status: 'ok', message: 'scaffold ready' }. Confirm the Supabase client initialises without throwing by adding a simple health-check query (e.g. select 1) inside the handler and asserting it does not return an error. Write a Deno unit test at supabase/functions/certification-expiry-cron/index_test.ts that mocks Deno.env and asserts the handler returns a Response with status 200.
This test should be runnable with deno test --allow-env.
Supabase Edge Functions can have cold-start latency that causes the nightly cron to time out when processing large cohorts of expiring certifications, resulting in partial reminder dispatches.
Mitigation & Contingency
Mitigation: Batch the cron processing in chunks of 50 mentors per iteration. Use pagination with a cursor to resume processing if the function is re-invoked. Keep total invocation time well under the Edge Function timeout limit.
Contingency: If timeouts occur in production, split the cron into two separate functions: one for reminders and one for auto-pauses, each with its own schedule offset to reduce peak load.
Certification BLoC covers three distinct workflows (view, renew, enrol) which may lead to an overly complex state machine that is hard to test and maintain, particularly when error states from multiple concurrent operations need to be differentiated in the UI.
Mitigation & Contingency
Mitigation: Use separate sealed state classes per workflow (CertificationViewState, RenewalState, EnrolmentState) composed into a single BLoC state wrapper. Follow the existing BLoC patterns established in the codebase for consistency.
Contingency: If the BLoC grows too complex, split into two BLoCs: CertificationBLoC (view/load) and CertificationActionBLoC (mutations), connected via a shared stream.