1 import { PromotionUtils, model } from "@medusajs/framework/utils"
2 import ApplicationMethod from "./application-method"
3 import Promotion from "./promotion"
4 import PromotionRuleValue from "./promotion-rule-value"
5
6 const PromotionRule = model
7 .define(
8 {
9 name: "PromotionRule",
10 tableName: "promotion_rule",
11 },
12 {
13 id: model.id({ prefix: "prorul" }).primaryKey(),
14 description: model.text().nullable(),
15 attribute: model.text().index("IDX_promotion_rule_attribute"),
16 operator: model
17 .enum(PromotionUtils.PromotionRuleOperator)
18 .index("IDX_promotion_rule_operator"),
19 values: model.hasMany(() => PromotionRuleValue, {
20 mappedBy: "promotion_rule",
21 }),
22 promotions: model.manyToMany(() => Promotion, {
23 mappedBy: "rules",
24 }),
25 method_target_rules: model.manyToMany(() => ApplicationMethod, {
26 mappedBy: "target_rules",
27 }),
28 method_buy_rules: model.manyToMany(() => ApplicationMethod, {
29 mappedBy: "buy_rules",
30 }),
31 }
32 )
33 .indexes([
34 {
35 on: ["attribute", "operator"],
36 unique: false,
37 where: "deleted_at IS NULL",
38 },
39 {
40 name: "IDX_promotion_rule_attribute_operator_id",
41 on: ["operator", "attribute", "id"],
42 unique: false,
43 where: "deleted_at IS NULL",
44 },
45 ])
46 .cascades({
47 delete: ["values"],
48 })
49
50 export default PromotionRule