fix(medusa-plugin-sendgrid): decorate order placed data for email
This commit is contained in:
33
packages/medusa-plugin-sendgrid/README.md
Normal file
33
packages/medusa-plugin-sendgrid/README.md
Normal file
@@ -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 <acme@mail.com>] (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 <acme@mail.com>", "customer@mail.com", { dynamic: "data" })
|
||||
|
||||
```
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user