fix: Only list price set prices when calling the price set methods (#7889)

FIXES CORE-2417
This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-07-01 14:24:14 +00:00
1 parent 9b5f26adab
commit 5c7c28709f
3 files changed
+132 -8

No files matched your search

@@ -284,5 +284,5 @@ export interface FilterablePriceSetProps
/** /**
* Filters to apply on a price set's associated money amounts. * Filters to apply on a price set's associated money amounts.
*/ */
money_amounts?: FilterableMoneyAmountProps prices?: FilterableMoneyAmountProps
} }
@@ -86,6 +86,52 @@ moduleIntegrationTestRunner<IPricingModuleService>({
}, },
]) ])
}) })
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", () => { describe("listAndCount", () => {
@@ -230,6 +276,46 @@ moduleIntegrationTestRunner<IPricingModuleService>({
id, 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", () => { describe("delete", () => {
@@ -29,7 +29,6 @@ import {
MedusaContext, MedusaContext,
MedusaError, MedusaError,
ModulesSdkUtils, ModulesSdkUtils,
PriceListStatus,
PriceListType, PriceListType,
promiseAll, promiseAll,
removeNullish, removeNullish,
@@ -130,6 +129,22 @@ export default class PricingModuleService
return pricingContext return pricingContext
} }
@InjectManager("baseRepository_")
// @ts-expect-error
async retrievePriceSet(
id: string,
config?: FindConfig<PriceSetDTO> | undefined,
sharedContext?: Context | undefined
): Promise<PriceSetDTO> {
const priceSet = await this.priceSetService_.retrieve(
id,
this.normalizePriceSetConfig(config),
sharedContext
)
return await this.baseRepository_.serialize<PriceSetDTO>(priceSet)
}
@InjectManager("baseRepository_") @InjectManager("baseRepository_")
// @ts-expect-error // @ts-expect-error
async listPriceSets( async listPriceSets(
@@ -137,9 +152,17 @@ export default class PricingModuleService
config: FindConfig<PricingTypes.PriceSetDTO> = {}, config: FindConfig<PricingTypes.PriceSetDTO> = {},
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<PriceSetDTO[]> { ): Promise<PriceSetDTO[]> {
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) { if (!pricingContext || !priceSets.length) {
return priceSets return priceSets
} }
@@ -170,11 +193,15 @@ export default class PricingModuleService
config: FindConfig<PricingTypes.PriceSetDTO> = {}, config: FindConfig<PricingTypes.PriceSetDTO> = {},
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<[PriceSetDTO[], number]> { ): 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( const [priceSets, count] = await super.listAndCountPriceSets(
filters, filters,
config, normalizedConfig,
sharedContext sharedContext
) )
if (!pricingContext || !priceSets.length) { if (!pricingContext || !priceSets.length) {
@@ -299,9 +326,9 @@ export default class PricingModuleService
// TODO: Remove the need to refetch the data here // TODO: Remove the need to refetch the data here
const dbPriceSets = await this.listPriceSets( const dbPriceSets = await this.listPriceSets(
{ id: priceSets.map((p) => p.id) }, { id: priceSets.map((p) => p.id) },
{ this.normalizePriceSetConfig({
relations: ["prices", "prices.price_rules"], relations: ["prices", "prices.price_rules"],
}, }),
sharedContext sharedContext
) )
@@ -1296,4 +1323,15 @@ export default class PricingModuleService
return priceListData return priceListData
}) })
} }
protected normalizePriceSetConfig(
config: FindConfig<PricingTypes.PriceSetDTO> | undefined
) {
return {
options: {
populateWhere: { prices: { price_list_id: null } },
},
...config,
}
}
} }