Organizations Module
Organizations Module
Relevant source files
The following files were used as context for generating this wiki page:
The Organizations Module manages the lifecycle of organizational entities within Primebrick, serving as a synchronized mirror of Casdoor organizations. It implements a Casdoor-first write strategy, ensuring that any changes to organizations are first committed to the Identity Provider (IDP) before being persisted in the local PostgreSQL database.
Architectural Overview
The module is built around the OrganizationEntity, which maps local UUIDs to Casdoor's idp_code (owner/name format). This decoupling allows the application to use immutable internal identifiers while maintaining compatibility with the external IDP.
Data Flow: Dual-Write Synchronization
When an organization is created or updated, the system performs a synchronous call to the Casdoor API. If the IDP operation fails, the local transaction is not committed (or the local write is not initiated), ensuring data consistency between Primebrick and Casdoor.
Organization Identity Mapping
| Field | Type | Role |
|---|---|---|
uuid | string | Internal Primebrick identifier used for all foreign keys and API references src/modules/auth/organization_entity.ts:39-39. |
idp_code | string | Casdoor identifier (e.g., admin/acme). Immutable after creation src/modules/auth/organization_entity.ts:41-44. |
idp_owner | string | The owner prefix in Casdoor (usually the IDP organization name) src/modules/auth/organization_entity.ts:45-46. |
idp_name | string | The specific organization name within the IDP src/modules/auth/organization_entity.ts:48-49. |
Sources: src/modules/auth/organization_entity.ts:1-15, src/modules/auth/organizations_dal.ts:16-32
Code Entity Map: Router to DAL
The following diagram illustrates the relationship between the HTTP layer, the Data Access Layer (DAL), and the underlying Entity definitions.
Diagram: Organizations Module Component Mapping
Code
Sources: src/modules/auth/organizations_router.ts:13-20, src/modules/auth/organizations_dal.ts:63-70
Data Access Layer (DAL)
The OrganizationsDal handles the complexity of joining audit trails and calculating aggregate data like user counts.
Key Methods
listOrganizations(query): Implements a complex pipeline using the Repository DSL. It filters by deletion status (EXCLUDED,ONLY,INCLUDED), performsILIKEsearches ondisplay_nameandidp_code, and executes aLEFT JOINonUserProfileEntityto resolve audit user display names src/modules/auth/organizations_dal.ts:106-180.getUserCountForOrganization(idpCode): Executes a direct count query against theuser_profilestable to determine how many users belong to a specific organization src/modules/auth/organizations_dal.ts:254-263.createOrganization(data): Generates a newuuid, assigns the current session actor tocreated_by, and persists the entity src/modules/auth/organizations_dal.ts:198-220.
Soft-Delete and Restore
The DAL implements soft-deletion by populating the deleted_at and deleted_by fields instead of removing the row.
- Delete:
repo.updatesets the deletion timestamp and actor src/modules/auth/organizations_dal.ts:284-295. - Restore: Sets
deleted_atanddeleted_bytonullsrc/modules/auth/organizations_dal.ts:303-314.
Sources: src/modules/auth/organizations_dal.ts:63-315
Casdoor Integration
The module uses CasdoorApiClient to synchronize changes. The client is initialized JIT (Just-In-Time) using credentials stored in the auth_configurations table src/modules/auth/organizations_router.ts:24-44.
Write Synchronization Logic:
- Validation: Request body is validated via Zod schemas in the router.
- IDP Write: The router calls
getCasdoorClient()and invokes the IDP update/create. - Local Write: If the IDP call succeeds,
OrganizationsDalis called to update the PostgreSQL mirror. - Audit: The
Repositoryautomatically calculates the delta and writes to theorganizations_audittable.
Diagram: Create Organization Sequence
Code
Sources: src/modules/auth/organizations_router.ts:24-44, src/modules/auth/organizations_dal.ts:198-220, src/modules/auth/organization_entity.ts:32-34
Entity Configuration
The OrganizationEntity uses decorators to define its behavior within the Primebrick Domain System.
| Decorator | Purpose |
|---|---|
@Entity("organizations") | Maps the class to the organizations table src/modules/auth/organization_entity.ts:32-32. |
@AuditTrail() | Enables automatic tracking of all changes in a shadow audit table src/modules/auth/organization_entity.ts:33-33. |
@SynchronizableField | Marks last_synced_at for tracking IDP synchronization state src/modules/auth/organization_entity.ts:57-59. |
@AuditableField | Defines standard fields like created_at, updated_by, and version for optimistic concurrency src/modules/auth/organization_entity.ts:62-75. |
@DeletableField | Hooks into the soft-delete system src/modules/auth/organization_entity.ts:77-81. |
Sources: src/modules/auth/organization_entity.ts:17-82