fix: zero decimal amounts in slack + gc total

This commit is contained in:
Sebastian Rindom committed 2021-03-23 10:39:29 +01:00
1 parent 7682980c58
commit c6a5dffa7b
1 file changed
+35 -18
@@ -1,4 +1,5 @@
import axios from "axios" import axios from "axios"
import { zeroDecimalCurrencies, humanizeAmount } from "medusa-core-utils"
import { BaseService } from "medusa-interfaces" import { BaseService } from "medusa-interfaces"
class SlackService extends BaseService { class SlackService extends BaseService {
@@ -58,6 +59,14 @@ class SlackService extends BaseService {
const currencyCode = order.currency_code.toUpperCase() const currencyCode = order.currency_code.toUpperCase()
const taxRate = order.tax_rate / 100 const taxRate = order.tax_rate / 100
const getDisplayAmount = (amount) => {
const humanAmount = humanizeAmount(amount, currencyCode)
if (zeroDecimalCurrencies.includes(currencyCode.toLowerCase())) {
return humanAmount
}
return humanAmount.toFixed(2)
}
let blocks = [ let blocks = [
{ {
type: "section", type: "section",
@@ -83,21 +92,33 @@ class SlackService extends BaseService {
type: "section", type: "section",
text: { text: {
type: "mrkdwn", type: "mrkdwn",
text: `*Subtotal*\t${(subtotal / 100).toFixed( text: `*Subtotal*\t${getDisplayAmount(
2 subtotal
)} ${currencyCode}\n*Shipping*\t${(shipping_total / 100).toFixed( )} ${currencyCode}\n*Shipping*\t${getDisplayAmount(
2 shipping_total
)} ${currencyCode}\n*Discount Total*\t${( )} ${currencyCode}\n*Discount Total*\t${getDisplayAmount(
discount_total / 100 discount_total
).toFixed(2)} ${currencyCode}\n*Tax*\t${(tax_total / 100).toFixed( )} ${currencyCode}\n*Tax*\t${getDisplayAmount(
2 tax_total
)} ${currencyCode}\n*Total*\t${(total / 100).toFixed( )} ${currencyCode}\n*Total*\t${getDisplayAmount(
2 total
)} ${currencyCode}`, )} ${currencyCode}`,
}, },
}, },
] ]
if (order.gift_card_total) {
blocks.push({
type: "section",
text: {
type: "mrkdwn",
text: `*Gift Card Total*\t${getDisplayAmount(
order.gift_card_total
)} ${currencyCode}`,
},
})
}
order.discounts.forEach((d) => { order.discounts.forEach((d) => {
blocks.push({ blocks.push({
type: "section", type: "section",
@@ -119,19 +140,15 @@ class SlackService extends BaseService {
type: "section", type: "section",
text: { text: {
type: "mrkdwn", type: "mrkdwn",
text: `*${lineItem.title}*\n${lineItem.quantity} x ${( text: `*${lineItem.title}*\n${lineItem.quantity} x ${getDisplayAmount(
(lineItem.unit_price / 100) * lineItem.unit_price * (1 + taxRate)
(1 + taxRate) )} ${currencyCode}`,
).toFixed(2)} ${currencyCode}`,
}, },
} }
if (lineItem.thumbnail) { if (lineItem.thumbnail) {
let url = lineItem.thumbnail let url = lineItem.thumbnail
if ( if (lineItem.thumbnail.startsWith("//")) {
!lineItem.thumbnail.startsWith("http:") &&
!lineItem.thumbnail.startsWith("https:")
) {
url = `https:${lineItem.thumbnail}` url = `https:${lineItem.thumbnail}`
} }