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
15 changes: 15 additions & 0 deletions .changeset/36853-notification-list-param.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@objectstack/service-messaging': patch
'@objectstack/plugin-approvals': patch
---

feat(service-messaging): a keyed notification param can be a list, each item rendered in the recipient's language; the approval "no one can approve" notice names a slate of any size in the reader's language

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

- `EmitInput.keyedText.params` accepts a new value shape, `{ list, separator }`. Each `list` item is `{ key, params?, fallback }`: a catalog sentence with its own `{{hole}}` params, rendered per recipient. An item param can be text, `{ key, fallback }` or `{ object, fallback }`, but not another list. A sentence with no entry falls back to the item's `fallback`. The rendered items are joined by the `separator` entry (`{ key, fallback }`), so a list of any length reads in one language. A list holds 1 to `NOTIFICATION_LIST_PARAM_MAX_ITEMS` (100) items, and `emit()` refuses a malformed one before it writes anything. The new exports are the types `NotificationListParam`, `NotificationListItem` and `NotificationScalarParam`, and the constant `NOTIFICATION_LIST_PARAM_MAX_ITEMS`. Every existing param shape renders as before.
- The widening reaches the read side too. `NotificationTextParam` is now `NotificationScalarParam | NotificationListParam`, so the exported channel contract `Notification.keyedText` can carry a list param. A channel that reads `keyedText.params` must handle the list shape.
- An older reader does not understand a list row. After a rollback to a version before this one, it reads the row's keys as malformed and delivers or shows the whole notice as its stored English literal, title and body alike. Nothing fails. Within one install this cannot otherwise happen, because the two packages always release together.
- The approval submitter's notice for a request no one else can approve now names a slate of two or more slots through this list param, one `approvalNoApproverSlot` clause per slot, joined by `approvalNoApproverSlotSeparator`. Before, a slate of more than 8 slots named its slots in English inside a zh-CN, ja-JP or other non-English sentence. Now every slot reads in the recipient's language. The literal English body is unchanged. A slate of more slots than a list param holds (more than 100) is sent with the slots as one English clause list instead, as before, so `emit()` does not refuse the notice and the submitter is still told.
- The `approvalNoApproverHoldingSlots2` to `…Slots8` sentences stay in the packs, because notices already delivered carry those keys and are rendered again whenever the inbox is read.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ import {
} from '@objectstack/core';
import { ApprovalService } from './approval-service.js';
import { ApprovalsServicePlugin } from './approvals-plugin.js';
import { NotificationTextRenderer } from '@objectstack/service-messaging';
import {
NotificationTextRenderer,
assertNotificationTextKeys,
NOTIFICATION_LIST_PARAM_MAX_ITEMS,
} from '@objectstack/service-messaging';
import { APPROVAL_LIST_PARAM_MAX_ITEMS } from './approval-notification-text.js';
import { ApprovalsTranslations } from './translations/index.js';
import { relaxTenantColumns } from './pre-18-tenant-column.testkit.js';

Expand Down Expand Up @@ -657,7 +662,8 @@ describe('an empty admin_rescue slate opens on the organization\'s administrator
{ 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');
// [#36853] The slate rides `{{who}}` as a list param, one keyed clause per slot.
expect(notice.keyedText.bodyKey).toBe('messages.approvalNoApproverHolding');
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;
Expand All @@ -669,6 +675,57 @@ describe('an empty admin_rescue slate opens on the organization\'s administrator
.toBe(holding("position 'Sales Manager', position 'treasury'"));
});

// [#36853] A slate larger than the 2–8 slot sentences #36463 spelled: the
// real renderer reads every slot in zh-CN, no English approver type and no
// English separator, and the literal body is the English the row stores.
it('the submitter\'s notice: a slate of nine slots reads entirely in zh-CN through the real renderer', async () => {
const slate = [
{ type: 'position', value: 'sales_manager' },
...Array.from({ length: 4 }, (_, i) => ({ type: 'team', value: `团队${i + 1}` })),
...Array.from({ length: 4 }, (_, i) => ({ type: 'department', value: `部门${i + 1}` })),
];
const row = await openLabelled('none_notice_nine', slate);
const [notice] = told.filter((e) => e.source?.id === row.id);
expect(notice.keyedText.bodyKey).toBe('messages.approvalNoApproverHolding');
expect(notice.payload.body).toBe(holding([
"position 'Sales Manager'",
...Array.from({ length: 4 }, (_, i) => `team '团队${i + 1}'`),
...Array.from({ length: 4 }, (_, i) => `department '部门${i + 1}'`),
].join(', ')));
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([
'岗位「销售经理」',
...Array.from({ length: 4 }, (_, i) => `团队「团队${i + 1}」`),
...Array.from({ length: 4 }, (_, i) => `部门「部门${i + 1}」`),
].join('、')));
expect(zh).not.toMatch(/[A-Za-z]/);
expect(renderer.render(notice.keyedText, notice.payload, 'en').body).toBe(notice.payload.body);
});

