Skip to main content
Version: 0.13.0 (Previous)

🧱 Organization blocks

Organization blocks are public constants, error classes, and reusable organization operations exported through blocks from @nodeblocks/backend-sdk. Functions return a neverthrow Result or Promise<Result<…>>; inspect the result before reading its value.

Inventory

ExportKindInputsResult / behavior / errors
AUDIT_STATUS_WAITING_FOR_REVIEWConstantNoneExact audit-status literal 'waiting_for_review'.
AUDIT_STATUS_REJECTEDConstantNoneExact audit-status literal 'rejected'.
AUDIT_STATUS_APPROVEDConstantNoneExact audit-status literal 'approved'.
ALLOWED_AUDIT_STATUSConstantNoneReadonly list of allowed audit-status literals.
OrganizationBlockErrorError classMessage, optional dataBase Organization BlockError; routes map concrete subclasses.
OrganizationInvalidQueryErrorError classMessage, optional dataInvalid descendant-query parameters; routes map it to 400.
OrganizationDbErrorError classMessage, optional dataOrganization database failure; routes map it to 500.
OrganizationMemberNotFoundErrorError classMessage, optional dataMissing organization member; routes map it to 404.
OrganizationMemberRoleNotFoundErrorError classMessage, optional dataMissing member role; routes map it to 404.
OrganizationBadRequestErrorError classMessage, optional dataMalformed organization request; routes map it to 400.
OrganizationChangeRequestDbErrorError classMessage, optional dataChange-request database failure; routes map it to 500.
OrganizationChangeRequestErrorError classMessage, optional dataChange-request normalization or processing failure.
OrganizationNotFoundErrorError classMessage, optional dataMissing organization; routes map it to 404.
calculateChildAncestorsFunctionParent organizationAncestor ID array for a child organization; never errors.
createOrganizationFunctionCollection, owner ID, organization, optional parent ID and ancestorsCreated organization ID or OrganizationBlockError.
getOrganizationByIdFunctionCollection, organization IDOrganization document or not-found/database error.
findOrganizationsFunctionCollection, filterMatching organizations or database error.
updateOrganizationFunctionCollection, organization ID, update payloadUpdate success or not-found/database error.
deleteOrganizationFunctionCollection, organization IDDeletion success or not-found/database error.
calculateDirectMemberRoleFunctionMembers array, identity IDDirect member role or member/role not-found error.
checkOrganizationMemberExistenceFunctionMembers array, identity IDMembership boolean or processing error.
findOrganizationMembersFunctionCollection, organization IDMembers with pagination metadata or not-found/database error.
upsertOrganizationMembersFunctionCollection, existing members, organization ID, members to upsertUpsert success or not-found/database error.
deleteOrganizationMemberFunctionCollection, organization ID, identity IDRemoval success or not-found/database error.
findOrganizationsForIdentityFunctionCollection, identity ID, optional roles filterOrganizations for the identity or database error.
buildDescendantsQueryFunctionOrganization, optional depthDescendant MongoDB filter or OrganizationInvalidQueryError.
normalizeOrganizationFunctionOrganization documentOrganization without MongoDB _id; never errors.
normalizeOrganizationsFunctionOrganization arrayNormalized organizations without _id; never errors.
buildOrganizationsForMemberByRoleQueryFunctionIdentity ID, optional rolesMongoDB membership filter; never errors.
buildOrganizationsForMemberByIdsQueryFunctionIdentity ID, organization IDsMongoDB membership-by-ID filter; never errors.
buildOrganizationsWithDescendantsQueryFunctionOrganization arrayMongoDB descendant/ancestor filter; never errors.
normalizeLogoOfOwnerFunctionFile-storage driver, owner with logoOwner with signed logo URL or OrganizationBlockError.
normalizeCertificateImageFunctionFile-storage driver, organizationOrganization with certificate image URL or error.
normalizeLogosOfOwnersFunctionFile-storage driver, ownersOwners with signed logo URLs or batch error.
normalizeCertificateImagesFunctionFile-storage driver, organizationsOrganizations with certificate URLs or batch error.
normalizeLogosOfPaginatedOwnersFunctionFile-storage driver, paginated ownersPaginated owners with signed logo URLs or batch error.
deleteLogoOfOwnerFunctionFile-storage driver, ownertrue or FileStorageServiceError.
extractAncestorsFunctionOrganization arrayUnique ancestor organization IDs; never errors.
calculateInheritedMemberRoleFunctionAncestor organizations, identity IDHighest inherited role or OrganizationMemberRoleNotFoundError.
calculateMemberRoleFunctionOrganization, ancestor organizations, identity IDDirect or inherited role or role-not-found error.
calculateMemberRoleForOrganizationsFunctionOrganizations, ancestor organizations, identity IDPer-organization member role info or role-not-found error.
normalizeOrganizationsForMemberFunctionOrganizations with member infoAPI-shaped member/organization pairs; never errors.
validateOrganizationNameFunctionCollection, organization, organization ID, optional nametrue, duplicate-name error, or database error.
createChangeRequestFunctionChange-requests collection, organization ID, identity ID, payloadCreated change-request ID or database error.
buildUpdateOrganizationAfterNewChangeRequestPayloadFunctionNoneAudit-status update payload; never errors.
validateAuditStatusFunctionOptional audit statustrue or OrganizationBadRequestError.
findChangeRequestsFunctionChange-requests collection, organization ID, optional filterMatching change requests or database error.
normalizeChangeRequestFunctionFile-storage driver, raw change requestNormalized change request with certificate URL or error.
normalizeChangeRequestsFunctionFile-storage driver, change requestsNormalized change requests or batch error.
buildOrganizationIdFilterFunctionOrganization IDFilter object with organizationId; never errors.
buildOrganizationFollowersByFollowOrganizationIdQueryFunctionFollowed organization IDMongoDB $elemMatch filter for organization follows; never errors.

