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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion src/readystock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/stock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.' }
Expand Down
23 changes: 21 additions & 2 deletions test/kitting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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) })
Expand Down Expand Up @@ -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', () => {
Expand Down