Document unit assignment and aggregation architecture
epic-organizational-hierarchy-management-assignment-aggregation-task-018 — Produce developer documentation covering: the unit assignment constraint model (single primary, 5-chapter max, RLS scoping), the aggregation rollup algorithm and cache invalidation strategy, the Hierarchy Node Editor's validation pipeline and cycle detection approach, Bufdir breakdown query API contract, and known edge cases for multi-chapter membership in NHF's 1,400-chapter tree. Include sequence diagrams for assignment and rollup flows.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 7 - 84 tasks
Can start after Tier 6 completes
Implementation Notes
Write the assignment constraint section first — it is the highest-risk area for misunderstanding (the single primary rule and 5-chapter cap are non-obvious). For Mermaid sequence diagrams, use sequenceDiagram with participant labels: Flutter App, UnitAssignmentService, HierarchyStructureValidator, Supabase DB. The aggregation rollup diagram should show the edge function call path separately from the direct DB CTE path. Pull exact RLS policy names from database.ts or the Supabase dashboard before writing to avoid documentation drift.
Use a callout box (> **Note:**) for each known edge case so they are visually distinct. Keep the document under 1,500 words of prose — diagrams and code blocks carry more weight than text here.
Testing Requirements
Documentation quality is validated by peer review. Reviewer checklist: (1) Can a new developer follow the assignment flow without asking questions? (2) Are all edge cases listed in section 3.2 of the workshop summary (multi-chapter NHF membership, 1,400-chapter tree depth) covered? (3) Do Mermaid diagrams render correctly in GitHub/GitLab Markdown preview?
(4) Are all API contract fields present and typed? No automated tests required, but the Mermaid syntax should be validated with a Mermaid CLI lint step in CI if available.
Recursive aggregation queries across four hierarchy levels (national → region → local) with 1,400 leaf nodes may be too slow for real-time dashboard requests, exceeding the 200ms target and causing spinner timeouts.
Mitigation & Contingency
Mitigation: Implement aggregation as a Supabase RPC using a single recursive CTE rather than multiple round-trip queries. Pre-compute aggregations nightly via a scheduled Edge Function and cache results. For real-time needs, aggregate only the immediate subtree on demand.
Contingency: Surface a 'Refreshing...' indicator and serve stale cached aggregations immediately. Queue an async recalculation and push updated data via Supabase Realtime when ready, avoiding blocking the admin dashboard.
The 5-chapter limit and primary-assignment constraint are NHF-specific. Applying these rules globally may break HLF and Blindeforbundet configurations where different limits apply, requiring per-organization configuration that was not initially scoped.
Mitigation & Contingency
Mitigation: Make the maximum assignment count a configurable value stored in the organization's feature-flag or settings table rather than a hardcoded constant. Design the assignment service to read this limit at runtime per organization.
Contingency: Default the limit to a high value (e.g., 100) for organizations other than NHF, effectively making it non-restrictive, while keeping the enforcement logic intact for when per-org configuration is fully implemented.
The searchable parent dropdown in HierarchyNodeEditor must search across up to 1,400 units efficiently. Client-side filtering of the full hierarchy may be slow; server-side search adds complexity and latency.
Mitigation & Contingency
Mitigation: Use the in-memory hierarchy cache as the search corpus — since the cache already holds the flat unit list, client-side filtering with a debounced input is sufficient and avoids extra Supabase calls. Pre-build a search index on cache load.
Contingency: Cap the dropdown to showing the 50 most recently accessed units by default, with a 'search all' option that triggers a server-side full-text query. This keeps the common case fast while supporting edge cases.