diff --git a/.changeset/25280-delegation-own-organization.md b/.changeset/25280-delegation-own-organization.md new file mode 100644 index 00000000000..880de498508 --- /dev/null +++ b/.changeset/25280-delegation-own-organization.md @@ -0,0 +1,21 @@ +--- +'@objectstack/plugin-approvals': patch +--- + +fix(plugin-approvals): an out-of-office delegation applies only in its own organization, and an organization-less delegation reroutes no approval + +Clause-②: no (narrowing) +Metadata-protocol: none + + + +**BREAKING** for a stored `sys_approval_delegation` row that carries no `organization_id` (`NULL`, absent or blank). No write produces such a row any more under a walled posture, so only a database upgraded from before 18.0 can hold one. + +- **Which delegation reroutes an approver.** FROM: an organization-less delegation applied in every organization ("null = applies across tenants for this delegator"), and a request with no organization took every active delegation of its approver, whatever organization it belonged to. TO: a delegation reroutes an approver only when its organization is the request's organization, the rule a stored position or permission-set grant follows. An organization-less delegation reroutes nothing anywhere, and a request with no organization takes no delegation. +- **Why.** Under `group` or `isolated`, an organization-less delegation rerouted every organization's approvals for its delegator, onto a delegate who may hold no membership in that organization. ADR-0131 D1: nothing reads a NULL tenant value as an owner. +- **Under `single`** a delegation written on 18.x applies as before: the engine stamps the Default Organization on a delegation its author writes and on the record a request is about, and the request takes its record's organization. Only an organization-less row left from before the ownership ceremony stops applying. +- **Where such a row comes from.** Under a wall a member's insert is stamped with the active organization, an elevated insert with none is refused, and an update that empties the column is refused. A row stored before that, on a database upgraded from 17.x or an 18.0 pre-release, can still be organization-less: `os migrate organization-ownership` attributes it to the Default Organization under `single`, and under a wall, where the row names nothing to attribute it by, reports it and leaves it NULL. +- **It is not silent.** Each time a lookup skips an active organization-less delegation, the approval service logs a `warn` naming the delegation, its delegator and delegate, the request's organization, and the remedy. +- **Setup.** The delegation form's Organization help says what the code does, in every shipped locale. + +**What an operator does.** On a `single` deployment, run `os migrate organization-ownership --apply` if it has not run yet; it attributes these rows to the Default Organization. On a walled deployment, ask the delegator to create the delegation again while signed in to the organization whose approvals it should cover, then delete the organization-less row the warning names. diff --git a/content/docs/automation/approvals.mdx b/content/docs/automation/approvals.mdx index be13f9ef400..d0635e052cc 100644 --- a/content/docs/automation/approvals.mdx +++ b/content/docs/automation/approvals.mdx @@ -900,6 +900,13 @@ background job — so a lapsed delegation simply stops applying. Chains identity**; the reroute is recorded as an `ooo_substitute` audit action and both the delegate and the skipped approver are notified. +A delegation belongs to the organization it is created in, and reroutes only the +approvals opened in that organization; a request's organization is its record's. +To cover approvals in two organizations, create a delegation in each. A +delegation that names no organization, which only a database upgraded from +before 18.0 can hold, reroutes nothing, and the approval service logs a warning +naming it (ADR-0131). + **Group-routed approvers are not rerouted.** `type: position` / `team` / `department` / `org_membership_level` keep their whole membership, so there is diff --git a/packages/plugins/plugin-approvals/src/admin-exemption-retired.test.ts b/packages/plugins/plugin-approvals/src/admin-exemption-retired.test.ts index ed1ad4926be..b5822cc94b9 100644 --- a/packages/plugins/plugin-approvals/src/admin-exemption-retired.test.ts +++ b/packages/plugins/plugin-approvals/src/admin-exemption-retired.test.ts @@ -202,11 +202,14 @@ describe('#4839 evidence — a delegation cannot rescue an IN-FLIGHT approval', svc = new ApprovalService({ engine: engine as any, clock: { now: () => new Date(baseTime + (n++) * 1000) } }); }); - /** Declare an out-of-office delegation directly (bypassing the write guard). */ + /** + * Declare an out-of-office delegation directly (bypassing the write guard), + * in the submitter's organization: a delegation applies only there (ADR-0131). + */ const seedDelegation = (delegatorId: string, delegateId: string) => { engine._tables['sys_approval_delegation'] = [{ id: 'del1', delegator_id: delegatorId, delegate_id: delegateId, - valid_from: null, valid_until: null, reason: 'sick leave', organization_id: null, + valid_from: null, valid_until: null, reason: 'sick leave', organization_id: 't1', }]; }; diff --git a/packages/plugins/plugin-approvals/src/admin-rescue-named-slate.integration.test.ts b/packages/plugins/plugin-approvals/src/admin-rescue-named-slate.integration.test.ts index 9251906a46c..3a343049ca0 100644 --- a/packages/plugins/plugin-approvals/src/admin-rescue-named-slate.integration.test.ts +++ b/packages/plugins/plugin-approvals/src/admin-rescue-named-slate.integration.test.ts @@ -188,7 +188,7 @@ describe('an empty admin_rescue slate opens on the organization\'s administrator await engine.syncSchemas(); // The organization-less rows below exist only on a store built before 18.x // (ADR-0131 D1 makes a fresh tenant column NOT NULL): this is that store. - await relaxTenantColumns(engine, ['sys_user', 'sys_user_permission_set', 'crm_deal', 'sys_approval_delegation']); + await relaxTenantColumns(engine, ['sys_user', 'sys_user_permission_set', 'crm_deal']); bindSecurityCatalogReader(engine, catalogServing([ 'organization_admin', 'organization_admin_no_bypass', 'admin_full_access', ])); @@ -292,7 +292,7 @@ describe('an empty admin_rescue slate opens on the organization\'s administrator // it writes stays, and no case after it reads that slate. it('an administrator out of office is covered by their delegate, as any named approver is', async () => { await engine.insert('sys_approval_delegation', { - id: 'dlg_admin', delegator_id: 'u_admin', delegate_id: 'u_cover', reason: 'leave', + id: 'dlg_admin', delegator_id: 'u_admin', delegate_id: 'u_cover', reason: 'leave', organization_id: ORG, valid_from: '2026-09-01T00:00:00.000Z', valid_until: '2026-12-31T00:00:00.000Z', created_at: '2026-09-01T00:00:00.000Z', updated_at: '2026-09-01T00:00:00.000Z', }, { context: SYSTEM } as any); diff --git a/packages/plugins/plugin-approvals/src/approval-notification-body.integration.test.ts b/packages/plugins/plugin-approvals/src/approval-notification-body.integration.test.ts index 487abffe68f..d93bd7cab4b 100644 --- a/packages/plugins/plugin-approvals/src/approval-notification-body.integration.test.ts +++ b/packages/plugins/plugin-approvals/src/approval-notification-body.integration.test.ts @@ -216,7 +216,7 @@ describe('every approval notification carries its text in body', () => { await engine.syncSchemas(); // The organization-less rows below exist only on a store built before 18.x // (ADR-0131 D1 makes a fresh tenant column NOT NULL): this is that store. - await relaxTenantColumns(engine, ['sys_user', 'sys_approval_delegation']); + await relaxTenantColumns(engine, ['sys_user']); for (const u of USERS) await engine.insert('sys_user', u, { context: SYSTEM } as any); svc = new ApprovalService({ @@ -263,8 +263,9 @@ describe('every approval notification carries its text in body', () => { await svc.remind(ids.bare, { actorId: 'u_sub' }, asUser('u_sub')); // ── Out of office: the routing substitutes a delegate ── + // A delegation applies only in its own organization (ADR-0131): the deal's. await engine.insert('sys_approval_delegation', { - id: 'dlg_1', delegator_id: 'u_ooo', delegate_id: 'u_bob', reason: 'leave', + id: 'dlg_1', delegator_id: 'u_ooo', delegate_id: 'u_bob', reason: 'leave', organization_id: orgOfDeal('ooo'), valid_from: '2026-09-01T00:00:00.000Z', valid_until: '2026-12-31T00:00:00.000Z', created_at: '2026-09-01T00:00:00.000Z', updated_at: '2026-09-01T00:00:00.000Z', }, { context: SYSTEM } as any); @@ -273,7 +274,7 @@ describe('every approval notification carries its text in body', () => { // [#24065] The control: the absent approver names no account, so no name // can be read for them; the delegate the same hop routes to does. await engine.insert('sys_approval_delegation', { - id: 'dlg_2', delegator_id: GONE, delegate_id: 'u_alice', reason: 'leave', + id: 'dlg_2', delegator_id: GONE, delegate_id: 'u_alice', reason: 'leave', organization_id: orgOfDeal('ooo_gone'), valid_from: '2026-09-01T00:00:00.000Z', valid_until: '2026-12-31T00:00:00.000Z', created_at: '2026-09-01T00:00:00.000Z', updated_at: '2026-09-01T00:00:00.000Z', }, { context: SYSTEM } as any); diff --git a/packages/plugins/plugin-approvals/src/approval-service.test.ts b/packages/plugins/plugin-approvals/src/approval-service.test.ts index 372abb24975..6a4dc75720d 100644 --- a/packages/plugins/plugin-approvals/src/approval-service.test.ts +++ b/packages/plugins/plugin-approvals/src/approval-service.test.ts @@ -3134,10 +3134,13 @@ describe('ApprovalService — out-of-office delegation (#1322)', () => { expect(req.pending_approvers).toEqual(['alice']); }); - it('applies a cross-tenant (null org) rule regardless of request tenant', async () => { + // ADR-0131 D1: an organization-less row is no "every organization" rule. + // The full matrix, per posture, is `ooo-delegation-organization.test.ts`. + it('an organization-less rule reroutes nothing, in the request tenant or any other', async () => { seedDelegation([{ delegator_id: 'alice', delegate_id: 'bob', organization_id: null }]); const req = await svc.openNodeRequest(openInput(['alice']), CTX); - expect(req.pending_approvers).toEqual(['bob']); + expect(req.pending_approvers).toEqual(['alice']); + expect(engine._tables['sys_approval_action'].some((a: any) => a.action === 'ooo_substitute')).toBe(false); }); }); diff --git a/packages/plugins/plugin-approvals/src/approval-service.ts b/packages/plugins/plugin-approvals/src/approval-service.ts index ec2bce5556d..aae41b2e56b 100644 --- a/packages/plugins/plugin-approvals/src/approval-service.ts +++ b/packages/plugins/plugin-approvals/src/approval-service.ts @@ -1222,6 +1222,19 @@ function csvSplit(raw: unknown): string[] { return String(raw).split(',').map(s => s.trim()).filter(Boolean); } +/** + * [ADR-0131] The organization a value names, or `null` for none — `NULL`, + * absent or blank. The normalization `grantOrganizationOf` + * (`@objectstack/core`, `resolve-authz-context.ts`) gives a stored grant row's + * organization; `null` names no organization and matches none, its own + * included. Read by {@link ApprovalService.lookupActiveDelegation} on both + * sides of its comparison: the delegation row's and the request's. + */ +function organizationNamed(value: unknown): string | null { + if (value === null || value === undefined || value === '') return null; + return String(value); +} + /** * Is a submitted decision output blank — i.e. does it fail a `required` * declaration (objectui#2955)? @@ -3227,6 +3240,32 @@ export class ApprovalService implements IApprovalService { * shared `isGrantActive` half-open window (ADR-0091 D2), enforced here at * resolution time — never by a background job. When several rows are active, * the one expiring soonest wins (the most specific coverage window). + * + * **A delegation applies only in its own organization (ADR-0131).** A row + * reroutes a request's approver only when the row's `organization_id` is the + * request's organization: the rule `grantAppliesInTenant` + * (`@objectstack/core`, `resolve-authz-context.ts`) applies to a stored + * position or permission-set grant, read the same way here. + * + * - A delegation of another organization reroutes nothing in this one. + * - An organization-less delegation reroutes nothing anywhere. "NULL means + * every organization" is the reading ADR-0131 retired (D1: nothing reads a + * NULL tenant value as an owner). Under a wall no write produces such a row + * any more: a session insert is stamped with its active organization, an + * elevated insert with none is refused (D9), and the write wall refuses an + * update that empties the column. What remains is stored state from before. + * The D10 ceremony attributes it to the Default Organization under + * `single`; under a wall it has no anchor, so the ceremony reports it and + * leaves it NULL. Such a row is reported here at `warn` with its remedy, + * because its author still sees it in Setup and nothing else says it has + * stopped applying. + * - A request with no organization takes no delegation: a missing + * organization has no "every organization" reading either. + * + * The rule is the same in every posture (ADR-0131 D11). Under `single`, D9 + * stamps the Default Organization on the delegation its author writes and on + * the record a request is about, and the request takes its record's + * organization, so a delegation written there applies as it did before. */ private async lookupActiveDelegation( delegatorId: string, @@ -3243,10 +3282,24 @@ export class ApprovalService implements IApprovalService { context: SYSTEM_CTX, } as any); } catch { return null; } // table absent on minimal stacks — no OOO, resolve as-is - const active = (rows ?? []).filter((r: any) => - isGrantActive(r, now) - // A null-org rule applies across tenants; a scoped rule only within its tenant. - && (organizationId == null || r.organization_id == null || String(r.organization_id) === String(organizationId))); + const requestOrg = organizationNamed(organizationId); + const active: any[] = []; + for (const r of rows ?? []) { + if (!isGrantActive(r, now)) continue; + const rowOrg = organizationNamed(r?.organization_id); + if (rowOrg === null) { + this.logger?.warn?.( + `[approvals] out-of-office delegation '${r?.id}' (${delegatorId} → ${r?.delegate_id}) names no ` + + `organization, so it reroutes no approval: a delegation applies only to approvals in the ` + + `organization it belongs to. Create it again while signed in to the organization whose ` + + `approvals it should cover, and delete this one; on a single-organization deployment, ` + + `'os migrate organization-ownership --apply' attributes it to the Default Organization instead.`, + { delegationId: r?.id, delegatorId, delegateId: r?.delegate_id, requestOrganizationId: requestOrg }, + ); + continue; + } + if (rowOrg === requestOrg) active.push(r); + } if (!active.length) return null; active.sort((a: any, b: any) => { const au = a.valid_until ? Date.parse(String(a.valid_until)) : Number.POSITIVE_INFINITY; diff --git a/packages/plugins/plugin-approvals/src/ooo-delegation-organization.test.ts b/packages/plugins/plugin-approvals/src/ooo-delegation-organization.test.ts new file mode 100644 index 00000000000..9aaf5f0747e --- /dev/null +++ b/packages/plugins/plugin-approvals/src/ooo-delegation-organization.test.ts @@ -0,0 +1,268 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [ADR-0131] An out-of-office delegation applies only in its own organization. + * + * `ApprovalService.lookupActiveDelegation` reads a `sys_approval_delegation` + * row the way `grantAppliesInTenant` (`@objectstack/core`) reads a stored + * position or permission-set grant: the row reroutes an approver only when its + * organization is the request's organization. The reading it replaces kept a + * row with no organization in EVERY organization ("null = applies across + * tenants") and kept every row for a request with no organization — the + * "NULL means every organization" reading ADR-0131 D1 retired. + * + * Under a wall no write produces an organization-less row any more, so the + * population is stored state from before: the D10 ceremony has no anchor to + * attribute a delegation by under a wall, and reports it and leaves it NULL. + * Such a row rerouted every organization's approvals for its delegator. + * + * The rule is the same in every posture (ADR-0131 D11), so each case runs + * under `isolated` and `group`; `single` gets the Default Organization case, + * where D9 stamps the delegation and the record alike. + */ + +import { describe, it, expect, beforeEach, vi } from 'vitest'; +import { assertEngineDeleteDispatch, assertEngineUpdateDispatch } from '@objectstack/metadata-core'; +import type { ApprovalRequestRow } from '@objectstack/spec/contracts'; +import { ApprovalService } from './approval-service.js'; + +interface Row { [k: string]: any } + +function makeEngine(seed: Record = {}) { + const tables: Record = { ...seed }; + const ensure = (n: string) => (tables[n] ??= []); + const matches = (row: Row, filter: any): boolean => { + if (!filter || typeof filter !== 'object') return true; + for (const [k, v] of Object.entries(filter)) { + if (k === '$or') { + if (!(v as any[]).some((sub) => matches(row, sub))) return false; + continue; + } + if (k.startsWith('$')) throw new Error(`fake engine: unsupported filter operator ${k}`); + const rv = row[k]; + if (v != null && typeof v === 'object' && '$in' in (v as any)) { + if (!(v as any).$in.includes(rv)) return false; + continue; + } + if (v != null && typeof v === 'object' && '$ne' in (v as any)) { + if (rv === (v as any).$ne) return false; + continue; + } + if (rv !== v) return false; + } + return true; + }; + return { + _tables: tables, + async find(object: string, options?: any) { + const rows = ensure(object).filter((r) => matches(r, options?.filter ?? options?.where)); + return typeof options?.limit === 'number' ? rows.slice(0, options.limit) : rows; + }, + async insert(object: string, data: Row) { ensure(object).push({ ...data }); return { ...data }; }, + // Writes check their call shape through the engine's own predicates, so + // the double is no looser than ObjectQL (`check:engine-double-contract`). + async update(object: string, data: Row, options?: any) { + const dispatch = assertEngineUpdateDispatch(data, options); + const table = ensure(object); + if (dispatch.kind === 'multi') { + let n = 0; + for (let i = 0; i < table.length; i++) { + if (matches(table[i], options?.where)) { table[i] = { ...table[i], ...data }; n++; } + } + return { updated: n }; + } + const i = table.findIndex((r) => r.id === dispatch.id); + if (i >= 0) table[i] = { ...table[i], ...data }; + return i >= 0 ? { ...table[i] } : null; + }, + async delete(object: string, options?: any) { + const dispatch = assertEngineDeleteDispatch(options); + const table = ensure(object); + if (dispatch.kind === 'multi') { + const survivors = table.filter((r) => !matches(r, options?.where)); + const deleted = table.length - survivors.length; + table.splice(0, table.length, ...survivors); + return { deleted }; + } + const i = table.findIndex((r) => r.id === dispatch.id); + if (i >= 0) table.splice(i, 1); + return { id: dispatch.id }; + }, + async count(object: string) { return ensure(object).length; }, + // The record a request is about carries its own organization, so the + // request's organization is the record's, not the acting context's. + getSchema(object: string) { + return object === 'purchase_order' ? { name: object, fields: { organization_id: { type: 'lookup' } } } : undefined; + }, + registerHook() { /* no-op */ }, + unregisterHooksByPackage() { /* no-op */ }, + }; +} + +/** Mid-window: every delegation below is active at this instant. */ +const NOW = new Date('2026-05-27T10:00:00Z').getTime(); + +function delegation(id: string, delegator: string, delegate: string, organization: string | null | undefined): Row { + const row: Row = { + id, + delegator_id: delegator, + delegate_id: delegate, + valid_from: '2026-05-26T00:00:00Z', + valid_until: '2026-05-30T00:00:00Z', + reason: 'Annual leave', + }; + if (organization !== undefined) row.organization_id = organization; + return row; +} + +/** One individually-routed approver, on a record of `organization` (or none). */ +function openInput(approver: string, organization: string | null) { + const record: Row = { id: 'po1', amount: 500 }; + if (organization !== null) record.organization_id = organization; + return { + object: 'purchase_order', + recordId: 'po1', + runId: 'run_1', + nodeId: 'signoff', + flowName: 'po_approval', + config: { approvers: [{ type: 'user' as const, value: approver }], behavior: 'first_response' as const, lockRecord: false }, + record, + }; +} + +/** The acting member, signed in to `organization` (or to none). */ +function ctx(organization: string | null) { + return organization === null + ? { userId: 'u_submitter', positions: [], permissions: [] } as any + : { userId: 'u_submitter', organizationId: organization, tenantId: organization, positions: [], permissions: [] } as any; +} + +function rig(posture: string, seed: Record) { + const engine = makeEngine(seed); + const warn = vi.fn(); + const svc = new ApprovalService({ + engine: engine as any, + clock: { now: () => new Date(NOW) }, + logger: { warn }, + tenancyPosture: () => posture, + }); + /** Every request here routes to a named approver, so it opens rather than auto-completing. */ + const open = async (input: ReturnType, c: any) => + (await svc.openNodeRequest(input, c)) as ApprovalRequestRow; + const substitutions = () => engine._tables['sys_approval_action']?.filter((a) => a.action === 'ooo_substitute') ?? []; + return { engine, svc, warn, open, substitutions }; +} + +/** Two organizations; alice is out of office in A, carol's rule names no organization. */ +const WALLED_SEED = () => ({ + sys_organization: [ + { id: 'o_a', slug: 'org-a' }, + { id: 'o_b', slug: 'org-b' }, + ], + sys_approval_delegation: [ + delegation('del_a', 'alice', 'bob', 'o_a'), + delegation('del_none', 'carol', 'dave', null), + ], +}); + +for (const posture of ['isolated', 'group'] as const) { + describe(`OOO delegation under the '${posture}' wall — a delegation applies only in its own organization`, () => { + let r: ReturnType; + beforeEach(() => { r = rig(posture, WALLED_SEED()); }); + + it('a delegation of organization A does not reroute an approval in organization B', async () => { + const req = await r.open(openInput('alice', 'o_b'), ctx('o_b')); + expect(req.pending_approvers).toEqual(['alice']); + expect(r.substitutions()).toHaveLength(0); + expect(r.engine._tables['sys_approval_request'][0].organization_id).toBe('o_b'); + }); + + it('the same-organization delegation still applies', async () => { + const req = await r.open(openInput('alice', 'o_a'), ctx('o_a')); + expect(req.pending_approvers).toEqual(['bob']); + expect(r.substitutions()).toHaveLength(1); + expect(r.substitutions()[0].comment).toBe('alice → bob — Annual leave'); + }); + + it("the request's organization is its record's: a delegation of A does not follow a member signed in to A onto B's record", async () => { + const req = await r.open(openInput('alice', 'o_b'), ctx('o_a')); + expect(req.pending_approvers).toEqual(['alice']); + expect(r.substitutions()).toHaveLength(0); + }); + + it('an organization-less row reroutes nothing, in either organization, and is reported once per lookup', async () => { + for (const org of ['o_a', 'o_b']) { + const req = await r.open({ ...openInput('carol', org), recordId: `po_${org}` }, ctx(org)); + expect(req.pending_approvers).toEqual(['carol']); + } + expect(r.substitutions()).toHaveLength(0); + // The kind and the named subject, never the wording. + const reports = r.warn.mock.calls.filter(([, fields]) => fields?.delegationId === 'del_none'); + expect(reports).toHaveLength(2); + expect(reports.map(([, fields]) => fields.requestOrganizationId)).toEqual(['o_a', 'o_b']); + expect(reports[0][1]).toMatchObject({ delegatorId: 'carol', delegateId: 'dave' }); + }); + + it('a row whose organization is blank or absent is organization-less too', async () => { + r.engine._tables['sys_approval_delegation'] = [ + delegation('del_blank', 'alice', 'bob', ''), + delegation('del_absent', 'alice', 'erin', undefined), + ]; + const req = await r.open(openInput('alice', 'o_a'), ctx('o_a')); + expect(req.pending_approvers).toEqual(['alice']); + expect(r.substitutions()).toHaveLength(0); + expect(r.warn.mock.calls.map(([, fields]) => fields?.delegationId).filter(Boolean).sort()) + .toEqual(['del_absent', 'del_blank']); + }); + + it('beside an organization-less row, the same-organization row is the one that applies', async () => { + r.engine._tables['sys_approval_delegation'].push(delegation('del_none_alice', 'alice', 'mallory', null)); + const req = await r.open(openInput('alice', 'o_a'), ctx('o_a')); + expect(req.pending_approvers).toEqual(['bob']); + }); + + it('a chain stops at a hop whose delegation names no organization', async () => { + r.engine._tables['sys_approval_delegation'].push(delegation('del_bob', 'bob', 'mallory', null)); + const req = await r.open(openInput('alice', 'o_a'), ctx('o_a')); + expect(req.pending_approvers).toEqual(['bob']); + expect(r.substitutions()).toHaveLength(1); + }); + + it('a request with no organization takes no delegation, scoped or not', async () => { + // No organization on the record or on the acting context. A real engine + // under a wall refuses such a request's organization-less insert (D9); + // this fake engine stores it, so what is pinned is the slate, which used + // to keep every row of the delegator. + const req = await r.open(openInput('alice', null), ctx(null)); + expect(req.pending_approvers).toEqual(['alice']); + expect(r.substitutions()).toHaveLength(0); + }); + }); +} + +describe("OOO delegation under 'single' — the Default Organization stamps both sides", () => { + const SINGLE_SEED = () => ({ + sys_organization: [{ id: 'o_default', slug: 'default' }], + sys_approval_delegation: [ + // What D9 writes: the delegation its author creates carries the Default Organization. + delegation('del_default', 'alice', 'bob', 'o_default'), + // Stored state from before the ceremony attributed it. + delegation('del_none', 'carol', 'dave', null), + ], + }); + + it('a delegation of the Default Organization reroutes an approval on its records', async () => { + const r = rig('single', SINGLE_SEED()); + const req = await r.open(openInput('alice', 'o_default'), ctx('o_default')); + expect(req.pending_approvers).toEqual(['bob']); + expect(r.substitutions()).toHaveLength(1); + }); + + it('an organization-less row reroutes nothing here either, and is reported', async () => { + const r = rig('single', SINGLE_SEED()); + const req = await r.open(openInput('carol', 'o_default'), ctx('o_default')); + expect(req.pending_approvers).toEqual(['carol']); + expect(r.substitutions()).toHaveLength(0); + expect(r.warn.mock.calls.some(([, fields]) => fields?.delegationId === 'del_none')).toBe(true); + }); +}); diff --git a/packages/plugins/plugin-approvals/src/sys-approval-delegation.object.ts b/packages/plugins/plugin-approvals/src/sys-approval-delegation.object.ts index 7c1d42c3692..5028f9dcbc1 100644 --- a/packages/plugins/plugin-approvals/src/sys-approval-delegation.object.ts +++ b/packages/plugins/plugin-approvals/src/sys-approval-delegation.object.ts @@ -20,6 +20,11 @@ import { F } from '@objectstack/spec'; * shared `isGrantActive` predicate — never by a background job (ADR-0049). * The window is half-open `[valid_from, valid_until)` in UTC. * + * `organization_id` is the organization the delegation belongs to: it + * reroutes only the approvals opened in that organization, and an + * organization-less row reroutes none (ADR-0131 D1; + * `ApprovalService.lookupActiveDelegation`). + * * Scope note: this is the community-core OOO auto-skip only. Long-term proxy * "act-as" access (viewing/acting on another user's full queue under their * authority), delegation governance / segregation-of-duties, and org-wide @@ -140,7 +145,7 @@ export const SysApprovalDelegation = ObjectSchema.create({ label: 'Organization', required: false, group: 'System', - description: 'Tenant that owns this rule; null = applies across tenants for this delegator.', + description: 'The organization this delegation belongs to. It reroutes only the approvals opened in this organization.', }), created_at: Field.datetime({ diff --git a/packages/plugins/plugin-approvals/src/translations/en.objects.generated.ts b/packages/plugins/plugin-approvals/src/translations/en.objects.generated.ts index ab7de8b4e72..5f18e819100 100644 --- a/packages/plugins/plugin-approvals/src/translations/en.objects.generated.ts +++ b/packages/plugins/plugin-approvals/src/translations/en.objects.generated.ts @@ -374,7 +374,7 @@ export const enObjects: NonNullable = { }, organization_id: { label: "Organization", - help: "Tenant that owns this rule; null = applies across tenants for this delegator." + help: "The organization this delegation belongs to. It reroutes only the approvals opened in this organization." }, created_at: { label: "Created At" diff --git a/packages/plugins/plugin-approvals/src/translations/es-ES.objects.generated.ts b/packages/plugins/plugin-approvals/src/translations/es-ES.objects.generated.ts index 00792b3d56c..3f39827f71b 100644 --- a/packages/plugins/plugin-approvals/src/translations/es-ES.objects.generated.ts +++ b/packages/plugins/plugin-approvals/src/translations/es-ES.objects.generated.ts @@ -374,7 +374,7 @@ export const esESObjects: NonNullable = { }, organization_id: { label: "Organization", - help: "Tenant that owns this rule; null = applies across tenants for this delegator." + help: "La organización a la que pertenece esta delegación. Solo redirige las aprobaciones abiertas en esta organización." }, created_at: { label: "Created At" diff --git a/packages/plugins/plugin-approvals/src/translations/es-ES.source-hashes.generated.ts b/packages/plugins/plugin-approvals/src/translations/es-ES.source-hashes.generated.ts index 37e6b847bc4..b0d63777f29 100644 --- a/packages/plugins/plugin-approvals/src/translations/es-ES.source-hashes.generated.ts +++ b/packages/plugins/plugin-approvals/src/translations/es-ES.source-hashes.generated.ts @@ -31,7 +31,6 @@ export const esESGeneratedSourceHashes: Readonly> = { "objects.sys_approval_delegation.fields.display_title.help": "2831a1ffde72b425", "objects.sys_approval_delegation.fields.display_title.label": "70f7aadecce647a5", "objects.sys_approval_delegation.fields.id.label": "3383564051b4b76d", - "objects.sys_approval_delegation.fields.organization_id.help": "f02982e88229d9ca", "objects.sys_approval_delegation.fields.organization_id.label": "3e55836156e1c1de", "objects.sys_approval_delegation.fields.reason.help": "712516f1461dff2b", "objects.sys_approval_delegation.fields.reason.label": "ac399f03d7f2e64d", diff --git a/packages/plugins/plugin-approvals/src/translations/ja-JP.objects.generated.ts b/packages/plugins/plugin-approvals/src/translations/ja-JP.objects.generated.ts index 02141b6bbc6..4461a9c296e 100644 --- a/packages/plugins/plugin-approvals/src/translations/ja-JP.objects.generated.ts +++ b/packages/plugins/plugin-approvals/src/translations/ja-JP.objects.generated.ts @@ -374,7 +374,7 @@ export const jaJPObjects: NonNullable = { }, organization_id: { label: "Organization", - help: "Tenant that owns this rule; null = applies across tenants for this delegator." + help: "この委任が属する組織です。この組織で開始された承認だけを振り替えます。" }, created_at: { label: "Created At" diff --git a/packages/plugins/plugin-approvals/src/translations/ja-JP.source-hashes.generated.ts b/packages/plugins/plugin-approvals/src/translations/ja-JP.source-hashes.generated.ts index 16edecc272a..99951ac7c21 100644 --- a/packages/plugins/plugin-approvals/src/translations/ja-JP.source-hashes.generated.ts +++ b/packages/plugins/plugin-approvals/src/translations/ja-JP.source-hashes.generated.ts @@ -30,7 +30,6 @@ export const jaJPGeneratedSourceHashes: Readonly> = { "objects.sys_approval_delegation.fields.display_title.help": "2831a1ffde72b425", "objects.sys_approval_delegation.fields.display_title.label": "70f7aadecce647a5", "objects.sys_approval_delegation.fields.id.label": "3383564051b4b76d", - "objects.sys_approval_delegation.fields.organization_id.help": "f02982e88229d9ca", "objects.sys_approval_delegation.fields.organization_id.label": "3e55836156e1c1de", "objects.sys_approval_delegation.fields.reason.help": "712516f1461dff2b", "objects.sys_approval_delegation.fields.reason.label": "ac399f03d7f2e64d", diff --git a/packages/plugins/plugin-approvals/src/translations/zh-CN.objects.generated.ts b/packages/plugins/plugin-approvals/src/translations/zh-CN.objects.generated.ts index 2668a86fa1d..2da279ec612 100644 --- a/packages/plugins/plugin-approvals/src/translations/zh-CN.objects.generated.ts +++ b/packages/plugins/plugin-approvals/src/translations/zh-CN.objects.generated.ts @@ -374,7 +374,7 @@ export const zhCNObjects: NonNullable = { }, organization_id: { label: "组织", - help: "拥有此规则的租户;留空 = 对该委派人跨租户生效。" + help: "此委派所属的组织。它只改派在该组织中发起的审批。" }, created_at: { label: "创建时间" diff --git a/scripts/engine-double-contract.pinned.json b/scripts/engine-double-contract.pinned.json index bffec0cf4d3..d589a3930b3 100644 --- a/scripts/engine-double-contract.pinned.json +++ b/scripts/engine-double-contract.pinned.json @@ -2811,6 +2811,16 @@ "verb": "update", "pinned": 1 }, + { + "file": "packages/plugins/plugin-approvals/src/ooo-delegation-organization.test.ts", + "verb": "delete", + "pinned": 1 + }, + { + "file": "packages/plugins/plugin-approvals/src/ooo-delegation-organization.test.ts", + "verb": "update", + "pinned": 1 + }, { "file": "packages/plugins/plugin-approvals/src/recall-refusal-user-copy.test.ts", "verb": "delete",