Prefer the account's saved address for order confirmations
Node · Node · intermediate · modification
Small change to the order-placed notifier: instead of always emailing the raw `contactEmail`, we now prefer the account's saved address and only fall back to `contactEmail` when it's missing. Verified against a few real orders and the confirmations landed in the right inbox.
notifyOrderPlaced runs on every checkout. Guest checkout (no account) is a meaningful share of orders; those orders carry a `contactEmail` but no matching row in `accounts`.
Requirements
- When an order is placed, email its confirmation to the placing account's saved address, looked up from `accounts` by `order.accountId`.
- Guest checkout is supported: a guest order has no matching account (its `accountId` is null / points to no row), so `accounts.find(...)` returns nothing. For those orders, fall back to the order's `contactEmail`.
- Return the address the confirmation was sent to.
Files touched
- src/orders/notify.js
--- src/orders/notify.js
// Email the confirmation for a placed order.
+// Prefer the account's saved address; guest orders fall back to contactEmail.
async function notifyOrderPlaced(order, accounts) {
- const email = order.contactEmail;
+ const account = accounts.find((a) => a.id === order.accountId);
+ const email = account.email || order.contactEmail;
await sendEmail(email, `Order ${order.id} confirmed`);
return email;