Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/35719-approvals-reject-route-requires-reason.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@objectstack/plugin-approvals': patch
---

fix(approvals): a reject through the approvals route or `IApprovalService.decide` requires a reason

Clause-②: no (narrowing)
Metadata-protocol: none

<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export changes shape. What moves is one runtime check in this plugin's decide(): a reject without a non-blank string comment is refused. No stored row, registry key or authored value changes, so there is nothing for a migration to rewrite and no tombstone. -->

**BREAKING** for a caller of `POST /api/v1/approvals/requests/:id/reject`, or of `IApprovalService.decide()` with `decision: 'reject'`, that sends no `comment`, an empty or whitespace-only one, or one that is not a string: the call used to reject the request and is now refused before anything is read or written. It ships `patch` because it is a non-protocol breaking change (AGENTS.md Post-Task Checklist step 3): nothing a metadata author writes changes, so `Metadata-protocol` is `none`.

The refusal is the one the `approval_reject` action door gives, judged by the same rules over the same `textarea` param: `400 VALIDATION_ERROR` with `Invalid action params: Action param "comment" is required` for an absent, `null`, empty or whitespace-only value, and `Invalid action params: Action param "comment" (textarea): …` for a value that is not a string (a number, a boolean, an array or an object). The request stays pending. A reject with a reason runs as before, and an approve keeps its comment optional. The platform's own rejects keep working: the SLA escalation's `auto_reject` and the revision-limit auto-reject do not pass through `decide()`, and the email action link records its fixed text. The email action link still records no reason the approver typed; that is a separate follow-up.

Fix for a caller: send the reason as `comment` in the reject body (`{ "comment": "Over budget." }`).
14 changes: 14 additions & 0 deletions .changeset/35719-client-approvals-reject-requires-comment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@objectstack/client': patch
---

fix(client): `client.approvals.reject` requires the decision and its `comment`

Clause-②: no (narrowing)
Metadata-protocol: none

<!-- adr-0087: not-required (no-migration-prescription) No spec key or authored value changes. What moves is the parameter type of one SDK method: the decision argument and its comment member become required, matching the approvals route, which now refuses a reject without a reason. No stored row changes, so there is nothing for a migration to rewrite. -->

**BREAKING** for TypeScript callers of `client.approvals.reject(requestId)` or `client.approvals.reject(requestId, { actorId })`: the decision argument and its `comment` are now required, so those calls no longer compile. At runtime the approvals route refuses a reject without a non-blank reason with `400 VALIDATION_ERROR`. It ships `patch` because it is a non-protocol breaking change (AGENTS.md Post-Task Checklist step 3). `client.approvals.approve` is unchanged; its comment stays optional.

Fix for a caller: pass the reason, `client.approvals.reject(requestId, { comment: 'Over budget.' })`.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ async function statusOf(booted: Booted, id: string): Promise<string> {
}

async function decide(booted: Booted, requestId: string, decision: 'approve' | 'reject', actorId: string) {
return booted.approvals.decide(requestId, { decision, actorId }, SYSTEM_CTX);
// A reject carries its reason: `decide()` refuses one without it.
return booted.approvals.decide(requestId, { decision, actorId, ...(decision === 'reject' ? { comment: 'Over budget.' } : {}) }, SYSTEM_CTX);
}

/** The pre-fix shape: the outcome arms go straight to bare `end` nodes. */
Expand Down
9 changes: 9 additions & 0 deletions packages/client/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,15 @@ describe('Approvals namespace (ADR-0019)', () => {
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('/api/v1/approvals/requests/req-1/reject');
expect(opts.method).toBe('POST');
expect(JSON.parse(opts.body).comment).toBe('Missing fields');
// A reject carries its reason: the decision and its `comment` are required.
const typeOnly = () => {
// @ts-expect-error — no decision, so no reason
void client.approvals.reject('req-1');
// @ts-expect-error — a decision without its reason
void client.approvals.reject('req-1', { actorId: 'user-1' });
};
void typeOnly;
});

