From cbfe646ce14d8436adbaa72cda95d38e6cdcd908 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Fri, 15 Aug 2025 11:43:52 -0300 Subject: [PATCH] chore(utils): round to zero credit lines (#13200) --- .changeset/lovely-tips-know.md | 5 +++++ .../core/utils/src/totals/credit-lines/index.ts | 9 +++++---- packages/core/utils/src/totals/promotion/index.ts | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 .changeset/lovely-tips-know.md diff --git a/.changeset/lovely-tips-know.md b/.changeset/lovely-tips-know.md new file mode 100644 index 0000000000..656753fde7 --- /dev/null +++ b/.changeset/lovely-tips-know.md @@ -0,0 +1,5 @@ +--- +"@medusajs/utils": patch +--- + +chore(utils): round to zero promotions and credit lines diff --git a/packages/core/utils/src/totals/credit-lines/index.ts b/packages/core/utils/src/totals/credit-lines/index.ts index fd05eb62fa..ac17217637 100644 --- a/packages/core/utils/src/totals/credit-lines/index.ts +++ b/packages/core/utils/src/totals/credit-lines/index.ts @@ -1,7 +1,7 @@ import { BigNumberInput } from "@medusajs/types" import { isDefined } from "../../common" import { BigNumber } from "../big-number" -import { MathBN } from "../math" +import { MathBN, MEDUSA_EPSILON } from "../math" export function calculateCreditLinesTotal({ creditLines, @@ -46,9 +46,10 @@ export function calculateCreditLinesTotal({ } } + const isZero = MathBN.lte(creditLinesTotal, MEDUSA_EPSILON) return { - creditLinesTotal, - creditLinesSubtotal, - creditLinesTaxTotal, + creditLinesTotal: isZero ? MathBN.convert(0) : creditLinesTotal, + creditLinesSubtotal: isZero ? MathBN.convert(0) : creditLinesSubtotal, + creditLinesTaxTotal: isZero ? MathBN.convert(0) : creditLinesTaxTotal, } } diff --git a/packages/core/utils/src/totals/promotion/index.ts b/packages/core/utils/src/totals/promotion/index.ts index eabfe0ee6c..14f9ce6c1f 100644 --- a/packages/core/utils/src/totals/promotion/index.ts +++ b/packages/core/utils/src/totals/promotion/index.ts @@ -108,7 +108,12 @@ export function calculateAdjustmentAmountFromPromotion( lineItemsAmount ) - return MathBN.min(promotionValue, applicableAmount) + const returnValue = MathBN.min(promotionValue, applicableAmount) + if (MathBN.lte(returnValue, MEDUSA_EPSILON)) { + return MathBN.convert(0) + } + + return returnValue } /* @@ -157,5 +162,10 @@ export function calculateAdjustmentAmountFromPromotion( lineItemsAmount ) - return MathBN.min(promotionValue, applicableAmount) + const returnValue = MathBN.min(promotionValue, applicableAmount) + if (MathBN.lte(returnValue, MEDUSA_EPSILON)) { + return MathBN.convert(0) + } + + return returnValue }