Skip to content

Commit ec8cedc

Browse files
committed
fix(pricing): fix pricing query when max_quantity is null
1 parent 1ba316a commit ec8cedc

3 files changed

Lines changed: 81 additions & 3 deletions

File tree

.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: 63 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,59 @@ 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+
console.log(
477+
"calculatedPrice - ",
478+
JSON.stringify(calculatedPrice, null, 2)
479+
)
480+
481+
expect(calculatedPrice).toEqual([
482+
{
483+
id: "price-set-PLN",
484+
is_calculated_price_price_list: false,
485+
is_calculated_price_tax_inclusive: false,
486+
calculated_amount: 1250,
487+
raw_calculated_amount: {
488+
value: "1250",
489+
precision: 20,
490+
},
491+
is_original_price_price_list: false,
492+
is_original_price_tax_inclusive: false,
493+
original_amount: 1250,
494+
raw_original_amount: {
495+
value: "1250",
496+
precision: 20,
497+
},
498+
currency_code: "PLN",
499+
calculated_price: {
500+
id: "price-PLN-min-quantity-only",
501+
price_list_id: null,
502+
price_list_type: null,
503+
min_quantity: 20,
504+
max_quantity: null,
505+
},
506+
original_price: {
507+
id: "price-PLN-min-quantity-only",
508+
price_list_id: null,
509+
price_list_type: null,
510+
min_quantity: 20,
511+
max_quantity: null,
512+
},
513+
},
514+
])
515+
})
516+
454517
it("should throw an error when currency code is not set", async () => {
455518
let result = service.calculatePrices(
456519
{ 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)