Details

AUDIT_STATUS_WAITING_FOR_REVIEW

Implementation

Exact audit-status literal used by Organization audit workflows.

Used by Custom composition only.

AUDIT_STATUS_REJECTED

Implementation

Exact audit-status literal used by Organization audit workflows.

Used by Custom composition only.

AUDIT_STATUS_APPROVED

Implementation

Exact audit-status literal used by Organization audit workflows.

Used by Custom composition only.

ALLOWED_AUDIT_STATUS

Implementation

Exact readonly list: 'waiting_for_review', 'rejected', and 'approved'.

Used by Custom composition only.

OrganizationBlockError

Implementation

OrganizationBlockError extends BlockError. It carries the block failure; the consuming route maps it to an HTTP response only where its source pipeline explicitly does so.

Used by Custom composition only.

OrganizationInvalidQueryError

Implementation

OrganizationInvalidQueryError extends OrganizationBlockError. It carries the block failure; the consuming route maps it to an HTTP response only where its source pipeline explicitly does so.

Used by findOrganizationDescendantsRoute.

OrganizationDbError

Implementation

OrganizationDbError extends OrganizationBlockError. It carries the block failure; the consuming route maps it to an HTTP response only where its source pipeline explicitly does so.

Used by getOrganizationMemberRoleRoute, findOrganizationsForMemberRoute, findOrganizationDescendantsRoute, createChangeRequestRoute, getOrganizationFollowersRoute.

OrganizationMemberNotFoundError

Implementation

OrganizationMemberNotFoundError extends OrganizationBlockError. It carries the block failure; the consuming route maps it to an HTTP response only where its source pipeline explicitly does so.

Used by getOrganizationMemberRoleRoute, findOrganizationsForMemberRoute.

OrganizationMemberRoleNotFoundError

Implementation

OrganizationMemberRoleNotFoundError extends OrganizationBlockError. It carries the block failure; the consuming route maps it to an HTTP response only where its source pipeline explicitly does so.

Used by getOrganizationMemberRoleRoute, findOrganizationsForMemberRoute.

OrganizationBadRequestError

Implementation

OrganizationBadRequestError extends OrganizationBlockError. It carries the block failure; the consuming route maps it to an HTTP response only where its source pipeline explicitly does so.

Used by createChangeRequestRoute.

OrganizationChangeRequestDbError

Implementation

