Implement WayForwardItemRepository CRUD
epic-structured-post-session-report-foundation-task-003 — Implement the WayForwardItemRepository Dart class with Supabase client integration. Provide methods: createItem, getItemsByReportId, updateItem, deleteItem, markComplete. Handle Supabase PostgREST errors, map rows to typed WayForwardItem models, and wrap all operations in try/catch with domain-specific exceptions.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Follow the repository pattern already established in the codebase — look at existing repositories (e.g., activity or contact repositories) for the error-handling pattern and exception hierarchy before creating new exception types. Use .select().single() after insert and update to get the server-returned row. For markComplete, use .update({'status': 'completed'}).eq('item_id', itemId).select().single() rather than fetching then updating to avoid a double round-trip. The WayForwardItemStatus enum should have a fromString factory that maps database string values and throws on unknown values to catch schema drift early.
Inject SupabaseClient rather than using Supabase.instance directly to keep the class testable.
Testing Requirements
Write unit tests using flutter_test with a mocked SupabaseClient (use mockito or mocktail). Test each method: (1) createItem returns mapped WayForwardItem on success, (2) getItemsByReportId returns correct list and empty list when no items exist, (3) updateItem sends correct fields and returns updated model, (4) deleteItem calls delete with correct item_id filter, (5) markComplete sends status='completed', (6) each method throws WayForwardItemRepositoryException when Supabase throws PostgrestException, (7) each method throws WayForwardItemRepositoryException on network timeout. Test fromJson/toJson round-trip for all WayForwardItem fields including null dueDate and null assignedTo. Aim for 100% method coverage on repository and model.
Supabase RLS policies for multi-org report access may be more complex than anticipated — coordinators need cross-peer-mentor access within their org but not across orgs, and draft reports should be invisible to coordinators until submitted. Misconfigured RLS could expose sensitive health data or block legitimate access.
Mitigation & Contingency
Mitigation: Define and test RLS policies in isolation before writing repository code. Create a dedicated SQL migration file with policy definitions and an automated integration test suite that verifies each role's access boundaries using real Supabase auth tokens.
Contingency: If RLS proves too complex to express declaratively, implement application-level access control in the repository layer with explicit org and role checks, and add a security audit task before the feature goes to production.
The org field config JSON stored in Supabase may lack a stable, versioned schema contract. If different organisations have drifted to different field-definition formats, org-field-config-loader will fail silently or crash, breaking form rendering for those orgs.
Mitigation & Contingency
Mitigation: Define a canonical JSON Schema for field config and validate all existing org configs against it before implementation begins. Store a schema version field in every config record and handle version migrations explicitly in the loader.
Contingency: If existing configs are too heterogeneous, implement a config normalisation pass in org-field-config-loader that coerces known variants to the canonical format, logging warnings for fields that cannot be normalised so operations can fix them in the admin console.
TTL-based schema cache invalidation may cause peer mentors to use stale field definitions for up to the TTL window after an admin updates the org config, potentially collecting data against outdated field structures.
Mitigation & Contingency
Mitigation: Set a conservative TTL (e.g. 15 minutes) and expose a manual cache-bust mechanism triggered on app foreground-resume. Document the maximum staleness window in the admin console so org admins know to plan config changes outside active reporting windows.
Contingency: If stale schema causes a data quality incident, add a Supabase Realtime subscription to the org config table that invalidates the cache immediately on any config update.