fix: format numbers correctly in sendgrid

This commit is contained in:
Sebastian Rindom committed 2021-03-22 11:48:28 +01:00
1 parent 3258955b46
commit 7682980c58
1 file changed
+67 -23
@@ -1,6 +1,8 @@
import { NotificationService } from "medusa-interfaces"
import SendGrid from "@sendgrid/mail" import SendGrid from "@sendgrid/mail"
import { NotificationService } from "medusa-interfaces"
import { humanizeAmount, zeroDecimalCurrencies } from "medusa-core-utils"
class SendGridService extends NotificationService { class SendGridService extends NotificationService {
static identifier = "sendgrid" static identifier = "sendgrid"
@@ -361,18 +363,24 @@ class SendGridService extends NotificationService {
date: order.created_at.toDateString(), date: order.created_at.toDateString(),
items, items,
discounts, discounts,
subtotal: `${this.humanPrice_(subtotal * (1 + taxRate))} ${currencyCode}`, subtotal: `${this.humanPrice_(
gift_card_total: `${this.humanPrice_( subtotal * (1 + taxRate),
gift_card_total * (1 + taxRate) currencyCode
)} ${currencyCode}`, )} ${currencyCode}`,
tax_total: `${this.humanPrice_(tax_total)} ${currencyCode}`, gift_card_total: `${this.humanPrice_(
gift_card_total * (1 + taxRate),
currencyCode
)} ${currencyCode}`,
tax_total: `${this.humanPrice_(tax_total, currencyCode)} ${currencyCode}`,
discount_total: `${this.humanPrice_( discount_total: `${this.humanPrice_(
discount_total * (1 + taxRate) discount_total * (1 + taxRate),
currencyCode
)} ${currencyCode}`, )} ${currencyCode}`,
shipping_total: `${this.humanPrice_( shipping_total: `${this.humanPrice_(
shipping_total * (1 + taxRate) shipping_total * (1 + taxRate),
currencyCode
)} ${currencyCode}`, )} ${currencyCode}`,
total: `${this.humanPrice_(total)} ${currencyCode}`, total: `${this.humanPrice_(total, currencyCode)} ${currencyCode}`,
} }
} }
@@ -450,15 +458,23 @@ class SendGridService extends NotificationService {
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),
subtotal: `${this.humanPrice_(item_subtotal)} ${currencyCode}`, subtotal: `${this.humanPrice_(
shipping_total: `${this.humanPrice_(shippingTotal)} ${currencyCode}`, item_subtotal,
currencyCode
)} ${currencyCode}`,
shipping_total: `${this.humanPrice_(
shippingTotal,
currencyCode
)} ${currencyCode}`,
refund_amount: `${this.humanPrice_( refund_amount: `${this.humanPrice_(
returnRequest.refund_amount returnRequest.refund_amount,
currencyCode
)} ${currencyCode}`, )} ${currencyCode}`,
return_request: { return_request: {
...returnRequest, ...returnRequest,
refund_amount: `${this.humanPrice_( refund_amount: `${this.humanPrice_(
returnRequest.refund_amount returnRequest.refund_amount,
currencyCode
)} ${currencyCode}`, )} ${currencyCode}`,
}, },
order, order,
@@ -539,9 +555,18 @@ class SendGridService extends NotificationService {
email: order.email, email: order.email,
items: this.processItems_(swap.additional_items, taxRate, currencyCode), items: this.processItems_(swap.additional_items, taxRate, currencyCode),
return_items: returnItems, return_items: returnItems,
return_total: `${this.humanPrice_(returnTotal)} ${currencyCode}`, return_total: `${this.humanPrice_(
refund_amount: `${this.humanPrice_(refundAmount)} ${currencyCode}`, returnTotal,
additional_total: `${this.humanPrice_(additionalTotal)} ${currencyCode}`, currencyCode
)} ${currencyCode}`,
refund_amount: `${this.humanPrice_(
refundAmount,
currencyCode
)} ${currencyCode}`,
additional_total: `${this.humanPrice_(
additionalTotal,
currencyCode
)} ${currencyCode}`,
} }
} }
@@ -602,12 +627,25 @@ class SendGridService extends NotificationService {
date: swap.updated_at.toDateString(), date: swap.updated_at.toDateString(),
email: order.email, email: order.email,
tax_amount: `${this.humanPrice_( tax_amount: `${this.humanPrice_(
swap.difference_due * taxRate swap.difference_due * taxRate,
currencyCode
)} ${currencyCode}`,
paid_total: `${this.humanPrice_(
swap.difference_due,
currencyCode
)} ${currencyCode}`,
return_total: `${this.humanPrice_(
returnTotal,
currencyCode
)} ${currencyCode}`,
refund_amount: `${this.humanPrice_(
refundAmount,
currencyCode
)} ${currencyCode}`,
additional_total: `${this.humanPrice_(
additionalTotal,
currencyCode
)} ${currencyCode}`, )} ${currencyCode}`,
paid_total: `${this.humanPrice_(swap.difference_due)} ${currencyCode}`,
return_total: `${this.humanPrice_(returnTotal)} ${currencyCode}`,
refund_amount: `${this.humanPrice_(refundAmount)} ${currencyCode}`,
additional_total: `${this.humanPrice_(additionalTotal)} ${currencyCode}`,
fulfillment: shipment, fulfillment: shipment,
tracking_links: shipment.tracking_links, tracking_links: shipment.tracking_links,
tracking_number: shipment.tracking_numbers.join(", "), tracking_number: shipment.tracking_numbers.join(", "),
@@ -647,14 +685,20 @@ class SendGridService extends NotificationService {
...i, ...i,
thumbnail: this.normalizeThumbUrl_(i.thumbnail), thumbnail: this.normalizeThumbUrl_(i.thumbnail),
price: `${this.humanPrice_( price: `${this.humanPrice_(
i.unit_price * (1 + taxRate) i.unit_price * (1 + taxRate),
currencyCode
)} ${currencyCode}`, )} ${currencyCode}`,
} }
}) })
} }
humanPrice_(amount) { humanPrice_(amount, currency) {
return amount ? (amount / 100).toFixed(2) : "0.00" if (!amount) {
return "0.00"
}
const normalized = humanizeAmount(amount, currency)
return normalized.toFixed(zeroDecimalCurrencies.includes(currency.toLowerCase()) ? 0 : 2)
} }
normalizeThumbUrl_(url) { normalizeThumbUrl_(url) {