diff --git a/.changeset/dry-bikes-burn.md b/.changeset/dry-bikes-burn.md new file mode 100644 index 0000000000..e09f816196 --- /dev/null +++ b/.changeset/dry-bikes-burn.md @@ -0,0 +1,5 @@ +--- +"@medusajs/pricing": patch +--- + +fix(pricing): fix pricing query when max_quantity is null diff --git a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts index 3b093b49ec..2d3239e36a 100644 --- a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts +++ b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts @@ -76,6 +76,16 @@ moduleIntegrationTestRunner({ 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({ ]) }) + 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"] }, diff --git a/packages/modules/pricing/src/repositories/pricing.ts b/packages/modules/pricing/src/repositories/pricing.ts index 43bee3505b..65f000d9a7 100644 --- a/packages/modules/pricing/src/repositories/pricing.ts +++ b/packages/modules/pricing/src/repositories/pricing.ts @@ -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 {