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 9bcfbd3ab3..7be89fd8f2 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 @@ -288,6 +288,25 @@ moduleIntegrationTestRunner({ ) }) + it("calculating prices when listing price sets should return null when there are no prices", async () => { + const [newSet] = await service.createPriceSets([{}]) + const calculatePricesResult = await service.calculatePrices( + { id: [newSet.id] }, + { context: { currency_code: "PLN" } } + ) + + const priceSetsResult = await service.listPriceSets( + { + id: [newSet.id], + context: { currency_code: "PLN" }, + }, + { relations: ["calculated_price"] } + ) + + expect(calculatePricesResult).toEqual([]) + expect(priceSetsResult[0].calculated_price).toEqual(null) + }) + it("should return filled prices when 1 context is present and price is setup for PLN", async () => { const priceSetsResult = await service.calculatePrices( { id: ["price-set-EUR", "price-set-PLN"] }, @@ -297,28 +316,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: false, @@ -353,28 +350,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: false, @@ -409,28 +384,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: false, @@ -464,52 +417,7 @@ moduleIntegrationTestRunner({ } ) - expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, - { - id: "price-set-PLN", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, - ]) + expect(priceSetsResult).toEqual([]) }) it("should return filled prices when 2 contexts are present and price is setup", async () => { @@ -521,28 +429,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: false, @@ -577,28 +463,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: false, @@ -672,28 +536,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: false, @@ -733,28 +575,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, // Currency Code + Region value + customer group id { id: "price-set-PLN", @@ -795,28 +615,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, // PLN price set is not setup for EUR currency_code so it will default to a null set { id: "price-set-PLN", @@ -857,28 +655,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, // PLN price set is not setup for EUR currency_code so it will default to a null set { id: "price-set-PLN", @@ -913,52 +689,7 @@ moduleIntegrationTestRunner({ } ) - expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, - { - id: "price-set-PLN", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, - ]) + expect(priceSetsResult).toEqual([]) }) it("should return filled prices when 2 context is present and prices are setup, but only for one of the contexts", async () => { @@ -974,28 +705,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: false, @@ -1038,28 +747,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: true, @@ -1109,28 +796,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: true, @@ -1172,28 +837,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: true, @@ -1232,28 +875,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: true, @@ -1295,28 +916,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: true, @@ -1357,28 +956,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: false, @@ -1419,28 +996,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: false, @@ -1494,28 +1049,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: true, @@ -1570,28 +1103,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: false, @@ -1645,28 +1156,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: false, @@ -1773,28 +1262,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: true, @@ -1846,28 +1313,6 @@ moduleIntegrationTestRunner({ ) expect(priceSetsResult).toEqual([ - { - id: "price-set-EUR", - is_calculated_price_price_list: false, - calculated_amount: null, - is_original_price_price_list: false, - original_amount: null, - currency_code: null, - calculated_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - original_price: { - id: null, - price_list_id: null, - price_list_type: null, - min_quantity: null, - max_quantity: null, - }, - }, { id: "price-set-PLN", is_calculated_price_price_list: true, diff --git a/packages/modules/pricing/src/services/pricing-module.ts b/packages/modules/pricing/src/services/pricing-module.ts index e14eb2f9ab..351de9f1e2 100644 --- a/packages/modules/pricing/src/services/pricing-module.ts +++ b/packages/modules/pricing/src/services/pricing-module.ts @@ -144,21 +144,20 @@ export default class PricingModuleService return priceSets } - const priceSetIds: string[] = [] - const priceSetMap = new Map() - for (const priceSet of priceSets) { - priceSetIds.push(priceSet.id) - priceSetMap.set(priceSet.id, priceSet) - } - const calculatedPrices = await this.calculatePrices( - { id: priceSetIds }, + { id: priceSets.map((p) => p.id) }, { context: pricingContext }, sharedContext ) + + const calculatedPricesMap = new Map() for (const calculatedPrice of calculatedPrices) { - const priceSet = priceSetMap.get(calculatedPrice.id) - priceSet.calculated_price = calculatedPrice + calculatedPricesMap.set(calculatedPrice.id, calculatedPrice) + } + + for (const priceSet of priceSets) { + const calculatedPrice = calculatedPricesMap.get(priceSet.id) + priceSet.calculated_price = calculatedPrice ?? null } return priceSets @@ -182,21 +181,20 @@ export default class PricingModuleService return [priceSets, count] } - const priceSetIds: string[] = [] - const priceSetMap = new Map() - for (const priceSet of priceSets) { - priceSetIds.push(priceSet.id) - priceSetMap.set(priceSet.id, priceSet) - } - const calculatedPrices = await this.calculatePrices( - { id: priceSetIds }, + { id: priceSets.map((p) => p.id) }, { context: pricingContext }, sharedContext ) + + const calculatedPricesMap = new Map() for (const calculatedPrice of calculatedPrices) { - const priceSet = priceSetMap.get(calculatedPrice.id) - priceSet.calculated_price = calculatedPrice + calculatedPricesMap.set(calculatedPrice.id, calculatedPrice) + } + + for (const priceSet of priceSets) { + const calculatedPrice = calculatedPricesMap.get(priceSet.id) + priceSet.calculated_price = calculatedPrice ?? null } return [priceSets, count] @@ -217,14 +215,18 @@ export default class PricingModuleService const pricesSetPricesMap = groupBy(results, "price_set_id") const calculatedPrices: PricingTypes.CalculatedPriceSet[] = - pricingFilters.id.map( - (priceSetId: string): PricingTypes.CalculatedPriceSet => { + pricingFilters.id + .map((priceSetId: string): PricingTypes.CalculatedPriceSet | null => { // This is where we select prices, for now we just do a first match based on the database results // which is prioritized by rules_count first for exact match and then deafult_priority of the rule_type // TODO: inject custom price selection here const prices = pricesSetPricesMap.get(priceSetId) || [] + if (!prices.length) { + return null + } + const priceListPrice = prices.find((p) => p.price_list_id) const defaultPrice = prices?.find((p) => !p.price_list_id) @@ -268,8 +270,8 @@ export default class PricingModuleService max_quantity: parseInt(originalPrice?.max_quantity || "") || null, }, } - } - ) + }) + .filter(Boolean) as PricingTypes.CalculatedPriceSet[] return JSON.parse(JSON.stringify(calculatedPrices)) }