Skip to content

Commit a527087

Browse files
chore(util): avoid promotion value precision limit (#13145)
1 parent b1cba9f commit a527087

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

.changeset/clever-deers-shout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/utils": patch
3+
---
4+
5+
chore(utils): avoid limit precision for promotion value

packages/core/utils/src/totals/math.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { BigNumber as BigNumberJS } from "bignumber.js"
33
import { isDefined } from "../common"
44
import { BigNumber } from "./big-number"
55

6+
export const MEDUSA_EPSILON = new BigNumber(
7+
process.env.MEDUSA_EPSILON || "0.0001"
8+
)
9+
610
type BNInput = BigNumberInput | BigNumber
711
export class MathBN {
812
static convert(num: BNInput, decimalPlaces?: number): BigNumberJS {

packages/core/utils/src/totals/promotion/index.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ import {
33
ApplicationMethodAllocation,
44
ApplicationMethodType,
55
} from "../../promotion"
6-
import { MathBN } from "../math"
6+
import { MathBN, MEDUSA_EPSILON } from "../math"
77

88
function getPromotionValueForPercentage(promotion, lineItemAmount) {
9-
return MathBN.convert(
10-
MathBN.mult(MathBN.div(promotion.value, 100), lineItemAmount),
11-
2
12-
)
9+
return MathBN.mult(MathBN.div(promotion.value, 100), lineItemAmount)
1310
}
1411

1512
function getPromotionValueForFixed(promotion, lineItemAmount, lineItemsAmount) {
@@ -28,10 +25,7 @@ function getPromotionValueForFixed(promotion, lineItemAmount, lineItemsAmount) {
2825
promotionValueForItem
2926
)
3027

31-
return MathBN.convert(
32-
MathBN.mult(promotionValueForItem, MathBN.div(percentage, 100)),
33-
2
34-
)
28+
return MathBN.mult(promotionValueForItem, MathBN.div(percentage, 100))
3529
}
3630
return promotion.value
3731
}
@@ -104,8 +98,8 @@ export function calculateAdjustmentAmountFromPromotion(
10498
)
10599
const applicableAmount = MathBN.sub(lineItemAmount, promotion.applied_value)
106100

107-
if (MathBN.lte(applicableAmount, 0)) {
108-
return applicableAmount
101+
if (MathBN.lte(applicableAmount, MEDUSA_EPSILON)) {
102+
return MathBN.convert(0)
109103
}
110104

111105
const promotionValue = getPromotionValue(
@@ -153,7 +147,7 @@ export function calculateAdjustmentAmountFromPromotion(
153147
maximumPromotionAmount
154148
)
155149

156-
if (MathBN.lte(applicableAmount, 0)) {
150+
if (MathBN.lte(applicableAmount, MEDUSA_EPSILON)) {
157151
return MathBN.convert(0)
158152
}
159153

0 commit comments

Comments
 (0)