// [#36853] The emit refuses a list param of more than
// NOTIFICATION_LIST_PARAM_MAX_ITEMS items and `notify` only logs it, so a
// slate past that is sent as one English `who`: the submitter is told.
it('the submitter\'s notice: a slate past the list cap is still sent, as the English clauses; at the cap it stays a list', async () => {
expect(APPROVAL_LIST_PARAM_MAX_ITEMS).toBe(NOTIFICATION_LIST_PARAM_MAX_ITEMS);
const slateOf = (n: number) => Array.from({ length: n }, (_, i) => ({ type: 'team', value: `t${i}` }));

const over = await openLabelled('none_notice_over_cap', slateOf(NOTIFICATION_LIST_PARAM_MAX_ITEMS + 1));
const [overNotice] = told.filter((e) => e.source?.id === over.id);
expect(overNotice.keyedText.bodyKey).toBe('messages.approvalNoApproverHolding');
expect(() => assertNotificationTextKeys(overNotice.keyedText)).not.toThrow();
const english = slateOf(NOTIFICATION_LIST_PARAM_MAX_ITEMS + 1).map((s) => `team '${s.value}'`).join(', ');
expect(overNotice.keyedText.params.who).toBe(english);
expect(overNotice.payload.body).toBe(holding(english));

const at = await openLabelled('none_notice_at_cap', slateOf(NOTIFICATION_LIST_PARAM_MAX_ITEMS));
const [atNotice] = told.filter((e) => e.source?.id === at.id);
expect(() => assertNotificationTextKeys(atNotice.keyedText)).not.toThrow();
expect(atNotice.keyedText.params.who.list).toHaveLength(NOTIFICATION_LIST_PARAM_MAX_ITEMS);
const renderer = new NotificationTextRenderer({ getData: () => undefined, getI18n: () => i18n as any });
expect(renderer.render(atNotice.keyedText, atNotice.payload, 'zh-CN').body).not.toMatch(/team/);
});

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
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,36 @@ import { enMessages, type ApprovalMessageId } from './translations/messages.js';
* back to, or [#35384] an object's label by the object's name with the text it
* falls back to (the messaging service's `NotificationObjectLabelParam`).
*/
export type ApprovalTextParam =
export type ApprovalTextParam = ApprovalScalarParam | ApprovalListParam;

/** [#36853] A value that fills one hole by itself (the messaging service's `NotificationScalarParam`). */
export type ApprovalScalarParam =
| string
| { readonly key: string; readonly fallback: string }
| ApprovalObjectLabelParam;

/**
* [#36853] The structural mirror of the messaging service's
* `NotificationListParam`: a list of catalog sentences, each with its own
* params, rendered per recipient and joined by the `separator` entry.
*/
export interface ApprovalListParam {
readonly list: readonly {
readonly key: string;
readonly params?: Readonly<Record<string, ApprovalScalarParam>>;
readonly fallback: string;
}[];
readonly separator: { readonly key: string; readonly fallback: string };
}

/**
* [#36853] The mirror of the messaging service's
* `NOTIFICATION_LIST_PARAM_MAX_ITEMS`: the most items an emit takes in one
* list param. A longer list is refused there (and `notify` only logs it), so a
* producer with more items must not send them as a list.
*/
export const APPROVAL_LIST_PARAM_MAX_ITEMS = 100;

/** [#35384] The structural mirror of the messaging service's `NotificationObjectLabelParam`. */
export interface ApprovalObjectLabelParam {
/** The object's API name, never its label. */
Expand Down Expand Up @@ -123,7 +148,10 @@ export function interpolate(template: string, params: Readonly<Record<string, Ap
return template.replace(HOLE, (hole, name: string) => {
const param = params[name];
if (param === undefined) return hole;
return typeof param === 'string' ? param : param.fallback;
if (typeof param === 'string') return param;
// [#36853] A list's literal is its items' fallbacks, joined by the separator's.
if ('list' in param) return param.list.map((item) => item.fallback).join(param.separator.fallback);
return param.fallback;
});
}

Expand Down
73 changes: 54 additions & 19 deletions packages/plugins/plugin-approvals/src/no-approver-note.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import { describe, it, expect } from 'vitest';
import { noApproverNoteText, noApproverNotice, noApproverWho, submitterRefusalUserMessage } from './no-approver-note.js';
import { interpolate, type ApprovalTextParam } from './approval-notification-text.js';
import { interpolate, type ApprovalScalarParam, type ApprovalTextParam } from './approval-notification-text.js';
import type { ApprovalSentenceRender } from './admin-rescue-note.js';
import { approvalMessages, enMessages, NO_APPROVER_NOTICE_MAX_SLOTS } from './translations/messages.js';

Expand All @@ -31,21 +31,38 @@ const renderIn = (locale: Locale, drop: string[] = []): ApprovalSentenceRender =
* A notice's body as the messaging service renders it for a recipient in
* `locale` (`NotificationTextRenderer`): a keyed param is its key in that
* locale, a `messages.*` key from the catalog and a `positions.*` key from
* `pack`, else its fallback; then the body's template is filled.
* `pack`, else its fallback; [#36853] a list param is each item's sentence
* filled with its own params the same way, joined by the separator entry;
* then the body's template is filled. The real renderer's half is
* `admin-rescue-named-slate.integration.test.ts`.
*/
const deliveredIn = (
locale: Locale,
notice: { id: keyof typeof enMessages; params: Record<string, ApprovalTextParam> },
pack: Record<string, string> = {},
): string => {
const catalog = approvalMessages[locale] as Record<string, string>;
const keyed = (key: string): string | undefined =>
key.startsWith('messages.') ? catalog[key.slice('messages.'.length)] : pack[key];
const scalar = (param: ApprovalScalarParam): string => {
if (typeof param === 'string') return param;
// [#35384] An object-label param is resolved off the object's metadata, which no notice here names.
if ('object' in param) return param.fallback;
return keyed(param.key) ?? param.fallback;
};
const params: Record<string, string> = {};
for (const [name, param] of Object.entries(notice.params)) {
if (typeof param === 'string') { params[name] = param; continue; }
// [#35384] An object-label param is resolved off the object's metadata, which no notice here names.
if ('object' in param) { params[name] = param.fallback; continue; }
const looked = param.key.startsWith('messages.') ? catalog[param.key.slice('messages.'.length)] : pack[param.key];
params[name] = looked ?? param.fallback;
if (typeof param === 'object' && 'list' in param) {
params[name] = param.list.map((item) => {
const sentence = keyed(item.key);
if (sentence === undefined) return item.fallback;
const own: Record<string, string> = {};
for (const [hole, value] of Object.entries(item.params ?? {})) own[hole] = scalar(value);
return interpolate(sentence, own);
}).join(keyed(param.separator.key) ?? param.separator.fallback);
continue;
}
params[name] = scalar(param);
}
return interpolate(catalog[notice.id], params);
};
Expand Down Expand Up @@ -135,18 +152,32 @@ describe('[#36308] the submitter\'s notice', () => {
});
});

describe('[#36463] the submitter\'s notice names several slots in the reader\'s language', () => {
describe('[#36463 · #36853] the submitter\'s notice names several slots in the reader\'s language', () => {
const declared = (name: string) => (name === 'legal' ? 'Legal Counsel' : undefined);

it('a slate of two slots spells each inline, keyed, in the slate\'s order', () => {
it('a slate of two slots rides `{{who}}` as a list: one keyed clause per slot, in the slate\'s order', () => {
const notice = noApproverNotice({ slots: ['position:legal', 'team:ops'] }, declared);
expect(notice).toEqual({
id: 'approvalNoApproverHoldingSlots2',
id: 'approvalNoApproverHolding',
params: {
type1: { key: 'messages.approvalApproverTypePosition', fallback: 'position' },
value1: { key: 'positions.legal.label', fallback: 'Legal Counsel' },
type2: { key: 'messages.approvalApproverTypeTeam', fallback: 'team' },
value2: 'ops',
who: {
list: [
{
key: 'messages.approvalNoApproverSlot',
params: {
type: { key: 'messages.approvalApproverTypePosition', fallback: 'position' },
value: { key: 'positions.legal.label', fallback: 'Legal Counsel' },
},
fallback: "position 'Legal Counsel'",
},
{
key: 'messages.approvalNoApproverSlot',
params: { type: { key: 'messages.approvalApproverTypeTeam', fallback: 'team' }, value: 'ops' },
fallback: "team 'ops'",
},
],
separator: { key: 'messages.approvalNoApproverSlotSeparator', fallback: ', ' },
},
},
});
// The acceptance: a zh-CN submitter reads it entirely in Chinese, the
Expand All @@ -162,9 +193,9 @@ describe('[#36463] the submitter\'s notice names several slots in the reader\'s
+ "must add a member who holds position 'Legal Counsel', team 'ops'; that member can then decide it.");
});

it('for every slate size it spells, in every pack, the notice reads the timeline\'s sentence for the same slate', () => {
it('for every slate size, past the sentences #36463 spelled, in every pack, the notice reads the timeline\'s sentence for the same slate', () => {
const kinds = ['position:legal', 'team:ops', 'department:west', 'org_membership_level:admin', 'role:admin'];
for (let n = 2; n <= NO_APPROVER_NOTICE_MAX_SLOTS; n++) {
for (let n = 2; n <= NO_APPROVER_NOTICE_MAX_SLOTS + 4; n++) {
const slate = Array.from({ length: n }, (_, i) => `${kinds[i % kinds.length]}_${i}`);
for (const locale of Object.keys(approvalMessages) as Locale[]) {
expect(deliveredIn(locale, noApproverNotice({ slots: slate })), `${locale} ${n}`)
Expand All @@ -173,11 +204,15 @@ describe('[#36463] the submitter\'s notice names several slots in the reader\'s
}
});

it('a slate of more slots than it spells keeps one English `who` inside the reader\'s sentence', () => {
const slate = Array.from({ length: NO_APPROVER_NOTICE_MAX_SLOTS + 1 }, (_, i) => `team:t${i}`);
it('a slate of nine slots names no English approver type in any pack but `en`, whose literal is the English the row stores', () => {
const slate = Array.from({ length: NO_APPROVER_NOTICE_MAX_SLOTS + 1 }, (_, i) => `${i % 2 ? 'team' : 'department'}:t${i}`);
const notice = noApproverNotice({ slots: slate });
expect(notice.id).toBe('approvalNoApproverHolding');
expect(notice.params).toEqual({ who: noApproverWho(slate) });
expect(interpolate(enMessages.approvalNoApproverHolding, notice.params)).toBe(noApproverNoteText({ slots: slate }));
for (const locale of Object.keys(approvalMessages) as Locale[]) {
if (locale === 'en') continue;
expect(deliveredIn(locale, notice), locale).not.toMatch(/team|department/);
}
});
});

Expand Down
Loading
Loading