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
10 changes: 10 additions & 0 deletions .changeset/36035-no-approver-position-label.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@objectstack/plugin-approvals': patch
---

fix(approvals): the no-approver note names its position by its label

Clause-②: no
Metadata-protocol: none

When a request opens with no one able to approve it and its approver list names an unstaffed position, its timeline note told the reader to add "a member who holds position 'regional_manager'", with the position's internal name, in every language. The note now stores the position as a reference, so a reader is served the position's label the way the administrators' routing note names one: the reader's language pack (`positions.<name>.label`) first, else the position's declared `label`, else the name as before. The English the row stores for audit keeps the name, and a note written before this change is shown exactly as it was.
4 changes: 3 additions & 1 deletion content/docs/automation/approvals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,9 @@ administrator is its submitter on a step that sets `excludeSubmitter`. The
server logs a warning, and the request's timeline and a notice to its submitter
say that no one else can approve it yet and that an organization administrator
must add a member who can (one who holds the unstaffed position, when the slate
names one). Only the admin override decides it, and never its submitter's own.
names one). The timeline's note names that position the way the note above
does: by its label for a reader, by its name in the text the row stores. Only the
admin override decides it, and never its submitter's own.

### The approver finds it in their queue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,77 @@ describe('an empty admin_rescue slate opens on the organization\'s administrator
expect(served.map((a) => a.comment)).toEqual(['审批人名单未指向任何人,因此已转交本组织管理员处理:岗位「sales_manager」无人任职。']);
});
});
// [#36035] The no-approver note (an organization with no administrator opens
// on the slate that names no one) stores its position as a `type:value`
// slot, not a finished string, and names it the way the note above does:
// the reader's pack, else the declared label, else the name.
describe('[#36035] the no-approver note names its position by its label', () => {
let labelled: ApprovalService;
const declared: Record<string, string> = { sales_manager: 'Sales Manager' };
const holding = (who: string) => 'No one else can approve this request yet. An organization administrator '
+ `must add a member who holds ${who}; that member can then decide it.`;
const holdingZh = (who: string) => `目前还没有其他人能审批此请求。组织管理员需要添加一名担任 ${who} 的成员,之后由该成员决定。`;
const ENGLISH = holding("position 'sales_manager'");
const noteFor = async (requestId: string, locale?: string) =>
(await labelled.listActions(requestId, { ...as('u_sub', LEADERLESS_ORG), ...(locale ? { locale } : {}) }))
.filter((a) => a.action === 'comment' && !a.actor_id)
.map((a) => a.comment);

beforeAll(() => {
const i18n = createMemoryI18n();
for (const [locale, data] of Object.entries(ApprovalsTranslations)) {
i18n.loadTranslations(locale, data as Record<string, unknown>);
}
i18n.loadTranslations('zh-CN', { positions: { sales_manager: { label: '销售经理' } } });
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) => declared[name],
});
});

it('the snapshot stores the slot, not a finished string; the row stores the English with the name', async () => {
const row = await open('none_stored', UNSTAFFED, {}, LEADERLESS_ORG);
const [note] = (await actionsOf(row.id)).filter((a) => a.action === 'comment' && !a.actor_id);
expect(note.comment).toBe(ENGLISH);
expect((await snapshotOf(row.id)).__noApproverNote).toEqual({
action_id: note.id, slots: ['position:sales_manager'],
});
expect(await noteFor(row.id)).toEqual([ENGLISH]);
});

it('a zh-CN reader reads the pack\'s label, and never the machine name', async () => {
const row = await open('none_zh', UNSTAFFED, {}, LEADERLESS_ORG);
const [comment] = await noteFor(row.id, 'zh-CN');
expect(comment).toBe(holdingZh("position '销售经理'"));
expect(comment).not.toContain('sales_manager');
});

it('a reader whose pack holds no translation reads the declared label', async () => {
const row = await open('none_declared', UNSTAFFED, {}, LEADERLESS_ORG);
expect(await noteFor(row.id, 'en')).toEqual([holding("position 'Sales Manager'")]);
const [ja] = await noteFor(row.id, 'ja-JP');
expect(ja).toContain("position 'Sales Manager'");
expect(ja).not.toContain('sales_manager');
});

