Skip to content

Commit 822217f

Browse files
riqwanolivermrbl
andauthored
fix(pricing): fix pricing query when max_quantity is null (medusajs#12981)
what: Prices when max_quantity value is null is accounted for when quantity is passed in context. Co-authored-by: Oli Juhl <[email protected]>
1 parent 3fa1db9 commit 822217f

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

.changeset/dry-bikes-burn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/pricing": patch
3+
---
4+
5+
fix(pricing): fix pricing query when max_quantity is null

packages/modules/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ moduleIntegrationTestRunner<IPricingModuleService>({
7676
max_quantity: 10,
7777
rules_count: 0,
7878
},
79+
{
80+
id: "price-PLN-min-quantity-only",
81+
title: "price PLN - min quantity only",
82+
price_set_id: "price-set-PLN",
83+
currency_code: "PLN",
84+
amount: 1250,
85+
min_quantity: 20,
86+
max_quantity: null,
87+
rules_count: 0,
88+
},
7989
{
8090
id: "price-ETH",
8191
title: "price ETH",
@@ -451,6 +461,55 @@ moduleIntegrationTestRunner<IPricingModuleService>({
451461
])
452462
})
453463

464+
it("should successfully calculate prices where only min quantity is set", async () => {
465+
const context = {
466+
currency_code: "PLN",
467+
region_id: "PL",
468+
quantity: 255,
469+
}
470+
471+
const calculatedPrice = await service.calculatePrices(
472+
{ id: ["price-set-EUR", "price-set-PLN"] },
473+
{ context }
474+
)
475+
476+
477+
expect(calculatedPrice).toEqual([
478+
{
479+
id: "price-set-PLN",
480+
is_calculated_price_price_list: false,
481+
is_calculated_price_tax_inclusive: false,
482+
calculated_amount: 1250,
483+
raw_calculated_amount: {
484+
value: "1250",
485+
precision: 20,
486+
},
487+
is_original_price_price_list: false,
488+
is_original_price_tax_inclusive: false,
489+
original_amount: 1250,
490+
raw_original_amount: {
491+
value: "1250",
492+
precision: 20,
493+
},
494+
currency_code: "PLN",
495+
calculated_price: {
496+
id: "price-PLN-min-quantity-only",
497+
price_list_id: null,
498+
price_list_type: null,
499+
min_quantity: 20,
500+
max_quantity: null,
501+
},
502+
original_price: {
503+
id: "price-PLN-min-quantity-only",
504+
price_list_id: null,
505+
price_list_type: null,
506+
min_quantity: 20,
507+
max_quantity: null,
508+
},
509+
},
510+
])
511+
})
512+
454513
it("should throw an error when currency code is not set", async () => {
455514
let result = service.calculatePrices(
456515
{ id: ["price-set-EUR", "price-set-PLN"] },

packages/modules/pricing/src/repositories/pricing.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,24 @@ export class PricingRepository
126126

127127
if (quantity !== undefined) {
128128
query.andWhere(function (this: Knex.QueryBuilder) {
129-
this.where(function (this: Knex.QueryBuilder) {
129+
this.orWhere(function (this: Knex.QueryBuilder) {
130130
this.where("price.min_quantity", "<=", quantity).andWhere(
131131
"price.max_quantity",
132132
">=",
133133
quantity
134134
)
135-
}).orWhere(function (this: Knex.QueryBuilder) {
136-
this.whereNull("price.min_quantity").whereNull("price.max_quantity")
135+
136+
this.orWhere("price.min_quantity", "<=", quantity).whereNull(
137+
"price.max_quantity"
138+
)
139+
140+
this.orWhereNull("price.min_quantity").whereNull("price.max_quantity")
141+
142+
this.orWhereNull("price.min_quantity").andWhere(
143+
"price.max_quantity",
144+
">=",
145+
quantity
146+
)
137147
})
138148
})
139149
} else {

0 commit comments

Comments
 (0)