From ba2e6880fc16dfc3ae2726f5d61ad483ab98a3fc Mon Sep 17 00:00:00 2001 From: Michael Gilardi <11632652+mgilardi@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:17:26 -0700 Subject: [PATCH] Send alert email only when canSend answers exactly true A truthy answer such as 'yes' or 1 from the host's canSend used to send email. Like every other host answer in the plugins, only an exact true counts now; anything else records the alert as not sent with "Email is off here." Found by Whitehat's host-answer lint. Tested: two new tests (stock alerts over five truthy answers, ready-stock alerts with 'yes') fail without the change and pass with it; full suite and typecheck pass. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 ++ README.md | 2 +- src/readystock.ts | 2 +- src/stock.ts | 2 +- test/kitting.test.ts | 23 +++++++++++++++++++++-- 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7dbc34..5ee0999 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- **`alerts.canSend` must answer exactly `true` to send.** A truthy answer such as `'yes'` or `1` used to send email; now it counts as email off, and the alert is recorded as not sent. + - **Build at packing, straight into one order:** `buildUnits(payload, recipe, ids, { scope, assignTo: 'Order 1042' })`. The new units go to that order first. They fill its waiting build request, or take the place of units kept for it, which go back to ready and can fill other waiting requests. An order holding nothing of the recipe, including one whose units have all shipped, is refused with `nothingReserved`, and nothing is built. The result's `assigned` says which unit went where. Agreed with Kits for its packing station. - **Tested with Payload's multi-tenant plugin:** `scopeField: 'tenant'` with `@payloadcms/plugin-multi-tenant` 3.92.1 adding the field (dev-only). Startup refuses when a Kitting collection is left out of its list, and the per-site rules and its access hold. No code changes. diff --git a/README.md b/README.md index 2eb8dc3..39875b6 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ With `scopeField`, a recipe key needs its site: `{ key, scope }`. `{ id }` works | `lots.warningDays` | `14` | How soon before its use-by date a lot shows under "Use soon". Items can set their own | | `lots.autoWriteOff` | `true` | Write off expired lots when the overview opens | | `alerts.recipients` | none | A list, or `({ payload, item }) => string[]` (`item.scope` is the site) | -| `alerts.canSend` | always | Return false where email is off, for example in development | +| `alerts.canSend` | always | Return false where email is off, for example in development. Only an exact `true` sends | | `alerts.sendEmail` | `payload.sendEmail` | Your own delivery | | `alerts.render` | a short plain message | Your own subject, HTML and text | | `overrides` | none | Change each collection before it's added, for example to add a tenant field or hooks | diff --git a/src/readystock.ts b/src/readystock.ts index 21ac3b1..b66bbc5 100644 --- a/src/readystock.ts +++ b/src/readystock.ts @@ -148,7 +148,7 @@ export async function sendStockAlerts(payload: Payload): Promise<{ sent: number; } async function deliverStock(payload: Payload, o: ResolvedSupplies, recipe: Doc): Promise<{ status: AlertStatus; note: string | null }> { - if (!(await o.canSend({ payload }))) return { status: 'not-sent', note: 'Email is off here.' } + if ((await o.canSend({ payload })) !== true) return { status: 'not-sent', note: 'Email is off here.' } const status = await readyStatus(payload, recipe) if (!status.state || status.state === 'ok') return { status: 'not-sent', note: 'Back to OK before it was sent.' } const scope = o.scopeField ? idOf(recipe[o.scopeField]) : null diff --git a/src/stock.ts b/src/stock.ts index ce826e8..ea1036e 100644 --- a/src/stock.ts +++ b/src/stock.ts @@ -598,7 +598,7 @@ export function defaultAlertEmail({ item, adminURL }: { item: AlertItem; adminUR } async function deliver(payload: Payload, o: ResolvedSupplies, item: Doc, req?: Req): Promise<{ status: AlertStatus; note: string | null }> { - if (!(await o.canSend({ payload }))) return { status: 'not-sent', note: 'Email is off here.' } + if ((await o.canSend({ payload })) !== true) return { status: 'not-sent', note: 'Email is off here.' } const alertItem = await alertItemOf(payload, o, item, req) const to = [...new Set((Array.isArray(o.recipients) ? o.recipients : await o.recipients({ payload, item: alertItem })).map((a) => a.trim()).filter(Boolean))] if (!to.length) return { status: 'not-sent', note: 'No alert addresses are set.' } diff --git a/test/kitting.test.ts b/test/kitting.test.ts index bbcf2f9..e57503c 100644 --- a/test/kitting.test.ts +++ b/test/kitting.test.ts @@ -14,7 +14,7 @@ let mongo: MongoMemoryReplSet const mail: { to: string[]; subject: string; text: string }[] = [] const onBuilt: string[] = [] let refuseBuild = '' -let emailOn = true +let emailOn: unknown = true const ids = (v: unknown) => ((v as unknown[]) ?? []).map((t) => (t && typeof t === 'object' ? String((t as Doc).id) : String(t))) /** Site staff manage their own sites; viewers only read; admins everything. */ @@ -60,7 +60,7 @@ beforeAll(async () => { access: { manage: siteAccess(['staff']), read: siteAccess(['staff', 'viewer']) }, alerts: { recipients: ({ item }) => [`stock-${String(item.scope)}@example.com`], - canSend: () => emailOn, + canSend: () => emailOn as boolean, sendEmail: async ({ to, subject, text }) => void mail.push({ to, subject, text }), }, overrides: { items: withTenant, suppliers: withTenant, movements: withTenant, recipes: withTenant, lots: withTenant, units: withTenant, requests: withTenant, boxes: withTenant, postage: withTenant, settings: withTenant }, @@ -264,6 +264,16 @@ describe('low-stock email', () => { expect(mail).toHaveLength(0) }) + it('sends only on an exact true from canSend: a truthy answer leaves email off', async () => { + for (const answer of ['yes', 1, {}, [], 'false']) { + const { swab, key } = await setup(siteA, { swab: 10, tube: 100 }, { swab: 10 }) + emailOn = answer + await applyRecipe(payload, { key }, 1, { scope: siteA.id }) + expect(mail).toHaveLength(0) + expect(await get('supply-items', swab.id)).toMatchObject({ alertStatus: 'not-sent', alertNote: 'Email is off here.' }) + } + }) + it('alerts when the level is raised above what is on hand', async () => { const { tube } = await setup(siteA, { swab: 10, tube: 5 }) await payload.update({ collection: 'supply-items' as never, id: tube.id, data: { lowStockLevel: 6 } as never, ...as(staffA) }) @@ -837,6 +847,15 @@ describe('ready-stock states and their emails', () => { expect(await get('supply-recipes', recipe.id)).toMatchObject({ stockState: 'build-more', stockAlertStatus: 'not-sent', stockAlertNote: 'Email is off here.' }) expect(mail.filter((m) => m.subject.includes('Quiet kit'))).toHaveLength(0) }) + + it('records ready-stock email as not sent when canSend answers truthy but not true', async () => { + emailOn = 'yes' + const swab = await create('supply-items', { tenant: siteA.id, name: 'Truthy swab', unit: 'each', openingStock: 10 }) + const recipe = await create('supply-recipes', { tenant: siteA.id, name: 'Truthy kit', key: `tk-${Math.random().toString(36).slice(2, 8)}`, lines: [{ item: swab.id, quantity: 1 }], readyStock: { baseline: 2 } }) + await sendPendingAlerts(payload) + expect(await get('supply-recipes', recipe.id)).toMatchObject({ stockState: 'build-more', stockAlertStatus: 'not-sent', stockAlertNote: 'Email is off here.' }) + expect(mail.filter((m) => m.subject.includes('Truthy kit'))).toHaveLength(0) + }) }) describe('box suggestions', () => {