Service Layer medium complexity Shared Component backend
2
Dependencies
3
Dependents
2
Entities
0
Integrations

Description

Manages the junction table that links users to one or more organization units. Enforces the is_primary constraint (only one primary per user per organization), validates assignment limits for multi-chapter organisations, and integrates with the active-chapter state for session context.

Feature: Organizational Hierarchy & Structure Management

unit-assignment-service

Summaries

The Unit Assignment Service governs how individual users are connected to organizational units — whether a coordinator belongs to a single chapter or spans multiple chapters in a national federation structure. By enforcing the single-primary-unit rule and the five-chapter limit for multi-chapter organizations like NHF, this component ensures that access control, reporting attribution, and organizational governance remain accurate and auditable. Without this enforcement, coordinators could be misattributed in compliance reports, have unintended access to data across chapters, or create ambiguous session contexts. This directly reduces compliance risk and administrative error, protecting the organization's reporting integrity with Bufdir and similar oversight bodies.

Unit Assignment Service is a medium-complexity shared backend service that depends on both unit-assignment-repository and hierarchy-service. The dependency on hierarchy-service means it cannot be fully tested in isolation until hierarchy management is stable. Key delivery risks include the multi-chapter limit validation (max 5 for NHF), which requires clear product sign-off on the exact constraint rules before implementation to avoid rework. The is_primary enforcement across assignments requires careful transaction handling to prevent race conditions when two concurrent requests attempt to set a new primary unit.

Plan integration testing for all assignment lifecycle scenarios: initial assignment, primary update, secondary additions, and removal. Because this service is shared across features that rely on session context, changes to its interface carry broad impact — scope a regression pass across all dependent features when modifying it.

UnitAssignmentService manages the unit_assignment junction table, linking users to one or more organization_units. Its interface covers assignUserToUnit(userId, unitId, isPrimary), removeAssignment(), setPrimaryUnit(), getAssignmentsForUser(userId, organizationId), getAssignmentsForUnit(unitId), and getUserCountForUnit(unitId). The is_primary constraint must be enforced transactionally — when setting a new primary, wrap the unset of any existing primary and the set of the new one in a single atomic operation to prevent orphaned or duplicate primaries. The five-unit limit for multi-chapter organizations (e.g., NHF) should be validated against a count query before inserting a new assignment, with organization-type awareness sourced from a config or the organization record.

It depends on hierarchy-service for validating that the target unit exists within the correct organization scope. getUserCountForUnit() is used by the admin portal for member count displays — consider caching or materializing this count for large units to avoid expensive joins on each render.

Responsibilities

  • Assign user to an organization unit
  • Remove or update existing assignments
  • Enforce single primary assignment per organization
  • Validate multi-chapter limit (max 5 for NHF)

Interfaces

assignUserToUnit(userId, unitId, isPrimary)
removeAssignment(userId, unitId)
setPrimaryUnit(userId, unitId)
getAssignmentsForUser(userId, organizationId)
getAssignmentsForUnit(unitId)
getUserCountForUnit(unitId)

Relationships

Dependencies (2)

Components this component depends on

Dependents (3)

Components that depend on this component

Related Data Entities (2)

Data entities managed by this component

API Contract

View full contract →
REST /api/v1/unit-assignments 6 endpoints
GET /api/v1/unit-assignments List assignments filtered by user or unit
GET /api/v1/unit-assignments/{assignmentId} Get a single assignment record
POST /api/v1/unit-assignments Assign a user to an organizational unit
PUT /api/v1/unit-assignments/{assignmentId} Update assignment (e.g. set as primary)
DELETE /api/v1/unit-assignments/{assignmentId} Remove a user from an organizational unit
PUT /api/v1/unit-assignments/users/{userId}/primary Set primary unit for a user within an organization