Hierarchy Service
Component Detail
Description
Core business logic service for managing organization unit trees. Wraps Supabase queries for the recursive adjacency-list table, enforces business rules such as depth limits and cycle prevention, and emits hierarchy-changed events consumed by downstream services.
hierarchy-service
Summaries
The Hierarchy Service is the foundational engine that makes multi-level organizational structures — national bodies, regions, districts, and local chapters — a reliable, governed feature of the platform. By enforcing business rules such as depth limits and cycle prevention at the service layer, it protects data integrity automatically, preventing structural errors that could corrupt reporting, access control, and compliance outputs. As a shared component consumed across the entire application, it ensures consistent organizational logic everywhere data is accessed. This investment pays dividends across every feature that depends on organizational context: membership management, activity reporting, compliance submissions, and coordinator access control.
Its reliability directly underpins the platform's suitability for regulated, multi-chapter organizations.
Hierarchy Service is a high-complexity, shared backend service with a broad interface surface and deep impact across the application. Because it is shared across multiple features, it sits on the critical path for nearly every team working on organizational functionality. Its dependency on organization-unit-repository means database schema must be finalized before full implementation can proceed. Cycle detection, ancestor/descendant path computation, and hierarchy-changed event emission are non-trivial algorithms that require careful unit testing and integration validation.
Plan for dedicated QA cycles covering edge cases: deep nesting, large sibling counts, concurrent mutations, and move operations that could temporarily violate constraints. Any breaking changes to its interface will cascade across all consumers, so API versioning discipline is essential from the outset.
HierarchyService wraps Supabase queries for a recursive adjacency-list table (organization_units) and enforces domain invariants before and after persistence. The interface includes getHierarchy(organizationId), createUnit(), updateUnit(), deleteUnit(), getAncestors(unitId), getDescendants(unitId), findUnitsByType(), and moveUnit(unitId, newParentId). Ancestor and descendant traversal should use recursive CTEs (WITH RECURSIVE) at the Supabase/PostgreSQL layer for efficiency rather than application-level tree walking. Cycle detection in moveUnit() must validate that the target newParentId is not a descendant of the unit being moved — a graph reachability check.
Depth limits should be configurable per organization type. The hierarchy-changed event emission pattern decouples this service from downstream consumers (cache invalidation, aggregation triggers) and should use a domain event bus or Supabase database triggers for consistency. This is a shared service — maintain strict backward compatibility on all public interface methods.
Responsibilities
- CRUD operations on organization_units table
- Detect and prevent circular parent references
- Compute full ancestor and descendant paths for any node
- Emit hierarchy-changed events for cache invalidation
Interfaces
getHierarchy(organizationId)
createUnit(organizationId, name, levelType, parentId?)
updateUnit(unitId, patch)
deleteUnit(unitId)
getAncestors(unitId)
getDescendants(unitId)
findUnitsByType(organizationId, levelType)
moveUnit(unitId, newParentId)
Relationships
Dependents (5)
Components that depend on this component
Related Data Entities (4)
Data entities managed by this component