Implement retry logic and failure alerting for HLF sync
epic-peer-mentor-pause-management-automated-expiry-task-008 — Add robust retry logic to HLFWebsiteSyncService with configurable max attempts and exponential backoff. On final retry exhaustion, record the failed sync operation in a dead-letter queue and dispatch a failure alert to the coordinator or system administrator. Ensure partial failures do not block subsequent sync operations for other mentors.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 4 - 323 tasks
Can start after Tier 3 completes
Implementation Notes
Implement the queue processor as a stateless Edge Function invoked by pg_cron every 1 minute. Use SELECT ... FOR UPDATE SKIP LOCKED to safely dequeue — this prevents two concurrent invocations from processing the same row. The dead-letter queue is a separate table (not just a status flag) to enable independent querying, alerting, and manual re-queue without touching the main sync_queue.
For the coordinator push alert, keep the FCM payload minimal: { type: 'hlf_sync_failure', mentor_id: uuid, operation: 'remove'|'restore' } — the Flutter app fetches human-readable details on open. Log all retry attempts and final outcome to the audit log table (covered by task-009) so the dead-letter alert is traceable.
Testing Requirements
Unit tests: mock HLFWebsiteSyncService to throw retryable (5xx) and non-retryable (4xx) errors; assert correct retry counts, correct backoff delay intervals (mock timers), correct dead-letter queue insertion on exhaustion. Assert non-retryable errors skip retry entirely. Integration tests: seed a sync_queue entry; simulate persistent 503 responses from mocked Dynamics API; assert dead_letter_queue row created after max_attempts; assert FCM dispatch was called with correct payload. Test per-mentor isolation: one dead-lettered operation must not prevent processing of a second mentor's pending operation.
The nightly expiry checker may run multiple times due to scheduler retries or infrastructure issues, causing duplicate auto-transitions and duplicate coordinator notifications that erode trust in the notification system.
Mitigation & Contingency
Mitigation: Implement idempotency via a unique constraint on (mentor_id, threshold_day, certification_expiry_date) in the cert_expiry_reminders table. Auto-transitions should be wrapped in a Postgres RPC that checks current status before applying, making repeated invocations safe.
Contingency: Add a compensation query in the reconciliation log that detects duplicate log entries for the same certification period and alerts the operations team for manual review within 24 hours.
The HLF Dynamics portal API may have eventual-consistency behaviour or rate limits that cause website listing updates to lag behind status changes, leaving expired mentors visible on the public website for an unacceptable window.
Mitigation & Contingency
Mitigation: Design the sync service to be triggered immediately on status transitions (event-driven via database webhook) in addition to the nightly batch run. Implement a reconciliation job that verifies sync state against app state and re-triggers any divergent records.
Contingency: If real-time sync cannot be guaranteed, implement a manual 'force sync' action in the coordinator dashboard so coordinators can trigger an immediate re-sync for urgent cases. Document the expected sync lag in coordinator onboarding materials.
Stakeholder requests to extend the expiry checker to handle additional certification types, grace periods, or organisation-specific threshold configurations may significantly increase scope beyond what is designed here, delaying delivery.
Mitigation & Contingency
Mitigation: Parameterise threshold day values (30, 14, 7) via configuration repository rather than hard-coding them, enabling per-organisation customisation without code changes. Document that grace period logic and additional cert types are out of scope for this epic and require a dedicated follow-up.
Contingency: Deliver the feature with hard-coded HLF-standard thresholds first and introduce the configuration repository as a follow-up task in the next sprint, using a feature flag to enable per-org threshold overrides.
Dynamics portal API credentials stored as environment secrets in Supabase Edge Function configuration may be rotated or invalidated by HLF IT without notice, causing silent sync failures that go undetected for multiple days.
Mitigation & Contingency
Mitigation: Implement credential health-check calls on each scheduler run and emit an immediate alert on auth failure rather than only alerting after N consecutive failures. Document the credential rotation procedure with HLF IT and establish a rotation notification protocol.
Contingency: Maintain a break-glass manual sync script accessible to HLF administrators that can re-execute the Dynamics sync with newly provided credentials while the automated system is restored.