Build StatsRepository with period and activity-type filters
epic-coordinator-statistics-dashboard-foundation-task-006 — Implement the StatsRepository class that queries the pre-aggregated Supabase views with period and activity-type filters. Expose methods: getCoordinatorStats(orgId, coordinatorId, period, activityType), getPeerMentorStats(orgId, coordinatorId, period), and getOrgStats(orgId, period). Integrate RoleAccessValidator before any query execution. Return typed Dart model objects.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Define a StatsPeriod value object with start and end DateTime fields validated on construction (start must be before end). Use Supabase's .from('coordinator_stats_view').select().eq('org_id', orgId).eq('coordinator_id', coordinatorId).gte('date', period.start.toIso8601String()).lte('date', period.end.toIso8601String()) pattern. Model the return types with freezed or manual immutable classes — do not return raw Map
Register via Riverpod: final statsRepositoryProvider = Provider
Testing Requirements
Unit tests with mocked SupabaseClient covering: (1) correct query parameters forwarded for each method; (2) RoleAccessValidator called before Supabase client for each method; (3) AccessDeniedException thrown when validator rejects; (4) network error mapped to StatsError.networkError; (5) empty result set returns empty model, not null or exception; (6) null activityType returns unfiltered results. Integration tests against a local Supabase instance verifying RLS blocks cross-org access even with direct client credentials. Target 90%+ branch coverage.
Pre-aggregated Supabase views may still be slow for orgs with very large activity datasets (NHF with 1,400 chapters). If the view query plan performs sequential scans, dashboard load times could exceed acceptable thresholds and degrade the perceived value of the feature.
Mitigation & Contingency
Mitigation: Design views with composite indexes on (org_id, coordinator_id, month) from the start. Run EXPLAIN ANALYZE during development against a seeded dataset of realistic scale. Add materialized view refresh strategy if needed.
Contingency: If live view performance is insufficient, convert to materialized views refreshed on a schedule or on activity-write triggers. Expose the refresh delay transparently in the UI with a 'last updated' timestamp.
Supabase RLS policies for the stats views may not be configured correctly during initial migration, potentially allowing cross-coordinator data leakage before the RoleAccessValidator layer is reached. This is a security and compliance risk.
Mitigation & Contingency
Mitigation: Write RLS integration tests as part of this epic that explicitly verify a coordinator JWT cannot read another coordinator's stats rows. Apply RLS policies in the migration script itself, not as a manual step.
Contingency: If an RLS gap is discovered post-deployment, immediately disable the stats screen via a feature flag, apply the corrected RLS migration, and re-enable after verification. Log and audit all queries that ran during the gap window.
Cache invalidation logic may not be triggered correctly when a new activity is registered by a peer mentor or when an expense approval is granted. Stale data could cause coordinators to make decisions based on outdated KPIs, undermining trust in the dashboard.
Mitigation & Contingency
Mitigation: Define explicit invalidation event contracts with the activity registration and expense approval pipelines. Implement an event bus subscription within StatsCacheManager. Document the invalidation contract in code.
Contingency: If event-driven invalidation proves unreliable, add a manual 'Refresh' pull-to-refresh gesture on the dashboard and reduce TTL to 5 minutes as a fallback degradation strategy.