Implement metrics repository read operations
epic-bufdir-data-aggregation-data-layer-task-006 — Implement the concrete Bufdir Metrics Repository class for all read operations: delegating fetchRawActivities, fetchRawEvents, fetchRawContacts, fetchParticipantCount, and fetchGeographicDistribution calls to the Aggregation Query Builder. The repository must provide error mapping from Supabase exceptions to domain-level typed failures and expose results as Either types or similar error-handling pattern consistent with the codebase.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 4 - 323 tasks
Can start after Tier 3 completes
Implementation Notes
Follow the existing repository pattern in the codebase — look at any existing SupabaseRepository implementations for error-mapping conventions before writing new code. Use a sealed class hierarchy for DomainFailure (QueryFailure, NetworkFailure, UnauthorizedFailure) if not already present. The Either pattern should use dartz or the project's existing Result type — do not introduce a new dependency. For geographic distribution, verify PostGIS returns municipality-level codes (kommunenummer) and not raw ST_Point coordinates before passing to the domain model.
Inject AggregationQueryBuilder via Riverpod Provider so tests can override it. Keep the repository thin — zero business logic, only delegation and error mapping.
Testing Requirements
Unit tests (flutter_test + mockito) must cover: (1) each of the five read methods delegates to MockAggregationQueryBuilder with correct parameters, (2) Supabase PostgrestException is mapped to QueryFailure with correct code, (3) generic Exception is mapped to NetworkFailure, (4) null/empty org_id causes ArgumentError before any builder call, (5) valid response is wrapped in Right() with correctly parsed domain model, (6) repository can be injected as IBufdirMetricsRepository mock in service-layer tests. Minimum 90% line coverage on the repository class. No real Supabase connections in unit tests.
Supabase RPC functions return JSON with PostgreSQL numeric types (bigint, numeric) that do not map cleanly to Dart int/double. Silent truncation or JSON parsing errors could corrupt participant counts in the final Bufdir submission without any runtime exception.
Mitigation & Contingency
Mitigation: Define explicit Dart fromJson factories for all RPC result models with type-safe parsing and assertion checks. Add a contract test that compares raw RPC JSON output against expected Dart model values using a known seed dataset.
Contingency: If type mismatches are found in production metrics, expose a validation endpoint in BufdirMetricsRepository that re-fetches and compares raw RPC output against the persisted snapshot, flagging any discrepancies before export proceeds.
Persisted metric snapshots can become stale if additional activities are registered after the snapshot is saved but before the export is finalized. Coordinators might unknowingly export data that does not reflect the latest activity registrations.
Mitigation & Contingency
Mitigation: Store a snapshot_generated_at timestamp and a record_count_at_generation field in the snapshot. When the coordinator views cached results, compare the current activity count for the period against the snapshot value and display a 'Data updated since last aggregation — re-run?' warning if counts differ.
Contingency: Add a mandatory staleness check before the export confirmation dialog can proceed: if the snapshot is more than 24 hours old or the record count has changed, require the coordinator to re-run aggregation before the export button is enabled.