fix(pricing): fix pricing query when max_quantity is null (#12981)
what: Prices when max_quantity value is null is accounted for when quantity is passed in context. Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
5
.changeset/dry-bikes-burn.md
Normal file
5
.changeset/dry-bikes-burn.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/pricing": patch
|
||||
---
|
||||
|
||||
fix(pricing): fix pricing query when max_quantity is null
|
||||
@@ -76,6 +76,16 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
max_quantity: 10,
|
||||
rules_count: 0,
|
||||
},
|
||||
{
|
||||
id: "price-PLN-min-quantity-only",
|
||||
title: "price PLN - min quantity only",
|
||||
price_set_id: "price-set-PLN",
|
||||
currency_code: "PLN",
|
||||
amount: 1250,
|
||||
min_quantity: 20,
|
||||
max_quantity: null,
|
||||
rules_count: 0,
|
||||
},
|
||||
{
|
||||
id: "price-ETH",
|
||||
title: "price ETH",
|
||||
@@ -451,6 +461,55 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
])
|
||||
})
|
||||
|
||||
it("should successfully calculate prices where only min quantity is set", async () => {
|
||||
const context = {
|
||||
currency_code: "PLN",
|
||||
region_id: "PL",
|
||||
quantity: 255,
|
||||
}
|
||||
|
||||
const calculatedPrice = await service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{ context }
|
||||
)
|
||||
|
||||
|
||||
expect(calculatedPrice).toEqual([
|
||||
{
|
||||
id: "price-set-PLN",
|
||||
is_calculated_price_price_list: false,
|
||||
is_calculated_price_tax_inclusive: false,
|
||||
calculated_amount: 1250,
|
||||
raw_calculated_amount: {
|
||||
value: "1250",
|
||||
precision: 20,
|
||||
},
|
||||
is_original_price_price_list: false,
|
||||
is_original_price_tax_inclusive: false,
|
||||
original_amount: 1250,
|
||||
raw_original_amount: {
|
||||
value: "1250",
|
||||
precision: 20,
|
||||
},
|
||||
currency_code: "PLN",
|
||||
calculated_price: {
|
||||
id: "price-PLN-min-quantity-only",
|
||||
price_list_id: null,
|
||||
price_list_type: null,
|
||||
min_quantity: 20,
|
||||
max_quantity: null,
|
||||
},
|
||||
original_price: {
|
||||
id: "price-PLN-min-quantity-only",
|
||||
price_list_id: null,
|
||||
price_list_type: null,
|
||||
min_quantity: 20,
|
||||
max_quantity: null,
|
||||
},
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should throw an error when currency code is not set", async () => {
|
||||
let result = service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
|
||||
@@ -126,14 +126,24 @@ export class PricingRepository
|
||||
|
||||
if (quantity !== undefined) {
|
||||
query.andWhere(function (this: Knex.QueryBuilder) {
|
||||
this.where(function (this: Knex.QueryBuilder) {
|
||||
this.orWhere(function (this: Knex.QueryBuilder) {
|
||||
this.where("price.min_quantity", "<=", quantity).andWhere(
|
||||
"price.max_quantity",
|
||||
">=",
|
||||
quantity
|
||||
)
|
||||
}).orWhere(function (this: Knex.QueryBuilder) {
|
||||
this.whereNull("price.min_quantity").whereNull("price.max_quantity")
|
||||
|
||||
this.orWhere("price.min_quantity", "<=", quantity).whereNull(
|
||||
"price.max_quantity"
|
||||
)
|
||||
|
||||
this.orWhereNull("price.min_quantity").whereNull("price.max_quantity")
|
||||
|
||||
this.orWhereNull("price.min_quantity").andWhere(
|
||||
"price.max_quantity",
|
||||
">=",
|
||||
quantity
|
||||
)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user