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 }