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:
1 parent
7bc7adeeb4
commit
f7f667cf18
6 files changed
+82
-82
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/pricing": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix(pricing): create price lists
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { DAL } from "@medusajs/types"
|
import { DAL } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
DALUtils,
|
|
||||||
createPsqlIndexStatementHelper,
|
createPsqlIndexStatementHelper,
|
||||||
|
DALUtils,
|
||||||
generateEntityId,
|
generateEntityId,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
@@ -76,10 +76,12 @@ export default class PriceListRuleValue {
|
|||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
onCreate() {
|
onCreate() {
|
||||||
this.id = generateEntityId(this.id, "plrv")
|
this.id = generateEntityId(this.id, "plrv")
|
||||||
|
this.price_list_rule_id ??= this.price_list_rule?.id
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnInit()
|
@OnInit()
|
||||||
onInit() {
|
onInit() {
|
||||||
this.id = generateEntityId(this.id, "plrv")
|
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 { DAL } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
DALUtils,
|
|
||||||
createPsqlIndexStatementHelper,
|
createPsqlIndexStatementHelper,
|
||||||
|
DALUtils,
|
||||||
generateEntityId,
|
generateEntityId,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
@@ -11,8 +11,8 @@ import {
|
|||||||
Entity,
|
Entity,
|
||||||
Filter,
|
Filter,
|
||||||
ManyToOne,
|
ManyToOne,
|
||||||
OnInit,
|
|
||||||
OneToMany,
|
OneToMany,
|
||||||
|
OnInit,
|
||||||
OptionalProps,
|
OptionalProps,
|
||||||
PrimaryKey,
|
PrimaryKey,
|
||||||
Property,
|
Property,
|
||||||
@@ -63,7 +63,7 @@ export default class PriceListRule {
|
|||||||
rule_type: RuleType
|
rule_type: RuleType
|
||||||
|
|
||||||
@OneToMany(() => PriceListRuleValue, (plrv) => plrv.price_list_rule, {
|
@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)
|
price_list_rule_values = new Collection<PriceListRuleValue>(this)
|
||||||
|
|
||||||
@@ -101,10 +101,14 @@ export default class PriceListRule {
|
|||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
beforeCreate() {
|
beforeCreate() {
|
||||||
this.id = generateEntityId(this.id, "plrule")
|
this.id = generateEntityId(this.id, "plrule")
|
||||||
|
this.price_list_id ??= this.price_list?.id!
|
||||||
|
this.rule_type_id ??= this.rule_type?.id!
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnInit()
|
@OnInit()
|
||||||
onInit() {
|
onInit() {
|
||||||
this.id = generateEntityId(this.id, "plrule")
|
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 { DAL } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
|
createPsqlIndexStatementHelper,
|
||||||
DALUtils,
|
DALUtils,
|
||||||
|
generateEntityId,
|
||||||
PriceListStatus,
|
PriceListStatus,
|
||||||
PriceListType,
|
PriceListType,
|
||||||
createPsqlIndexStatementHelper,
|
|
||||||
generateEntityId,
|
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
BeforeCreate,
|
BeforeCreate,
|
||||||
@@ -14,8 +14,8 @@ import {
|
|||||||
Enum,
|
Enum,
|
||||||
Filter,
|
Filter,
|
||||||
ManyToMany,
|
ManyToMany,
|
||||||
OnInit,
|
|
||||||
OneToMany,
|
OneToMany,
|
||||||
|
OnInit,
|
||||||
OptionalProps,
|
OptionalProps,
|
||||||
PrimaryKey,
|
PrimaryKey,
|
||||||
Property,
|
Property,
|
||||||
@@ -36,6 +36,8 @@ const PriceListDeletedAtIndex = createPsqlIndexStatementHelper({
|
|||||||
where: "deleted_at IS NOT NULL",
|
where: "deleted_at IS NOT NULL",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const PriceListIdPrefix = "plist"
|
||||||
|
|
||||||
@Entity({ tableName })
|
@Entity({ tableName })
|
||||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||||
export default class PriceList {
|
export default class PriceList {
|
||||||
@@ -69,12 +71,12 @@ export default class PriceList {
|
|||||||
ends_at: Date | null = null
|
ends_at: Date | null = null
|
||||||
|
|
||||||
@OneToMany(() => Price, (price) => price.price_list, {
|
@OneToMany(() => Price, (price) => price.price_list, {
|
||||||
cascade: ["soft-remove" as Cascade],
|
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
|
||||||
})
|
})
|
||||||
prices = new Collection<Price>(this)
|
prices = new Collection<Price>(this)
|
||||||
|
|
||||||
@OneToMany(() => PriceListRule, (pr) => pr.price_list, {
|
@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)
|
price_list_rules = new Collection<PriceListRule>(this)
|
||||||
|
|
||||||
@@ -108,11 +110,11 @@ export default class PriceList {
|
|||||||
|
|
||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
onCreate() {
|
onCreate() {
|
||||||
this.id = generateEntityId(this.id, "plist")
|
this.id = generateEntityId(this.id, PriceListIdPrefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnInit()
|
@OnInit()
|
||||||
onInit() {
|
onInit() {
|
||||||
this.id = generateEntityId(this.id, "plist")
|
this.id = generateEntityId(this.id, PriceListIdPrefix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
DALUtils,
|
|
||||||
createPsqlIndexStatementHelper,
|
createPsqlIndexStatementHelper,
|
||||||
|
DALUtils,
|
||||||
generateEntityId,
|
generateEntityId,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
@@ -10,8 +10,8 @@ import {
|
|||||||
Entity,
|
Entity,
|
||||||
Filter,
|
Filter,
|
||||||
ManyToMany,
|
ManyToMany,
|
||||||
OnInit,
|
|
||||||
OneToMany,
|
OneToMany,
|
||||||
|
OnInit,
|
||||||
PrimaryKey,
|
PrimaryKey,
|
||||||
Property,
|
Property,
|
||||||
} from "@mikro-orm/core"
|
} from "@mikro-orm/core"
|
||||||
@@ -27,6 +27,8 @@ const PriceSetDeletedAtIndex = createPsqlIndexStatementHelper({
|
|||||||
where: "deleted_at IS NOT NULL",
|
where: "deleted_at IS NOT NULL",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const PriceSetIdPrefix = "pset"
|
||||||
|
|
||||||
@Entity({ tableName })
|
@Entity({ tableName })
|
||||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||||
export default class PriceSet {
|
export default class PriceSet {
|
||||||
@@ -71,11 +73,11 @@ export default class PriceSet {
|
|||||||
|
|
||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
onCreate() {
|
onCreate() {
|
||||||
this.id = generateEntityId(this.id, "pset")
|
this.id = generateEntityId(this.id, PriceSetIdPrefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnInit()
|
@OnInit()
|
||||||
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 {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 {PriceListIdPrefix} from "../models/price-list";
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
baseRepository: DAL.RepositoryService
|
baseRepository: DAL.RepositoryService
|
||||||
@@ -299,7 +301,7 @@ export default class PricingModuleService<
|
|||||||
const toCreate = input.map((inputData) => {
|
const toCreate = input.map((inputData) => {
|
||||||
const id = generateEntityId(
|
const id = generateEntityId(
|
||||||
(inputData as unknown as TPriceSet).id,
|
(inputData as unknown as TPriceSet).id,
|
||||||
"pset"
|
PriceSetIdPrefix
|
||||||
)
|
)
|
||||||
|
|
||||||
const { prices, rules = [], ...rest } = inputData
|
const { prices, rules = [], ...rest } = inputData
|
||||||
@@ -713,79 +715,62 @@ export default class PricingModuleService<
|
|||||||
ruleTypes.map((rt) => [rt.rule_attribute, rt])
|
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 { prices = [], rules = {}, ...rest } = priceListData
|
||||||
const { rules = {}, prices = [], ...priceListOnlyData } = priceListData
|
|
||||||
|
|
||||||
validatePriceListDates(priceListData)
|
validatePriceListDates(priceListData)
|
||||||
|
|
||||||
priceListsToCreate.push({
|
const priceListRules = Object.entries(rules).map(
|
||||||
...priceListOnlyData,
|
([attribute, value]) => {
|
||||||
rules_count: Object.keys(rules).length,
|
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,
|
priceListsToCreate,
|
||||||
sharedContext
|
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_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
|
|||||||
Reference in new issue
Block a user