feat(pricing): add price rule entity (#5050)
**What** - add price-rule entity to pricing module blocked by #4977 Fixes CORE-1497 Co-authored-by: Riqwan Thamir <5105988+riqwan@users.noreply.github.com> Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
20 files changed
+1444
-18
No files matched your search
@@ -0,0 +1,24 @@
|
||||
import { CreatePriceRuleDTO } from "@medusajs/types"
|
||||
|
||||
export const defaultPriceRuleData = [
|
||||
{
|
||||
id: "price-rule-1",
|
||||
price_set_id: "price-set-1",
|
||||
rule_type_id: "rule-type-1",
|
||||
value: "region_1",
|
||||
price_list_id: "test",
|
||||
price_set_money_amount: {
|
||||
money_amount: { amount: 100, currency_code: "EUR" },
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "price-rule-2",
|
||||
price_set_id: "price-set-2",
|
||||
rule_type_id: "rule-type-1",
|
||||
value: "region_2",
|
||||
price_list_id: "test",
|
||||
price_set_money_amount: {
|
||||
money_amount: { amount: 100, currency_code: "EUR" },
|
||||
},
|
||||
},
|
||||
] as unknown as CreatePriceRuleDTO[]
|
||||
@@ -0,0 +1,85 @@
|
||||
import { PriceRule, PriceSet, PriceSetMoneyAmount, RuleType } from "@models"
|
||||
|
||||
import { CreatePriceRuleDTO } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { createMoneyAmounts } from "../money-amount"
|
||||
import { createRuleTypes } from "../rule-type"
|
||||
import { defaultPriceRuleData } from "./data"
|
||||
|
||||
export async function createPriceRules(
|
||||
manager: SqlEntityManager,
|
||||
pricesRulesData: CreatePriceRuleDTO[] = defaultPriceRuleData
|
||||
): Promise<PriceRule[]> {
|
||||
const priceRules: PriceRule[] = []
|
||||
|
||||
for (let priceRuleData of pricesRulesData) {
|
||||
const priceRuleDataClone = { ...priceRuleData }
|
||||
|
||||
if (priceRuleDataClone.price_set_id) {
|
||||
priceRuleDataClone.price_set = manager.getReference(
|
||||
PriceSet,
|
||||
priceRuleDataClone.price_set_id
|
||||
)
|
||||
}
|
||||
|
||||
let dbRuleType: RuleType | undefined = await manager.findOne(RuleType, {
|
||||
id: priceRuleDataClone.rule_type_id,
|
||||
})
|
||||
|
||||
if (!dbRuleType) {
|
||||
const [createdRuleType] = await createRuleTypes(manager, [
|
||||
{
|
||||
id: priceRuleDataClone.rule_type_id,
|
||||
name: "rule 1",
|
||||
rule_attribute: "region_id",
|
||||
},
|
||||
])
|
||||
|
||||
dbRuleType = createdRuleType
|
||||
}
|
||||
|
||||
priceRuleDataClone.rule_type = manager.getReference(
|
||||
RuleType,
|
||||
dbRuleType!.id
|
||||
)
|
||||
|
||||
const priceSetMoneyAmountId =
|
||||
priceRuleDataClone.price_set_money_amount_id ||
|
||||
priceRuleDataClone.price_set_money_amount?.id
|
||||
|
||||
let psma: PriceSetMoneyAmount = await manager.findOne(PriceSetMoneyAmount, {id: priceSetMoneyAmountId})
|
||||
|
||||
if (!psma) {
|
||||
const [ma] = await createMoneyAmounts(manager, [
|
||||
priceRuleDataClone.price_set_money_amount.money_amount ?? {
|
||||
amount: 100,
|
||||
currency_code: "EUR",
|
||||
},
|
||||
])
|
||||
|
||||
psma = manager.create(PriceSetMoneyAmount, {
|
||||
price_set: manager.getReference(
|
||||
PriceSet,
|
||||
priceRuleDataClone.price_set.id
|
||||
),
|
||||
money_amount: ma.id,
|
||||
title: "test",
|
||||
})
|
||||
|
||||
await manager.persist(psma).flush()
|
||||
}
|
||||
|
||||
priceRuleDataClone.price_set_money_amount = manager.getReference(
|
||||
PriceSetMoneyAmount,
|
||||
psma.id
|
||||
)
|
||||
|
||||
const priceRule = manager.create(PriceRule, priceRuleDataClone)
|
||||
|
||||
priceRules.push(priceRule)
|
||||
}
|
||||
|
||||
await manager.persistAndFlush(priceRules)
|
||||
|
||||
return priceRules
|
||||
}
|
||||
@@ -1,20 +1,19 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { RuleType } from "@models"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { defaultRuleTypesData } from "./data"
|
||||
|
||||
export async function createRuleTypes(
|
||||
manager: SqlEntityManager,
|
||||
ruletypesData: any[] = defaultRuleTypesData
|
||||
): Promise<RuleType[]> {
|
||||
const RuleTypes: RuleType[] = []
|
||||
const ruleTypes: RuleType[] = []
|
||||
|
||||
for (let ruleTypeData of ruletypesData) {
|
||||
const ruleType = manager.create(RuleType, ruleTypeData)
|
||||
|
||||
RuleTypes.push(ruleType)
|
||||
await manager.persistAndFlush(ruleType)
|
||||
ruleTypes.push(ruleType)
|
||||
}
|
||||
|
||||
await manager.persistAndFlush(RuleTypes)
|
||||
|
||||
return RuleTypes
|
||||
return ruleTypes
|
||||
}
|
||||
Reference in new issue
Block a user