import fs from 'fs/promises'; import path from 'path'; import { select } from '@evershop/postgres-query-builder'; import { CONSTANTS } from '../../../../lib/helpers.js'; import { translate } from '../../../../lib/locale/translate/translate.js'; import { debug, error } from '../../../../lib/log/logger.js'; import { sendEmail } from '../../../../lib/mail/emailHelper.js'; import { pool } from '../../../../lib/postgres/connection.js'; import { getConfig } from '../../../../lib/util/getConfig.js'; import { getValue } from '../../../../lib/util/registry.js'; import { EventData } from '../../../../types/event.js'; import { getStoreLanguage } from '../../../setting/services/setting.js'; // Renders into the shared email layout (header/logo + footer) like the other // transactional emails, so the redesign is consistent across all of them. // Overridable per store via `system.notification_emails.order_canceled.templatePath`. const TEMPLATE = `{{#> emailLayout preheader=(t "Order #\${number} has been canceled." number=order.order_number)}}

{{t "Your order has been canceled"}}

{{t "Order #\${number}" number=order.order_number}}

{{t "This order has been canceled. If a payment was authorized, it has been released and you will not be charged."}}{{#if reason}} {{t "Reason: \${reason}." reason=reason}}{{/if}}

{{t "Order number"}}#{{order.order_number}}
{{t "Order total"}}{{currency order.grand_total}}
{{/emailLayout}}`; /** * Emails the customer when their order is canceled. Fires from `cancelOrder`, * which emits the dedicated `order_canceled` event carrying the cancellation * reason. * * Not for an order that was never placed (`placed_at` NULL): its payment was * abandoned or failed, the customer got no order confirmation either, and the * abandoned order crons (`cancelAbandonedOrder`) give its cart back, so "your * order is canceled" would only contradict an abandoned-cart reminder. The * admin still sees the cancellation on the order. */ export default async function sendCancellationEmail( data: EventData<'order_canceled'> ) { try { const config = getConfig('system.notification_emails.order_canceled', { enabled: true }); if (config?.enabled === false) { return; } const order = await select() .from('order') .where('order_id', '=', data.orderId) .load(pool); if (!order || !order.customer_email || !order.placed_at) { return; } let template = TEMPLATE; if (config?.templatePath) { const filePath = path.join(CONSTANTS.ROOTPATH, config.templatePath); try { await fs.access(filePath); template = await fs.readFile(filePath, 'utf8'); } catch { debug( `Cancellation email template not found at ${filePath}. Using default template.` ); } } const dynamicData = await getValue('orderCanceledEmailData', { order, reason: data.reason }); // Off-request subscriber (D7): resolve the store locale explicitly and pass // it so the subject and the body's currency helper agree. const locale = await getStoreLanguage(); const subject = translate('Your order has been canceled', {}, locale); const args = await getValue( 'orderCanceledEmailArguments', { to: order.customer_email, subject, template, data: dynamicData, locale }, { order } ); await sendEmail('order_canceled', args); } catch (e) { error(e); } }