1 import { model, OrderStatus } from "@medusajs/framework/utils"
2 import { Return } from "@models"
3 import { OrderAddress } from "./address"
4 import { OrderCreditLine } from "./credit-line"
5 import { OrderItem } from "./order-item"
6 import { OrderShipping } from "./order-shipping-method"
7 import { OrderSummary } from "./order-summary"
8 import { OrderTransaction } from "./transaction"
9
10 const _Order = model
11 .define("Order", {
12 id: model.id({ prefix: "order" }).primaryKey(),
13 /**
14 * An auto-incremented display ID for the order.
15 */
16 display_id: model.autoincrement().searchable(),
17 /**
18 * A custom display ID for the order.
19 */
20 custom_display_id: model.text().searchable().nullable(),
21 /**
22 * The ID of the region the order belongs to.
23 */
24 region_id: model.text().nullable(),
25 /**
26 * The ID of the customer that placed the order.
27 */
28 customer_id: model.text().nullable(),
29 /**
30 * The order's version number.
31 */
32 version: model.number().default(1),
33 /**
34 * The ID of the sales channel the order was placed in.
35 */
36 sales_channel_id: model.text().nullable(),
37 /**
38 * The order's status.
39 */
40 status: model.enum(OrderStatus).default(OrderStatus.PENDING),
41 /**
42 * Whether the order is a draft order.
43 */
44 is_draft_order: model.boolean().default(false),
45 /**
46 * The email associated with the order.
47 */
48 email: model.text().searchable().nullable(),
49 /**
50 * The ISO 3 character currency code of the order.
51 * @example "usd"
52 */
53 currency_code: model.text(),
54 /**
55 * The BCP 47 language tag of the order's locale.
56 * @example "en-US"
57 */
58 locale: model.text().nullable(),
59 /**
60 * Whether notifications should be sent for this order.
61 */
62 no_notification: model.boolean().nullable(),
63 /**
64 * Custom key-value metadata for the order.
65 */
66 metadata: model.json().nullable(),
67 /**
68 * The date the order was canceled.
69 */
70 canceled_at: model.dateTime().nullable(),
71 /**
72 * The associated shipping address.
73 */
74 shipping_address: model
75 .hasOne<any>(() => OrderAddress, {
76 mappedBy: undefined,
77 foreignKey: true,
78 })
79 .searchable()
80 .nullable(),
81 /**
82 * The associated billing address.
83 */
84 billing_address: model
85 .hasOne<any>(() => OrderAddress, {
86 mappedBy: undefined,
87 foreignKey: true,
88 })
89 .searchable()
90 .nullable(),
91 /**
92 * The associated order summaries.
93 */
94 summary: model.hasMany<any>(() => OrderSummary, {
95 mappedBy: "order",
96 }),
97 /**
98 * The associated order items.
99 */
100 items: model.hasMany<any>(() => OrderItem, {
101 mappedBy: "order",
102 }),
103 /**
104 * The associated shipping methods.
105 */
106 shipping_methods: model.hasMany<any>(() => OrderShipping, {
107 mappedBy: "order",
108 }),
109 /**
110 * The associated transactions.
111 */
112 transactions: model.hasMany<any>(() => OrderTransaction, {
113 mappedBy: "order",
114 }),
115 /**
116 * The associated credit lines.
117 */
118 credit_lines: model.hasMany<any>(() => OrderCreditLine, {
119 mappedBy: "order",
120 }),
121 /**
122 * The associated returns.
123 */
124 returns: model.hasMany<any>(() => Return, {
125 mappedBy: "order",
126 }),
127 })
128 .cascades({
129 delete: [
130 "summary",
131 "items",
132 "shipping_methods",
133 "transactions",
134 "credit_lines",
135 ],
136 })
137 .indexes([
138 {
139 name: "IDX_order_display_id",
140 on: ["display_id"],
141 unique: false,
142 where: "deleted_at IS NULL",
143 },
144 {
145 name: "IDX_order_custom_display_id",
146 on: ["custom_display_id"],
147 unique: true,
148 where: "deleted_at IS NULL",
149 },
150 {
151 name: "IDX_order_region_id",
152 on: ["region_id"],
153 unique: false,
154 where: "deleted_at IS NULL",
155 },
156 {
157 name: "IDX_order_customer_id",
158 on: ["customer_id"],
159 unique: false,
160 where: "deleted_at IS NULL",
161 },
162 {
163 name: "IDX_order_sales_channel_id",
164 on: ["sales_channel_id"],
165 unique: false,
166 where: "deleted_at IS NULL",
167 },
168 {
169 name: "IDX_order_deleted_at",
170 on: ["deleted_at"],
171 unique: false,
172 where: "deleted_at IS NOT NULL",
173 },
174 {
175 name: "IDX_order_currency_code",
176 on: ["currency_code"],
177 unique: false,
178 where: "deleted_at IS NULL",
179 },
180 {
181 name: "IDX_order_shipping_address_id",
182 on: ["shipping_address_id"],
183 unique: false,
184 where: "deleted_at IS NULL",
185 },
186 {
187 name: "IDX_order_billing_address_id",
188 on: ["billing_address_id"],
189 unique: false,
190 where: "deleted_at IS NULL",
191 },
192 {
193 name: "IDX_order_is_draft_order",
194 on: ["is_draft_order"],
195 unique: false,
196 where: "deleted_at IS NULL",
197 },
198 ])
199
200 /**
201 * An order placed by a customer.
202 */
203 export const Order = _Order