fix(medusa-plugin-sendgrid): decorate order placed data for email

This commit is contained in:
Sebastian Rindom
2020-08-28 15:38:18 +02:00
parent ee80cfe145
commit 2edf152c8e
2 changed files with 82 additions and 4 deletions

View File

@@ -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) => {