Wire HierarchyCache invalidation to repository mutations
epic-organizational-hierarchy-management-foundation-task-012 — Connect HierarchyCache invalidation hooks to all mutating operations in OrganizationUnitRepository and UnitAssignmentRepository. After any create, update, softDelete, or restore operation the cache must be automatically invalidated and re-populated. Ensure invalidation is also triggered when ActiveChapterState broadcasts a chapter change that affects cached scope.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 5 - 253 tasks
Can start after Tier 4 completes
Implementation Notes
Use the Observer/callback pattern: inject HierarchyCache into both repositories at construction time and call cache.invalidate() at the end of each mutating method (after the Supabase call succeeds). Do not call invalidate() in a fire-and-forget manner — await the re-population so callers can be confident the cache is fresh when the mutation Future resolves. For concurrency safety, wrap the invalidate+populate sequence in a Mutex (using the synchronized package) to serialize concurrent invalidation calls. For debouncing rapid triggers from ActiveChapterState, use a StreamTransformer with debounceTime (from rxdart) on the activeChapterProvider stream.
Keep invalidation logic in a CacheInvalidationCoordinator helper class rather than scattering it across repositories — this makes the dependency graph explicit and testable.
Testing Requirements
Unit tests (flutter_test): (1) create() on OrganizationUnitRepository calls HierarchyCache.invalidate() exactly once. (2) softDelete() causes the deleted unit to be absent from cache after re-population. (3) restore() causes the restored unit to be present after re-population. (4) UnitAssignment mutation triggers cache invalidation.
(5) Failed re-population preserves previous in-memory cache. (6) Concurrent mutations do not corrupt cache state (use async test with multiple simultaneous calls). (7) Debounce — rapid successive mutations result in only one re-population call. Mock HierarchyCache and capture invalidate() call count.
Integration test: perform a softDelete against a local Supabase instance and assert the deleted unit is absent from a subsequent getNode() call.
Recursive CTE queries for large hierarchies (1,400+ nodes) may exceed Supabase query timeouts or produce unacceptably slow responses, degrading tree load time beyond the 1-second target.
Mitigation & Contingency
Mitigation: Implement Supabase RPC functions for subtree fetches rather than client-side recursive calls. Use materialized path or closure table as a supplemental index for depth-first traversal. Benchmark with realistic NHF data volumes during development.
Contingency: Fall back to a pre-computed flat unit list stored in the hierarchy cache with client-side tree reconstruction, trading freshness for speed. Add a background refresh job to keep the cache warm.
Concurrent writes from multiple admin sessions could cause cache staleness, leading to stale tree views and incorrect ancestor path computations that corrupt aggregation results.
Mitigation & Contingency
Mitigation: Use optimistic versioning on cache entries with a short TTL (5 minutes) as a safety net. Subscribe to Supabase Realtime on the organization_units table to push invalidation events to all connected clients.
Contingency: Provide a manual 'Refresh Hierarchy' action in the admin portal that forces a full cache bust, and display a staleness warning banner when the cache age exceeds the TTL.
Persisting the flat unit list to local storage may expose organization structure data if the device is compromised or the storage is not properly encrypted, violating data protection requirements.
Mitigation & Contingency
Mitigation: Use flutter_secure_storage (AES-256 backed by Keychain/Keystore) for the local unit list cache rather than SharedPreferences. Include only unit IDs, names, and types — no member PII.
Contingency: Disable local-storage persistence entirely and rely on in-memory cache only. Accept the trade-off of no offline hierarchy access for the security guarantee.