feat(medusa-plugin-sendgrid): adds localization option (#238)
This commit is contained in:
@@ -16,7 +16,18 @@ If no values are defined for a given option, the plugin will not try to send an
|
|||||||
order_shipped_template: [used on order.shipment_created],
|
order_shipped_template: [used on order.shipment_created],
|
||||||
order_completed_template: [used on order.completed],
|
order_completed_template: [used on order.completed],
|
||||||
user_password_reset_template: [used on user.password_reset],
|
user_password_reset_template: [used on user.password_reset],
|
||||||
customer_password_reset_template: [used on customer.password_reset]
|
customer_password_reset_template: [used on customer.password_reset],
|
||||||
|
localization: {
|
||||||
|
"de-DE": { // locale key
|
||||||
|
gift_card_created_template: [used on gift_card.created],
|
||||||
|
order_placed_template: [used on order.placed],
|
||||||
|
order_cancelled_template: [used on order.cancelled],
|
||||||
|
order_shipped_template: [used on order.shipment_created],
|
||||||
|
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],
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ class SendGridService extends NotificationService {
|
|||||||
orderService,
|
orderService,
|
||||||
returnService,
|
returnService,
|
||||||
swapService,
|
swapService,
|
||||||
|
cartService,
|
||||||
lineItemService,
|
lineItemService,
|
||||||
claimService,
|
claimService,
|
||||||
fulfillmentService,
|
fulfillmentService,
|
||||||
@@ -42,6 +43,7 @@ class SendGridService extends NotificationService {
|
|||||||
this.storeService_ = storeService
|
this.storeService_ = storeService
|
||||||
this.lineItemService_ = lineItemService
|
this.lineItemService_ = lineItemService
|
||||||
this.orderService_ = orderService
|
this.orderService_ = orderService
|
||||||
|
this.cartService_ = cartService
|
||||||
this.claimService_ = claimService
|
this.claimService_ = claimService
|
||||||
this.returnService_ = returnService
|
this.returnService_ = returnService
|
||||||
this.swapService_ = swapService
|
this.swapService_ = swapService
|
||||||
@@ -133,6 +135,45 @@ class SendGridService extends NotificationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getLocalizedTemplateId(event, locale) {
|
||||||
|
if (this.options_.localization && this.options_.localization[locale]) {
|
||||||
|
const map = this.options_.localization[locale]
|
||||||
|
switch (event) {
|
||||||
|
case "order.return_requested":
|
||||||
|
return map.order_return_requested_template
|
||||||
|
case "swap.shipment_created":
|
||||||
|
return map.swap_shipment_created_template
|
||||||
|
case "claim.shipment_created":
|
||||||
|
return map.claim_shipment_created_template
|
||||||
|
case "order.items_returned":
|
||||||
|
return map.order_items_returned_template
|
||||||
|
case "order.swap_received":
|
||||||
|
return map.order_swap_received_template
|
||||||
|
case "swap.created":
|
||||||
|
return map.swap_created_template
|
||||||
|
case "gift_card.created":
|
||||||
|
return map.gift_card_created_template
|
||||||
|
case "order.gift_card_created":
|
||||||
|
return map.gift_card_created_template
|
||||||
|
case "order.placed":
|
||||||
|
return map.order_placed_template
|
||||||
|
case "order.shipment_created":
|
||||||
|
return map.order_shipped_template
|
||||||
|
case "order.canceled":
|
||||||
|
return map.order_canceled_template
|
||||||
|
case "user.password_reset":
|
||||||
|
return map.user_password_reset_template
|
||||||
|
case "customer.password_reset":
|
||||||
|
return map.customer_password_reset_template
|
||||||
|
case "restock-notification.restocked":
|
||||||
|
return map.medusa_restock_template
|
||||||
|
default:
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
getTemplateId(event) {
|
getTemplateId(event) {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case "order.return_requested":
|
case "order.return_requested":
|
||||||
@@ -182,6 +223,10 @@ class SendGridService extends NotificationService {
|
|||||||
attachmentGenerator
|
attachmentGenerator
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (data.locale) {
|
||||||
|
templateId = this.getLocalizedTemplateId(event, data.locale) || templateId
|
||||||
|
}
|
||||||
|
|
||||||
const sendOptions = {
|
const sendOptions = {
|
||||||
template_id: templateId,
|
template_id: templateId,
|
||||||
from: this.options_.from,
|
from: this.options_.from,
|
||||||
@@ -289,7 +334,10 @@ class SendGridService extends NotificationService {
|
|||||||
relations: ["items", "tracking_links"],
|
relations: ["items", "tracking_links"],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const locale = await this.extractLocale(order)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
locale,
|
||||||
order,
|
order,
|
||||||
date: shipment.shipped_at.toDateString(),
|
date: shipment.shipped_at.toDateString(),
|
||||||
email: order.email,
|
email: order.email,
|
||||||
@@ -365,8 +413,11 @@ class SendGridService extends NotificationService {
|
|||||||
discounts.concat(giftCards)
|
discounts.concat(giftCards)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const locale = await this.extractLocale(order)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...order,
|
...order,
|
||||||
|
locale,
|
||||||
has_discounts: order.discounts.length,
|
has_discounts: order.discounts.length,
|
||||||
has_gift_cards: order.gift_cards.length,
|
has_gift_cards: order.gift_cards.length,
|
||||||
date: order.created_at.toDateString(),
|
date: order.created_at.toDateString(),
|
||||||
@@ -404,8 +455,11 @@ class SendGridService extends NotificationService {
|
|||||||
|
|
||||||
const taxRate = giftCard.region.tax_rate / 100
|
const taxRate = giftCard.region.tax_rate / 100
|
||||||
|
|
||||||
|
const locale = await this.extractLocale(order)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...giftCard,
|
...giftCard,
|
||||||
|
locale,
|
||||||
email: giftCard.order.email,
|
email: giftCard.order.email,
|
||||||
display_value: giftCard.value * (1 + taxRate),
|
display_value: giftCard.value * (1 + taxRate),
|
||||||
}
|
}
|
||||||
@@ -463,7 +517,10 @@ class SendGridService extends NotificationService {
|
|||||||
shippingTotal = returnRequest.shipping_method.price * (1 + taxRate)
|
shippingTotal = returnRequest.shipping_method.price * (1 + taxRate)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const locale = await this.extractLocale(order)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
locale,
|
||||||
has_shipping: !!returnRequest.shipping_method,
|
has_shipping: !!returnRequest.shipping_method,
|
||||||
email: order.email,
|
email: order.email,
|
||||||
items: this.processItems_(returnItems, taxRate, currencyCode),
|
items: this.processItems_(returnItems, taxRate, currencyCode),
|
||||||
@@ -555,7 +612,10 @@ class SendGridService extends NotificationService {
|
|||||||
|
|
||||||
const refundAmount = swap.return_order.refund_amount
|
const refundAmount = swap.return_order.refund_amount
|
||||||
|
|
||||||
|
const locale = await this.extractLocale(order)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
locale,
|
||||||
swap,
|
swap,
|
||||||
order,
|
order,
|
||||||
return_request: returnRequest,
|
return_request: returnRequest,
|
||||||
@@ -629,7 +689,10 @@ class SendGridService extends NotificationService {
|
|||||||
relations: ["tracking_links"],
|
relations: ["tracking_links"],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const locale = await this.extractLocale(order)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
locale,
|
||||||
swap,
|
swap,
|
||||||
order,
|
order,
|
||||||
items: this.processItems_(swap.additional_items, taxRate, currencyCode),
|
items: this.processItems_(swap.additional_items, taxRate, currencyCode),
|
||||||
@@ -670,7 +733,10 @@ class SendGridService extends NotificationService {
|
|||||||
relations: ["tracking_links"],
|
relations: ["tracking_links"],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const locale = await this.extractLocale(claim.order)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
locale,
|
||||||
email: claim.order.email,
|
email: claim.order.email,
|
||||||
claim,
|
claim,
|
||||||
order: claim.order,
|
order: claim.order,
|
||||||
@@ -745,6 +811,25 @@ class SendGridService extends NotificationService {
|
|||||||
}
|
}
|
||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async extractLocale(fromOrder) {
|
||||||
|
if (fromOrder.cart_id) {
|
||||||
|
try {
|
||||||
|
const cart = await this.cartService_.retrieve(fromOrder.cart_id, {
|
||||||
|
select: ["context"],
|
||||||
|
})
|
||||||
|
|
||||||
|
if (cart.context && cart.context.locale) {
|
||||||
|
return cart.context.locale
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
console.warn("Failed to gather context for order")
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default SendGridService
|
export default SendGridService
|
||||||
|
|||||||
Reference in New Issue
Block a user