Skip to main content

Organization

organization is the tenant โ€” a hospital, clinic, or chain. It supports a self-referencing parent tree (organizations can nest), and almost everything else hangs off it: units belong to it, purchase orders are raised in it, budget lines and tenders are scoped to it.

// models/organization.ts (definition, trimmed of the doc comment)
import { coreApp } from "../mod.ts";
import {
optional, type RelationDataType, type RelationSortOrderType, string,
} from "lesan";
import { createUpdateAt } from "@lib";
import { organization_excludes } from "./excludes.ts";

export const organization_pure = {
name: string(),
code: string(),
description: optional(string()),
...createUpdateAt,
};

export const organization_relations = {
parent: {
schemaName: "organization",
type: "single" as RelationDataType,
optional: true,
excludes: organization_excludes,
relatedRelations: {
children: { type: "multiple" as RelationDataType, limit: 50, sort: { field: "_id", order: "desc" as RelationSortOrderType } },
},
},
};

Pure fieldsโ€‹

FieldTypeNotes
namestring()display name
codestring()business code; indexed unique
descriptionoptional(string())
createdAt / updatedAtspread from createUpdateAt

Relationsโ€‹

RelationTargetTypeBack-reference
parentorganization (self)single (optional)organization.children

The self-reference is how you build a tree: a chain โ†’ hospitals โ†’ departments. parent is optional so the root organization has none. A nested organization also inherits all the implicit back-references added by other models (users, units, purchaseOrders, budgetLines, tenders, processes).

Factoryโ€‹

export const organizations = () =>
coreApp.odm.newModel("organization", organization_pure, organization_relations, {
createIndex: {
indexSpec: { code: 1 },
options: { unique: true },
},
});

In the workflowโ€‹

Organizations scope the role-based access (scopeType: "organization"), and are referenced by unit, purchaseOrder, budgetLine, and tender.

Run itโ€‹

curl -X POST http://localhost:1380/lesan \
-H "Content-Type: application/json" \
-H "token: <jwt>" \
-d '{
"service": "main",
"model": "organization",
"act": "getOrganizations",
"details": { "set": { "page": 1 }, "get": { "name": true, "code": true, "children": { "name": true } } }
}'

Errors & fixesโ€‹

ErrorCauseFix
E11000 duplicate key error collection: advancedTutorial.organizationscode already usedpick a different code
organization not foundupdateOrganizationRelations given an unknown _idpass a real _id
note

Runtime On npm/Bun import the framework from @hemedani/lesan; on Deno from jsr:@hemedani/lesan. The repo app itself uses the lesan path alias.