Define HierarchyService data models and interfaces
epic-organizational-hierarchy-management-core-services-task-001 — Define the core Dart interfaces, data classes, and type definitions for the HierarchyService layer. This includes OrganizationUnit model, HierarchyNode, parent-child relationships, and the service interface contract that all downstream components will depend on.
Acceptance Criteria
Technical Requirements
Implementation Notes
Define the interface contract before implementation details. The abstract HierarchyService should return typed Futures (e.g., Future>) not dynamic. Use Dart's sealed classes (Dart 3+) for HierarchyServiceException to enable exhaustive switch matching downstream. OrganizationUnit.parentId being nullable is the canonical signal for a root node — document this with a comment.
Align naming conventions with the rest of the Flutter project (check existing model files for whether the project uses snake_case JSON keys with camelCase Dart fields, and match that pattern). The HierarchyNode children list enables BLoC/Riverpod state to hold a fully materialized tree without repeated database queries — this is the key architectural decision that enables efficient UI rendering of NHF's 1,400-chapter tree.
Testing Requirements
Write unit tests in test/features/hierarchy/data/models/ for: (1) OrganizationUnit.fromJson parses a valid JSON map correctly including null parentId; (2) OrganizationUnit.toJson round-trips correctly; (3) HierarchyNode children list is empty by default; (4) sealed exception classes can be matched in a switch statement without a default branch (exhaustiveness check). Tests should use flutter_test. No mocking required for this task — pure Dart data model tests only. Target 100% line coverage for the models and interface files.
Injecting all unit assignment IDs into JWT claims for users assigned to many units (up to 5 for NHF peer mentors, many more for national coordinators) may exceed JWT size limits, causing authentication failures.
Mitigation & Contingency
Mitigation: Store unit IDs in a Supabase session variable or a dedicated Postgres function rather than embedding them directly in the JWT payload. Use set_config('app.unit_ids', ...) within RLS helper functions querying the assignments table at policy evaluation time.
Contingency: Fall back to querying the unit_assignments table directly within RLS policies using the authenticated user ID, accepting a small per-query overhead in exchange for removing the JWT size constraint.
Rendering 1,400+ nodes in a recursive Flutter tree widget may cause jank or memory pressure on lower-end devices used by field peer mentors, degrading the admin experience.
Mitigation & Contingency
Mitigation: Implement lazy tree expansion — only the root level is rendered on initial load. Child nodes are rendered on demand when the parent is expanded. Use const constructors and ListView.builder for all node lists to minimize rebuild scope.
Contingency: Add a search/filter bar that scopes the visible tree to matching nodes, reducing the visible node count. Provide a 'flat list' fallback view for administrators who prefer searching over browsing the tree.
Requirements for what constitutes a valid hierarchy structure may expand during NHF sign-off (e.g., mandatory coordinator assignments per chapter, minimum member counts per region), requiring repeated validator redesign.
Mitigation & Contingency
Mitigation: Design the validator as a pluggable rule engine where each check is a discrete, independently testable function. New rules can be added without changing the core validation orchestration. Surface all rules in a configuration table per organization.
Contingency: Defer non-blocking validation rules to warning-level feedback rather than hard blocks, allowing structural changes to proceed while flagging potential issues for admin review.
Deploying RLS policy migrations to a shared Supabase project used by multiple organizations simultaneously could lock tables or interrupt active sessions, causing downtime during production migration.
Mitigation & Contingency
Mitigation: Write all RLS policies as CREATE POLICY IF NOT EXISTS statements. Schedule migrations during off-peak hours. Use Supabase's migration preview environment to validate policies against production data shapes before applying.
Contingency: Prepare rollback migration scripts for every RLS policy. If a migration causes issues, execute the rollback immediately and re-test the policy logic in staging before reattempting.