Implement ActivitySummaryAggregator service
epic-benefit-calculator-foundation-task-005 — Build the ActivitySummaryAggregator service that queries completed activity records for the current peer mentor from Supabase (filtering by mentor_id, status='completed', and an optional date range). Compute aggregate sessionCount and averageDurationMinutes in-memory after fetching lightweight rows (id, duration_minutes, completed_at). Return an ActivitySummary model to pre-populate calculator inputs. Handle empty result sets gracefully by returning zeroed ActivitySummary.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Implement in lib/features/benefit_calculator/data/services/activity_summary_aggregator.dart. Inject the Supabase client via constructor. Use supabase.from('activities').select('id, duration_minutes, completed_at').eq('mentor_id', mentorId).eq('status', 'completed').gte('completed_at', dateRange.start.toIso8601String()).lte('completed_at', dateRange.end.toIso8601String()). Cast result to List
Use null-aware arithmetic: (row['duration_minutes'] as num?)?.toDouble() ?? 0.0. For averageDurationMinutes, guard against division by zero: sessionCount > 0 ? totalDurationMinutes / sessionCount : 0.0.
Register as a Riverpod provider or inject via constructor in the BLoC that uses it. Define ActivitySummaryFetchException in the domain layer with a message and optional Supabase error code field.
Testing Requirements
Unit tests using flutter_test and mocktail. Mock the Supabase client to return predefined row lists. Test file: test/services/activity_summary_aggregator_test.dart. Scenarios: (1) multiple completed activities returns correct sessionCount, totalDurationMinutes, averageDurationMinutes, (2) empty result set returns ActivitySummary.empty() with correct period dates, (3) row with null duration_minutes treated as 0 and logged, (4) PostgrestException mapped to ActivitySummaryFetchException, (5) single-row result (averageDurationMinutes equals totalDurationMinutes).
Verify that the Supabase query mock was called with the exact expected filters using mocktail's verify().
Supabase organisation configuration table may not yet have the five multiplier columns, requiring a migration. If the migration is not coordinated with other teams touching the same table, schema conflicts could delay delivery.
Mitigation & Contingency
Mitigation: Add multiplier columns in a dedicated, non-destructive ALTER TABLE migration script. Review the organisation config table schema with the backend team before writing the migration. Use nullable columns with defaults so existing rows are unaffected.
Contingency: If the migration cannot be deployed in time, stub the repository to return hardcoded default multiplier values from a local config file, allowing parallel development. Swap in the real Supabase fetch once the migration is live.
The ActivitySummaryAggregator depends on activity records already persisted in the database. For newly onboarded peer mentors with no activity history, the aggregator will return zero counts, which could make the calculator appear broken on first use.
Mitigation & Contingency
Mitigation: Design the pre-fill value object to distinguish between 'no data yet' and 'zero activities'. The calculator input panel should display empty inputs (not zero) when no history exists, with placeholder text guiding the user to enter values manually.
Contingency: If the distinction cannot be surfaced cleanly in the UI timeline, fall back to always showing empty inputs and document the manual-entry path as the primary UX until activity data accumulates.