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
18 changes: 18 additions & 0 deletions .changeset/36463-no-approver-text-in-reader-language.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@objectstack/plugin-approvals': patch
'@objectstack/rest': patch
'@object-ui/console': patch
'@object-ui/app-shell': patch
---

fix(plugin-approvals): the decide refusal of a submitter's own approval, and a submitter notice that names several slots, read in the reader's language

Clause-②: yes (widening)
Metadata-protocol: none

**Widened door response.** The approvals decide door (`packages/rest/src/approvals-routes.ts`) gains one optional member, `userMessage`, at the top level of its flat `{ code, error }` refusal body. Two rows answer it today: the two `403 FORBIDDEN` decide refusals of a submitter's own approval, the `excludeSubmitter` refusal and the administrator-override self-approve refusal. It is absent whenever no producer marked one, so every other refusal body is unchanged.

- **The decide refusal.** When the decision door refuses a submitter's own approval (the step sets `excludeSubmitter`, or the administrator override would approve the actor's own request), the thrown error now carries a `userMessage`: the same refusal, with its remedy, in the decider's language (`approvalDecideRefused*` in every approvals message pack). A position is named by the decider's pack label, then its declared label, then its name. The error's `message` is unchanged: it stays English for the developer and the log. With no reader language, `userMessage` is English and names no user id.
- **The REST door.** The approvals routes answer a refusal the service marked with `userMessage` at the top level of their flat `{ code, error }` body, beside `error`, on every mapped row. A refusal with no marking answers exactly as before.
- **The console.** The record page's `type: 'api'` actions (the request's Approve button) read a flat body's top-level `userMessage` as well as the `error.userMessage` of the ADR-0112 envelope, and show it in place of the English `error`. The Approval Center's inline decision toast shows a marked refusal as written, before its generic line.
- **The submitter notice.** A notice for a slate of 2 to 8 slots names each slot inline, one keyed type word and value per slot (`approvalNoApproverHoldingSlots2` to `…Slots8`, derived from each pack's own `approvalNoApproverHolding`, `approvalNoApproverSlot` and `approvalNoApproverSlotSeparator`), so the messaging service renders every slot in the recipient's language at delivery. A slate of more than 8 slots still names its slots in English inside the recipient's sentence. The literal English body is unchanged.
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,27 @@ describe('an empty admin_rescue slate opens on the organization\'s administrator
expect(renderer.render(notice.keyedText, notice.payload, 'zh-CN').body).toBe(holdingZh('岗位「treasury」'));
});

// [#36463] A slate of several slots is spelled inline too, one keyed
// type and value per slot, so the submitter reads every slot in their
// language: the sentence, the type words and the separator, each
// position by its pack label, else as stored.
it('the submitter\'s notice: a slate of several slots reads entirely in the recipient\'s language', async () => {
const row = await openLabelled('none_notice_two', [
{ type: 'position', value: 'sales_manager' }, { type: 'position', value: 'treasury' },
]);
const [notice] = told.filter((e) => e.source?.id === row.id);
expect(notice.keyedText.bodyKey).toBe('messages.approvalNoApproverHoldingSlots2');
expect(notice.payload.body).toBe(holding("position 'Sales Manager', position 'treasury'"));
const renderer = new NotificationTextRenderer({ getData: () => undefined, getI18n: () => i18n as any });
const zh = renderer.render(notice.keyedText, notice.payload, 'zh-CN').body;
expect(zh).toBe(holdingZh('岗位「销售经理」、岗位「treasury」'));
expect(zh).not.toMatch(/position|sales_manager/);
expect(renderer.render(notice.keyedText, notice.payload, 'ja-JP').body)
.toContain('役職「Sales Manager」、役職「treasury」を担うメンバー');
expect(renderer.render(notice.keyedText, notice.payload, 'en').body)
.toBe(holding("position 'Sales Manager', position 'treasury'"));
});