OrganizationChangeRequestDbError extends OrganizationBlockError. It carries the block failure; the consuming route maps it to an HTTP response only where its source pipeline explicitly does so.

Used by createChangeRequestRoute, findChangeRequestsForOrganizationRoute.

OrganizationChangeRequestError

Implementation

OrganizationChangeRequestError extends OrganizationBlockError. It carries the block failure; the consuming route maps it to an HTTP response only where its source pipeline explicitly does so.

Used by findChangeRequestsForOrganizationRoute.

OrganizationNotFoundError

Implementation

OrganizationNotFoundError extends OrganizationBlockError. It carries the block failure; the consuming route maps it to an HTTP response only where its source pipeline explicitly does so.

Used by getOrganizationFollowersRoute.

calculateChildAncestors

Implementation

Signature: export async function calculateChildAncestors( parentOrganization: Record<string, unknown> ): Promise<Result<string[], never>>

Calculates ancestors array for a child organization based on parent organization.

Used by createOrganizationRoute.

createOrganization

Implementation

Signature: export const createOrganization = async ( organizationsCollection: OrganizationServiceDataStore['organizations'], ownerId: string, organization: Record<string, unknown>, parentId?: string, ancestors: string[] = [] ): Promise<Result<string, OrganizationBlockError>> =>

Creates a new organization in the database with the specified owner.

Used by createOrganizationRoute.

getOrganizationById

Implementation

Signature: export const getOrganizationById = async ( organizationsCollection: OrganizationServiceDataStore['organizations'], organizationId: string ): Promise<Result<WithId<Document>, Error>> =>

Retrieves an organization by its ID from the database.

Used by createOrganizationRoute, getOrganizationRoute, updateOrganizationRoute, updateOrganizationAsAdminRoute, deleteOrganizationRoute, getOrganizationMemberRoleRoute, checkOrganizationMemberExistenceRoute, upsertOrganizationMembersRoute, findOrganizationDescendantsRoute, createChangeRequestRoute, getOrganizationFollowersRoute.

findOrganizations

Implementation

Signature: export const findOrganizations = async ( organizationsCollection: OrganizationServiceDataStore['organizations'], filter: Record<string, unknown> ): Promise<Result<Record<string, unknown>[], Error>> =>

Finds organizations in the database based on the provided filter criteria.

Used by findOrganizationsRoute, getOrganizationMemberRoleRoute, findOrganizationsForMemberRoute, findOrganizationDescendantsRoute.

updateOrganization

Implementation

Signature: export const updateOrganization = async ( organizationsCollection: OrganizationServiceDataStore['organizations'], organizationId: string, organization: Record<string, unknown> ): Promise<Result<boolean, Error>> =>

Updates an organization in the database with the provided data.

Used by updateOrganizationRoute, updateOrganizationAsAdminRoute, createChangeRequestRoute.

deleteOrganization

Implementation

Signature: export const deleteOrganization = async ( organizationsCollection: OrganizationServiceDataStore['organizations'], organizationId: string ): Promise<Result<boolean, Error>> =>

Deletes an organization from the database by its ID.

Used by deleteOrganizationRoute.

calculateDirectMemberRole

Implementation

Signature: export function calculateDirectMemberRole( members: OrganizationMember[], identityId: string ): Result< string, OrganizationMemberNotFoundError | OrganizationMemberRoleNotFoundError >

Calculates the direct role of a member within an organization.

Used by Custom composition only.

checkOrganizationMemberExistence

Implementation

Signature: export const checkOrganizationMemberExistence = async ( members: OrganizationMember[], identityId: string ): Promise<Result<boolean, Error>> =>

Checks if a specific identity is a member of an organization.

Used by checkOrganizationMemberExistenceRoute.

findOrganizationMembers

Implementation

Signature: export const findOrganizationMembers = async ( organizationsCollection: OrganizationServiceDataStore['organizations'], organizationId: string ): Promise<Result<OrganizationMember[], Error>> =>

Retrieves all members of an organization with pagination metadata.

Used by findOrganizationMembersRoute.

upsertOrganizationMembers

Implementation

