From a52708769da675e2ade6cb6fe28e1cd9741c562d Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Wed, 6 Aug 2025 15:49:10 -0300 Subject: [PATCH] chore(util): avoid promotion value precision limit (#13145) --- .changeset/clever-deers-shout.md | 5 +++++ packages/core/utils/src/totals/math.ts | 4 ++++ .../core/utils/src/totals/promotion/index.ts | 18 ++++++------------ 3 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 .changeset/clever-deers-shout.md diff --git a/.changeset/clever-deers-shout.md b/.changeset/clever-deers-shout.md new file mode 100644 index 0000000000..a984f66a2e --- /dev/null +++ b/.changeset/clever-deers-shout.md @@ -0,0 +1,5 @@ +--- +"@medusajs/utils": patch +--- + +chore(utils): avoid limit precision for promotion value diff --git a/packages/core/utils/src/totals/math.ts b/packages/core/utils/src/totals/math.ts index 458ea39102..626ed2db06 100644 --- a/packages/core/utils/src/totals/math.ts +++ b/packages/core/utils/src/totals/math.ts @@ -3,6 +3,10 @@ import { BigNumber as BigNumberJS } from "bignumber.js" import { isDefined } from "../common" import { BigNumber } from "./big-number" +export const MEDUSA_EPSILON = new BigNumber( + process.env.MEDUSA_EPSILON || "0.0001" +) + type BNInput = BigNumberInput | BigNumber export class MathBN { static convert(num: BNInput, decimalPlaces?: number): BigNumberJS { diff --git a/packages/core/utils/src/totals/promotion/index.ts b/packages/core/utils/src/totals/promotion/index.ts index 15d9c5bdff..eabfe0ee6c 100644 --- a/packages/core/utils/src/totals/promotion/index.ts +++ b/packages/core/utils/src/totals/promotion/index.ts @@ -3,13 +3,10 @@ import { ApplicationMethodAllocation, ApplicationMethodType, } from "../../promotion" -import { MathBN } from "../math" +import { MathBN, MEDUSA_EPSILON } from "../math" function getPromotionValueForPercentage(promotion, lineItemAmount) { - return MathBN.convert( - MathBN.mult(MathBN.div(promotion.value, 100), lineItemAmount), - 2 - ) + return MathBN.mult(MathBN.div(promotion.value, 100), lineItemAmount) } function getPromotionValueForFixed(promotion, lineItemAmount, lineItemsAmount) { @@ -28,10 +25,7 @@ function getPromotionValueForFixed(promotion, lineItemAmount, lineItemsAmount) { promotionValueForItem ) - return MathBN.convert( - MathBN.mult(promotionValueForItem, MathBN.div(percentage, 100)), - 2 - ) + return MathBN.mult(promotionValueForItem, MathBN.div(percentage, 100)) } return promotion.value } @@ -104,8 +98,8 @@ export function calculateAdjustmentAmountFromPromotion( ) const applicableAmount = MathBN.sub(lineItemAmount, promotion.applied_value) - if (MathBN.lte(applicableAmount, 0)) { - return applicableAmount + if (MathBN.lte(applicableAmount, MEDUSA_EPSILON)) { + return MathBN.convert(0) } const promotionValue = getPromotionValue( @@ -153,7 +147,7 @@ export function calculateAdjustmentAmountFromPromotion( maximumPromotionAmount ) - if (MathBN.lte(applicableAmount, 0)) { + if (MathBN.lte(applicableAmount, MEDUSA_EPSILON)) { return MathBN.convert(0) }