1 import { model } from "@medusajs/framework/utils"
2 import Cart from "./cart"
3 import ShippingMethodAdjustment from "./shipping-method-adjustment"
4 import ShippingMethodTaxLine from "./shipping-method-tax-line"
5
6 const ShippingMethod = model
7 .define(
8 {
9 name: "ShippingMethod",
10 tableName: "cart_shipping_method",
11 },
12 {
13 id: model.id({ prefix: "casm" }).primaryKey(),
14 name: model.text(),
15 description: model.json().nullable(),
16 amount: model.bigNumber(),
17 is_tax_inclusive: model.boolean().default(false),
18 shipping_option_id: model.text().nullable(),
19 data: model.json().nullable(),
20 metadata: model.json().nullable(),
21 original_total: model.bigNumber().computed(),
22 original_subtotal: model.bigNumber().computed(),
23 original_tax_total: model.bigNumber().computed(),
24 total: model.bigNumber().computed(),
25 subtotal: model.bigNumber().computed(),
26 tax_total: model.bigNumber().computed(),
27 discount_total: model.bigNumber().computed(),
28 discount_tax_total: model.bigNumber().computed(),
29 cart: model.belongsTo(() => Cart, {
30 mappedBy: "shipping_methods",
31 }),
32 tax_lines: model.hasMany(() => ShippingMethodTaxLine, {
33 mappedBy: "shipping_method",
34 }),
35 adjustments: model.hasMany(() => ShippingMethodAdjustment, {
36 mappedBy: "shipping_method",
37 }),
38 }
39 )
40 .indexes([
41 {
42 name: "IDX_cart_shipping_method_cart_id",
43 on: ["cart_id"],
44 where: "deleted_at IS NULL",
45 },
46 {
47 name: "IDX_shipping_method_option_id",
48 on: ["shipping_option_id"],
49 where: "deleted_at IS NULL AND shipping_option_id IS NOT NULL",
50 },
51 ])
52 .checks([(columns) => `${columns.amount} >= 0`])
53
54 export default ShippingMethod