Signature: export const upsertOrganizationMembers = async ( organizationsCollection: OrganizationServiceDataStore['organizations'], existingMembers: OrganizationMember[] = [], organizationId: string, membersToUpsert: OrganizationMember[] ): Promise<Result<boolean, Error>> =>

Updates or inserts organization members by merging with existing members.

Used by upsertOrganizationMembersRoute.

deleteOrganizationMember

Implementation

Signature: export const deleteOrganizationMember = async ( organizationsCollection: OrganizationServiceDataStore['organizations'], organizationId: string, identityId: string ): Promise<Result<boolean, Error>> =>

Removes a specific member from an organization using MongoDB $pull operation.

Used by deleteOrganizationMemberRoute.

findOrganizationsForIdentity

Implementation

Signature: export const findOrganizationsForIdentity = async ( organizationsCollection: OrganizationServiceDataStore['organizations'], identityId: string, roles?: string ): Promise< Result<

Finds all organizations where a specific identity is a member, optionally filtered by roles.

Used by Custom composition only.

buildDescendantsQuery

Implementation

Signature: export function buildDescendantsQuery( organization: Record<string, unknown>, depth?: string | number ): Result<Record<string, unknown>, OrganizationInvalidQueryError>

Builds MongoDB query for finding descendant organizations in hierarchy.

Used by findOrganizationDescendantsRoute.

normalizeOrganization

Implementation

Signature: export function normalizeOrganization( organization: Record<string, unknown> ): Result<Record<string, unknown>, never>

Removes MongoDB _id field from organization data.

Used by Custom composition only.

normalizeOrganizations

Implementation

Signature: export function normalizeOrganizations( organizations: Record<string, unknown>[] ): Result<Record<string, unknown>[], never>

Normalizes array of organizations by removing _id fields from each.

Used by findOrganizationDescendantsRoute.

buildOrganizationsForMemberByRoleQuery

Implementation

Signature: export function buildOrganizationsForMemberByRoleQuery( identityId: string, roles?: string ): Result<Record<string, unknown>, never>

Builds a MongoDB query to find organizations where a specific identity is a member, optionally filtered by roles.

Used by findOrganizationsForMemberRoute.

buildOrganizationsForMemberByIdsQuery

Implementation

Signature: export function buildOrganizationsForMemberByIdsQuery( identityId: string, organizationIds: string[] ): Result<Record<string, unknown>, never>

Builds a MongoDB query to find organizations by IDs where a specific identity is a member.

Used by getOrganizationMemberRoleRoute, findOrganizationsForMemberRoute.

buildOrganizationsWithDescendantsQuery

Implementation

Signature: export function buildOrganizationsWithDescendantsQuery( organizations: Record<string, unknown>[] ): Result<Record<string, unknown>, never>

Builds a MongoDB query to find organizations by their own IDs or by ancestor IDs.

Used by findOrganizationsForMemberRoute.

normalizeLogoOfOwner

Implementation

Signature: export async function normalizeLogoOfOwner< T extends

Normalizes owner logo by converting objectId to signed download URL.

Used by createOrganizationRoute, getOrganizationRoute, updateOrganizationRoute, updateOrganizationAsAdminRoute.

normalizeCertificateImage

Implementation

