1 import { PriceRule } from "@models"
2
3 import { CreatePriceRuleDTO } from "@medusajs/framework/types"
4 import { SqlEntityManager } from "@medusajs/framework/mikro-orm/postgresql"
5 import { toMikroORMEntity } from "@medusajs/framework/utils"
6 import { defaultPriceRuleData } from "./data"
7
8 export * from "./data"
9 export * from "./operators"
10
11 export async function createPriceRules(
12 manager: SqlEntityManager,
13 pricesRulesData: CreatePriceRuleDTO[] = defaultPriceRuleData
14 ): Promise<PriceRule[]> {
15 const priceRules: PriceRule[] = []
16
17 for (let priceRuleData of pricesRulesData) {
18 const priceRuleDataClone: CreatePriceRuleDTO = { ...priceRuleData }
19
20 priceRuleDataClone.price_set_id = priceRuleDataClone.price_set_id
21 priceRuleDataClone.attribute = priceRuleDataClone.attribute
22 priceRuleDataClone.price_id = priceRuleDataClone.price_id
23
24 const priceRule = manager.create(
25 toMikroORMEntity(PriceRule),
26 priceRuleDataClone
27 )
28
29 priceRules.push(priceRule)
30 }
31
32 await manager.persistAndFlush(priceRules)
33
34 return priceRules
35 }