Skip to content

Commit

Permalink
fix(emails): Fix getting email render function from event/status
Browse files Browse the repository at this point in the history
Additional debug for emails skip reasons
  • Loading branch information
leomp12 committed May 6, 2024
1 parent fd9aa9c commit 20e6575
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/apps/emails/src/event-to-emails.ts
Expand Up @@ -15,17 +15,26 @@ const handleApiEvent: ApiEventHandler = async ({
}) => {
const { action, resource } = apiEvent;
if (resource !== 'orders' || action === 'delete') {
warn(`Skipping ${resource} ${action}`);
return null;
}
const order = apiDoc as Orders;
if (!order.buyers?.length) {
warn('Skipping order document without buyer', { order });
return null;
}
const appData = { ...app.data, ...app.hidden_data };
const store = getStore();
const modifiedFields = apiEvent.modified_fields.filter((field) => {
return field === 'payments_history' || field === 'fulfillment';
}) as Array<'payments_history' | 'fulfillment'>;
if (!modifiedFields.length) {
warn('Skipping event without `payments_history`/`fulfillments` changes', {
order,
modifiedFields: apiEvent.modified_fields,
});
return null;
}
const lang = appData.lang === 'Inglês' ? 'en_us' : (store.lang || 'pt_br');
const orderId = order._id;
const customerId = order.buyers[0]._id;
Expand Down Expand Up @@ -61,6 +70,7 @@ const handleApiEvent: ApiEventHandler = async ({
? 'new_order' : status;
const mailTempl = getMailTempl(templKey);
if (!mailTempl) {
warn(`Skipped unmatched template for ${templKey}`);
continue;
}
const subject = `${mailTempl.subject[lang]} #${order.number}`;
Expand All @@ -82,7 +92,7 @@ const handleApiEvent: ApiEventHandler = async ({
current: status as FulfillmentsEntry['status'],
};
}
const render = getMailRender(templKey);
const render = getMailRender(mailTempl.templ);
// eslint-disable-next-line no-await-in-loop
const html = await render(
store,
Expand Down

0 comments on commit 20e6575

Please sign in to comment.