Implement DeclarationAuditLogger with insert-only write pattern
epic-driver-and-confidentiality-management-foundation-task-010 — Implement the Dart audit logger component that writes immutable records to the declaration_audit_log table. Expose strongly-typed methods for each lifecycle event: logDeclarationSent, logDeclarationOpened, logDeclarationAcknowledged, logDeclarationExpired, logDeclarationRevoked. Never expose any update or delete operations. Wrap all writes in error handling with local buffering fallback.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 3 - 413 tasks
Can start after Tier 2 completes
Implementation Notes
The fire-and-forget vs await decision: for audit logging, prefer unawaited(logger.logDeclarationSent(...)) at the call sites in the repository/service layer so that audit failures never block the main user action. However, the logger itself should still be async internally to handle retry.
Implement the local buffer as a simple List
Ensure the AuditEventType enum values map exactly to the Postgres enum strings ('sent', 'opened', etc.) via a toSupabaseString() extension method.
Testing Requirements
Unit tests using flutter_test with mocked Supabase client (mocktail): (1) each log method inserts a row with the correct event_type string; (2) actor_id in the inserted row matches supabase.auth.currentUser.id; (3) a Supabase network error causes the event to be added to the local buffer; (4) after network recovery, buffered events are retried and inserted; (5) buffer overflow (>50 events) triggers persistence to local storage; (6) no update or delete method exists on the public interface (verified by reflection or code review check); (7) AuditLogException is thrown (not rethrown raw) on Supabase error. Integration test against local Supabase emulator: full lifecycle log (sent → opened → acknowledged) produces 3 immutable rows in declaration_audit_log.
Row-level security policies for driver assignments and declarations must correctly scope data to the coordinator's chapter without leaking records across organizations. An incorrect RLS predicate could silently return empty result sets or, worse, expose cross-org data, both of which are difficult to detect in unit tests.
Mitigation & Contingency
Mitigation: Write dedicated RLS integration test scenarios with multiple org fixtures asserting both data isolation and correct data visibility. Use Supabase's built-in policy testing utilities and review policies with a second developer.
Contingency: If RLS policies prove too complex to get right quickly, implement application-layer org scoping as a temporary guard while RLS is fixed in a follow-up, with an explicit security review gate before production deployment.
The declaration audit logger must produce tamper-evident records. If the database allows updates or deletes on audit rows, the compliance guarantee is broken. Supabase does not natively prevent row deletion by default.
Mitigation & Contingency
Mitigation: Implement an insert-only RLS policy on the audit table that denies UPDATE and DELETE for all roles including the service role. Add a database trigger that rejects mutation attempts and logs the attempt itself.
Contingency: If immutability cannot be enforced at the database level within the sprint, store audit entries in an append-only Supabase Edge Function log stream as a temporary alternative, with a migration plan to the proper table once constraints are implemented.
The org-feature-flag-service caches flag values to avoid repeated database reads. If the cache is not invalidated promptly after an admin toggles the flag, coordinators may see stale UI state — either seeing driver features when they should not, or not seeing them when they should.
Mitigation & Contingency
Mitigation: Use a Supabase Realtime subscription to listen for changes on the driver_feature_flag_config table and invalidate the in-memory cache immediately on change. Set a short TTL (60 seconds) as a safety net.
Contingency: If Realtime subscription proves unreliable, expose a manual cache-bust endpoint accessible from the admin toggle action, ensuring the cache is cleared synchronously on every flag change.