it('CONTROL: a note written before the slot was stored renders exactly as before', async () => {
const row = await open('none_legacy', UNSTAFFED, {}, LEADERLESS_ORG);
// The stored shape of a note written before #36035: `who`, pre-composed.
Expand Down
36 changes: 25 additions & 11 deletions packages/plugins/plugin-approvals/src/approval-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ import {
noApproverPositions,
noApproverSlots,
noApproverWho,
submitterRefusalUserMessage,
type NoApproverNote,
type SubmitterApprovalRefusal,
} from './no-approver-note.js';
import {
localizeRequestLabels,
Expand Down Expand Up @@ -5957,21 +5959,35 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
// [#36308] Composed only when it is said, naming a position as the
// timeline note does: the decider's pack label, else the declared label,
// else its name. The sentence stays the English the note stores.
// [#36463] Beside it, the end user's half: the same refusal in the
// decider's language, as the error's `userMessage`
// (`submitterRefusalUserMessage`), which the REST door carries to the
// wire. The `message` is unchanged byte for byte.
const noOneElse = namesNoOneBut(pendingApprovers, String(raw.submitter_id ?? ''));
const noApproverRemedy = async (): Promise<string> => {
const labels = await this.notePositionLabels(slatePositions(pendingApprovers), this.readerLabels(context));
return noApproverNoteText({ who: noApproverWho(pendingApprovers, (name) => labels.get(name)) });
const refuseOwnApproval = async (
refusal: SubmitterApprovalRefusal,
message: (remedy: string | undefined) => string,
): Promise<never> => {
const reader = this.readerLabels(context);
const labels = noOneElse
? await this.notePositionLabels(slatePositions(pendingApprovers), reader)
: undefined;
const labelOf = (name: string): string | undefined => labels?.get(name);
const remedy = noOneElse ? noApproverNoteText({ who: noApproverWho(pendingApprovers, labelOf) }) : undefined;
const err = new Error(message(remedy)) as Error & { userMessage?: string };
err.userMessage = submitterRefusalUserMessage(
refusal, noOneElse ? pendingApprovers : undefined, reader?.sentence, labelOf,
);
throw err;
};
if (
input.decision === 'approve' && config.excludeSubmitter === true
&& raw.submitter_id && decider && decider === String(raw.submitter_id)
) {
const remedy = noOneElse ? await noApproverRemedy() : undefined;
throw new Error(
await refuseOwnApproval('excludeSubmitter', (remedy) =>
`FORBIDDEN: '${decider}' submitted this request, and its approval step sets excludeSubmitter, so they `
+ `cannot approve it. ${remedy
?? 'Another approver on the step decides it; if the submitter holds the only slot, reassign it to someone else.'}`,
);
?? 'Another approver on the step decides it; if the submitter holds the only slot, reassign it to someone else.'}`);
}
// [Maintainer ruling 2026-11-17, option B] The #3424 override never
// approves a request its actor submitted, whatever `excludeSubmitter`
Expand All @@ -5983,14 +5999,12 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
// `attachViewers` computes `viewer.can_override_approve` from the same
// predicate, so the Approve control is not offered where this refuses.
if (input.decision === 'approve' && viaOverride && overrideApprovesOwnRequest(raw, decider)) {
const remedy = noOneElse ? await noApproverRemedy() : undefined;
throw new Error(
await refuseOwnApproval('override', (remedy) =>
`FORBIDDEN: '${decider}' submitted this request and holds no slot on it, so they cannot approve it `
+ `through the administrator override. ${remedy !== undefined
? `${remedy} To withdraw it instead, reject it or recall it.`
: 'An approver on the step decides it. To move it, reassign it to someone else, reject it, or recall '
+ 'it and send the record for approval again.'}`,
);
+ 'it and send the record for approval again.'}`);
}

// #3447 P2: decision outputs — validated BEFORE any write (audit included)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,51 @@ describe('[#25573] excludeSubmitter keeps the submitter from approving their own
.rejects.toThrow(remedy("position 'Legal Counsel', team 'ops'"));
});

// [#36463] The end user's half rides beside the English: the thrown
// error's `userMessage`, the same refusal in the decider's language (the
// REST door carries it to the wire). The `message` is the English above.
it('each refusal carries the decider\'s language as its userMessage, the message keeping its English', async () => {
const i18n = createMemoryI18n();
for (const [locale, data] of Object.entries(ApprovalsTranslations)) {
i18n.loadTranslations(locale, data as Record<string, unknown>);
}
i18n.loadTranslations('zh-CN', { positions: { legal: { label: '法务' } } });
const labelled = new ApprovalService({
engine: engine as any,
clock: { now: () => new Date(now += 1000) },
logger: { warn: () => {}, info: () => {}, error: () => {}, debug: () => {} } as any,
i18nServiceSource: () => i18n,
positionLabelSource: async (name) => (name === 'legal' ? 'Legal Counsel' : undefined),
});
const refusalOf = (requestId: string, context: any): Promise<any> =>
labelled.decide(requestId, { decision: 'approve', actorId: 'u_sub' } as any, context).then(
() => { throw new Error('expected a refusal'); }, (err) => err);
const zhRemedy = '目前还没有其他人能审批此请求。组织管理员需要添加一名担任岗位「法务」、团队「ops」的成员,之后由该成员决定。';

const overridden = await open('solo_user_message', FIELD_STEP, {}, { org: SOLO_ORG });
await engine.update('sys_approval_request', { id: overridden.id, pending_approvers: 'position:legal,team:ops' },
{ context: SYSTEM } as any);
const zhOverride = await refusalOf(overridden.id, { ...soloAdmin, locale: 'zh-CN' });
expect(zhOverride.message).toBe("FORBIDDEN: 'u_sub' submitted this request and holds no slot on it, so they cannot "
+ 'approve it through the administrator override. No one else can approve this request yet. An organization '
+ "administrator must add a member who holds position '法务', team 'ops'; that member can then decide it. To "
+ 'withdraw it instead, reject it or recall it.');
expect(zhOverride.userMessage).toBe(`此请求由您提交,且您不在其审批人之列,因此不能以管理员身份代为批准它。${zhRemedy}如需撤销,请驳回或撤回该请求。`);

const excluded = await open('solo_user_message_excluded', FIELD_STEP, ON, { org: SOLO_ORG });
await engine.update('sys_approval_request', { id: excluded.id, pending_approvers: 'position:legal,team:ops' },
{ context: SYSTEM } as any);
expect((await refusalOf(excluded.id, { ...soloAdmin, locale: 'zh-CN' })).userMessage)
.toBe(`此请求由您提交,而其审批步骤不允许提交人批准自己的请求,因此您不能批准它。${zhRemedy}`);

// A decider who asked in no language reads the English, which names no id.
const plain = await refusalOf(overridden.id, soloAdmin);
expect(plain.userMessage).toBe('You submitted this request and hold no slot on it, so you cannot approve it '
+ 'through the administrator override. No one else can approve this request yet. An organization administrator '
+ "must add a member who holds position 'Legal Counsel', team 'ops'; that member can then decide it. To withdraw "
+ 'it instead, reject it or recall it.');
});

it('the excludeSubmitter refusal names a labelled position the same way', async () => {
const labelled = new ApprovalService({
engine: engine as any,
Expand Down Expand Up @@ -513,6 +558,11 @@ describe('[#25573] excludeSubmitter keeps the submitter from approving their own
{ context: SYSTEM } as any);
await expect(svc.decide(overridden.id, { decision: 'approve', actorId: 'u_sub' } as any, soloAdmin))
.rejects.toThrow('through the administrator override. An approver on the step decides it. To move it');
// [#36463] …and the end user's half names that approver too (a service with no i18n reads the English).
await expect(svc.decide(excluded.id, { decision: 'approve', actorId: 'u_sub' } as any, SUB)).rejects.toMatchObject({
userMessage: 'You submitted this request, and its approval step keeps its submitter from approving it, so you '
+ 'cannot approve it. Another approver on the step decides it; if you hold the only slot, reassign it to someone else.',
});
expect(sent.filter((e) => e.source?.id === excluded.id && e.topic === 'approval.comment')).toEqual([]);
});
});
Expand Down
Loading
Loading