it('a position with no label anywhere keeps its internal name', async () => {
const row = await open('none_unlabelled', [{ type: 'position', value: 'treasury' }], {}, LEADERLESS_ORG);
expect(await noteFor(row.id, 'zh-CN')).toEqual([holdingZh("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.
const snapshot = await snapshotOf(row.id);
snapshot.__noApproverNote = { action_id: snapshot.__noApproverNote.action_id, who: "position 'sales_manager'" };
await engine.update('sys_approval_request', { id: row.id, node_config_json: JSON.stringify(snapshot) },
{ context: SYSTEM } as any);
expect(await noteFor(row.id, 'zh-CN')).toEqual([holdingZh("position 'sales_manager'")]);
expect(await noteFor(row.id, 'en')).toEqual([ENGLISH]);
expect(await noteFor(row.id)).toEqual([ENGLISH]);
});
});
});
7 changes: 6 additions & 1 deletion packages/plugins/plugin-approvals/src/admin-rescue-note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,13 @@ function labelledPosition(clause: Clause, positionLabel: PositionLabelOf | undef

/** The position names an admin_rescue note's abandoned slate holds, each once. */
export function adminRescuePositions(note: Pick<AdminRescueNote, 'abandoned'>): string[] {
return slatePositions(note.abandoned);
}

/** [#36035] The position names a slate's `type:value` slots hold, each once: the no-approver note's too. */
export function slatePositions(slots: readonly string[]): string[] {
const names = new Set<string>();
for (const slot of note.abandoned) {
for (const slot of slots) {
if (typeof slot === 'string' && slot.startsWith('position:') && slot.length > 'position:'.length) {
names.add(slot.slice('position:'.length));
}
Expand Down
11 changes: 8 additions & 3 deletions packages/plugins/plugin-approvals/src/approval-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ import {
namesNoOneBut,
noApproverNoteOf,
noApproverNoteText,
noApproverPositions,
noApproverSentence,
noApproverSlots,
noApproverWho,
type NoApproverNote,
} from './no-approver-note.js';
Expand Down Expand Up @@ -5352,8 +5354,8 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
slateSource = 'administrators';
} else {
slateSource = 'no_one';
const who = noApproverWho(approvers);
noApproverNote = { action_id: uid('aact'), ...(who ? { who } : {}) };
const slots = noApproverSlots(approvers);
noApproverNote = { action_id: uid('aact'), ...(slots ? { slots } : {}) };
// Break-glass stays break-glass (#3424): an organization whose
// administrators' slate resolves to nobody opens the request on the
// slate that names no one — the only option that neither waves the
Expand Down Expand Up @@ -11385,7 +11387,10 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
const exclusion = submitterExclusionNoteOf(snapshot);
if (exclusion) overlay(exclusion.action_id, () => submitterExclusionNoteText(exclusion, labels.sentence));
const noApprover = noApproverNoteOf(snapshot);
if (noApprover) overlay(noApprover.action_id, () => noApproverNoteText(noApprover, labels.sentence));
if (noApprover) {
const positionLabels = await this.notePositionLabels(noApproverPositions(noApprover), labels);
overlay(noApprover.action_id, () => noApproverNoteText(noApprover, labels.sentence, (name) => positionLabels.get(name)));
}
}

/**
Expand Down
80 changes: 63 additions & 17 deletions packages/plugins/plugin-approvals/src/no-approver-note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,54 +24,96 @@

import { approvalMessageKey, interpolate } from './approval-notification-text.js';
import { enMessages, type ApprovalMessageId } from './translations/messages.js';
import type { ApprovalSentenceRender } from './admin-rescue-note.js';
import { slatePositions, type ApprovalSentenceRender, type PositionLabelOf } from './admin-rescue-note.js';
import { isPlatformAdminAddress, slateNamesAPerson } from './approver-address.js';

/** The node-config snapshot key the note rides under. */
export const NO_APPROVER_NOTE_SNAPSHOT_KEY = '__noApproverNote';

/** The note as the request's snapshot stores it. */
/**
* The note as the request's snapshot stores it.
*
* [#36035] A note stores the slate's literals as `slots`, the `type:value`
* references themselves, so a read can name a position by its label the way
* the administrators' note does (`adminRescueNoteText`). A note written before
* stored `who` instead: the literals pre-composed as English, which no read
* can take apart again, so it is rendered exactly as it always was.
*/
export interface NoApproverNote {
/** The `sys_approval_action` row (an `action: 'comment'` with no actor) this note is. */
readonly action_id: string;
/** The slate's literals, as `type 'value'`, which a member must hold; absent when it kept none. */
/** The slate's literals, as `type:value`, which a member must hold; absent when it kept none. */
readonly slots?: readonly string[];
/** A note written before `slots`: the literals as `type 'value'`, read as stored. */
readonly who?: string;
}

/** What the literals on `slate` ask a member to hold, or `undefined` when it holds none. */
export function noApproverWho(slate: readonly string[]): string | undefined {
const named = slate
.filter((slot) => !!slot && slot.includes(':') && !isPlatformAdminAddress(slot))
.map((slot) => `${slot.slice(0, slot.indexOf(':'))} '${slot.slice(slot.indexOf(':') + 1)}'`);
/** The literals on `slate` a member must hold, as `type:value`, or `undefined` when it holds none. */
export function noApproverSlots(slate: readonly string[]): string[] | undefined {
const slots = slate.filter((slot) => !!slot && slot.includes(':') && !isPlatformAdminAddress(slot));
return slots.length ? slots : undefined;
}

/**
* What the literals on `slate` ask a member to hold, as `type 'value'`, or
* `undefined` when it holds none. [#36035] With `positionLabel`, a position's
* value is the label it answers; with none, or for a position it does not
* know, the position's name stands, which is the English the row stores.
*/
export function noApproverWho(slate: readonly string[], positionLabel?: PositionLabelOf): string | undefined {
const named = (noApproverSlots(slate) ?? []).map((slot) => {
const type = slot.slice(0, slot.indexOf(':'));
const value = slot.slice(slot.indexOf(':') + 1);
return `${type} '${(type === 'position' && positionLabel?.(value)) || value}'`;
});
return named.length ? named.join(', ') : undefined;
}

/** [#36035] The position names a note's `slots` hold, each once; none for a note that stored `who`. */
export function noApproverPositions(note: Pick<NoApproverNote, 'slots'>): string[] {
return slatePositions(note.slots ?? []);
}

/** Does `slate` name no person other than `submitter`, so no one else can decide it? */
export function namesNoOneBut(slate: readonly string[], submitter: string): boolean {
return !slateNamesAPerson(slate.filter((slot) => slot !== submitter));
}

/** The catalog sentence for the note, and its parameters. */
export function noApproverSentence(note: Pick<NoApproverNote, 'who'>): {
/**
* The catalog sentence for the note, and its parameters. A note's `slots` are
* composed here, each position named by `positionLabel` where it knows one; a
* note that stored `who` keeps it as written.
*/
export function noApproverSentence(note: Pick<NoApproverNote, 'slots' | 'who'>, positionLabel?: PositionLabelOf): {
id: ApprovalMessageId; params: Record<string, string>;
} {
return note.who
? { id: 'approvalNoApproverHolding', params: { who: note.who } }
const who = note.slots ? noApproverWho(note.slots, positionLabel) : note.who;
return who
? { id: 'approvalNoApproverHolding', params: { who } }
: { id: 'approvalNoApprover', params: {} };
}

/**
* The note's text. With no `render`, the English the row stores and the
* refusal says; with one, the reader's language, or `undefined` when the pack
* does not hold the sentence (the stored comment then stands).
*
* [#36035] With `positionLabel` too, a position the note's `slots` name is
* named by the label it answers, as `adminRescueNoteText` names one. The
* stored English keeps the name: it is the audit text.
*/
export function noApproverNoteText(note: Pick<NoApproverNote, 'who'>): string;
export function noApproverNoteText(note: Pick<NoApproverNote, 'who'>, render: ApprovalSentenceRender): string | undefined;
export function noApproverNoteText(note: Pick<NoApproverNote, 'slots' | 'who'>): string;
export function noApproverNoteText(
note: Pick<NoApproverNote, 'slots' | 'who'>,
render: ApprovalSentenceRender,
positionLabel?: PositionLabelOf,
): string | undefined;
export function noApproverNoteText(
note: Pick<NoApproverNote, 'who'>,
note: Pick<NoApproverNote, 'slots' | 'who'>,
render?: ApprovalSentenceRender,
positionLabel?: PositionLabelOf,
): string | undefined {
const { id, params } = noApproverSentence(note);
const { id, params } = noApproverSentence(note, render ? positionLabel : undefined);
return render ? render(approvalMessageKey(id), params) : interpolate(enMessages[id], params);
}

Expand All @@ -83,7 +125,11 @@ export function noApproverNoteOf(snapshot: unknown): NoApproverNote | undefined
if (!snapshot || typeof snapshot !== 'object') return undefined;
const note = (snapshot as Record<string, unknown>)[NO_APPROVER_NOTE_SNAPSHOT_KEY];
if (!note || typeof note !== 'object') return undefined;
const { action_id, who } = note as Record<string, unknown>;
const { action_id, slots, who } = note as Record<string, unknown>;
if (typeof action_id !== 'string' || !action_id) return undefined;
if (slots !== undefined) {
if (!Array.isArray(slots) || !slots.every((slot) => typeof slot === 'string')) return undefined;
return slots.length ? { action_id, slots } : { action_id };
}
return typeof who === 'string' && who ? { action_id, who } : { action_id };
}
Loading