Skip to content

Commit

Permalink
feat: send email to administrators when order is created
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotcorreia committed Aug 20, 2024
1 parent e071b76 commit fb27787
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
9 changes: 7 additions & 2 deletions backend/api/email/services/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const paymentGatewayMap = {
BANK_TRANSFER: 'Tranferência Bancária',
};

const sendAdminOrderCreatedEmail = (data) => sendPugEmail(data, 'admin-order-created');

const sendOrderCreatedEmail = (data) => sendPugEmail(data, 'order-created');

const sendOrderPaidEmail = (data) => sendPugEmail(data, 'order-paid');
Expand All @@ -33,19 +35,22 @@ const sendOrderShippedEmail = (data) => sendPugEmail(data, 'order-shipped');
const sendOrderCancelledEmail = (data) => sendPugEmail(data, 'order-cancelled');

module.exports = {
sendAdminOrderCreatedEmail,
sendOrderCreatedEmail,
sendOrderPaidEmail,
sendOrderReadyToPickupEmail,
sendOrderShippedEmail,
sendOrderCancelledEmail,
};

const sendPugEmail = async ({ order, user }, template) => {
const sendPugEmail = async ({ order, user, emailTo }, template) => {
const shippingAddress = order.shippingAddress || {};
const adminUrl = strapi.config.get('custom.adminUrl', 'http://localhost:3337/admin');
const frontendUrl = strapi.config.get('custom.frontendUrl', 'http://localhost:3000');
const variables = {
frontendUrl,
id: order.id,
adminOrderUrl: `${adminUrl}/plugins/order-management/view/${order.id}?redirectUrl=/plugins/order-management/pending`,
orderUrl: `${frontendUrl}/dashboard/order/${order.id}`,
productUrlPrefix: `${frontendUrl}/product/`,
clientName: user.username,
Expand Down Expand Up @@ -73,7 +78,7 @@ const sendPugEmail = async ({ order, user }, template) => {
const { subject, text, html } = await email.renderAll(template, variables);

await strapi.plugins['email'].services.email.send({
to: user.email,
to: emailTo || user.email,
subject,
text,
html,
Expand Down
15 changes: 15 additions & 0 deletions backend/api/order/controllers/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ module.exports = {
`Failed to send order create email for order ${entity.invoiceId}: ${JSON.stringify(e)}`
);
}
try {
const emailTo = strapi.config.get('custom.orderNotificationEmail', '');
if (emailTo) {
strapi.services.email.sendAdminOrderCreatedEmail({
order: entity,
user: ctx.state.user,
emailTo,
});
}
} catch (e) {
strapi.log.error(
{ error: e },
`Failed to send admin order create email for order ${entity.invoiceId}: ${JSON.stringify(e)}`
);
}

return sanitizeOrderData(sanitizeEntity(entity, { model: strapi.models.order }));
},
Expand Down
2 changes: 2 additions & 0 deletions backend/config/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ module.exports = ({ env }) => ({
lowStockThreshold: 1,
euPagoToken: env('EUPAGO_TOKEN', 'demo-f9ee-b006-8de5-8e8'),
euPagoSandbox: env('EUPAGO_TOKEN', 'demo-').startsWith('demo-'),
adminUrl: env('ADMIN_URL', 'http://localhost:3337/admin'),
frontendUrl: env('FRONTEND_URL', 'http://localhost:3000'),
previewSecret: env('PREVIEW_SECRET', ''),
meiliHost: env('MEILI_HOST', 'http://127.0.0.1:7700'),
meiliApiKey: env('MEILI_API_KEY', ''),
orderNotificationEmail: env('ORDER_NOTIFICATION_EMAIL', ''),
});
10 changes: 10 additions & 0 deletions backend/emails/admin-order-created/html.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extends ../layout/layout.pug

block header
//- :tada: emoji (Party Popper)
h1 Nova encomenda! 🎉

block content
p O/a cliente #{clientName} efetuou a encomenda abaixo.
.center
a(href=adminOrderUrl).button Ver encomenda
1 change: 1 addition & 0 deletions backend/emails/admin-order-created/subject.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| Encomenda ##{invoiceId} criada com sucesso
3 changes: 3 additions & 0 deletions backend/emails/admin-order-created/text.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| A encomenda ##{invoiceId} foi criada com sucesso.
| O seu cliente de email não consegue renderizar o email.
| Aceda ao painel de administrador para ver mais informações sobre a encomenda: #{orderUrl}

0 comments on commit fb27787

Please sign in to comment.