From 2edf152c8eafbd54c5c99602e340ad41fcc783f2 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 28 Aug 2020 15:38:18 +0200 Subject: [PATCH] fix(medusa-plugin-sendgrid): decorate order placed data for email --- packages/medusa-plugin-sendgrid/README.md | 33 ++++++++++++ .../src/subscribers/order.js | 53 +++++++++++++++++-- 2 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 packages/medusa-plugin-sendgrid/README.md diff --git a/packages/medusa-plugin-sendgrid/README.md b/packages/medusa-plugin-sendgrid/README.md new file mode 100644 index 0000000000..ca25765b47 --- /dev/null +++ b/packages/medusa-plugin-sendgrid/README.md @@ -0,0 +1,33 @@ +# medusa-plugin-sendgrid + +Sendgrid Plugin for Medusa to send transactional emails. + + +If no values are defined for a given option, the plugin will not try to send an email for that event. + +## Plugin Options +``` +{ + api_key: [your sendgrid api key] (required), + from: [the from field, i.e. ACME ] (required), + gift_card_created_template: [used on gift_card.created], + order_placed_template: [used on order.placed], + order_cancelled_template: [used on order.cancelled], + order_completed_template: [used on order.completed], + user_password_reset_template: [used on user.password_reset], + customer_password_reset_template: [used on customer.password_reset] +} +``` + +## Dynamic usage + +You can resolve the SendGrid service to dynamically send emails via sendgrid. + +Example: + +```js + +const sendgridService = scope.resolve("sendgridService") +sendgridService.sendEmail("d-123....", "ACME ", "customer@mail.com", { dynamic: "data" }) + +``` diff --git a/packages/medusa-plugin-sendgrid/src/subscribers/order.js b/packages/medusa-plugin-sendgrid/src/subscribers/order.js index f345b6e192..19f4daecf5 100644 --- a/packages/medusa-plugin-sendgrid/src/subscribers/order.js +++ b/packages/medusa-plugin-sendgrid/src/subscribers/order.js @@ -1,15 +1,60 @@ class OrderSubscriber { - constructor({ sendgridService, eventBusService }) { + constructor({ totalsService, sendgridService, eventBusService }) { + this.totalsService_ = totalsService this.sendgridService_ = sendgridService - this.eventBus_ = eventBusService this.eventBus_.subscribe("order.gift_card_created", async (order) => { - await this.sendgridService_.transactionalEmail("order.gift_card_created", order) + await this.sendgridService_.transactionalEmail( + "order.gift_card_created", + order + ) }) this.eventBus_.subscribe("order.placed", async (order) => { - await this.sendgridService_.transactionalEmail("order.placed", order) + const subtotal = await this.totalsService_.getSubtotal(order) + const tax_total = await this.totalsService_.getTaxTotal(order) + const discount_total = await this.totalsService_.getDiscountTotal(order) + const shipping_total = await this.totalsService_.getShippingTotal(order) + const total = await this.totalsService_.getTotal(order) + + const date = new Date(parseInt(order.created)) + const data = { + ...order, + date: date.toDateString(), + items: order.items.map((i) => { + return { + ...i, + price: `${(i.content.unit_price * (1 + order.tax_rate)).toFixed( + 2 + )} ${order.currency_code}`, + } + }), + discounts: order.discounts.map((discount) => { + return { + is_giftcard: discount.is_giftcard, + code: discount.code, + descriptor: `${discount.discount_rule.value}${ + discount.discount_rule.type === "percentage" + ? "%" + : ` ${order.currency_code}` + }`, + } + }), + subtotal: `${(subtotal * (1 + order.tax_rate)).toFixed(2)} ${ + order.currency_code + }`, + tax_total: `${tax_total.toFixed(2)} ${order.currency_code}`, + discount_total: `${(discount_total * (1 + order.tax_rate)).toFixed( + 2 + )} ${order.currency_code}`, + shipping_total: `${(shipping_total * (1 + order.tax_rate)).toFixed( + 2 + )} ${order.currency_code}`, + total: `${total.toFixed(2)} ${order.currency_code}`, + } + + await this.sendgridService_.transactionalEmail("order.placed", data) }) this.eventBus_.subscribe("order.cancelled", async (order) => {