diff --git a/packages/core/types/src/pricing/common/price-set.ts b/packages/core/types/src/pricing/common/price-set.ts index cfcddb740c..37dae37d86 100644 --- a/packages/core/types/src/pricing/common/price-set.ts +++ b/packages/core/types/src/pricing/common/price-set.ts @@ -284,5 +284,5 @@ export interface FilterablePriceSetProps /** * Filters to apply on a price set's associated money amounts. */ - money_amounts?: FilterableMoneyAmountProps + prices?: FilterableMoneyAmountProps } diff --git a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts index dd23de31be..216f2f63bb 100644 --- a/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts +++ b/packages/modules/pricing/integration-tests/__tests__/services/pricing-module/price-set.spec.ts @@ -86,6 +86,52 @@ moduleIntegrationTestRunner({ }, ]) }) + + it("list priceSets should return only prices from a price set (and not the ones from a price list)", async () => { + const [priceList] = await service.createPriceLists([ + { + title: "test", + description: "test", + prices: [ + { + amount: 400, + currency_code: "EUR", + price_set_id: "price-set-1", + rules: { + region_id: "DE", + }, + }, + { + amount: 600, + currency_code: "EUR", + price_set_id: "price-set-1", + }, + ], + }, + ]) + + const priceSetsResult = await service.listPriceSets( + { + id: ["price-set-1"], + }, + { + relations: ["prices"], + } + ) + + expect(priceSetsResult).toHaveLength(1) + expect(priceSetsResult).toEqual([ + expect.objectContaining({ + id: "price-set-1", + prices: [ + expect.objectContaining({ + id: "price-set-money-amount-USD", + amount: 500, + }), + ], + }), + ]) + }) }) describe("listAndCount", () => { @@ -230,6 +276,46 @@ moduleIntegrationTestRunner({ id, }) }) + + it("should return priceSet with only its own prices", async () => { + const [priceList] = await service.createPriceLists([ + { + title: "test", + description: "test", + prices: [ + { + amount: 400, + currency_code: "EUR", + price_set_id: id, + rules: { + region_id: "DE", + }, + }, + { + amount: 600, + currency_code: "EUR", + price_set_id: id, + }, + ], + }, + ]) + + const priceSetResult = await service.retrievePriceSet(id, { + relations: ["prices"], + }) + + expect(priceSetResult).toEqual( + expect.objectContaining({ + id: "price-set-1", + prices: [ + expect.objectContaining({ + id: "price-set-money-amount-USD", + amount: 500, + }), + ], + }) + ) + }) }) describe("delete", () => { diff --git a/packages/modules/pricing/src/services/pricing-module.ts b/packages/modules/pricing/src/services/pricing-module.ts index 351de9f1e2..62ccd9aee5 100644 --- a/packages/modules/pricing/src/services/pricing-module.ts +++ b/packages/modules/pricing/src/services/pricing-module.ts @@ -29,7 +29,6 @@ import { MedusaContext, MedusaError, ModulesSdkUtils, - PriceListStatus, PriceListType, promiseAll, removeNullish, @@ -130,6 +129,22 @@ export default class PricingModuleService return pricingContext } + @InjectManager("baseRepository_") + // @ts-expect-error + async retrievePriceSet( + id: string, + config?: FindConfig | undefined, + sharedContext?: Context | undefined + ): Promise { + const priceSet = await this.priceSetService_.retrieve( + id, + this.normalizePriceSetConfig(config), + sharedContext + ) + + return await this.baseRepository_.serialize(priceSet) + } + @InjectManager("baseRepository_") // @ts-expect-error async listPriceSets( @@ -137,9 +152,17 @@ export default class PricingModuleService config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} ): Promise { - const pricingContext = this.setupCalculatedPriceConfig_(filters, config) + const normalizedConfig = this.normalizePriceSetConfig(config) + const pricingContext = this.setupCalculatedPriceConfig_( + filters, + normalizedConfig + ) - const priceSets = await super.listPriceSets(filters, config, sharedContext) + const priceSets = await super.listPriceSets( + filters, + normalizedConfig, + sharedContext + ) if (!pricingContext || !priceSets.length) { return priceSets } @@ -170,11 +193,15 @@ export default class PricingModuleService config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} ): Promise<[PriceSetDTO[], number]> { - const pricingContext = this.setupCalculatedPriceConfig_(filters, config) + const normalizedConfig = this.normalizePriceSetConfig(config) + const pricingContext = this.setupCalculatedPriceConfig_( + filters, + normalizedConfig + ) const [priceSets, count] = await super.listAndCountPriceSets( filters, - config, + normalizedConfig, sharedContext ) if (!pricingContext || !priceSets.length) { @@ -299,9 +326,9 @@ export default class PricingModuleService // TODO: Remove the need to refetch the data here const dbPriceSets = await this.listPriceSets( { id: priceSets.map((p) => p.id) }, - { + this.normalizePriceSetConfig({ relations: ["prices", "prices.price_rules"], - }, + }), sharedContext ) @@ -1296,4 +1323,15 @@ export default class PricingModuleService return priceListData }) } + + protected normalizePriceSetConfig( + config: FindConfig | undefined + ) { + return { + options: { + populateWhere: { prices: { price_list_id: null } }, + }, + ...config, + } + } }