Implement MentorActivityLogRepository aggregated statistics fetch
epic-peer-mentor-detail-screen-data-layer-task-006 — Implement fetchActivitySummary(String mentorId, ActivityPeriod period) in MentorActivityLogRepository. The method must query aggregated activity statistics from the mentor_activity_logs table (total sessions, total hours, unique contacts reached), apply period filters (last 30 days, last 90 days, year-to-date, all time), and return a typed MentorActivitySummary. Use Supabase aggregate functions or a dedicated RPC call to minimize data transfer.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Prefer a Supabase Postgres RPC function for aggregation: CREATE OR REPLACE FUNCTION get_mentor_activity_summary(p_mentor_id uuid, p_start_date timestamptz DEFAULT NULL, p_end_date timestamptz DEFAULT NULL) RETURNS json. This keeps aggregation logic in the database, avoids large data transfers, and can be optimized with indexes independently of the Flutter app. Call via _supabase.rpc('get_mentor_activity_summary', params: {'p_mentor_id': mentorId, 'p_start_date': startDate?.toIso8601String(), 'p_end_date': endDate?.toIso8601String()}). For date range computation: use DateTime.now().toUtc() as the reference point; for yearToDate, compute DateTime(now.year, 1, 1, 0, 0, 0, 0, 0) in UTC.
If the RPC approach is not yet available (function not created), implement a fallback that fetches raw rows with date filters and aggregates in Dart — clearly mark this as a temporary workaround with a TODO and the RPC ticket reference. The zero-value fallback for empty periods should be implemented in fromJson by defaulting all numeric fields to 0 when the JSON values are null.
Testing Requirements
Unit tests using flutter_test with mocked SupabaseClient. Test file: test/features/peer_mentor/data/repositories/mentor_activity_log_repository_test.dart. Required cases: (1) allTime period produces call without date filter and maps to populated MentorActivitySummary, (2) last30Days period passes correct start_date (today minus 30 days in UTC) and end_date (today in UTC) to the query or RPC, (3) last90Days period passes correct start_date (today minus 90 days), (4) yearToDate period passes January 1st of current year as start_date, (5) Supabase returns empty/zero aggregates and result maps to MentorActivitySummary with all zeros, (6) RepositoryException thrown on PostgrestException. For date assertions, freeze the clock using a Clock abstraction or pass DateTime as parameter to allow deterministic testing.
Supabase RLS policies for peer mentor data may block coordinator queries if the RLS rules are written for peer-mentor-self access only, requiring policy updates that affect other features sharing the same tables.
Mitigation & Contingency
Mitigation: Review existing RLS policies on peer_mentors, certification_records, and activity_log tables before writing repository queries. Coordinate with the database team to add coordinator-role predicates without weakening existing mentor-self policies.
Contingency: If policy changes are blocked, implement a Supabase Edge Function as a secure query proxy that enforces authorization server-side, avoiding direct RLS policy modification.
The activity log table schema may not have a mentor_id foreign key column or may require a JOIN through an intermediate table, making the aggregation query significantly more complex than anticipated.
Mitigation & Contingency
Mitigation: Inspect the actual Supabase activity_log table schema before starting the MentorActivityLogRepository implementation. Document the exact JOIN path needed and validate it returns correct results for a known mentor.
Contingency: If schema requires complex multi-table aggregation, implement a Supabase database function (RPC) and expose it via the repository's fetchSummary method to keep Dart code clean.
The Blindeforbundet assignment table may not yet exist in the shared Supabase schema or may have a different structure than assumed, blocking the AssignmentHistoryRepository implementation.
Mitigation & Contingency
Mitigation: Verify the assignments table exists and confirm its column structure with the Contact Detail & Edit Screen team which also depends on assignment data (assignment-repository in that feature).
Contingency: If the assignments table is not yet available, implement the AssignmentHistoryRepository with a stub returning empty list and a TODO marker, unblocking the aggregation service while the schema is finalized.