Skip to content

Commit f5e6709

Browse files
fix(service-messaging): declare currency on EmailSenderSurface template inputs (#28523)
<!-- claude-projects-attribution: {"github_login":"os-project-manager"} --> _Requested by **Leehom** · [project thread](https://claude.ai/code/project/chan_01TenzsRopZj4jMbvjX8uz1X?thread=cmsg_01TenzsRopZj4jMbvjX8uz1XPJsT4EEFrtYn1kzHjMVaWz)_ Fixes #27754 Clause-②: yes (widening) Metadata-protocol: none ## 维护者速读 - **改了什么**:消息服务对外导出的 `EmailSenderSurface` 类型里,`sendTemplate` 和 `renderTemplate` 的入参补上可选的 `currency` 字段。邮件渠道和站内信渠道早就在传这个字段(租户默认币种,以及读取失败后试渲染用的 `XXX`),现在改成按名字赋值,不再靠对象展开塞进去。 - **为什么改**:#27730 的合约复核(6509670944,S3)指出:字段是靠展开写法传的,TypeScript 不检查,类型上看不出渠道会传币种,两边任何一边改名都不会编译报错。 - **风险与回滚**:只是类型扩面,运行时行为不变:没有默认币种时仍然不带 `currency` 键,有就原样传。字段可选,已有的第三方实现不用改。回滚即 revert。 - **先看**:`packages/services/service-messaging/src/email-channel.ts`(接口两处声明与 `sendInput` 的赋值);`packages/services/service-messaging/src/inbox-channel.ts`(`renderInput` 的赋值)。 - **两分钟验证**:`pnpm --filter @objectstack/service-messaging exec vitest run src/template-tenant-currency.test.ts` → 22 passed,其中「无默认币种时不带 `currency` 键」的断言原样通过。 - **需要你做的**:无,席位落地(发布类型扩面,席位先做合约评审)。 ## What changes - **`EmailSenderSurface` is published.** `packages/services/service-messaging/src/index.ts:64` re-exports it (`export type { EmailChannelOptions, EmailSenderSurface } from './email-channel.js'`), and the package is public (`@objectstack/service-messaging` 18.2.0, `files: ["dist", …]`). The card's "not re-exported" was measured false, so this is `Clause-②: yes (widening)` per the order's ruling: one optional member on each of two input types, nothing refused that compiled before. - **The declarations.** `sendTemplate`'s and `renderTemplate`'s inputs gain `currency?: string`, documented as the structural mirror of `RenderTemplateInput.currency` (the default a no-argument `{{ amount | currency }}` hole formats in, #26939). - **The call sites name the key.** The email channel builds its `sendTemplate` input as a typed `TemplateSendInput` and sets `if (currency) sendInput.currency = currency;`; the inbox channel does the same with `TemplateRenderInput` for `renderTemplate`. Both aliases are module-private (each is the parameter type of the surface's own member), so no export name is added. The trial render on both channels writes `currency: probe` as a plain property instead of `...{ currency: probe }`. - **Why an assignment and not `currency,` in the literal.** "No default sends no key" is pinned: `template-tenant-currency.test.ts` asserts `not.toHaveProperty('currency')` at lines 111, 161, 253, 313 and 378, and a literal `currency: undefined` is an own property. A conditional spread (`...(currency ? { currency } : {})`) keeps the key out but is exactly the shape TypeScript does not check: measured in a scratch file under `--strict`, a spread of `{ currency }` (conditional or not) into a call whose input names `currencyX` compiles, while the plain property and the property assignment both fail. So the key is set by name, after the literal. - **No behaviour change.** Same keys sent in every case (absent stays absent, a default is passed as is, the probe passes `XXX`); only the `sendTemplate` input's key order moves (`currency` now after `organizationId`), which no caller or test reads. ## Tests - **Pinned by typecheck, as an ablation (six legs).** Committed first (`88a517da`), each leg through `node scripts/ablation-replace.mjs` (anchor must hit, blob proven moved, restored to the HEAD blob, `git diff HEAD` empty after each), each running `tsc --noEmit` in `packages/services/service-messaging`: - L1 type side, `sendTemplate`'s `currency?` renamed `currencyCode?` → `email-channel.ts(438,41): error TS2339: Property 'currency' does not exist on type …`, tsc exit 2. - L2 channel side, `sendInput.currency` renamed → `email-channel.ts(438,41): error TS2551: Property 'currencyCode' does not exist … Did you mean 'currency'?`, exit 2. - L3 type side, `renderTemplate`'s `currency?` renamed → three errors: `email-channel.ts(466,33)` TS2353 (email probe), `inbox-channel.ts(220,43)` TS2339 (inbox first render), `inbox-channel.ts(240,33)` TS2353 (inbox probe), exit 2. - L4 channel side, `renderInput.currency` renamed → `inbox-channel.ts(220,43): error TS2551`, exit 2. - L5 channel side, inbox probe `currency: probe` renamed → `inbox-channel.ts(240,33): error TS2561`, exit 2. - L6 channel side, email probe `currency: probe` renamed → `email-channel.ts(466,33): error TS2561`, exit 2. - Every restore: blob after restore == blob at HEAD (`c1d8f83e1eb7` email, `53d4cbd196b9` inbox). - **Existing suites, unchanged and green.** On `88a517da` under `os-verify-lock`: `pnpm --filter @objectstack/service-messaging typecheck` and `test` → VERDICT command-exit 0, Test Files 62 passed (62), Tests 759 passed (759). No test file is edited. ## Acceptance notes - The probe calls keep their other conditional spreads (`data`, `locale`, `organizationId`); the card and the order scope this to `currency`. ## Verification All readings on head `88a517da` (the only commit; base `origin/main` `23d1303d`). - **Derived gates.** `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack` named 56 commands; each run with its exit code captured before any pipe, then reconciled: `✓ dispatch-gates --ran: 56 derived famil(ies) accounted for — 55 run, 1 NOT-MEASURED (1 DERIVED from a recorded exit 3).` - NOT MEASURED: `pnpm check:dual-build-cjs-loads` (exit 3, PREREQUISITE NOT MET: needs every package's `dist`, a full build). CI's lint job runs it. - `node scripts/check-plugin-teardown-shape.mjs --self-test` first answered exit 3 (shallow clone, positive control unreachable); after `git fetch --depth=1 origin 621a487…` it answered exit 0, 48 cases pass. - `pnpm check:i18n` first answered exit 3 (CLI not built); after the gate's own one-command prerequisite build (under the lock, VERDICT command-exit 0) it answered exit 0: `check-i18n-bundles: OK (10 package(s) — all bundles in sync, no undeclared authoring keys).` - The four roster gates marked as rostered under a touched directory also ran, each exit 0: `check:authz-resolver`, `check:changeset-fixed`, `check:error-code-casing`, `check:filter-alias-parity`. - **Lint.** `pnpm exec eslint --no-inline-config packages/services/service-messaging` → exit 0, no findings. - **Package.** `pnpm --filter @objectstack/service-messaging typecheck` + `test` under `os-verify-lock` (after the `^...` closure build, VERDICT command-exit 0) → VERDICT command-exit 0; 62 files, 759 tests passed. - **Changeset.** `patch` on `@objectstack/service-messaging`: the changed type ships in `dist/index.d.ts` (the entry re-exports it), so `skip-changeset` does not fit. `Clause-②: yes (widening)` and `Metadata-protocol: none` are line-initial here and in `.changeset/27754-email-sender-surface-currency.md`. No `packages/spec/**`. - **Open PRs touching these files.** None of the 19 open PRs touches `email-channel.ts` or `inbox-channel.ts` (each PR's `/files` read before `pr_create`). - **CI.** Every family CI runs is read from this PR's check-runs by the seat. --- _Generated by [Claude Code](https://claude.ai/code/session_01FEkQ5mAGmMrWhFqtKfMqYt)_ Co-authored-by: Claude <noreply@anthropic.com>
1 parent bff315c commit f5e6709

3 files changed

Lines changed: 58 additions & 20 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/service-messaging': patch
3+
---
4+
5+
fix(service-messaging): `EmailSenderSurface.sendTemplate` and `renderTemplate` declare the optional `currency` the email and inbox channels already send
6+
7+
Clause-②: yes (widening)
8+
Metadata-protocol: none
9+
10+
- **Before.** Both channels passed the organization's default currency to the email service's template render (#26939), and the trial render that grades a refusal after a failed currency read passed `XXX` (#27257), but the published `EmailSenderSurface` (re-exported from the package entry) named no `currency` on either input. The key reached the call only through an object spread, which TypeScript does not check against the target type, so a typed implementer of the surface was never told the key is sent and a rename on either side compiled.
11+
- **Now.** Both input types declare `currency?: string`, mirroring `RenderTemplateInput.currency`. The channels build the input by name and set `currency` with a plain assignment, and the trial render writes `currency: probe` as a plain property, so TypeScript checks the key on every call.
12+
- **Unchanged.** No default currency still sends no `currency` key; a default is still passed as is; the trial render still passes `XXX`. No delivery, grade or error text changes.

‎packages/services/service-messaging/src/email-channel.ts‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ export interface EmailSenderSurface {
5959
locale?: string;
6060
/** Same tenant stamp as on `send` (commit b706af987) — forwarded by the email service into the send it performs. */
6161
organizationId?: string;
62+
/**
63+
* Structural mirror of `RenderTemplateInput.currency` — the ISO 4217
64+
* code a `{{ amount | currency }}` hole with no argument formats in
65+
* (the organization's `localization.currency`, #26939). Absent when
66+
* the delivery has no tenant default; the renderer then refuses such
67+
* a hole with `TEMPLATE_INVALID` rather than guessing a currency.
68+
*/
69+
currency?: string;
6270
}): Promise<{ id?: string; status?: string; error?: string } | unknown>;
6371
/**
6472
* Structural mirror of `IEmailService.renderTemplate` (#9225) — resolves a
@@ -74,9 +82,14 @@ export interface EmailSenderSurface {
7482
template: string;
7583
data?: Record<string, unknown>;
7684
locale?: string;
85+
/** Same default currency as on `sendTemplate` (#26939). */
86+
currency?: string;
7787
}): Promise<{ subject: string; html: string; text: string }>;
7888
}
7989

