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

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

View File

@@ -284,5 +284,5 @@ export interface FilterablePriceSetProps
/**
* Filters to apply on a price set's associated money amounts.
*/
money_amounts?: FilterableMoneyAmountProps
prices?: FilterableMoneyAmountProps
}

View File

@@ -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", () => {
@@ -230,6 +276,46 @@ moduleIntegrationTestRunner<IPricingModuleService>({
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", () => {

View File

@@ -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<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_")
// @ts-expect-error
async listPriceSets(
@@ -137,9 +152,17 @@ export default class PricingModuleService
config: FindConfig<PricingTypes.PriceSetDTO> = {},
@MedusaContext() sharedContext: Context = {}
): 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) {
return priceSets
}
@@ -170,11 +193,15 @@ export default class PricingModuleService
config: FindConfig<PricingTypes.PriceSetDTO> = {},
@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<PricingTypes.PriceSetDTO> | undefined
) {
return {
options: {
populateWhere: { prices: { price_list_id: null } },
},
...config,
}
}
}