Implement auto-pause trigger on certification expiry
epic-certification-management-core-logic-task-005 — Integrate PauseManagementService within CertificationManagementService so that when computeExpiryState returns expired, autoPauseMentor is called automatically. Log the auto-pause event with reason 'certification_expired'. Ensure the operation is idempotent — re-running expiry checks on an already-paused mentor must not produce duplicate pause records.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 4 - 323 tasks
Can start after Tier 3 completes
Implementation Notes
Implement idempotency at the `PauseManagementService` layer using an `INSERT ... ON CONFLICT DO NOTHING` upsert in Supabase rather than a check-then-insert pattern. In `CertificationManagementService`, the sequence after detecting expiry should be: (1) update certification status in repository, (2) call `autoPauseMentor`, (3) emit pause event for notification delivery. Steps 2 and 3 are fire-and-handle-error — a failure does not undo step 1.
For the Supabase Edge Function batch job, implement a scheduled Deno function (`check-certification-expiry`) that runs nightly via pg_cron or Supabase scheduled functions. The Edge Function queries all certifications where `expires_at < now()` and `status != 'expired'`, calls the same pause logic server-side, and logs each action to the audit table. This ensures certifications are caught even when the mobile client has not been opened.
Testing Requirements
Unit tests: (1) verify `autoPauseMentor` is called when status is `expired`, (2) verify it is NOT called for `valid` and `expiring_soon`, (3) verify idempotency — mock PauseManagementService to return a 'already paused' response and assert no exception is thrown and no duplicate record is created, (4) verify that a PauseManagementService failure does not roll back the expiry status update. Integration test: seed a mentor with an expired certification, run the expiry check twice, assert exactly one pause record exists in the database. Edge Function test: trigger the nightly batch via Supabase CLI and assert pause records are created for all expired mentors without duplicates.
The auto-pause workflow requires CertificationManagementService to call PauseManagementService and HLFDynamicsSyncService in the same logical transaction. If PauseManagementService succeeds but the Dynamics webhook fails, the mentor is paused locally but remains visible on the HLF portal.
Mitigation & Contingency
Mitigation: Implement a saga pattern: write a pending sync event to the database before calling Dynamics, and have a background retry job consume pending events. This guarantees eventual consistency even if the webhook fails transiently.
Contingency: If the Dynamics sync fails after auto-pause, surface an explicit coordinator alert in the dashboard indicating 'Dynamics sync pending — mentor may still be visible on portal'. Allow manual retry from coordinator UI.
If the nightly cron job runs concurrently (e.g., due to infra retry), CertificationReminderService could dispatch duplicate notifications to mentors before the cert_notification_log insert is visible to the second invocation.
Mitigation & Contingency
Mitigation: Use Supabase's upsert with a unique constraint on (mentor_id, threshold_days, cert_id) in cert_notification_log. The second concurrent insert will fail gracefully and the duplicate dispatch will be skipped.
Contingency: If duplicate notifications do reach mentors, add a post-dispatch dedup check and include a 'you may receive this notification again' disclaimer until the constraint is deployed.