Define typed Dart models for OrganizationUnit
epic-organizational-hierarchy-management-foundation-task-004 — Create typed Dart model classes for OrganizationUnit and related types: OrganizationUnitType enum (national, region, chapter), OrganizationUnit with all schema fields, and OrganizationUnitTree for the in-memory tree representation. Include fromJson/toJson serialization, copyWith, equality, and hashCode. Follow existing project model conventions.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Follow the existing project model conventions ā inspect neighboring model files before writing to match constructor style (named parameters, required keyword), file naming, and directory placement. Use Dart's const constructor where all fields are final for compile-time immutability. For equality and hashCode, using only id is intentional and consistent with entity identity semantics. For OrganizationUnitTree.fromFlatList, build a Map
The root of the tree is the node with parentId == null. Handle the edge case where multiple roots exist (should not occur in valid data, but log a warning rather than throwing). DateTime parsing from Supabase returns ISO 8601 strings with timezone ā use DateTime.parse() which handles this correctly. Do not use code generation (json_serializable, freezed) unless the rest of the project already uses it ā hand-write serialization to match existing patterns.
Testing Requirements
Write unit tests using flutter_test. Test file location: test/models/organization_unit_test.dart. Scenarios: (1) fromJson round-trip with all fields populated. (2) fromJson with deletedAt = null.
(3) fromJson with an unknown unit_type string throws ArgumentError. (4) toJson produces correct snake_case keys. (5) copyWith produces a new instance with updated fields, original unchanged. (6) Two OrganizationUnit instances with the same id are == regardless of other field differences.
(7) OrganizationUnitTree.fromFlatList with 1 national + 3 regions + 9 chapters builds correct tree structure. (8) findById returns correct node for leaf, intermediate, and root. (9) findById returns null for non-existent id. Aim for 100% branch coverage on model logic.
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.