Implement BLoC Event Handlers with Service Calls
epic-certification-management-automation-task-007 — Implement the event handler methods inside CertificationBLoC for LoadCertification, RecordRenewal, and EnrolInCourse events. Each handler should emit a loading state, call the appropriate CertificationManagementService method, then emit success or error states. Use async emit-while pattern to prevent state leaks on concurrent events.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Import `bloc_concurrency` and apply `transformer: droppable()` to the `LoadCertification` handler and `transformer: sequential()` to `RecordRenewal` and `EnrolInCourse`. Use the `on
Ensure `CertificationManagementService` is defined as an abstract interface so it can be mocked cleanly in tests without hitting Supabase.
Testing Requirements
Unit tests using `bloc_test` package and `mocktail` (or Mockito) for mocking `CertificationManagementService`. Test 1 (LoadCertification success): `blocTest` expects states `[CertificationLoading, CertificationLoaded]` when service returns a valid Certification. Test 2 (LoadCertification error): expects `[CertificationLoading, CertificationError]` when service throws `ServiceException`. Test 3 (RecordRenewal success): expects `[CertificationLoading, RenewalSuccess]`.
Test 4 (RecordRenewal error): expects `[CertificationLoading, CertificationError]`. Test 5 (EnrolInCourse success): expects `[CertificationLoading, EnrolmentSuccess]`. Test 6 (concurrent LoadCertification with droppable): fire two `LoadCertification` events in rapid succession; assert service is called only once. Test 7 (unexpected exception): service throws a raw `Exception`; assert `CertificationError` is emitted with the generic message string.
Minimum 80% branch coverage.
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.