Skip to main content
Version: 0.15.0 (Latest)

🏢 Organization

Organization provides an SDK service for managing organizations, memberships, hierarchy, change requests, file uploads, and followers through source-composed HTTP features.

Start here​

Mount organizationService with the required identities, organizations, and profiles collections. Use express.json() for JSON request bodies; when authMode is 'cookie', register cookie-parser before mounting the service.

import express from 'express';
import cookieParser from 'cookie-parser';
import {services} from '@nodeblocks/backend-sdk';

const app = express();
app.use(express.json());
app.use(cookieParser()); // Required only for authMode: 'cookie'.
app.use('/api', services.organizationService(
{identities, organizations, profiles},
{
authSecrets: {
authEncSecret: process.env.AUTH_ENC_SECRET!,
authSignSecret: process.env.AUTH_SIGN_SECRET!,
},
authMode: 'bearer',
identity: {typeIds: {admin: 'identity-type-admin', guest: 'identity-type-guest', regular: 'identity-type-regular'}},
organization: {roles: {owner: 'owner', admin: 'admin', member: 'member'}},
},
{fileStorageDriver},
));
ConfigurationDefault / source behaviorEffect
authSecrets.authEncSecret / authSecrets.authSignSecretRequired; no runtime defaultService authentication configuration.
authModeOmitted → 'bearer'Chooses Bearer-header or cookie authentication.
identity.typeIdsPassed unchanged to validators when suppliedEnables identity-type checks when configured routes invoke them.
organization.rolesPassed unchanged to validators when suppliedEnables role-based Organization checks when configured routes invoke them.

Common tasks​

TaskStart withContract
Create or retrieve an organizationcreateOrganizationFeaturecreateOrganizationFeature and getOrganizationRoute
List organizations or membersfindOrganizationsRoutefindOrganizationsRoute and findOrganizationMembersRoute
Maintain members safelyupsertOrganizationMembersRouteupsertOrganizationMembersRoute and Organization validators
Work with a hierarchyfindOrganizationDescendantsRoutefindOrganizationDescendantsRoute and findOrganizationsForMemberRoute
Submit or review a change requestcreateChangeRequestRoutecreateChangeRequestRoute and findChangeRequestsForOrganizationRoute
Generate an upload URLgetLogoUploadUrlRoutegetLogoUploadUrlRoute and getCertificateUploadUrlRoute

Bearer HTTP workflow​

With an authenticated administrator Bearer token, create an organization through createOrganizationRoute. This fragment requires the service mounted above and a valid administrator identity.

export API_BASE_URL='https://api.example.test/api'
export ACCESS_TOKEN='replace-with-a-valid-access-token'
curl --request POST "$API_BASE_URL/organizations" \
--header "Authorization: Bearer $ACCESS_TOKEN" \
--header 'Content-Type: application/json' \
--data '{"ownerId":"identity-owner-001","organization":{"name":"Example organization","description":"Example description","contact_email":"org@example.test"}}'

The route returns the normalized organization on success; authentication or administrator authorization failure is rejected before its pipeline. See the creation schema.

Set authMode: 'cookie' and register cookie-parser; protected routes then read the access token through the configured cookie transport. For example, a member may request an organization using the cookie supplied by the host application:

export API_BASE_URL='https://api.example.test/api'
export ACCESS_COOKIE='accessToken=replace-with-a-valid-access-token'
curl "$API_BASE_URL/organizations/org-001" --header "Cookie: $ACCESS_COOKIE"

The route's access rule is documented with getOrganizationRoute; its path contract is getOrganizationSchema.

Custom feature composition​

Features are SDK composers, not Express middleware. A host may compose a single public feature into a service router when it supplies the same runtime context required by the route:

import {partial} from 'ramda';
import {features, primitives, utils} from '@nodeblocks/backend-sdk';

app.use('/api', primitives.defService(partial(features.getOrganizationFeatures, [{
authenticate: utils.getBearerTokenInfo,
configuration,
dataStores: {identities, organizations, profiles},
fileStorageDriver,
}])));

This fragment requires the host's auth adapter and the collections used by the route. Prefer organizationService when all Organization endpoints are needed.

Reference map​

PagePurpose
BlocksReusable organization operations, constants, and errors.
FeaturesSchema-to-route SDK compositions.
HandlersPipeline terminators.
RoutesEndpoint and response contracts.
SchemasField-level validation contracts.
ValidatorsAuthentication, role, rank, and ownership guards.

Organization service is the service-level reference. Authentication utilities and cookie utilities provide selected token transport. File-storage blocks supply delegated upload/download operations, while Mongo blocks provide paginated database helpers used by Organization routes.