1 import { PromotionUtils, model } from "@medusajs/framework/utils"
2 import ApplicationMethod from "./application-method"
3 import Campaign from "./campaign"
4 import PromotionRule from "./promotion-rule"
5
6 const Promotion = model
7 .define("Promotion", {
8 id: model.id({ prefix: "promo" }).primaryKey(),
9 code: model.text().searchable(),
10 is_automatic: model.boolean().default(false),
11 is_tax_inclusive: model.boolean().default(false),
12 /**
13 * @since 2.12.0
14 */
15 limit: model.number().nullable(),
16 /**
17 * @since 2.12.0
18 */
19 used: model.number().default(0),
20 type: model.enum(PromotionUtils.PromotionType).index("IDX_promotion_type"),
21 status: model
22 .enum(PromotionUtils.PromotionStatus)
23 .index("IDX_promotion_status")
24 .default(PromotionUtils.PromotionStatus.DRAFT),
25 campaign: model
26 .belongsTo(() => Campaign, {
27 mappedBy: "promotions",
28 })
29 .nullable(),
30 application_method: model
31 .hasOne<() => typeof ApplicationMethod>(() => ApplicationMethod, {
32 mappedBy: "promotion",
33 })
34 .nullable(),
35 rules: model.manyToMany<() => typeof PromotionRule>(() => PromotionRule, {
36 pivotTable: "promotion_promotion_rule",
37 mappedBy: "promotions",
38 }),
39 /**
40 * @since 2.12.0
41 */
42 metadata: model.json().nullable(),
43 })
44 .cascades({
45 delete: ["application_method"],
46 })
47 .indexes([
48 {
49 name: "IDX_unique_promotion_code",
50 on: ["code"],
51 where: "deleted_at IS NULL",
52 unique: true,
53 },
54 {
55 name: "IDX_promotion_is_automatic",
56 on: ["is_automatic"],
57 unique: false,
58 where: "deleted_at IS NULL",
59 },
60 ])
61
62 export default Promotion