Publish recruitment milestone events to badge layer
epic-membership-recruitment-core-services-task-010 — Implement the publishMilestoneEvent method in ReferralAttributionService. After each confirmed attribution, evaluate whether the mentor's cumulative confirmed count has crossed a recruitment milestone threshold (e.g., 1st, 5th, 10th recruit). If a threshold is crossed, publish a structured milestone event to the BadgeCriteriaIntegration layer using the agreed event contract so the badge system can subscribe and award the appropriate badge.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 5 - 253 tasks
Can start after Tier 4 completes
Implementation Notes
Define MilestoneEvent as an immutable Dart class with copyWith. Use a static const List
Avoid tight coupling — BadgeCriteriaIntegration should only depend on the abstract MilestoneEventSource interface, not on ReferralAttributionService directly. Ensure the StreamController is closed in the provider's onDispose to prevent memory leaks.
Testing Requirements
Unit tests (flutter_test): test publishMilestoneEvent with mocked BadgeCriteriaIntegration using Mockito-style fakes. Scenarios: count exactly at threshold (1, 5, 10), count one below threshold (no event), count jumps over threshold in one increment (single event for crossed threshold), duplicate attribution_id (no second event), BadgeCriteriaIntegration not yet registered (buffer then replay on register), integration throws during onMilestoneEvent (exception isolated, attribution not rolled back). All tests run in < 2 seconds total. No network calls — fully synchronous with in-memory fakes.
Confirmed registration events originate from the membership system (Dynamics portal for HLF), which may call back asynchronously with significant delay. If the attribution service only accepts synchronous confirmation at registration time, late callbacks will fail to match the originating referral code, resulting in under-counted conversions.
Mitigation & Contingency
Mitigation: Design the attribution confirmation path as a webhook endpoint (Supabase Edge Function) that accepts a referral_code + new_member_id pair at any time after click. The service matches by code string, not by session. Persist pending_signup events immediately at onboarding screen submission so there is always a record to upgrade to 'confirmed' when the webhook fires.
Contingency: If the membership system cannot reliably call the webhook, implement a polling reconciliation job (Supabase pg_cron, daily) that queries the membership system for recently registered members and back-fills any unmatched attribution records.
If confirmRegistration() is called more than once for the same new member (e.g., idempotency retry from the webhook), duplicate milestone events could be emitted, causing the badge system to award badges multiple times.
Mitigation & Contingency
Mitigation: Use a UNIQUE constraint on (referral_code_id, new_member_id) in the referral_events table for confirmed events. The confirmRegistration() method uses upsert semantics; milestone evaluation reads the confirmed count from the aggregation query rather than counting individual calls.
Contingency: If duplicate awards occur in production, the badge system should support idempotent award checks (query existing badges before awarding). Add a deduplication guard in BadgeCriteriaIntegration as a secondary defence.
Stakeholder review may expand attribution requirements mid-epic to include click-through tracking per channel (WhatsApp vs SMS vs email), which is not currently in scope but was mentioned in user story discussions. This would require schema changes in the foundation epic and delay delivery.
Mitigation & Contingency
Mitigation: Capture per-channel data in the device_metadata JSONB field from day one as an unstructured field (share_channel: 'whatsapp'). This preserves data without requiring a schema column, allowing structured querying to be added later without migrations.
Contingency: If channel-level analytics become a hard requirement during this epic, timebox the change to adding a nullable channel column to referral_events and a corresponding filter parameter on the aggregation query, deferring dashboard UI to a separate task.