it('should list the action audit trail', async () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7059,9 +7059,10 @@ export class ObjectStackClient {

/**
* Record a reject decision on a request. Resumes the owning flow run down
* the `reject` edge.
* the `reject` edge. A reject carries its reason: `comment` is required,
* and a blank one is refused with `400 VALIDATION_ERROR`.
*/
reject: async (requestId: string, decision?: { actorId?: string; comment?: string; attachments?: string[] }): Promise<ApprovalDecisionResult> => {
reject: async (requestId: string, decision: { actorId?: string; comment: string; attachments?: string[] }): Promise<ApprovalDecisionResult> => {
const route = this.getRoute('approvals');
const res = await this.fetch(`${this.baseUrl}${route}/requests/${encodeURIComponent(requestId)}/reject`, {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe('the decision that ends an approval tells its submitter (approval.appro

it('a rejection at the first step ends the process, so it tells the submitter', async () => {
const step1 = await submit('deal_ladder', 'vetoed', 5000);
await svc.decide(step1.id, { decision: 'reject', actorId: 'u_mgr' }, asUser('u_mgr'));
await svc.decide(step1.id, { decision: 'reject', comment: 'Over budget.', actorId: 'u_mgr' }, asUser('u_mgr'));
expect(await pendingOn('D_vetoed')).toBeUndefined();
expect(outcomesAbout(step1.id).map((e) => [e.topic, e.audience])).toEqual([['approval.rejected', ['u_sub']]]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ describe('Approval node bridge (ADR-0019)', () => {
await automation.execute('deal_approval', { object: 'crm_deal', record: { id: 'd1' } });
const request = (await fake.find('sys_approval_request', { where: { status: 'pending' } }))[0];

const out = await service.decide(request.id, { decision: 'reject', actorId: 'u1' }, SYSTEM_CTX);
const out = await service.decide(request.id, { decision: 'reject', comment: 'Over budget.', actorId: 'u1' }, SYSTEM_CTX);

expect(out).toMatchObject({ finalized: true, decision: 'reject', resumed: true });
expect(marks).toEqual(['on_rejected']);
Expand Down
47 changes: 43 additions & 4 deletions packages/plugins/plugin-approvals/src/approval-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1415,11 +1415,50 @@ describe('ApprovalService (node era)', () => {

it('decide: finalizes even when no automation is attached (resumed=false)', async () => {
const req = await svc.openNodeRequest(openInput(['u9']), CTX);
const out = await svc.decide(req.id, { decision: 'reject', actorId: 'u9' }, SYS);
const out = await svc.decide(req.id, { decision: 'reject', actorId: 'u9', comment: 'Over budget.' }, SYS);
expect(out.finalized).toBe(true);
expect(out.resumed).toBe(false);
});

// ── a reject carries its reason ─────────────────────────────────
//
// The `approval_reject` action door's refusal, at the member every human
// door reaches: the same code, status and sentence, nothing read or written.

it('decide: refuses a reject whose comment is missing, empty, blank, null or not a string, and leaves the request pending', async () => {
const resumed: any[] = [];
svc.attachAutomation({ async resume(runId) { resumed.push(runId); } });
const req: any = await svc.openNodeRequest(openInput(['u9']), CTX);
for (const comment of [undefined, '', ' ', null]) {
const err: any = await svc.decide(req.id, { decision: 'reject', actorId: 'u9', comment } as any, SYS)
.then(() => undefined, (e) => e);
expect([err?.status, err?.code], String(comment)).toEqual([400, 'VALIDATION_ERROR']);
expect(err.message).toBe('Invalid action params: Action param "comment" is required');
}
// Not a string is no reason either: the door's `textarea` value shape refuses it.
for (const [comment, received] of [[0, 'number'], [false, 'boolean'], [[], 'array'], [{}, 'object']] as const) {
const err: any = await svc.decide(req.id, { decision: 'reject', actorId: 'u9', comment } as any, SYS)
.then(() => undefined, (e) => e);
expect([err?.status, err?.code], JSON.stringify(comment)).toEqual([400, 'VALIDATION_ERROR']);
expect(err.message).toBe(
`Invalid action params: Action param "comment" (textarea): Invalid input: expected string, received ${received}`,
);
}
const [row] = await engine.find('sys_approval_request', { where: { id: req.id } });
expect(row.status).toBe('pending');
expect((engine._tables['sys_approval_action'] ?? []).filter((a: any) => a.request_id === req.id && a.action === 'reject')).toEqual([]);
expect(resumed).toEqual([]);
});

// An approve keeps its comment optional: the resume test above approves with none.
it('decide: CONTROL — a reject with its reason is recorded with it', async () => {
const req: any = await svc.openNodeRequest(openInput(['u9']), CTX);
const out = await svc.decide(req.id, { decision: 'reject', actorId: 'u9', comment: 'Over budget.' }, SYS);
expect([out.finalized, out.request.status]).toEqual([true, 'rejected']);
expect((engine._tables['sys_approval_action'] ?? []).find((a: any) => a.request_id === req.id && a.action === 'reject')?.comment)
.toBe('Over budget.');
});

// ── #4420: a REPORTED resume failure is a failure ────────────────
//
// The engine answers a lost run with `{ success: false }` rather than by
Expand Down Expand Up @@ -1726,7 +1765,7 @@ describe('ApprovalService (node era)', () => {

it('reject: names the record, the way the inbox row\'s inline Reject does', async () => {
const id = await openRequestId(openInput(['u9']), CTX);
const out = await svc.decide(id, { decision: 'reject', actorId: 'u9' }, SYS);
const out = await svc.decide(id, { decision: 'reject', comment: 'Over budget.', actorId: 'u9' }, SYS);
expect(out.request.status).toBe('rejected');
expect(named('approval_reject', out)).toBe('Rejected "Acme Renewal"');
});
Expand Down Expand Up @@ -2799,7 +2838,7 @@ describe('ApprovalService — admin override (#3424)', () => {

it('a platform admin can reject a stuck request', async () => {
const req = await svc.openNodeRequest(stuckInput(), CTX);
const out = await svc.decide(req.id, { decision: 'reject', actorId: 'root' }, PLATFORM_ADMIN);
const out = await svc.decide(req.id, { decision: 'reject', comment: 'Over budget.', actorId: 'root' }, PLATFORM_ADMIN);
expect(out.finalized).toBe(true);
expect(out.request.status).toBe('rejected');
});
Expand Down Expand Up @@ -6603,7 +6642,7 @@ describe('in-band transitions finalise before they resume (#3456 invariant)', ()

it('decide(reject) finalises before resuming', async () => {
const req = await open();
await svc.decide(req.id, { decision: 'reject', actorId: 'u9' }, SYS);
await svc.decide(req.id, { decision: 'reject', comment: 'Over budget.', actorId: 'u9' }, SYS);
expectCleanHandoffs();
});

Expand Down
20 changes: 20 additions & 0 deletions packages/plugins/plugin-approvals/src/approval-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {
canServeApiOperation, isFileIdToken, referenceTargetOf, SINGLE_OPTION_TYPES, MULTI_OPTION_TYPES,
} from '@objectstack/spec/data';
import type { EnableLike, ObjectCapabilitiesParsed } from '@objectstack/spec/data';
import { validateActionParams } from '@objectstack/spec/ui';
// [#11993] The SANCTIONED renderer for OPERATION-level refusal copy. The
// Operation Message Catalog is the ONE seat for these sentences — its own
// header bars both a package-local string table and a second rendering
Expand Down Expand Up @@ -6481,12 +6482,31 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
* [#24753] A decision that ends the approval process tells the submitter its
* outcome (`approval.approved` / `approval.rejected`,
* {@link ApprovalService.notifyDecisionOutcome}).
*
* A reject carries its reason: a missing or blank `comment`, or one that is
* not a string, is refused before anything is read or written —
* `400 VALIDATION_ERROR` with the sentence the `approval_reject` action door
* answers, judged by the same rules (`validateActionParams` over the
* `textarea` param that action declares). Every human door reaches this
* member (the REST route, the SDK, the action door that forwards to the
* route, the action link with its fixed text). The SLA sweep's auto-reject
* calls `decideAndResume` and the revision-limit reject writes its own row,
* so neither passes through here. An approve keeps its comment optional.
*/
async decide(
requestId: string,
input: ApprovalDecisionInput,
context: ExecutionContext,
): Promise<ApprovalDecisionResult> {
if (input?.decision === 'reject') {
const issues = validateActionParams([{ name: 'comment', required: true, type: 'textarea' }], { comment: input.comment });
if (issues.length > 0) {
throw Object.assign(
new Error(`Invalid action params: ${issues.map((i) => i.message).join('; ')}`),
{ status: 400, code: 'VALIDATION_ERROR' },
);
}
}
return this.decideAndResume(requestId, input, context, 'outcome');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ describe('[ADR-0131 D1] under a walled posture, a request with no organization i
try {
switch (door) {
case 'approve': await svc.decide(id, { decision: 'approve', actorId } as any, ctx); break;
case 'reject': await svc.decide(id, { decision: 'reject', actorId } as any, ctx); break;
case 'reject': await svc.decide(id, { decision: 'reject', comment: 'Over budget.', actorId } as any, ctx); break;
case 'recall': await svc.recall(id, { actorId } as any, ctx); break;
case 'reassign': await svc.reassign(id, { actorId, to: 'usr_target' }, ctx); break;
case 'sendBack': await svc.sendBack(id, { actorId, comment: 'please revise' } as any, ctx); break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ describe('a revise-window recall on a batch item resumes the batch (#28183)', ()
await service.sendBack(first.id, { actorId: 'u1' }, asUser('u1'));
await service.resubmit(first.id, { actorId: 'submitter' }, asUser('submitter'));
const round2 = await requestFor('t1', 'pending');
await service.decide(round2.id, { decision: 'reject', actorId: 'u1' }, SYSTEM_CTX);
await service.decide(round2.id, { decision: 'reject', comment: 'Over budget.', actorId: 'u1' }, SYSTEM_CTX);
const second = await requestFor('t2', 'pending');
await service.decide(second.id, { decision: 'approve', actorId: 'u1' }, SYSTEM_CTX);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ describe('#13807 — a stranded decision publishes its facts, and keeps its stat
const req = await park(automation);

const out = await service.decide(
req.id, { decision: 'reject', actorId: 'u1' }, SYSTEM_CTX,
req.id, { decision: 'reject', comment: 'Over budget.', actorId: 'u1' }, SYSTEM_CTX,
);

expect(out.finalized).toBe(true);
Expand Down Expand Up @@ -289,7 +289,7 @@ describe('#13807 — a stranded decision publishes its facts, and keeps its stat
};

const err = await service
.decide(req.id, { decision: 'reject', actorId: 'u1' }, SYSTEM_CTX)
.decide(req.id, { decision: 'reject', comment: 'Over budget.', actorId: 'u1' }, SYSTEM_CTX)
.then(() => null, (e: Error) => e);

expect(seen.length).toBe(1);
Expand Down Expand Up @@ -348,7 +348,7 @@ describe('#13807 — a stranded decision publishes its facts, and keeps its stat
const runId = req.flow_run_id;

const err = await service
.decide(req.id, { decision: 'reject', actorId: 'u1' }, SYSTEM_CTX)
.decide(req.id, { decision: 'reject', comment: 'Over budget.', actorId: 'u1' }, SYSTEM_CTX)
.then(() => null, (e: Error) => e);

// The door still throws and the decision still stands — this card moves
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ describe('[#25573] excludeSubmitter keeps the submitter from approving their own
await expect(svc.decide(row.id, { decision: 'approve', actorId: 'u_sub' } as any, SUB))
.rejects.toThrow(/^FORBIDDEN: 'u_sub' submitted this request, and its approval step sets excludeSubmitter/);
expect((await actionsOf(row.id)).map((a) => a.action)).toEqual(['submit']);
const out = await svc.decide(row.id, { decision: 'reject', actorId: 'u_sub' } as any, SUB);
const out = await svc.decide(row.id, { decision: 'reject', comment: 'Over budget.', actorId: 'u_sub' } as any, SUB);
expect(out.request.status).toBe('rejected');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ describe('platform_admin approver: one slot the platform administrators decide u
const row = await open('owner', 'host_review', PLATFORM);
expect(await inbox(as('u_owner_cfg'))).toContain(row.id);
// Naming the slot is admitted for the owner as well as the default actor.
const out = await svc.decide(row.id, { decision: 'reject', actorId: SLOT } as any, as('u_owner_cfg'));
const out = await svc.decide(row.id, { decision: 'reject', comment: 'Over budget.', actorId: SLOT } as any, as('u_owner_cfg'));
expect(out.request.status).toBe('rejected');
const decision = (await actionsOf(row.id)).find((a) => a.action === 'reject');
expect(decision).toMatchObject({ actor_id: 'u_owner_cfg', acted_as: SLOT });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe('an object that declares enable.approvalsVisibleToReaders, with the hos
// Accepted: `FORBIDDEN:` is the code this service's refusals carry, the
// prefix the REST layer parses into a 403.
await expect(rig.svc.decide(requestA, { decision: 'approve', actorId: READER }, ctx)).rejects.toThrow(/^FORBIDDEN:/);
await expect(rig.svc.decide(requestA, { decision: 'reject', actorId: READER }, ctx)).rejects.toThrow(/^FORBIDDEN:/);
await expect(rig.svc.decide(requestA, { decision: 'reject', comment: 'Over budget.', actorId: READER }, ctx)).rejects.toThrow(/^FORBIDDEN:/);
await expect(rig.svc.reassign(requestA, { actorId: READER, to: READER }, ctx)).rejects.toThrow(/^FORBIDDEN:/);
await expect(rig.svc.comment(requestA, { actorId: READER, comment: 'hi' }, ctx)).rejects.toThrow(/^FORBIDDEN:/);
await expect(rig.svc.recall(requestA, { actorId: READER }, ctx)).rejects.toThrow(/^FORBIDDEN:/);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ describe('record-reader approval visibility (#8652)', () => {
// request is not a licence to decide it.
await expect(svc.decide(REQUEST, { decision: 'approve', actorId: LEDGER_ROLE }, ctx))
.rejects.toThrow(/^FORBIDDEN/);
await expect(svc.decide(REQUEST, { decision: 'reject', actorId: LEDGER_ROLE }, ctx))
await expect(svc.decide(REQUEST, { decision: 'reject', comment: 'Over budget.', actorId: LEDGER_ROLE }, ctx))
.rejects.toThrow(/^FORBIDDEN/);
await expect(svc.reassign(REQUEST, { actorId: LEDGER_ROLE, to: LEDGER_ROLE }, ctx))
.rejects.toThrow(/^FORBIDDEN/);
Expand Down
Loading
Loading