Signature: export async function normalizeCertificateImage( fileStorageDriver: FileStorageDriver, organization: Record<string, unknown> &

Normalizes organization certificate image by generating signed download URL.

Used by getOrganizationRoute, updateOrganizationRoute, updateOrganizationAsAdminRoute.

normalizeLogosOfOwners

Implementation

Signature: export async function normalizeLogosOfOwners< T extends

Normalizes logos for multiple owners by converting objectIds to signed URLs.

Used by findOrganizationsRoute, findOrganizationsForMemberRoute.

normalizeCertificateImages

Implementation

Signature: export async function normalizeCertificateImages( fileStorageDriver: FileStorageDriver, organizations: (Record<string, unknown> &

Normalizes certificate image URLs for multiple organizations using file storage driver.

Used by findOrganizationsRoute, findOrganizationsForMemberRoute.

normalizeLogosOfPaginatedOwners

Implementation

Signature: export async function normalizeLogosOfPaginatedOwners< T extends

Normalizes logos for paginated owners by converting objectIds to signed URLs.

Used by Custom composition only.

deleteLogoOfOwner

Implementation

Signature: export async function deleteLogoOfOwner< T extends

Deletes logo file from file storage for organization owner.

Used by deleteOrganizationRoute.

extractAncestors

Implementation

Signature: export function extractAncestors( organizations: Record<string, unknown>[] ): Result<string[], never>

Extracts all unique ancestor organization IDs from a list of organizations.

Used by findOrganizationsForMemberRoute.

calculateInheritedMemberRole

Implementation

Signature: export function calculateInheritedMemberRole( ancestorOrganizations: Record<string, unknown>[], identityId: string ): Result<

Calculates the highest inherited member role for an identity from a set of ancestor organizations.

Used by Custom composition only.

calculateMemberRole

Implementation

Signature: export function calculateMemberRole( organization: Record<string, unknown>, ancestorOrganizations: Record<string, unknown>[], identityId: string ): Result<

Calculates the member role (direct or inherited) for a single organization and identity.

Used by getOrganizationMemberRoleRoute.

calculateMemberRoleForOrganizations

Implementation

Signature: export async function calculateMemberRoleForOrganizations( organizations: Record<string, unknown>[], ancestorOrganizations: Record<string, unknown>[], identityId: string ): Promise< Result<

Calculates member role information (direct and inherited) for each organization in a list.

Used by findOrganizationsForMemberRoute.

normalizeOrganizationsForMember

Implementation

Signature: export function normalizeOrganizationsForMember( organizationsWithMemberInfo: Record<string, unknown>[] ): Result<Record<string, unknown>[], never>

Normalizes organizations for member response by structuring the data according to the API specification.

Used by findOrganizationsForMemberRoute.

validateOrganizationName

Implementation

Signature: export async function validateOrganizationName( organizationsCollection: OrganizationServiceDataStore['organizations'], organization:

Organization name uniqueness validation against organization owners.

Used by createChangeRequestRoute.

createChangeRequest

Implementation

Signature: export const createChangeRequest = async ( organizationChangeRequestsCollection: Collection, organizationId: string, identityId: string, changeRequest: Record<string, string> &

Organization change request creation in database.

Used by createChangeRequestRoute.

buildUpdateOrganizationAfterNewChangeRequestPayload

Implementation

Signature: export function buildUpdateOrganizationAfterNewChangeRequestPayload(): Result<

Organization audit status update payload after new change request.

Used by createChangeRequestRoute.

validateAuditStatus

Implementation

Signature: export const validateAuditStatus = ( auditStatus?: string ): Result<boolean, OrganizationBadRequestError> =>

Organization audit status validation for change request workflows.

Used by updateOrganizationAsAdminRoute.

findChangeRequests

Implementation

Signature: export async function findChangeRequests( organizationChangeRequestsCollection: Collection, organizationId: string ): Promise< Result<Record<string, unknown>[], OrganizationChangeRequestDbError> >

Organization change requests retrieval for an organization with optional filters.

Used by findChangeRequestsForOrganizationRoute.

normalizeChangeRequest

Implementation

Signature: export async function normalizeChangeRequest( fileStorageDriver: FileStorageDriver, rawChangeRequest: Record<string, unknown> &

Normalizes organization change request data with certificate image URL generation.

Used by Custom composition only.

normalizeChangeRequests

Implementation

Signature: export async function normalizeChangeRequests( fileStorageDriver: FileStorageDriver, changeRequests: Record<string, unknown>[] ): Promise<Result<Record<string, unknown>[], OrganizationChangeRequestError>>

Normalizes multiple organization change requests with file storage URL generation.

Used by findChangeRequestsForOrganizationRoute.

buildOrganizationIdFilter

Implementation

Signature: export function buildOrganizationIdFilter( organizationId: string ): Result<

Builds organization ID filter object for database queries.

Used by Custom composition only.

buildOrganizationFollowersByFollowOrganizationIdQuery

Implementation

Signature: export function buildOrganizationFollowersByFollowOrganizationIdQuery< T extends

Builds MongoDB query filter to find organizations that follow a specific organization.

Used by getOrganizationFollowersRoute.