From cf8b5ce85bcc44f3bab8a75b1f4021787ef861e1 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 21 Mar 2024 11:01:52 +0100 Subject: [PATCH] chore: pricing models uses standardized relationships attributes (#6767) what: - all relationships under all models are standardized --- .../price-lists/admin/price-lists.spec.ts | 6 +- .../__fixtures__/price-list-rules/data.ts | 8 +- .../price-set-money-amount/data.ts | 12 +- .../__fixtures__/price-set/index.ts | 2 +- .../pricing-module/calculate-price.spec.ts | 36 +++--- .../pricing-module/price-rule.spec.ts | 5 +- .../src/models/price-list-rule-value.ts | 7 +- .../pricing/src/models/price-list-rule.ts | 17 ++- packages/pricing/src/models/price-rule.ts | 26 +++- .../models/price-set-money-amount-rules.ts | 89 ------------- .../src/models/price-set-money-amount.ts | 37 +++++- packages/pricing/src/services/index.ts | 3 - .../src/services/price-list-rule-value.ts | 50 -------- .../pricing/src/services/price-list-rule.ts | 95 -------------- packages/pricing/src/services/price-rule.ts | 48 ------- .../pricing/src/services/pricing-module.ts | 118 +++++++----------- 16 files changed, 157 insertions(+), 402 deletions(-) delete mode 100644 packages/pricing/src/models/price-set-money-amount-rules.ts delete mode 100644 packages/pricing/src/services/price-list-rule-value.ts delete mode 100644 packages/pricing/src/services/price-list-rule.ts delete mode 100644 packages/pricing/src/services/price-rule.ts diff --git a/integration-tests/modules/__tests__/price-lists/admin/price-lists.spec.ts b/integration-tests/modules/__tests__/price-lists/admin/price-lists.spec.ts index 3cbb452779..ef6b5c2efb 100644 --- a/integration-tests/modules/__tests__/price-lists/admin/price-lists.spec.ts +++ b/integration-tests/modules/__tests__/price-lists/admin/price-lists.spec.ts @@ -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( diff --git a/packages/pricing/integration-tests/__fixtures__/price-list-rules/data.ts b/packages/pricing/integration-tests/__fixtures__/price-list-rules/data.ts index b40f9279fa..7c7cb9ecd8 100644 --- a/packages/pricing/integration-tests/__fixtures__/price-list-rules/data.ts +++ b/packages/pricing/integration-tests/__fixtures__/price-list-rules/data.ts @@ -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", }, ] diff --git a/packages/pricing/integration-tests/__fixtures__/price-set-money-amount/data.ts b/packages/pricing/integration-tests/__fixtures__/price-set-money-amount/data.ts index 8d6879cb99..01b691ce86 100644 --- a/packages/pricing/integration-tests/__fixtures__/price-set-money-amount/data.ts +++ b/packages/pricing/integration-tests/__fixtures__/price-set-money-amount/data.ts @@ -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, }, ] diff --git a/packages/pricing/integration-tests/__fixtures__/price-set/index.ts b/packages/pricing/integration-tests/__fixtures__/price-set/index.ts index 1dfbce1abf..36195ea675 100644 --- a/packages/pricing/integration-tests/__fixtures__/price-set/index.ts +++ b/packages/pricing/integration-tests/__fixtures__/price-set/index.ts @@ -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", }) diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts index 9c8d1b4490..5104694f3b 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/calculate-price.spec.ts @@ -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, }, ] diff --git a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts index 0125f92965..c46a0246d3 100644 --- a/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts +++ b/packages/pricing/integration-tests/__tests__/services/pricing-module/price-rule.spec.ts @@ -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, } diff --git a/packages/pricing/src/models/price-list-rule-value.ts b/packages/pricing/src/models/price-list-rule-value.ts index ecaedc1a7f..07d957f808 100644 --- a/packages/pricing/src/models/price-list-rule-value.ts +++ b/packages/pricing/src/models/price-list-rule-value.ts @@ -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" }) diff --git a/packages/pricing/src/models/price-list-rule.ts b/packages/pricing/src/models/price-list-rule.ts index 8633bc2cf1..daef96e848 100644 --- a/packages/pricing/src/models/price-list-rule.ts +++ b/packages/pricing/src/models/price-list-rule.ts @@ -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(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({ diff --git a/packages/pricing/src/models/price-rule.ts b/packages/pricing/src/models/price-rule.ts index 34eb3ab59a..4800703a1e 100644 --- a/packages/pricing/src/models/price-rule.ts +++ b/packages/pricing/src/models/price-rule.ts @@ -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({ diff --git a/packages/pricing/src/models/price-set-money-amount-rules.ts b/packages/pricing/src/models/price-set-money-amount-rules.ts deleted file mode 100644 index 2324472cea..0000000000 --- a/packages/pricing/src/models/price-set-money-amount-rules.ts +++ /dev/null @@ -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") - } -} diff --git a/packages/pricing/src/models/price-set-money-amount.ts b/packages/pricing/src/models/price-set-money-amount.ts index 0cd2020426..3a7649f382 100644 --- a/packages/pricing/src/models/price-set-money-amount.ts +++ b/packages/pricing/src/models/price-set-money-amount.ts @@ -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(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(), diff --git a/packages/pricing/src/services/index.ts b/packages/pricing/src/services/index.ts index 5d08291b40..6538f16d7d 100644 --- a/packages/pricing/src/services/index.ts +++ b/packages/pricing/src/services/index.ts @@ -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" diff --git a/packages/pricing/src/services/price-list-rule-value.ts b/packages/pricing/src/services/price-list-rule-value.ts deleted file mode 100644 index 88d4f02414..0000000000 --- a/packages/pricing/src/services/price-list-rule-value.ts +++ /dev/null @@ -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( - PriceListRuleValue -) { - constructor(container: InjectedDependencies) { - // @ts-ignore - super(...arguments) - } - - create( - data: ServiceTypes.CreatePriceListRuleValueDTO[], - context: Context - ): Promise - - create( - data: ServiceTypes.CreatePriceListRuleValueDTO, - context: Context - ): Promise - - async create( - data: - | ServiceTypes.CreatePriceListRuleValueDTO - | ServiceTypes.CreatePriceListRuleValueDTO[], - context: Context = {} - ): Promise { - 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) - } -} diff --git a/packages/pricing/src/services/price-list-rule.ts b/packages/pricing/src/services/price-list-rule.ts deleted file mode 100644 index 438b712b2f..0000000000 --- a/packages/pricing/src/services/price-list-rule.ts +++ /dev/null @@ -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( - PriceListRule -) { - constructor(container: InjectedDependencies) { - // @ts-ignore - super(...arguments) - } - - create( - data: ServiceTypes.CreatePriceListRuleDTO[], - sharedContext?: Context - ): Promise - create( - data: ServiceTypes.CreatePriceListRuleDTO, - sharedContext?: Context - ): Promise - - async create( - data: - | ServiceTypes.CreatePriceListRuleDTO - | ServiceTypes.CreatePriceListRuleDTO[], - context: Context = {} - ): Promise { - 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 - - // @ts-ignore - update( - data: ServiceTypes.UpdatePriceListRuleDTO, - context: Context - ): Promise - - // TODO add support for selector? and then rm ts ignore - // @ts-ignore - async update( - data: - | ServiceTypes.UpdatePriceListRuleDTO - | ServiceTypes.UpdatePriceListRuleDTO[], - context: Context = {} - ): Promise { - 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) - } -} diff --git a/packages/pricing/src/services/price-rule.ts b/packages/pricing/src/services/price-rule.ts deleted file mode 100644 index a0d8ce13ca..0000000000 --- a/packages/pricing/src/services/price-rule.ts +++ /dev/null @@ -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( - PriceRule -) { - constructor(container: InjectedDependencies) { - // @ts-ignore - super(...arguments) - } - - create( - data: ServiceTypes.CreatePriceRuleDTO[], - sharedContext?: Context - ): Promise - create( - data: ServiceTypes.CreatePriceRuleDTO, - sharedContext?: Context - ): Promise - - async create( - data: ServiceTypes.CreatePriceRuleDTO | ServiceTypes.CreatePriceRuleDTO[], - sharedContext: Context = {} - ): Promise { - 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) - } -} diff --git a/packages/pricing/src/services/pricing-module.ts b/packages/pricing/src/services/pricing-module.ts index 8f3b45c314..6234016572 100644 --- a/packages/pricing/src/services/pricing-module.ts +++ b/packages/pricing/src/services/pricing-module.ts @@ -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 priceSetService: ModulesSdkTypes.InternalModuleService ruleTypeService: RuleTypeService - priceRuleService: PriceRuleService + priceRuleService: ModulesSdkTypes.InternalModuleService priceSetRuleTypeService: ModulesSdkTypes.InternalModuleService priceSetMoneyAmountService: ModulesSdkTypes.InternalModuleService priceListService: PriceListService - priceListRuleService: PriceListRuleService - priceListRuleValueService: PriceListRuleValueService + priceListRuleService: ModulesSdkTypes.InternalModuleService + priceListRuleValueService: ModulesSdkTypes.InternalModuleService } const generateMethodForModels = [ @@ -117,12 +111,12 @@ export default class PricingModuleService< protected readonly moneyAmountService_: ModulesSdkTypes.InternalModuleService protected readonly ruleTypeService_: RuleTypeService protected readonly priceSetService_: ModulesSdkTypes.InternalModuleService - protected readonly priceRuleService_: PriceRuleService + protected readonly priceRuleService_: ModulesSdkTypes.InternalModuleService protected readonly priceSetRuleTypeService_: ModulesSdkTypes.InternalModuleService protected readonly priceSetMoneyAmountService_: ModulesSdkTypes.InternalModuleService protected readonly priceListService_: PriceListService - protected readonly priceListRuleService_: PriceListRuleService - protected readonly priceListRuleValueService_: PriceListRuleValueService + protected readonly priceListRuleService_: ModulesSdkTypes.InternalModuleService + protected readonly priceListRuleValueService_: ModulesSdkTypes.InternalModuleService 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, }) })