Implement Stats Repository with date-range and role filters
epic-activity-statistics-dashboard-data-foundation-task-008 — Build the Stats Repository Dart class that queries the Supabase materialized views. It must expose: fetchSnapshot(TimeWindow, chapterIds) → StatsSnapshot, fetchPeerMentorRows(TimeWindow, chapterId) → List<PeerMentorStatRow>, and fetchChartPoints(TimeWindow, chapterId, granularity) → List<ChartDataPoint>. All methods accept a DateTimeRange (from TimeWindowService) and a List<String> chapterIds (from ChapterScopeResolver). Use Supabase postgrest-dart client with .gte/.lte date filters. Return typed models from task-001.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 4 - 323 tasks
Can start after Tier 3 completes
Implementation Notes
Define `StatsRepository` as an abstract class (interface) first — this is critical for BLoC testability. The Riverpod provider should be a `Provider
Add a `StatsViewColumns` constants class early to centralise column name strings — this prevents silent breakage when views are renamed. Convert all `DateTime` to UTC via `.toUtc().toIso8601String()` before passing to filters. Wrap the entire query in a try/catch that catches `PostgrestException` and rethrows as `StatsRepositoryException(code, message, originalException)`.
Testing Requirements
Unit tests: mock the Supabase client using `mocktail`, verify that correct PostgREST filter chains (`.gte`, `.lte`, `.in_`) are called for each method; verify that `PostgrestException` is wrapped into `StatsRepositoryException`; verify empty list handling. Integration tests (against local Supabase): seed two chapters with known activity counts; assert `fetchSnapshot` totals match seed data; assert RLS blocks cross-chapter access when authenticated as a restricted coordinator. Minimum 90% branch coverage on `SupabaseStatsRepository`.
Materialized views over large activity tables may have refresh latency exceeding the 2-second SLA under high insert load, causing stale data to appear on the dashboard immediately after a peer mentor registers an activity.
Mitigation & Contingency
Mitigation: Design the materialized view refresh trigger to run asynchronously via a Supabase Edge Function rather than a synchronous trigger, and set a maximum staleness tolerance of 5 seconds documented in the feature spec. Add a CONCURRENTLY refresh strategy so reads are never blocked.
Contingency: If refresh latency cannot meet SLA, fall back to a regular (non-materialized) view for the dashboard and accept slightly higher query cost per request. Revisit materialized approach once Supabase pg_cron or background workers are available.
The aggregation counting rules for the dashboard may diverge from those used in the Bufdir export pipeline (e.g., which activity types count, how duplicate registrations are handled), creating a reconciliation burden for coordinators at reporting time.
Mitigation & Contingency
Mitigation: Run the BufDir Alignment Validator against a shared reference dataset before any view is merged to main. Encode the counting rules as a shared Supabase function called by both the stats views and the export query builder so there is a single source of truth.
Contingency: If divergence is discovered post-launch, ship a visible banner on the dashboard stating that numbers are indicative and may differ from the export until the reconciliation fix is deployed. Prioritize the fix as a P0 defect.
Multi-chapter coordinators (up to 5 chapters per NHF requirement) require RLS policies that filter on an array of chapter IDs, which is more complex than single-value RLS and could be misconfigured, leaking data across chapters or blocking legitimate access.
Mitigation & Contingency
Mitigation: Write integration tests that verify cross-chapter isolation for a coordinator assigned to chapters A and B cannot see data from chapter C. Use parameterized RLS policies with auth.uid()-based chapter lookup to avoid hardcoded values.
Contingency: If RLS misconfiguration is detected in testing, temporarily restrict coordinator queries to single-chapter scope (coordinator's primary chapter) and ship multi-chapter support as a fast-follow patch once RLS logic is verified.