Fix/pricing data manipulation (#6845)
**What** - Fix returned data order - improve implentation - rm unnecessary promises and/or promise all - replace promise.all for promiseAll
This commit is contained in:
1 parent
8e3e49fbbf
commit
cf3c9b13b5
3 files changed
+294
-306
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/pricing": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix/pricing data manipulation
|
||||||
@@ -1,6 +1 @@
|
|||||||
describe("Noop test", () => {
|
it("noop", () => {})
|
||||||
it("noop check", async () => {
|
|
||||||
expect(true).toBe(true)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
@@ -21,10 +21,12 @@ import {
|
|||||||
groupBy,
|
groupBy,
|
||||||
InjectManager,
|
InjectManager,
|
||||||
InjectTransactionManager,
|
InjectTransactionManager,
|
||||||
|
isDefined,
|
||||||
MedusaContext,
|
MedusaContext,
|
||||||
MedusaError,
|
MedusaError,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
PriceListType,
|
PriceListType,
|
||||||
|
promiseAll,
|
||||||
removeNullish,
|
removeNullish,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
|
|
||||||
@@ -39,11 +41,11 @@ import {
|
|||||||
RuleType,
|
RuleType,
|
||||||
} from "@models"
|
} from "@models"
|
||||||
|
|
||||||
import {PriceListService, RuleTypeService} from "@services"
|
import { PriceListService, RuleTypeService } from "@services"
|
||||||
import {validatePriceListDates} from "@utils"
|
import { validatePriceListDates } from "@utils"
|
||||||
import {entityNameToLinkableKeysMap, joinerConfig} from "../joiner-config"
|
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||||
import {PriceSetIdPrefix} from "../models/price-set";
|
import { PriceSetIdPrefix } from "../models/price-set"
|
||||||
import {PriceListIdPrefix} from "../models/price-list";
|
import { PriceListIdPrefix } from "../models/price-list"
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
baseRepository: DAL.RepositoryService
|
baseRepository: DAL.RepositoryService
|
||||||
@@ -247,6 +249,236 @@ export default class PricingModuleService<
|
|||||||
return Array.isArray(data) ? results : results[0]
|
return Array.isArray(data) ? results : results[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async addRules(
|
||||||
|
data: PricingTypes.AddRulesDTO,
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<PricingTypes.PriceSetDTO>
|
||||||
|
|
||||||
|
async addRules(
|
||||||
|
data: PricingTypes.AddRulesDTO[],
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<PricingTypes.PriceSetDTO[]>
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async addRules(
|
||||||
|
data: PricingTypes.AddRulesDTO | PricingTypes.AddRulesDTO[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO> {
|
||||||
|
const inputs = Array.isArray(data) ? data : [data]
|
||||||
|
|
||||||
|
const priceSets = await this.addRules_(inputs, sharedContext)
|
||||||
|
|
||||||
|
const dbPriceSets = await this.list(
|
||||||
|
{ id: priceSets.map(({ id }) => id) },
|
||||||
|
{ relations: ["rule_types"] }
|
||||||
|
)
|
||||||
|
|
||||||
|
const orderedPriceSets = priceSets.map((priceSet) => {
|
||||||
|
return dbPriceSets.find((p) => p.id === priceSet.id)!
|
||||||
|
})
|
||||||
|
|
||||||
|
return Array.isArray(data) ? orderedPriceSets : orderedPriceSets[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
async addPrices(
|
||||||
|
data: AddPricesDTO,
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<PricingTypes.PriceSetDTO>
|
||||||
|
|
||||||
|
async addPrices(
|
||||||
|
data: AddPricesDTO[],
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<PricingTypes.PriceSetDTO[]>
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async addPrices(
|
||||||
|
data: AddPricesDTO | AddPricesDTO[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO> {
|
||||||
|
const input = Array.isArray(data) ? data : [data]
|
||||||
|
|
||||||
|
await this.addPrices_(input, sharedContext)
|
||||||
|
|
||||||
|
const dbPrices = await this.list(
|
||||||
|
{ id: input.map((d) => d.priceSetId) },
|
||||||
|
{ relations: ["prices"] },
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
const orderedPriceSets = input.map((inputItem) => {
|
||||||
|
return dbPrices.find((p) => p.id === inputItem.priceSetId)!
|
||||||
|
})
|
||||||
|
|
||||||
|
return Array.isArray(data) ? orderedPriceSets : orderedPriceSets[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectTransactionManager("baseRepository_")
|
||||||
|
async removeRules(
|
||||||
|
data: PricingTypes.RemovePriceSetRulesDTO[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<void> {
|
||||||
|
const priceSets = await this.priceSetService_.list(
|
||||||
|
{ id: data.map((d) => d.id) },
|
||||||
|
{},
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
const priceSetIds = priceSets.map((ps) => ps.id)
|
||||||
|
const ruleTypes = await this.ruleTypeService_.list(
|
||||||
|
{
|
||||||
|
rule_attribute: data.map((d) => d.rules || []).flat(),
|
||||||
|
},
|
||||||
|
{ take: null },
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
const ruleTypeIds = ruleTypes.map((rt) => rt.id)
|
||||||
|
const priceSetRuleTypes = await this.priceSetRuleTypeService_.list(
|
||||||
|
{ price_set_id: priceSetIds, rule_type_id: ruleTypeIds },
|
||||||
|
{ take: null },
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
const priceRules = await this.priceRuleService_.list(
|
||||||
|
{ price_set_id: priceSetIds, rule_type_id: ruleTypeIds },
|
||||||
|
{ select: ["price"], take: null },
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
await this.priceSetRuleTypeService_.delete(
|
||||||
|
priceSetRuleTypes.map((psrt) => psrt.id),
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
await this.priceService_.delete(
|
||||||
|
priceRules.map((pr) => pr.price.id),
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectTransactionManager("baseRepository_")
|
||||||
|
async update(
|
||||||
|
data: PricingTypes.UpdatePriceSetDTO[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
) {
|
||||||
|
const priceSets = await this.priceSetService_.update(data, sharedContext)
|
||||||
|
|
||||||
|
return await this.baseRepository_.serialize<PricingTypes.PriceSetDTO[]>(
|
||||||
|
priceSets
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async createPriceLists(
|
||||||
|
data: PricingTypes.CreatePriceListDTO[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<PricingTypes.PriceListDTO[]> {
|
||||||
|
const priceLists = await this.createPriceLists_(data, sharedContext)
|
||||||
|
|
||||||
|
return await this.baseRepository_.serialize<PricingTypes.PriceListDTO[]>(
|
||||||
|
priceLists
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectTransactionManager("baseRepository_")
|
||||||
|
async updatePriceLists(
|
||||||
|
data: PricingTypes.UpdatePriceListDTO[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<PricingTypes.PriceListDTO[]> {
|
||||||
|
const priceLists = await this.updatePriceLists_(data, sharedContext)
|
||||||
|
|
||||||
|
return await this.baseRepository_.serialize<PricingTypes.PriceListDTO[]>(
|
||||||
|
priceLists
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async createPriceListRules(
|
||||||
|
data: PricingTypes.CreatePriceListRuleDTO[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<PricingTypes.PriceListRuleDTO[]> {
|
||||||
|
const priceLists = await this.createPriceListRules_(data, sharedContext)
|
||||||
|
|
||||||
|
return await this.baseRepository_.serialize<
|
||||||
|
PricingTypes.PriceListRuleDTO[]
|
||||||
|
>(priceLists, {
|
||||||
|
populate: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectTransactionManager("baseRepository_")
|
||||||
|
async createPriceListRules_(
|
||||||
|
data: PricingTypes.CreatePriceListRuleDTO[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
) {
|
||||||
|
return await this.priceListRuleService_.create(data, sharedContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectTransactionManager("baseRepository_")
|
||||||
|
async updatePriceListRules(
|
||||||
|
data: PricingTypes.UpdatePriceListRuleDTO[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<PricingTypes.PriceListRuleDTO[]> {
|
||||||
|
const priceLists = await this.priceListRuleService_.update(
|
||||||
|
data,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
return await this.baseRepository_.serialize<
|
||||||
|
PricingTypes.PriceListRuleDTO[]
|
||||||
|
>(priceLists, {
|
||||||
|
populate: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async updatePriceListPrices(
|
||||||
|
data: PricingTypes.UpdatePriceListPricesDTO[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<PricingTypes.PriceListDTO[]> {
|
||||||
|
return await this.updatePriceListPrices_(data, sharedContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async removePrices(
|
||||||
|
ids: string[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<void> {
|
||||||
|
await this.removePrices_(ids, sharedContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async addPriceListPrices(
|
||||||
|
data: PricingTypes.AddPriceListPricesDTO[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<PricingTypes.PriceListDTO[]> {
|
||||||
|
return await this.addPriceListPrices_(data, sharedContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async setPriceListRules(
|
||||||
|
data: PricingTypes.SetPriceListRulesDTO,
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<PricingTypes.PriceListDTO> {
|
||||||
|
const [priceList] = await this.setPriceListRules_([data], sharedContext)
|
||||||
|
|
||||||
|
return await this.baseRepository_.serialize<PricingTypes.PriceListDTO>(
|
||||||
|
priceList
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async removePriceListRules(
|
||||||
|
data: PricingTypes.RemovePriceListRulesDTO,
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<PricingTypes.PriceListDTO> {
|
||||||
|
const [priceList] = await this.removePriceListRules_([data], sharedContext)
|
||||||
|
|
||||||
|
return await this.baseRepository_.serialize<PricingTypes.PriceListDTO>(
|
||||||
|
priceList
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async create_(
|
protected async create_(
|
||||||
data: PricingTypes.CreatePriceSetDTO[],
|
data: PricingTypes.CreatePriceSetDTO[],
|
||||||
@@ -379,31 +611,6 @@ export default class PricingModuleService<
|
|||||||
return createdPriceSets
|
return createdPriceSets
|
||||||
}
|
}
|
||||||
|
|
||||||
async addRules(
|
|
||||||
data: PricingTypes.AddRulesDTO,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<PricingTypes.PriceSetDTO>
|
|
||||||
|
|
||||||
async addRules(
|
|
||||||
data: PricingTypes.AddRulesDTO[],
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<PricingTypes.PriceSetDTO[]>
|
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
|
||||||
async addRules(
|
|
||||||
data: PricingTypes.AddRulesDTO | PricingTypes.AddRulesDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO> {
|
|
||||||
const inputs = Array.isArray(data) ? data : [data]
|
|
||||||
|
|
||||||
const priceSets = await this.addRules_(inputs, sharedContext)
|
|
||||||
|
|
||||||
return await this.list(
|
|
||||||
{ id: priceSets.map(({ id }) => id) },
|
|
||||||
{ relations: ["rule_types"] }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async addRules_(
|
protected async addRules_(
|
||||||
inputs: PricingTypes.AddRulesDTO[],
|
inputs: PricingTypes.AddRulesDTO[],
|
||||||
@@ -487,32 +694,6 @@ export default class PricingModuleService<
|
|||||||
return priceSets
|
return priceSets
|
||||||
}
|
}
|
||||||
|
|
||||||
async addPrices(
|
|
||||||
data: AddPricesDTO,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<PricingTypes.PriceSetDTO>
|
|
||||||
|
|
||||||
async addPrices(
|
|
||||||
data: AddPricesDTO[],
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<PricingTypes.PriceSetDTO[]>
|
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
|
||||||
async addPrices(
|
|
||||||
data: AddPricesDTO | AddPricesDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO> {
|
|
||||||
const input = Array.isArray(data) ? data : [data]
|
|
||||||
|
|
||||||
await this.addPrices_(input, sharedContext)
|
|
||||||
|
|
||||||
return await this.list(
|
|
||||||
{ id: input.map((d) => d.priceSetId) },
|
|
||||||
{ relations: ["prices"] },
|
|
||||||
sharedContext
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async addPrices_(
|
protected async addPrices_(
|
||||||
input: AddPricesDTO[],
|
input: AddPricesDTO[],
|
||||||
@@ -566,110 +747,25 @@ export default class PricingModuleService<
|
|||||||
prices.map((price) => {
|
prices.map((price) => {
|
||||||
const numberOfRules = Object.entries(price?.rules ?? {}).length
|
const numberOfRules = Object.entries(price?.rules ?? {}).length
|
||||||
|
|
||||||
|
const priceRules = Object.entries(price.rules ?? {}).map(
|
||||||
|
([attribute, value]) => ({
|
||||||
|
rule_type_id: ruleTypeMap.get(priceSetId)!.get(attribute)!.id,
|
||||||
|
price_set_id: priceSetId,
|
||||||
|
value,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...price,
|
...price,
|
||||||
price_set_id: priceSetId,
|
price_set_id: priceSetId,
|
||||||
title: "test", // TODO: accept title
|
title: "test", // TODO: accept title
|
||||||
rules_count: numberOfRules,
|
rules_count: numberOfRules,
|
||||||
|
priceRules,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
const createdPrices = await this.priceService_.create(
|
await this.priceService_.create(pricesToCreate, sharedContext)
|
||||||
pricesToCreate,
|
|
||||||
sharedContext
|
|
||||||
)
|
|
||||||
|
|
||||||
// Price rules
|
|
||||||
let rulesCursor = 0
|
|
||||||
const priceRulesBulkData = input.flatMap(({ priceSetId, prices }) =>
|
|
||||||
prices.flatMap((ma) => {
|
|
||||||
const rules = ma.rules ?? {}
|
|
||||||
const price = createdPrices[rulesCursor]
|
|
||||||
rulesCursor++
|
|
||||||
|
|
||||||
return Object.entries(rules).map(([k, v]) => ({
|
|
||||||
price_id: price.id,
|
|
||||||
rule_type_id: ruleTypeMap.get(priceSetId)!.get(k)!.id,
|
|
||||||
price_set_id: priceSetId,
|
|
||||||
value: v,
|
|
||||||
}))
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
if (priceRulesBulkData.length > 0) {
|
|
||||||
await this.priceRuleService_.create(priceRulesBulkData, sharedContext)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
|
||||||
async removeRules(
|
|
||||||
data: PricingTypes.RemovePriceSetRulesDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<void> {
|
|
||||||
const priceSets = await this.priceSetService_.list(
|
|
||||||
{ id: data.map((d) => d.id) },
|
|
||||||
{},
|
|
||||||
sharedContext
|
|
||||||
)
|
|
||||||
|
|
||||||
const priceSetIds = priceSets.map((ps) => ps.id)
|
|
||||||
const ruleTypes = await this.ruleTypeService_.list(
|
|
||||||
{
|
|
||||||
rule_attribute: data.map((d) => d.rules || []).flat(),
|
|
||||||
},
|
|
||||||
{ take: null },
|
|
||||||
sharedContext
|
|
||||||
)
|
|
||||||
|
|
||||||
const ruleTypeIds = ruleTypes.map((rt) => rt.id)
|
|
||||||
const priceSetRuleTypes = await this.priceSetRuleTypeService_.list(
|
|
||||||
{ price_set_id: priceSetIds, rule_type_id: ruleTypeIds },
|
|
||||||
{ take: null },
|
|
||||||
sharedContext
|
|
||||||
)
|
|
||||||
|
|
||||||
const priceRules = await this.priceRuleService_.list(
|
|
||||||
{ price_set_id: priceSetIds, rule_type_id: ruleTypeIds },
|
|
||||||
{ select: ["price"], take: null },
|
|
||||||
sharedContext
|
|
||||||
)
|
|
||||||
|
|
||||||
await this.priceSetRuleTypeService_.delete(
|
|
||||||
priceSetRuleTypes.map((psrt) => psrt.id),
|
|
||||||
sharedContext
|
|
||||||
)
|
|
||||||
|
|
||||||
await this.priceService_.delete(
|
|
||||||
priceRules.map((pr) => pr.price.id),
|
|
||||||
sharedContext
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
|
||||||
async update(
|
|
||||||
data: PricingTypes.UpdatePriceSetDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
) {
|
|
||||||
const priceSets = await this.priceSetService_.update(data, sharedContext)
|
|
||||||
|
|
||||||
return await this.baseRepository_.serialize<PricingTypes.PriceSetDTO[]>(
|
|
||||||
priceSets,
|
|
||||||
{ populate: true }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
|
||||||
async createPriceLists(
|
|
||||||
data: PricingTypes.CreatePriceListDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<PricingTypes.PriceListDTO[]> {
|
|
||||||
const priceLists = await this.createPriceLists_(data, sharedContext)
|
|
||||||
|
|
||||||
return await this.baseRepository_.serialize<PricingTypes.PriceListDTO[]>(
|
|
||||||
priceLists,
|
|
||||||
{ populate: true }
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
@@ -732,7 +828,7 @@ export default class PricingModuleService<
|
|||||||
return {
|
return {
|
||||||
price_list_id: id,
|
price_list_id: id,
|
||||||
rule_type_id: ruleType.id,
|
rule_type_id: ruleType.id,
|
||||||
price_list_rule_values: value.map((v) => ({ value: v }))
|
price_list_rule_values: value.map((v) => ({ value: v })),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -773,19 +869,6 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
|
||||||
async updatePriceLists(
|
|
||||||
data: PricingTypes.UpdatePriceListDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<PricingTypes.PriceListDTO[]> {
|
|
||||||
const priceLists = await this.updatePriceLists_(data, sharedContext)
|
|
||||||
|
|
||||||
return await this.baseRepository_.serialize<PricingTypes.PriceListDTO[]>(
|
|
||||||
priceLists,
|
|
||||||
{ populate: true }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async updatePriceLists_(
|
protected async updatePriceLists_(
|
||||||
data: PricingTypes.UpdatePriceListDTO[],
|
data: PricingTypes.UpdatePriceListDTO[],
|
||||||
@@ -891,53 +974,6 @@ export default class PricingModuleService<
|
|||||||
return updatedPriceLists
|
return updatedPriceLists
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
|
||||||
async createPriceListRules(
|
|
||||||
data: PricingTypes.CreatePriceListRuleDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<PricingTypes.PriceListRuleDTO[]> {
|
|
||||||
const priceLists = await this.createPriceListRules_(data, sharedContext)
|
|
||||||
|
|
||||||
return await this.baseRepository_.serialize<
|
|
||||||
PricingTypes.PriceListRuleDTO[]
|
|
||||||
>(priceLists, {
|
|
||||||
populate: true,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
|
||||||
async createPriceListRules_(
|
|
||||||
data: PricingTypes.CreatePriceListRuleDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
) {
|
|
||||||
return await this.priceListRuleService_.create(data, sharedContext)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
|
||||||
async updatePriceListRules(
|
|
||||||
data: PricingTypes.UpdatePriceListRuleDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<PricingTypes.PriceListRuleDTO[]> {
|
|
||||||
const priceLists = await this.priceListRuleService_.update(
|
|
||||||
data,
|
|
||||||
sharedContext
|
|
||||||
)
|
|
||||||
|
|
||||||
return await this.baseRepository_.serialize<
|
|
||||||
PricingTypes.PriceListRuleDTO[]
|
|
||||||
>(priceLists, {
|
|
||||||
populate: true,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
|
||||||
async updatePriceListPrices(
|
|
||||||
data: PricingTypes.UpdatePriceListPricesDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<PricingTypes.PriceListDTO[]> {
|
|
||||||
return await this.updatePriceListPrices_(data, sharedContext)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async updatePriceListPrices_(
|
protected async updatePriceListPrices_(
|
||||||
data: PricingTypes.UpdatePriceListPricesDTO[],
|
data: PricingTypes.UpdatePriceListPricesDTO[],
|
||||||
@@ -1046,36 +1082,34 @@ export default class PricingModuleService<
|
|||||||
|
|
||||||
const priceRuleIdsToDelete: string[] = []
|
const priceRuleIdsToDelete: string[] = []
|
||||||
const priceRulesToCreate: PricingTypes.CreatePriceRuleDTO[] = []
|
const priceRulesToCreate: PricingTypes.CreatePriceRuleDTO[] = []
|
||||||
const pricesToUpdate: (PricingTypes.UpdatePriceDTO & {
|
const pricesToUpdate: Partial<TPrice>[] = []
|
||||||
rules_count: number
|
|
||||||
})[] = []
|
|
||||||
|
|
||||||
for (const priceData of prices) {
|
for (const priceData of prices) {
|
||||||
const { rules, price_set_id, ...rest } = priceData
|
const { rules, price_set_id, ...rest } = priceData
|
||||||
const price = priceMap.get(rest.id)!
|
const price = priceMap.get(rest.id)!
|
||||||
const priceRules = price.price_rules!
|
const priceRules = price.price_rules!
|
||||||
|
|
||||||
if (typeof rules === "undefined") {
|
if (!isDefined(rules)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
pricesToUpdate.push({
|
pricesToUpdate.push({
|
||||||
...rest,
|
...rest,
|
||||||
rules_count: Object.keys(rules).length,
|
rules_count: Object.keys(rules).length,
|
||||||
})
|
price_rules: Object.entries(rules).map(
|
||||||
|
([ruleAttribute, ruleValue]) => ({
|
||||||
priceRuleIdsToDelete.push(...priceRules.map((pr) => pr.id))
|
|
||||||
priceRulesToCreate.push(
|
|
||||||
...Object.entries(rules).map(([ruleAttribute, ruleValue]) => ({
|
|
||||||
price_set_id,
|
price_set_id,
|
||||||
rule_type_id: ruleTypeMap.get(ruleAttribute)!.id,
|
rule_type_id: ruleTypeMap.get(ruleAttribute)!.id,
|
||||||
value: ruleValue,
|
value: ruleValue,
|
||||||
price_id: price.id,
|
price_id: price.id,
|
||||||
}))
|
})
|
||||||
)
|
),
|
||||||
|
} as unknown as TPrice)
|
||||||
|
|
||||||
|
priceRuleIdsToDelete.push(...priceRules.map((pr) => pr.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all([
|
await promiseAll([
|
||||||
this.priceRuleService_.delete(priceRuleIdsToDelete),
|
this.priceRuleService_.delete(priceRuleIdsToDelete),
|
||||||
this.priceRuleService_.create(priceRulesToCreate),
|
this.priceRuleService_.create(priceRulesToCreate),
|
||||||
this.priceService_.update(pricesToUpdate),
|
this.priceService_.update(pricesToUpdate),
|
||||||
@@ -1085,14 +1119,6 @@ export default class PricingModuleService<
|
|||||||
return priceLists
|
return priceLists
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
|
||||||
async removePrices(
|
|
||||||
ids: string[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<void> {
|
|
||||||
await this.removePrices_(ids, sharedContext)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async removePrices_(
|
protected async removePrices_(
|
||||||
ids: string[],
|
ids: string[],
|
||||||
@@ -1101,14 +1127,6 @@ export default class PricingModuleService<
|
|||||||
await this.priceService_.delete(ids, sharedContext)
|
await this.priceService_.delete(ids, sharedContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
|
||||||
async addPriceListPrices(
|
|
||||||
data: PricingTypes.AddPriceListPricesDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<PricingTypes.PriceListDTO[]> {
|
|
||||||
return await this.addPriceListPrices_(data, sharedContext)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async addPriceListPrices_(
|
protected async addPriceListPrices_(
|
||||||
data: PricingTypes.AddPriceListPricesDTO[],
|
data: PricingTypes.AddPriceListPricesDTO[],
|
||||||
@@ -1191,6 +1209,8 @@ export default class PricingModuleService<
|
|||||||
|
|
||||||
const priceListMap = new Map(priceLists.map((p) => [p.id, p]))
|
const priceListMap = new Map(priceLists.map((p) => [p.id, p]))
|
||||||
|
|
||||||
|
const pricesToCreate: Partial<TPrice>[] = []
|
||||||
|
|
||||||
for (const { price_list_id: priceListId, prices } of data) {
|
for (const { price_list_id: priceListId, prices } of data) {
|
||||||
const priceList = priceListMap.get(priceListId)
|
const priceList = priceListMap.get(priceListId)
|
||||||
|
|
||||||
@@ -1201,59 +1221,45 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(
|
const priceListPricesToCreate = prices.map((priceData) => {
|
||||||
prices.map(async (priceData) => {
|
|
||||||
const priceRules = priceData.rules || {}
|
const priceRules = priceData.rules || {}
|
||||||
const noOfRules = Object.keys(priceRules).length
|
const noOfRules = Object.keys(priceRules).length
|
||||||
|
|
||||||
const [price] = await this.priceService_.create(
|
const priceRulesToCreate = Object.entries(priceRules).map(
|
||||||
[
|
([ruleAttribute, ruleValue]) => {
|
||||||
{
|
return {
|
||||||
|
price_set_id: priceData.price_set_id,
|
||||||
|
rule_type_id: ruleTypeMap.get(ruleAttribute)!?.id,
|
||||||
|
value: ruleValue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
...priceData,
|
...priceData,
|
||||||
price_set_id: priceData.price_set_id,
|
price_set_id: priceData.price_set_id,
|
||||||
title: "test",
|
title: "test",
|
||||||
price_list_id: priceList.id,
|
price_list_id: priceList.id,
|
||||||
rules_count: noOfRules,
|
rules_count: noOfRules,
|
||||||
},
|
price_rules: priceRulesToCreate,
|
||||||
],
|
} as unknown as TPrice
|
||||||
sharedContext
|
|
||||||
)
|
|
||||||
|
|
||||||
await this.priceRuleService_.create(
|
|
||||||
Object.entries(priceRules).map(([ruleAttribute, ruleValue]) => {
|
|
||||||
return {
|
|
||||||
price_set_id: priceData.price_set_id,
|
|
||||||
rule_type_id: ruleTypeMap.get(ruleAttribute)!?.id,
|
|
||||||
value: ruleValue,
|
|
||||||
price_id: price.id,
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
sharedContext
|
|
||||||
)
|
|
||||||
|
|
||||||
return price
|
|
||||||
})
|
})
|
||||||
)
|
|
||||||
|
pricesToCreate.push(...priceListPricesToCreate)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this.priceService_.create(pricesToCreate, sharedContext)
|
||||||
|
|
||||||
return priceLists
|
return priceLists
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
|
||||||
async setPriceListRules(
|
|
||||||
data: PricingTypes.SetPriceListRulesDTO,
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<PricingTypes.PriceListDTO> {
|
|
||||||
const [priceList] = await this.setPriceListRules_([data], sharedContext)
|
|
||||||
|
|
||||||
return priceList
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async setPriceListRules_(
|
protected async setPriceListRules_(
|
||||||
data: PricingTypes.SetPriceListRulesDTO[],
|
data: PricingTypes.SetPriceListRulesDTO[],
|
||||||
sharedContext: Context = {}
|
sharedContext: Context = {}
|
||||||
): Promise<PricingTypes.PriceListDTO[]> {
|
): Promise<TPriceList[]> {
|
||||||
|
// TODO: re think this method
|
||||||
|
|
||||||
const priceLists = await this.priceListService_.list(
|
const priceLists = await this.priceListService_.list(
|
||||||
{ id: data.map((d) => d.price_list_id) },
|
{ id: data.map((d) => d.price_list_id) },
|
||||||
{ relations: ["price_list_rules", "price_list_rules.rule_type"] },
|
{ relations: ["price_list_rules", "price_list_rules.rule_type"] },
|
||||||
@@ -1267,6 +1273,7 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
|
|
||||||
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]))
|
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]))
|
||||||
|
|
||||||
const ruleIdsToUpdate: string[] = []
|
const ruleIdsToUpdate: string[] = []
|
||||||
const rulesToCreate: CreatePriceListRuleDTO[] = []
|
const rulesToCreate: CreatePriceListRuleDTO[] = []
|
||||||
const priceRuleValues = new Map<string, Map<string, string[]>>()
|
const priceRuleValues = new Map<string, Map<string, string[]>>()
|
||||||
@@ -1286,7 +1293,7 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
|
|
||||||
const priceListRuleValues = new Map<string, string[]>()
|
const priceListRuleValues = new Map<string, string[]>()
|
||||||
await Promise.all(
|
|
||||||
Object.entries(rules).map(async ([key, value]) => {
|
Object.entries(rules).map(async ([key, value]) => {
|
||||||
const ruleType = ruleTypeMap.get(key)
|
const ruleType = ruleTypeMap.get(key)
|
||||||
if (!ruleType) {
|
if (!ruleType) {
|
||||||
@@ -1312,12 +1319,11 @@ export default class PricingModuleService<
|
|||||||
ruleIdsToUpdate.push(rule.id)
|
ruleIdsToUpdate.push(rule.id)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
|
||||||
|
|
||||||
priceRuleValues.set(priceListId, priceListRuleValues)
|
priceRuleValues.set(priceListId, priceListRuleValues)
|
||||||
}
|
}
|
||||||
|
|
||||||
const [createdRules, priceListValuesToDelete] = await Promise.all([
|
const [createdRules, priceListValuesToDelete] = await promiseAll([
|
||||||
this.priceListRuleService_.create(rulesToCreate),
|
this.priceListRuleService_.create(rulesToCreate),
|
||||||
this.priceListRuleValueService_.list(
|
this.priceListRuleValueService_.list(
|
||||||
{ price_list_rule_id: ruleIdsToUpdate },
|
{ price_list_rule_id: ruleIdsToUpdate },
|
||||||
@@ -1348,7 +1354,7 @@ export default class PricingModuleService<
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all([
|
await promiseAll([
|
||||||
this.priceListRuleValueService_.delete(
|
this.priceListRuleValueService_.delete(
|
||||||
priceListValuesToDelete.map((p) => p.id),
|
priceListValuesToDelete.map((p) => p.id),
|
||||||
sharedContext
|
sharedContext
|
||||||
@@ -1359,27 +1365,14 @@ export default class PricingModuleService<
|
|||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
|
||||||
return await this.baseRepository_.serialize<PricingTypes.PriceListDTO[]>(
|
return priceLists
|
||||||
priceLists,
|
|
||||||
{ populate: true }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
|
||||||
async removePriceListRules(
|
|
||||||
data: PricingTypes.RemovePriceListRulesDTO,
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<PricingTypes.PriceListDTO> {
|
|
||||||
const [priceList] = await this.removePriceListRules_([data], sharedContext)
|
|
||||||
|
|
||||||
return priceList
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
protected async removePriceListRules_(
|
protected async removePriceListRules_(
|
||||||
data: PricingTypes.RemovePriceListRulesDTO[],
|
data: PricingTypes.RemovePriceListRulesDTO[],
|
||||||
sharedContext: Context = {}
|
sharedContext: Context = {}
|
||||||
): Promise<PricingTypes.PriceListDTO[]> {
|
): Promise<TPriceList[]> {
|
||||||
const priceLists = await this.priceListService_.list(
|
const priceLists = await this.priceListService_.list(
|
||||||
{ id: data.map((d) => d.price_list_id) },
|
{ id: data.map((d) => d.price_list_id) },
|
||||||
{ relations: ["price_list_rules", "price_list_rules.rule_type"] },
|
{ relations: ["price_list_rules", "price_list_rules.rule_type"] },
|
||||||
@@ -1403,7 +1396,6 @@ export default class PricingModuleService<
|
|||||||
priceList.price_list_rules.map((p) => [p.rule_type.rule_attribute, p])
|
priceList.price_list_rules.map((p) => [p.rule_type.rule_attribute, p])
|
||||||
)
|
)
|
||||||
|
|
||||||
await Promise.all(
|
|
||||||
rules.map(async (rule_attribute) => {
|
rules.map(async (rule_attribute) => {
|
||||||
const rule = priceListRulesMap.get(rule_attribute)
|
const rule = priceListRulesMap.get(rule_attribute)
|
||||||
|
|
||||||
@@ -1411,14 +1403,10 @@ export default class PricingModuleService<
|
|||||||
idsToDelete.push(rule.id)
|
idsToDelete.push(rule.id)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.priceListRuleService_.delete(idsToDelete)
|
await this.priceListRuleService_.delete(idsToDelete)
|
||||||
|
|
||||||
return await this.baseRepository_.serialize<PricingTypes.PriceListDTO[]>(
|
return priceLists
|
||||||
priceLists,
|
|
||||||
{ populate: true }
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user