Fix/pricing create price lists (#6818)
* chore: rename price set money amounts to prices * chore: remove more stuff * chore: more deletions * chore: delete more stuff * chore: fix module tests * chore: skip product create test * chore: undo package changes * chore: remove log * chore: added changeset * Fix/pricing create missmatch (#6802) * fix pricing using index and mixing data * cleanup * cleanup * cleanup * Ensure data order * finished * fix(pricing): createPriceLists_ should not rely on indexes * feedback * Create spotty-mirrors-thank.md --------- Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
This commit is contained in:
co-authored by
Riqwan Thamir
parent
7bc7adeeb4
commit
f7f667cf18
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/pricing": patch
|
||||
---
|
||||
|
||||
Fix(pricing): create price lists
|
||||
@@ -1,7 +1,7 @@
|
||||
import { DAL } from "@medusajs/types"
|
||||
import {
|
||||
DALUtils,
|
||||
createPsqlIndexStatementHelper,
|
||||
DALUtils,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
@@ -76,10 +76,12 @@ export default class PriceListRuleValue {
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "plrv")
|
||||
this.price_list_rule_id ??= this.price_list_rule?.id
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "plrv")
|
||||
this.price_list_rule_id ??= this.price_list_rule?.id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { DAL } from "@medusajs/types"
|
||||
import {
|
||||
DALUtils,
|
||||
createPsqlIndexStatementHelper,
|
||||
DALUtils,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
@@ -11,8 +11,8 @@ import {
|
||||
Entity,
|
||||
Filter,
|
||||
ManyToOne,
|
||||
OnInit,
|
||||
OneToMany,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
@@ -63,7 +63,7 @@ export default class PriceListRule {
|
||||
rule_type: RuleType
|
||||
|
||||
@OneToMany(() => PriceListRuleValue, (plrv) => plrv.price_list_rule, {
|
||||
cascade: ["soft-remove" as Cascade],
|
||||
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
|
||||
})
|
||||
price_list_rule_values = new Collection<PriceListRuleValue>(this)
|
||||
|
||||
@@ -101,10 +101,14 @@ export default class PriceListRule {
|
||||
@BeforeCreate()
|
||||
beforeCreate() {
|
||||
this.id = generateEntityId(this.id, "plrule")
|
||||
this.price_list_id ??= this.price_list?.id!
|
||||
this.rule_type_id ??= this.rule_type?.id!
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "plrule")
|
||||
this.price_list_id ??= this.price_list?.id!
|
||||
this.rule_type_id ??= this.rule_type?.id!
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { DAL } from "@medusajs/types"
|
||||
import {
|
||||
createPsqlIndexStatementHelper,
|
||||
DALUtils,
|
||||
generateEntityId,
|
||||
PriceListStatus,
|
||||
PriceListType,
|
||||
createPsqlIndexStatementHelper,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
@@ -14,8 +14,8 @@ import {
|
||||
Enum,
|
||||
Filter,
|
||||
ManyToMany,
|
||||
OnInit,
|
||||
OneToMany,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
@@ -36,6 +36,8 @@ const PriceListDeletedAtIndex = createPsqlIndexStatementHelper({
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
export const PriceListIdPrefix = "plist"
|
||||
|
||||
@Entity({ tableName })
|
||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||
export default class PriceList {
|
||||
@@ -69,12 +71,12 @@ export default class PriceList {
|
||||
ends_at: Date | null = null
|
||||
|
||||
@OneToMany(() => Price, (price) => price.price_list, {
|
||||
cascade: ["soft-remove" as Cascade],
|
||||
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
|
||||
})
|
||||
prices = new Collection<Price>(this)
|
||||
|
||||
@OneToMany(() => PriceListRule, (pr) => pr.price_list, {
|
||||
cascade: ["soft-remove" as Cascade],
|
||||
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
|
||||
})
|
||||
price_list_rules = new Collection<PriceListRule>(this)
|
||||
|
||||
@@ -108,11 +110,11 @@ export default class PriceList {
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "plist")
|
||||
this.id = generateEntityId(this.id, PriceListIdPrefix)
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "plist")
|
||||
this.id = generateEntityId(this.id, PriceListIdPrefix)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {
|
||||
DALUtils,
|
||||
createPsqlIndexStatementHelper,
|
||||
DALUtils,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
@@ -10,8 +10,8 @@ import {
|
||||
Entity,
|
||||
Filter,
|
||||
ManyToMany,
|
||||
OnInit,
|
||||
OneToMany,
|
||||
OnInit,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
@@ -27,6 +27,8 @@ const PriceSetDeletedAtIndex = createPsqlIndexStatementHelper({
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
export const PriceSetIdPrefix = "pset"
|
||||
|
||||
@Entity({ tableName })
|
||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||
export default class PriceSet {
|
||||
@@ -71,11 +73,11 @@ export default class PriceSet {
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "pset")
|
||||
this.id = generateEntityId(this.id, PriceSetIdPrefix)
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "pset")
|
||||
this.id = generateEntityId(this.id, PriceSetIdPrefix)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,8 @@ import {
|
||||
import {PriceListService, RuleTypeService} from "@services"
|
||||
import {validatePriceListDates} from "@utils"
|
||||
import {entityNameToLinkableKeysMap, joinerConfig} from "../joiner-config"
|
||||
import {PriceSetIdPrefix} from "../models/price-set";
|
||||
import {PriceListIdPrefix} from "../models/price-list";
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
@@ -299,7 +301,7 @@ export default class PricingModuleService<
|
||||
const toCreate = input.map((inputData) => {
|
||||
const id = generateEntityId(
|
||||
(inputData as unknown as TPriceSet).id,
|
||||
"pset"
|
||||
PriceSetIdPrefix
|
||||
)
|
||||
|
||||
const { prices, rules = [], ...rest } = inputData
|
||||
@@ -713,79 +715,62 @@ export default class PricingModuleService<
|
||||
ruleTypes.map((rt) => [rt.rule_attribute, rt])
|
||||
)
|
||||
|
||||
const priceListsToCreate: PricingTypes.CreatePriceListDTO[] = []
|
||||
const priceListsToCreate: PricingTypes.CreatePriceListDTO[] = data.map(
|
||||
(priceListData) => {
|
||||
const id = generateEntityId(
|
||||
(priceListData as unknown as TPriceList).id,
|
||||
PriceListIdPrefix
|
||||
)
|
||||
|
||||
for (const priceListData of data) {
|
||||
const { rules = {}, prices = [], ...priceListOnlyData } = priceListData
|
||||
const { prices = [], rules = {}, ...rest } = priceListData
|
||||
|
||||
validatePriceListDates(priceListData)
|
||||
validatePriceListDates(priceListData)
|
||||
|
||||
priceListsToCreate.push({
|
||||
...priceListOnlyData,
|
||||
rules_count: Object.keys(rules).length,
|
||||
})
|
||||
}
|
||||
const priceListRules = Object.entries(rules).map(
|
||||
([attribute, value]) => {
|
||||
const ruleType = ruleTypeMap.get(attribute)!
|
||||
return {
|
||||
price_list_id: id,
|
||||
rule_type_id: ruleType.id,
|
||||
price_list_rule_values: value.map((v) => ({ value: v }))
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
const priceLists = await this.priceListService_.create(
|
||||
const pricesData = prices.map((price) => {
|
||||
const priceRules = Object.entries(price.rules ?? {}).map(
|
||||
([ruleAttribute, ruleValue]) => {
|
||||
return {
|
||||
price_set_id: price.price_set_id,
|
||||
rule_type_id: ruleTypeMap.get(ruleAttribute)!?.id,
|
||||
value: ruleValue,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
price_list_id: id,
|
||||
title: "test",
|
||||
rules_count: Object.keys(price.rules ?? {}).length,
|
||||
price_rules: priceRules,
|
||||
...price,
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
id,
|
||||
...rest,
|
||||
rules_count: Object.keys(rules).length,
|
||||
price_list_rules: priceListRules,
|
||||
prices: pricesData,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return await this.priceListService_.create(
|
||||
priceListsToCreate,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
const { rules = {}, prices = [] } = data[i]
|
||||
const priceList = priceLists[i]
|
||||
|
||||
for (const [ruleAttribute, ruleValues = []] of Object.entries(rules)) {
|
||||
let ruleType = ruleTypeMap.get(ruleAttribute)!
|
||||
|
||||
// Create the rule
|
||||
const [priceListRule] = await this.priceListRuleService_.create(
|
||||
[{ price_list_id: priceList.id, rule_type_id: ruleType.id }],
|
||||
sharedContext
|
||||
)
|
||||
|
||||
// Create the values for the rule
|
||||
for (const ruleValue of ruleValues) {
|
||||
await this.priceListRuleValueService_.create(
|
||||
[{ price_list_rule_id: priceListRule.id, value: ruleValue }],
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
for (const priceData of prices) {
|
||||
const {
|
||||
price_set_id: priceSetId,
|
||||
rules: priceRules = {},
|
||||
...rest
|
||||
} = priceData
|
||||
|
||||
const createdPrice = await this.priceService_.create(
|
||||
{
|
||||
price_set_id: priceSetId,
|
||||
price_list_id: priceList.id,
|
||||
title: "test",
|
||||
rules_count: Object.keys(priceRules).length,
|
||||
...rest,
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
await this.priceRuleService_.create(
|
||||
Object.entries(priceRules).map(([ruleAttribute, ruleValue]) => {
|
||||
return {
|
||||
price_set_id: priceSetId,
|
||||
rule_type_id: ruleTypeMap.get(ruleAttribute)!?.id,
|
||||
value: ruleValue,
|
||||
price_id: createdPrice.id,
|
||||
}
|
||||
}),
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return priceLists
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
|
||||
Reference in New Issue
Block a user