chore: pricing models uses standardized relationships attributes (#6767)

what:

- all relationships under all models are standardized
This commit is contained in:
Riqwan Thamir
2024-03-21 11:01:52 +01:00
committed by GitHub
parent d930ebc1ed
commit cf8b5ce85b
16 changed files with 157 additions and 402 deletions

View File

@@ -590,7 +590,7 @@ medusaIntegrationTestRunner({
prices: [],
})
const [priceList] = await pricingModule.createPriceLists([
const [createdPriceList] = await pricingModule.createPriceLists([
{
title: "test price list",
description: "test",
@@ -607,6 +607,10 @@ medusaIntegrationTestRunner({
},
])
const [priceList] = await pricingModule.listPriceLists(
{ id: [createdPriceList.id] },
{ relations: ["price_set_money_amounts"] }
)
const psmaIdToDelete = priceList.price_set_money_amounts![0].id
const response = await api.post(

View File

@@ -1,12 +1,12 @@
export const defaultPriceListRuleData = [
{
id: "price-list-rule-1",
price_list: "price-list-1",
rule_type: "rule-type-1",
price_list_id: "price-list-1",
rule_type_id: "rule-type-1",
},
{
id: "price-list-rule-2",
price_list: "price-list-1",
rule_type: "rule-type-2",
price_list_id: "price-list-1",
rule_type_id: "rule-type-2",
},
]

View File

@@ -2,22 +2,22 @@ export const defaultPriceSetMoneyAmountsData = [
{
id: "price-set-money-amount-USD",
title: "price set money amount USD",
price_set: "price-set-1",
money_amount: "money-amount-USD",
price_set_id: "price-set-1",
money_amount_id: "money-amount-USD",
rules_count: 1,
},
{
id: "price-set-money-amount-EUR",
title: "price set money amount EUR",
price_set: "price-set-2",
money_amount: "money-amount-EUR",
price_set_id: "price-set-2",
money_amount_id: "money-amount-EUR",
rules_count: 1,
},
{
id: "price-set-money-amount-CAD",
title: "price set money amount CAD",
price_set: "price-set-3",
money_amount: "money-amount-CAD",
price_set_id: "price-set-3",
money_amount_id: "money-amount-CAD",
rules_count: 1,
},
]

View File

@@ -23,7 +23,7 @@ export async function createPriceSets(
for (let moneyAmount of moneyAmountsData) {
const psma = manager.create(PriceSetMoneyAmount, {
price_set: priceSet,
money_amount: moneyAmount.id,
money_amount_id: moneyAmount.id,
title: "test",
})

View File

@@ -136,65 +136,65 @@ moduleIntegrationTestRunner({
{
id: "psma-PLN",
title: "psma PLN",
price_set: "price-set-PLN",
money_amount: "money-amount-PLN",
price_set_id: "price-set-PLN",
money_amount_id: "money-amount-PLN",
rules_count: 0,
},
{
id: "psma-company_id-EUR",
title: "psma EUR - company_id",
price_set: "price-set-EUR",
money_amount: "money-amount-company_id-EUR",
price_set_id: "price-set-EUR",
money_amount_id: "money-amount-company_id-EUR",
rules_count: 1,
},
{
id: "psma-company_id-PLN",
title: "psma PLN - company_id",
price_set: "price-set-PLN",
money_amount: "money-amount-company_id-PLN",
price_set_id: "price-set-PLN",
money_amount_id: "money-amount-company_id-PLN",
rules_count: 1,
},
{
id: "psma-region_id-PLN",
title: "psma PLN - region_id",
price_set: "price-set-PLN",
money_amount: "money-amount-region_id-PLN",
price_set_id: "price-set-PLN",
money_amount_id: "money-amount-region_id-PLN",
rules_count: 1,
},
{
id: "psma-region_id+company_id-PLN",
title: "psma region_id + company_id",
price_set: "price-set-PLN",
money_amount: "money-amount-region_id+company_id-PLN",
price_set_id: "price-set-PLN",
money_amount_id: "money-amount-region_id+company_id-PLN",
rules_count: 2,
},
{
id: "psma-region_id-PLN-5-qty",
title: "psma PLN - region_id 5 qty",
price_set: "price-set-PLN",
money_amount: "money-amount-region_id-PLN-5-qty",
price_set_id: "price-set-PLN",
money_amount_id: "money-amount-region_id-PLN-5-qty",
rules_count: 1,
},
{
id: "psma-region_id_company_id-PL-EUR",
title: "psma PLN - region_id PL with EUR currency",
price_set: "price-set-PLN",
money_amount: "money-amount-region_id-PL-EUR",
price_set_id: "price-set-PLN",
money_amount_id: "money-amount-region_id-PL-EUR",
rules_count: 2,
},
{
id: "psma-region_id_company_id-PL-EUR-4-qty",
title: "psma PLN - region_id PL with EUR currency for quantity 4",
price_set: "price-set-PLN",
money_amount: "money-amount-region_id-PL-EUR-4-qty",
price_set_id: "price-set-PLN",
money_amount_id: "money-amount-region_id-PL-EUR-4-qty",
rules_count: 2,
},
{
id: "psma-region_id_company_id-PL-EUR-customer-group",
title:
"psma PLN - region_id PL with EUR currency for customer group",
price_set: "price-set-PLN",
money_amount: "money-amount-region_id-PL-EUR-customer-group",
price_set_id: "price-set-PLN",
money_amount_id: "money-amount-region_id-PL-EUR-customer-group",
rules_count: 3,
},
]

View File

@@ -130,6 +130,7 @@ moduleIntegrationTestRunner({
price_set: {
id: "price-set-1",
},
price_set_id: "price-set-1",
},
])
})
@@ -283,8 +284,8 @@ moduleIntegrationTestRunner({
const psma: PriceSetMoneyAmount = testManager.create(
PriceSetMoneyAmount,
{
price_set: "price-set-1",
money_amount: ma.id,
price_set_id: "price-set-1",
money_amount_id: ma.id,
title: "test",
rules_count: 0,
}

View File

@@ -41,9 +41,14 @@ export default class PriceListRuleValue {
@PriceListPriceListRuleIdIndex.MikroORMIndex()
@ManyToOne(() => PriceListRule, {
onDelete: "cascade",
columnType: "text",
mapToPk: true,
fieldName: "price_list_rule_id",
onDelete: "cascade",
})
price_list_rule_id: string
@ManyToOne(() => PriceListRule, { persist: false })
price_list_rule: PriceListRule
@Property({ columnType: "text" })

View File

@@ -52,7 +52,14 @@ export default class PriceListRule {
id!: string
@PriceListRuleRuleTypeIdIndex.MikroORMIndex()
@ManyToOne({ entity: () => RuleType, fieldName: "rule_type_id" })
@ManyToOne(() => RuleType, {
columnType: "text",
mapToPk: true,
fieldName: "rule_type_id",
})
rule_type_id: string
@ManyToOne(() => RuleType, { persist: false })
rule_type: RuleType
@OneToMany(() => PriceListRuleValue, (plrv) => plrv.price_list_rule, {
@@ -61,11 +68,15 @@ export default class PriceListRule {
price_list_rule_values = new Collection<PriceListRuleValue>(this)
@PriceListRulePriceListIdIndex.MikroORMIndex()
@ManyToOne({
entity: () => PriceList,
@ManyToOne(() => PriceList, {
columnType: "text",
mapToPk: true,
fieldName: "price_list_id",
onDelete: "cascade",
})
price_list_id: string
@ManyToOne(() => PriceList, { persist: false })
price_list: PriceList
@Property({

View File

@@ -55,15 +55,26 @@ export default class PriceRule {
id!: string
@PriceRulePriceSetIdIndex.MikroORMIndex()
@ManyToOne({
entity: () => PriceSet,
@ManyToOne(() => PriceSet, {
columnType: "text",
mapToPk: true,
fieldName: "price_set_id",
onDelete: "cascade",
})
price_set_id: string
@ManyToOne(() => PriceSet, { persist: false })
price_set: PriceSet
@PriceRuleRuleTypeIdIndex.MikroORMIndex()
@ManyToOne({ entity: () => RuleType })
@ManyToOne(() => RuleType, {
columnType: "text",
mapToPk: true,
fieldName: "rule_type_id",
})
rule_type_id: string
@ManyToOne(() => RuleType, { persist: false })
rule_type: RuleType
@Property({ columnType: "text" })
@@ -73,10 +84,15 @@ export default class PriceRule {
priority: number = 0
@PriceRulePriceSetMoneyAmountIdIndex.MikroORMIndex()
@ManyToOne({
@ManyToOne(() => PriceSetMoneyAmount, {
columnType: "text",
mapToPk: true,
fieldName: "price_set_money_amount_id",
onDelete: "cascade",
entity: () => PriceSetMoneyAmount,
})
price_set_money_amount_id: string
@ManyToOne(() => PriceSetMoneyAmount, { persist: false })
price_set_money_amount: PriceSetMoneyAmount
@Property({

View File

@@ -1,89 +0,0 @@
import { DAL } from "@medusajs/types"
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import PriceSetMoneyAmount from "./price-set-money-amount"
import RuleType from "./rule-type"
type OptionalFields = DAL.SoftDeletableEntityDateColumns
const tableName = "price_set_money_amount_rules"
const PriceSetMoneyAmountRulesDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const PriceSetMoneyAmountRulesPriceSetMoneyAmountIdIndex =
createPsqlIndexStatementHelper({
tableName: tableName,
columns: "price_set_money_amount_id",
where: "deleted_at IS NULL",
})
const PriceSetMoneyAmountRulesRuleTypeIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "rule_type_id",
where: "deleted_at IS NULL",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceSetMoneyAmountRules {
[OptionalProps]?: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@PriceSetMoneyAmountRulesPriceSetMoneyAmountIdIndex.MikroORMIndex()
@ManyToOne(() => PriceSetMoneyAmount, { onDelete: "cascade" })
price_set_money_amount: PriceSetMoneyAmount
@PriceSetMoneyAmountRulesRuleTypeIdIndex.MikroORMIndex()
@ManyToOne(() => RuleType, { onDelete: "cascade" })
rule_type: RuleType
@Property({ columnType: "text" })
value: string
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@PriceSetMoneyAmountRulesDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "psmar")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "psmar")
}
}

View File

@@ -62,12 +62,28 @@ export default class PriceSetMoneyAmount {
title: string | null = null
@PriceSetMoneyAmountPriceSetIdIndex.MikroORMIndex()
@ManyToOne(() => PriceSet, { onDelete: "cascade" })
price_set: PriceSet
@ManyToOne(() => PriceSet, {
columnType: "text",
mapToPk: true,
fieldName: "price_set_id",
onDelete: "cascade",
})
price_set_id: string
@ManyToOne(() => PriceSet, { persist: false })
price_set?: PriceSet
@PriceSetMoneyAmountMoneyAmountIdIndex.MikroORMIndex()
@OneToOne(() => MoneyAmount, { onDelete: "cascade" })
money_amount: MoneyAmount
@OneToOne(() => MoneyAmount, {
columnType: "text",
mapToPk: true,
fieldName: "money_amount_id",
onDelete: "cascade",
})
money_amount_id: string
@OneToOne(() => MoneyAmount, { persist: false })
money_amount?: MoneyAmount
@Property({ columnType: "integer", default: 0 })
rules_count: number = 0
@@ -80,8 +96,17 @@ export default class PriceSetMoneyAmount {
price_rules = new Collection<PriceRule>(this)
@PriceSetMoneyAmountPriceListIdIndex.MikroORMIndex()
@ManyToOne(() => PriceList, { onDelete: "cascade", nullable: true })
price_list: PriceList | null
@ManyToOne(() => PriceList, {
columnType: "text",
mapToPk: true,
nullable: true,
fieldName: "price_list_id",
onDelete: "cascade",
})
price_list_id: string
@ManyToOne(() => PriceList, { persist: false, nullable: true })
price_list?: PriceList
@Property({
onCreate: () => new Date(),

View File

@@ -1,6 +1,3 @@
export { default as PriceListService } from "./price-list"
export { default as PriceListRuleService } from "./price-list-rule"
export { default as PriceListRuleValueService } from "./price-list-rule-value"
export { default as PriceRuleService } from "./price-rule"
export { default as PricingModuleService } from "./pricing-module"
export { default as RuleTypeService } from "./rule-type"

View File

@@ -1,50 +0,0 @@
import { Context, DAL } from "@medusajs/types"
import { ModulesSdkUtils } from "@medusajs/utils"
import { PriceListRuleValue } from "@models"
import { ServiceTypes } from "@types"
type InjectedDependencies = {
priceListRuleValueRepository: DAL.RepositoryService
}
export default class PriceListRuleValueService<
TEntity extends PriceListRuleValue = PriceListRuleValue
> extends ModulesSdkUtils.internalModuleServiceFactory<InjectedDependencies>(
PriceListRuleValue
)<TEntity> {
constructor(container: InjectedDependencies) {
// @ts-ignore
super(...arguments)
}
create(
data: ServiceTypes.CreatePriceListRuleValueDTO[],
context: Context
): Promise<TEntity[]>
create(
data: ServiceTypes.CreatePriceListRuleValueDTO,
context: Context
): Promise<TEntity>
async create(
data:
| ServiceTypes.CreatePriceListRuleValueDTO
| ServiceTypes.CreatePriceListRuleValueDTO[],
context: Context = {}
): Promise<TEntity | TEntity[]> {
const data_ = Array.isArray(data) ? data : [data]
const priceListRuleValues = data_.map((priceRuleValueData) => {
const { price_list_rule_id: priceListRuleId, ...priceRuleValue } =
priceRuleValueData
if (priceListRuleId) {
priceRuleValue.price_list_rule = priceListRuleId
}
return priceRuleValue
})
return await super.create(priceListRuleValues, context)
}
}

View File

@@ -1,95 +0,0 @@
import { Context, DAL } from "@medusajs/types"
import { ModulesSdkUtils } from "@medusajs/utils"
import { PriceListRule } from "@models"
import { ServiceTypes } from "@types"
type InjectedDependencies = {
priceListRuleRepository: DAL.RepositoryService
}
export default class PriceListRuleService<
TEntity extends PriceListRule = PriceListRule
> extends ModulesSdkUtils.internalModuleServiceFactory<InjectedDependencies>(
PriceListRule
)<TEntity> {
constructor(container: InjectedDependencies) {
// @ts-ignore
super(...arguments)
}
create(
data: ServiceTypes.CreatePriceListRuleDTO[],
sharedContext?: Context
): Promise<TEntity[]>
create(
data: ServiceTypes.CreatePriceListRuleDTO,
sharedContext?: Context
): Promise<TEntity>
async create(
data:
| ServiceTypes.CreatePriceListRuleDTO
| ServiceTypes.CreatePriceListRuleDTO[],
context: Context = {}
): Promise<TEntity | TEntity[]> {
const data_ = Array.isArray(data) ? data : [data]
const priceListRule = data_.map((priceListRule) => {
const {
price_list_id: priceListId,
rule_type_id: ruleTypeId,
...createData
} = priceListRule
if (priceListId) {
createData.price_list = priceListId
}
if (ruleTypeId) {
createData.rule_type = ruleTypeId
}
return createData
})
return await super.create(priceListRule, context)
}
// @ts-ignore
update(
data: ServiceTypes.UpdatePriceListRuleDTO[],
context: Context
): Promise<TEntity[]>
// @ts-ignore
update(
data: ServiceTypes.UpdatePriceListRuleDTO,
context: Context
): Promise<TEntity>
// TODO add support for selector? and then rm ts ignore
// @ts-ignore
async update(
data:
| ServiceTypes.UpdatePriceListRuleDTO
| ServiceTypes.UpdatePriceListRuleDTO[],
context: Context = {}
): Promise<TEntity | TEntity[]> {
const data_ = Array.isArray(data) ? data : [data]
const priceListRules = data_.map((priceListRule) => {
const { price_list_id, rule_type_id, ...priceListRuleData } =
priceListRule
if (price_list_id) {
priceListRuleData.price_list = price_list_id
}
if (rule_type_id) {
priceListRuleData.rule_type = rule_type_id
}
return priceListRuleData
})
return await super.update(priceListRules, context)
}
}

View File

@@ -1,48 +0,0 @@
import { Context, DAL } from "@medusajs/types"
import { ModulesSdkUtils } from "@medusajs/utils"
import { PriceRule } from "@models"
import { ServiceTypes } from "@types"
type InjectedDependencies = {
priceRuleRepository: DAL.RepositoryService
}
export default class PriceRuleService<
TEntity extends PriceRule = PriceRule
> extends ModulesSdkUtils.internalModuleServiceFactory<InjectedDependencies>(
PriceRule
)<TEntity> {
constructor(container: InjectedDependencies) {
// @ts-ignore
super(...arguments)
}
create(
data: ServiceTypes.CreatePriceRuleDTO[],
sharedContext?: Context
): Promise<TEntity[]>
create(
data: ServiceTypes.CreatePriceRuleDTO,
sharedContext?: Context
): Promise<TEntity>
async create(
data: ServiceTypes.CreatePriceRuleDTO | ServiceTypes.CreatePriceRuleDTO[],
sharedContext: Context = {}
): Promise<TEntity | TEntity[]> {
const data_ = Array.isArray(data) ? data : [data]
const toCreate = data_.map((ruleData) => {
const ruleDataClone = { ...ruleData } as any
ruleDataClone.rule_type ??= ruleData.rule_type_id
ruleDataClone.price_set ??= ruleData.price_set_id
ruleDataClone.price_set_money_amount ??=
ruleData.price_set_money_amount_id
return ruleDataClone
})
return await super.create(toCreate, sharedContext)
}
}

View File

@@ -39,13 +39,7 @@ import {
RuleType,
} from "@models"
import {
PriceListRuleService,
PriceListRuleValueService,
PriceListService,
PriceRuleService,
RuleTypeService,
} from "@services"
import { PriceListService, RuleTypeService } from "@services"
import { ServiceTypes } from "@types"
import { validatePriceListDates } from "@utils"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
@@ -56,12 +50,12 @@ type InjectedDependencies = {
moneyAmountService: ModulesSdkTypes.InternalModuleService<any>
priceSetService: ModulesSdkTypes.InternalModuleService<any>
ruleTypeService: RuleTypeService<any>
priceRuleService: PriceRuleService<any>
priceRuleService: ModulesSdkTypes.InternalModuleService<any>
priceSetRuleTypeService: ModulesSdkTypes.InternalModuleService<any>
priceSetMoneyAmountService: ModulesSdkTypes.InternalModuleService<any>
priceListService: PriceListService<any>
priceListRuleService: PriceListRuleService<any>
priceListRuleValueService: PriceListRuleValueService<any>
priceListRuleService: ModulesSdkTypes.InternalModuleService<any>
priceListRuleValueService: ModulesSdkTypes.InternalModuleService<any>
}
const generateMethodForModels = [
@@ -117,12 +111,12 @@ export default class PricingModuleService<
protected readonly moneyAmountService_: ModulesSdkTypes.InternalModuleService<TMoneyAmount>
protected readonly ruleTypeService_: RuleTypeService<TRuleType>
protected readonly priceSetService_: ModulesSdkTypes.InternalModuleService<TPriceSet>
protected readonly priceRuleService_: PriceRuleService<TPriceRule>
protected readonly priceRuleService_: ModulesSdkTypes.InternalModuleService<TPriceRule>
protected readonly priceSetRuleTypeService_: ModulesSdkTypes.InternalModuleService<TPriceSetRuleType>
protected readonly priceSetMoneyAmountService_: ModulesSdkTypes.InternalModuleService<TPriceSetMoneyAmount>
protected readonly priceListService_: PriceListService<TPriceList>
protected readonly priceListRuleService_: PriceListRuleService<TPriceListRule>
protected readonly priceListRuleValueService_: PriceListRuleValueService<TPriceListRuleValue>
protected readonly priceListRuleService_: ModulesSdkTypes.InternalModuleService<TPriceListRule>
protected readonly priceListRuleValueService_: ModulesSdkTypes.InternalModuleService<TPriceListRuleValue>
constructor(
{
@@ -356,8 +350,8 @@ export default class PricingModuleService<
const numberOfRules = Object.entries(cleanRules).length
const priceSetMoneyAmount = {
price_set: createdPriceSets[index],
money_amount: createdMoneyAmounts[moneyAmountIndex++],
price_set_id: createdPriceSets[index].id,
money_amount_id: createdMoneyAmounts[moneyAmountIndex++].id,
title: "test", // TODO: accept title
rules_count: numberOfRules,
}
@@ -366,8 +360,8 @@ export default class PricingModuleService<
for (const [k, v] of Object.entries(cleanRules)) {
priceRulesData.push({
price_set_money_amount: null, // Updated later
rule_type: ruleTypeMap.get(k),
price_set: createdPriceSets[index],
rule_type_id: ruleTypeMap.get(k).id,
price_set_id: createdPriceSets[index].id,
value: v,
price_list_id: "test",
})
@@ -386,8 +380,8 @@ export default class PricingModuleService<
for (let i = 0, j = 0; i < priceSetMoneyAmountData.length; i++) {
const rulesCount = (priceSetMoneyAmountData[i] as any).rules_count
for (let k = 0; k < rulesCount; k++, j++) {
;(priceRulesData[j] as any).price_set_money_amount =
createdPriceSetMoneyAmounts[i]
;(priceRulesData[j] as any).price_set_money_amount_id =
createdPriceSetMoneyAmounts[i].id
}
}
@@ -607,8 +601,8 @@ export default class PricingModuleService<
).length
maCursor++
return {
price_set: priceSetId,
money_amount: ma,
price_set_id: priceSetId,
money_amount_id: ma.id,
title: "test", // TODO: accept title
rules_count: numberOfRules,
}
@@ -628,9 +622,9 @@ export default class PricingModuleService<
const priceSetMoneyAmount = createdPriceSetMoneyAmounts[rulesCursor]
rulesCursor++
return Object.entries(rules).map(([k, v]) => ({
price_set_money_amount: priceSetMoneyAmount,
price_set_money_amount_id: priceSetMoneyAmount.id,
rule_type_id: ruleTypeMap.get(priceSetId)!.get(k)!.id,
price_set: priceSetId,
price_set_id: priceSetId,
value: v,
}))
})
@@ -747,7 +741,8 @@ export default class PricingModuleService<
const ruleTypes = await this.listRuleTypes(
{ rule_attribute: ruleTypeAttributes },
{ take: null }
{ take: null },
sharedContext
)
const invalidRuleTypes = arrayDifference(
@@ -781,9 +776,10 @@ export default class PricingModuleService<
})
}
const priceLists = (await this.priceListService_.create(
priceListsToCreate
)) as unknown as PricingTypes.PriceListDTO[]
const priceLists = await this.priceListService_.create(
priceListsToCreate,
sharedContext
)
for (let i = 0; i < data.length; i++) {
const { rules = {}, prices = [] } = data[i]
@@ -794,24 +790,14 @@ export default class PricingModuleService<
// Create the rule
const [priceListRule] = await this.priceListRuleService_.create(
[
{
price_list: priceList,
rule_type: ruleType.id,
},
],
[{ price_list_id: priceList.id, rule_type_id: ruleType.id }],
sharedContext
)
// Create the values for the rule
for (const ruleValue of ruleValues as string[]) {
await this.priceListRuleValueService_.create(
[
{
price_list_rule: priceListRule,
value: ruleValue,
},
],
[{ price_list_rule_id: priceListRule.id, value: ruleValue }],
sharedContext
)
}
@@ -833,25 +819,23 @@ export default class PricingModuleService<
await this.priceSetMoneyAmountService_.create(
[
{
price_set: priceSetId,
price_list: priceList,
money_amount: moneyAmount,
price_set_id: priceSetId,
price_list_id: priceList.id,
money_amount_id: moneyAmount.id,
title: "test",
rules_count: Object.keys(priceRules).length,
},
] as unknown as ServiceTypes.CreatePriceSetMoneyAmountDTO[],
],
sharedContext
)
await this.createPriceRules(
await this.priceRuleService_.create(
Object.entries(priceRules).map(([ruleAttribute, ruleValue]) => {
return {
price_set_id: priceSetId,
rule_type:
ruleTypeMap.get(ruleAttribute)!?.id ||
ruleTypeMap.get(ruleAttribute)!,
rule_type_id: ruleTypeMap.get(ruleAttribute)!?.id,
value: ruleValue,
price_set_money_amount: priceSetMoneyAmount as any,
price_set_money_amount_id: priceSetMoneyAmount.id,
}
}),
sharedContext
@@ -963,8 +947,8 @@ export default class PricingModuleService<
const [priceListRule] = await this.priceListRuleService_.create(
[
{
price_list: updatedPriceList,
rule_type: ruleType?.id || ruleType,
price_list_id: updatedPriceList.id,
rule_type_id: ruleType?.id,
},
],
sharedContext
@@ -972,7 +956,7 @@ export default class PricingModuleService<
for (const ruleValue of ruleValues as string[]) {
await this.priceListRuleValueService_.create(
[{ price_list_rule: priceListRule, value: ruleValue }],
[{ price_list_rule_id: priceListRule.id, value: ruleValue }],
sharedContext
)
}
@@ -1313,25 +1297,23 @@ export default class PricingModuleService<
const [psma] = await this.priceSetMoneyAmountService_.create(
[
{
price_set: price.price_set_id,
money_amount: moneyAmount.id,
price_set_id: price.price_set_id,
money_amount_id: moneyAmount.id,
title: "test",
price_list: priceList.id,
price_list_id: priceList.id,
rules_count: noOfRules,
},
],
sharedContext
)
await this.createPriceRules(
await this.priceRuleService_.create(
Object.entries(priceRules).map(([ruleAttribute, ruleValue]) => {
return {
price_set_id: price.price_set_id,
rule_type:
ruleTypeMap.get(ruleAttribute)!?.id ||
ruleTypeMap.get(ruleAttribute)!,
rule_type_id: ruleTypeMap.get(ruleAttribute)!?.id,
value: ruleValue,
price_set_money_amount: psma as any,
price_set_money_amount_id: psma.id,
}
}),
sharedContext
@@ -1369,7 +1351,6 @@ export default class PricingModuleService<
)
const priceListMap = new Map(priceLists.map((p) => [p.id, p]))
const ruleTypes = await this.listRuleTypes(
{
rule_attribute: data.map((d) => Object.keys(d.rules)).flat(),
@@ -1422,8 +1403,8 @@ export default class PricingModuleService<
if (!rule) {
rulesToCreate.push({
rule_type: ruleType.id,
price_list: priceListId,
rule_type_id: ruleType.id,
price_list_id: priceListId,
})
} else {
ruleIdsToUpdate.push(rule.id)
@@ -1446,24 +1427,21 @@ export default class PricingModuleService<
const priceListRuleValuesToCreate: unknown[] = []
for (const { id, price_list, rule_type } of createdRules) {
const ruleValues = priceRuleValues.get(
price_list.id ?? (price_list as unknown as string)
)
for (const { id, price_list_id, rule_type_id } of createdRules) {
const ruleValues = priceRuleValues.get(price_list_id)
if (!ruleValues) {
continue
}
const values = ruleValues.get(
rule_type.id ?? (rule_type as unknown as string)
)
const values = ruleValues.get(rule_type_id)
if (!values) {
continue
}
values.forEach((v) => {
priceListRuleValuesToCreate.push({
price_list_rule: id,
price_list_rule_id: id,
value: v,
})
})