Skip to content

Commit 18d201c

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

3 files changed

Lines changed: 97 additions & 19 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: 79 additions & 16 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: 1000,
487+
raw_calculated_amount: {
488+
value: "1000",
489+
precision: 20,
490+
},
491+
is_original_price_price_list: false,
492+
is_original_price_tax_inclusive: false,
493+
original_amount: 1000,
494+
raw_original_amount: {
495+
value: "1000",
496+
precision: 20,
497+
},
498+
currency_code: "PLN",
499+
calculated_price: {
500+
id: "price-PLN",
501+
price_list_id: null,
502+
price_list_type: null,
503+
min_quantity: 1,
504+
max_quantity: 10,
505+
},
506+
original_price: {
507+
id: "price-PLN",
508+
price_list_id: null,
509+
price_list_type: null,
510+
min_quantity: 1,
511+
max_quantity: 10,
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"] },
@@ -765,11 +828,11 @@ moduleIntegrationTestRunner<IPricingModuleService>({
765828
])
766829
})
767830

768-
it("should return filled prices when 2 contexts are present and price is setup along with declaring quantity", async () => {
831+
it.only("should return filled prices when 2 contexts are present and price is setup along with declaring quantity", async () => {
769832
const priceSetsResult = await service.calculatePrices(
770833
{ id: ["price-set-PLN"] },
771834
{
772-
context: { currency_code: "PLN", region_id: "PL", quantity: 5 },
835+
context: { currency_code: "PLN", region_id: "PL", quantity: 25 },
773836
}
774837
)
775838

@@ -778,32 +841,32 @@ moduleIntegrationTestRunner<IPricingModuleService>({
778841
id: "price-set-PLN",
779842
is_calculated_price_price_list: false,
780843
is_calculated_price_tax_inclusive: false,
781-
calculated_amount: 250,
844+
calculated_amount: 1250,
782845
raw_calculated_amount: {
783-
value: "250",
846+
value: "1250",
784847
precision: 20,
785848
},
786849
is_original_price_price_list: false,
787850
is_original_price_tax_inclusive: false,
788-
original_amount: 250,
851+
original_amount: 1250,
789852
raw_original_amount: {
790-
value: "250",
853+
value: "1250",
791854
precision: 20,
792855
},
793856
currency_code: "PLN",
794857
calculated_price: {
795-
id: "price-region_id-PLN-5-qty",
858+
id: "price-PLN-min-quantity-only",
796859
price_list_id: null,
797860
price_list_type: null,
798-
min_quantity: 4,
799-
max_quantity: 10,
861+
min_quantity: 20,
862+
max_quantity: null,
800863
},
801864
original_price: {
802-
id: "price-region_id-PLN-5-qty",
865+
id: "price-PLN-min-quantity-only",
803866
price_list_id: null,
804867
price_list_type: null,
805-
min_quantity: 4,
806-
max_quantity: 10,
868+
min_quantity: 20,
869+
max_quantity: null,
807870
},
808871
},
809872
])
@@ -844,15 +907,15 @@ moduleIntegrationTestRunner<IPricingModuleService>({
844907
id: "price-region_id-PLN",
845908
price_list_id: null,
846909
price_list_type: null,
847-
min_quantity: 1,
848-
max_quantity: 4,
910+
min_quantity: 20,
911+
max_quantity: null,
849912
},
850913
original_price: {
851914
id: "price-region_id-PLN",
852915
price_list_id: null,
853916
price_list_type: null,
854-
min_quantity: 1,
855-
max_quantity: 4,
917+
min_quantity: 20,
918+
max_quantity: null,
856919
},
857920
},
858921
])

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)