|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { ObjectSchema, Field } from '@objectstack/spec/data'; |
| 4 | + |
| 5 | +/** |
| 6 | + * sys_department — Enterprise Org-Skeleton Node |
| 7 | + * |
| 8 | + * The persistent, hierarchical org chart node. **This is distinct from |
| 9 | + * `sys_team`** (which is the flat better-auth collaboration grouping). |
| 10 | + * |
| 11 | + * A single tenant typically has one `kind='company'` root, then nested |
| 12 | + * `division` / `department` / `team` / `office` nodes underneath. The |
| 13 | + * `kind` enum is purely a display/categorisation hint — the recursive |
| 14 | + * structure works identically regardless of value. |
| 15 | + * |
| 16 | + * Drives: |
| 17 | + * - `recipient_type='department'` sharing rules |
| 18 | + * - `dept:` approver prefix in the approval engine |
| 19 | + * - Report rollups and manager chains in CRM/PM apps |
| 20 | + * |
| 21 | + * @namespace sys |
| 22 | + */ |
| 23 | +export const SysDepartment = ObjectSchema.create({ |
| 24 | + name: 'sys_department', |
| 25 | + label: 'Department', |
| 26 | + pluralLabel: 'Departments', |
| 27 | + icon: 'building', |
| 28 | + isSystem: true, |
| 29 | + managedBy: 'platform', |
| 30 | + description: 'Hierarchical org-skeleton node (department / division / business unit / office).', |
| 31 | + displayNameField: 'name', |
| 32 | + titleFormat: '{name}', |
| 33 | + compactLayout: ['name', 'kind', 'parent_department_id', 'manager_user_id'], |
| 34 | + |
| 35 | + fields: { |
| 36 | + // ── Identity ───────────────────────────────────────────────── |
| 37 | + name: Field.text({ |
| 38 | + label: 'Name', |
| 39 | + required: true, |
| 40 | + searchable: true, |
| 41 | + maxLength: 255, |
| 42 | + group: 'Identity', |
| 43 | + }), |
| 44 | + |
| 45 | + code: Field.text({ |
| 46 | + label: 'Code', |
| 47 | + required: false, |
| 48 | + searchable: true, |
| 49 | + maxLength: 64, |
| 50 | + description: 'Short stable code (e.g. EMEA-SALES). Unique within tenant.', |
| 51 | + group: 'Identity', |
| 52 | + }), |
| 53 | + |
| 54 | + kind: Field.select( |
| 55 | + ['company', 'division', 'department', 'team', 'office', 'cost_center'], |
| 56 | + { |
| 57 | + label: 'Kind', |
| 58 | + required: true, |
| 59 | + defaultValue: 'department', |
| 60 | + description: 'Categorisation hint — does not change graph semantics.', |
| 61 | + group: 'Identity', |
| 62 | + }, |
| 63 | + ), |
| 64 | + |
| 65 | + // ── Hierarchy ──────────────────────────────────────────────── |
| 66 | + parent_department_id: Field.lookup('sys_department', { |
| 67 | + label: 'Parent Department', |
| 68 | + required: false, |
| 69 | + description: 'Self-reference for the org tree. Null = root of tenant.', |
| 70 | + group: 'Hierarchy', |
| 71 | + }), |
| 72 | + |
| 73 | + organization_id: Field.lookup('sys_organization', { |
| 74 | + label: 'Organization', |
| 75 | + required: true, |
| 76 | + description: 'Tenant scope.', |
| 77 | + group: 'Hierarchy', |
| 78 | + }), |
| 79 | + |
| 80 | + // ── Leadership ─────────────────────────────────────────────── |
| 81 | + manager_user_id: Field.lookup('sys_user', { |
| 82 | + label: 'Department Head', |
| 83 | + required: false, |
| 84 | + description: 'User responsible for this org unit (department head / lead).', |
| 85 | + group: 'Leadership', |
| 86 | + }), |
| 87 | + |
| 88 | + // ── Lifecycle ──────────────────────────────────────────────── |
| 89 | + active: Field.boolean({ |
| 90 | + label: 'Active', |
| 91 | + required: false, |
| 92 | + defaultValue: true, |
| 93 | + description: 'When false, members are not expanded by graph queries.', |
| 94 | + group: 'Lifecycle', |
| 95 | + }), |
| 96 | + |
| 97 | + effective_from: Field.datetime({ |
| 98 | + label: 'Effective From', |
| 99 | + required: false, |
| 100 | + description: 'When this department came into existence (HRIS sync).', |
| 101 | + group: 'Lifecycle', |
| 102 | + }), |
| 103 | + |
| 104 | + effective_to: Field.datetime({ |
| 105 | + label: 'Effective To', |
| 106 | + required: false, |
| 107 | + description: 'When this department was retired (HRIS sync).', |
| 108 | + group: 'Lifecycle', |
| 109 | + }), |
| 110 | + |
| 111 | + external_ref: Field.text({ |
| 112 | + label: 'External Reference', |
| 113 | + required: false, |
| 114 | + maxLength: 200, |
| 115 | + description: 'ID in upstream HRIS (Workday / SAP HR / 北森).', |
| 116 | + group: 'Lifecycle', |
| 117 | + }), |
| 118 | + |
| 119 | + // ── System ─────────────────────────────────────────────────── |
| 120 | + id: Field.text({ |
| 121 | + label: 'Department ID', |
| 122 | + required: true, |
| 123 | + readonly: true, |
| 124 | + group: 'System', |
| 125 | + }), |
| 126 | + |
| 127 | + created_at: Field.datetime({ |
| 128 | + label: 'Created At', |
| 129 | + defaultValue: 'NOW()', |
| 130 | + readonly: true, |
| 131 | + group: 'System', |
| 132 | + }), |
| 133 | + |
| 134 | + updated_at: Field.datetime({ |
| 135 | + label: 'Updated At', |
| 136 | + defaultValue: 'NOW()', |
| 137 | + readonly: true, |
| 138 | + group: 'System', |
| 139 | + }), |
| 140 | + }, |
| 141 | + |
| 142 | + indexes: [ |
| 143 | + { fields: ['organization_id'] }, |
| 144 | + { fields: ['parent_department_id'] }, |
| 145 | + { fields: ['code', 'organization_id'], unique: true }, |
| 146 | + { fields: ['active'] }, |
| 147 | + ], |
| 148 | + |
| 149 | + enable: { |
| 150 | + trackHistory: true, |
| 151 | + searchable: true, |
| 152 | + apiEnabled: true, |
| 153 | + apiMethods: ['get', 'list', 'create', 'update', 'delete'], |
| 154 | + trash: true, |
| 155 | + mru: false, |
| 156 | + }, |
| 157 | +}); |
0 commit comments