From c6a5dffa7bd8f627310d1f90364492084051dd47 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Tue, 23 Mar 2021 10:39:29 +0100 Subject: [PATCH] fix: zero decimal amounts in slack + gc total --- .../src/services/slack.js | 53 ++++++++++++------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/packages/medusa-plugin-slack-notification/src/services/slack.js b/packages/medusa-plugin-slack-notification/src/services/slack.js index d67069d9c3..9a45674fd7 100644 --- a/packages/medusa-plugin-slack-notification/src/services/slack.js +++ b/packages/medusa-plugin-slack-notification/src/services/slack.js @@ -1,4 +1,5 @@ import axios from "axios" +import { zeroDecimalCurrencies, humanizeAmount } from "medusa-core-utils" import { BaseService } from "medusa-interfaces" class SlackService extends BaseService { @@ -58,6 +59,14 @@ class SlackService extends BaseService { const currencyCode = order.currency_code.toUpperCase() 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 = [ { type: "section", @@ -83,21 +92,33 @@ class SlackService extends BaseService { type: "section", text: { type: "mrkdwn", - text: `*Subtotal*\t${(subtotal / 100).toFixed( - 2 - )} ${currencyCode}\n*Shipping*\t${(shipping_total / 100).toFixed( - 2 - )} ${currencyCode}\n*Discount Total*\t${( - discount_total / 100 - ).toFixed(2)} ${currencyCode}\n*Tax*\t${(tax_total / 100).toFixed( - 2 - )} ${currencyCode}\n*Total*\t${(total / 100).toFixed( - 2 + text: `*Subtotal*\t${getDisplayAmount( + subtotal + )} ${currencyCode}\n*Shipping*\t${getDisplayAmount( + shipping_total + )} ${currencyCode}\n*Discount Total*\t${getDisplayAmount( + discount_total + )} ${currencyCode}\n*Tax*\t${getDisplayAmount( + tax_total + )} ${currencyCode}\n*Total*\t${getDisplayAmount( + total )} ${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) => { blocks.push({ type: "section", @@ -119,19 +140,15 @@ class SlackService extends BaseService { type: "section", text: { type: "mrkdwn", - text: `*${lineItem.title}*\n${lineItem.quantity} x ${( - (lineItem.unit_price / 100) * - (1 + taxRate) - ).toFixed(2)} ${currencyCode}`, + text: `*${lineItem.title}*\n${lineItem.quantity} x ${getDisplayAmount( + lineItem.unit_price * (1 + taxRate) + )} ${currencyCode}`, }, } if (lineItem.thumbnail) { let url = lineItem.thumbnail - if ( - !lineItem.thumbnail.startsWith("http:") && - !lineItem.thumbnail.startsWith("https:") - ) { + if (lineItem.thumbnail.startsWith("//")) { url = `https:${lineItem.thumbnail}` }