90+
/** The input `EmailSenderSurface.sendTemplate` declares — the email channel builds it by name. */
91+
type TemplateSendInput = Parameters<NonNullable<EmailSenderSurface['sendTemplate']>>[0];
92+
8093
export interface EmailChannelOptions {
8194
/**
8295
* Resolve the email service; `undefined` ⇒ there is no transport, which
@@ -408,19 +421,25 @@ export function createEmailChannel(opts: EmailChannelOptions): MessagingChannel
408421
// currency, and grade a refusal below (#27257).
409422
currencyReadFault = (err as Error)?.message ?? String(err);
410423
}
424+
const sendInput: TemplateSendInput = {
425+
template: templateName,
426+
to: address,
427+
...(data !== undefined ? { data } : {}),
428+
...(templateLocale ? { locale: templateLocale } : {}),
429+
// Commit b706af987 — this channel HOLDS the organization (the
430+
// tenant stamp the outbox snapshots per delivery), so
431+
// it threads it for the sys_email.organization_id
432+
// stamp. Absent stays absent — never fabricated.
433+
...(n.organizationId ? { organizationId: n.organizationId } : {}),
434+
};
435+
// A plain assignment, not a spread: TypeScript checks the key
436+
// against the declared input, so a rename on either side fails
437+
// to compile. No default currency sends no key (#27754).
438+
if (currency) sendInput.currency = currency;
411439
try {
412-
const result = (await email.sendTemplate({
413-
template: templateName,
414-
to: address,
415-
...(data !== undefined ? { data } : {}),
416-
...(templateLocale ? { locale: templateLocale } : {}),
417-
...(currency ? { currency } : {}),
418-
// Commit b706af987 — this channel HOLDS the organization (the
419-
// tenant stamp the outbox snapshots per delivery), so
420-
// it threads it for the sys_email.organization_id
421-
// stamp. Absent stays absent — never fabricated.
422-
...(n.organizationId ? { organizationId: n.organizationId } : {}),
423-
})) as { id?: unknown; status?: unknown; error?: unknown } | undefined;
440+
const result = (await email.sendTemplate(sendInput)) as
441+
| { id?: unknown; status?: unknown; error?: unknown }
442+
| undefined;
424443
// `IEmailService.send` reports transport failure as
425444
// `status: 'failed'` rather than throwing — surface it.
426445
if (result && result.status === 'failed') {
@@ -444,7 +463,7 @@ export function createEmailChannel(opts: EmailChannelOptions): MessagingChannel
444463
template: templateName,
445464
...(data !== undefined ? { data } : {}),
446465
...(templateLocale ? { locale: templateLocale } : {}),
447-
...{ currency: probe },
466+
currency: probe,
448467
})),
449468
),
450469
};

‎packages/services/service-messaging/src/inbox-channel.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export const INBOX_OBJECT = 'sys_inbox_message';
1919
/** The receipt object the inbox channel writes a `delivered` row to (ADR-0030). */
2020
export const RECEIPT_OBJECT = 'sys_notification_receipt';
2121

22+
/** The input `EmailSenderSurface.renderTemplate` declares — the inbox channel builds it by name. */
23+
type TemplateRenderInput = Parameters<NonNullable<EmailSenderSurface['renderTemplate']>>[0];
24+
2225
export interface InboxChannelOptions {
2326
/**
2427
* Resolve the runtime data engine. Returns `undefined` when no data layer
@@ -206,13 +209,17 @@ export function createInboxChannel(opts: InboxChannelOptions): MessagingChannel
206209
// currency, and grade a refusal below (#27257).
207210
currencyReadFault = (err as Error)?.message ?? String(err);
208211
}
212+
const renderInput: TemplateRenderInput = {
213+
template: templateName,
214+
...(templateData !== undefined ? { data: templateData } : {}),
215+
...(templateLocale ? { locale: templateLocale } : {}),
216+
};
217+
// A plain assignment, not a spread: TypeScript checks the key
218+
// against the declared input, so a rename on either side fails
219+
// to compile. No default currency sends no key (#27754).
220+
if (currency) renderInput.currency = currency;
209221
try {
210-
const rendered = await email.renderTemplate({
211-
template: templateName,
212-
...(templateData !== undefined ? { data: templateData } : {}),
213-
...(templateLocale ? { locale: templateLocale } : {}),
214-
...(currency ? { currency } : {}),
215-
});
222+
const rendered = await email.renderTemplate(renderInput);
216223
title = rendered.subject;
217224
bodyMd = rendered.text;
218225
} catch (err) {
@@ -230,7 +237,7 @@ export function createInboxChannel(opts: InboxChannelOptions): MessagingChannel
230237
template: templateName,
231238
...(templateData !== undefined ? { data: templateData } : {}),
232239
...(templateLocale ? { locale: templateLocale } : {}),
233-
...{ currency: probe },
240+
currency: probe,
234241
}),
235242
),
236243
};

0 commit comments

Comments
 (0)