From 058d24fbcdc56f1e22c702ab05b33fdde4d7cac1 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Mon, 2 Dec 2024 13:48:44 +0530 Subject: [PATCH 01/12] refactor: migrate cart module to DML --- .../__fixtures__/cart/index.ts | 9 +- .../src/migrations/.snapshot-medusa-cart.json | 579 +++++++++--------- .../src/migrations/Migration20241202080921.ts | 163 +++++ packages/modules/cart/src/models/address.ts | 193 +++--- .../cart/src/models/adjustment-line.ts | 83 ++- packages/modules/cart/src/models/cart.ts | 323 +++++----- .../cart/src/models/line-item-adjustment.ts | 132 ++-- .../cart/src/models/line-item-tax-line.ts | 122 ++-- packages/modules/cart/src/models/line-item.ts | 368 +++++------ .../src/models/shipping-method-adjustment.ts | 123 ++-- .../src/models/shipping-method-tax-line.ts | 123 ++-- .../cart/src/models/shipping-method.ts | 267 ++++---- packages/modules/cart/src/models/tax-line.ts | 89 +-- .../modules/cart/src/services/cart-module.ts | 62 +- 14 files changed, 1366 insertions(+), 1270 deletions(-) create mode 100644 packages/modules/cart/src/migrations/Migration20241202080921.ts diff --git a/packages/modules/cart/integration-tests/__fixtures__/cart/index.ts b/packages/modules/cart/integration-tests/__fixtures__/cart/index.ts index d143925d6bd81..1bbac62243bc7 100644 --- a/packages/modules/cart/integration-tests/__fixtures__/cart/index.ts +++ b/packages/modules/cart/integration-tests/__fixtures__/cart/index.ts @@ -1,18 +1,19 @@ -import { CreateCartDTO } from "@medusajs/framework/types" +import { CreateCartDTO, InferEntityType } from "@medusajs/framework/types" import { SqlEntityManager } from "@mikro-orm/postgresql" import { Cart } from "../../../src/models" import { defaultCartsData } from "./data" +import { toMikroORMEntity } from "@medusajs/framework/utils" export * from "./data" export async function createCarts( manager: SqlEntityManager, cartsData: CreateCartDTO[] = defaultCartsData -): Promise { - const carts: Cart[] = [] +): Promise[]> { + const carts: InferEntityType[] = [] for (let cartData of cartsData) { - let cart = manager.create(Cart, cartData) + let cart = manager.create(toMikroORMEntity(Cart), cartData) await manager.persistAndFlush(cart) } diff --git a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json index a48b7e87a0894..7f91d43aa0df6 100644 --- a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json +++ b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json @@ -161,13 +161,11 @@ "indexes": [ { "keyName": "IDX_cart_address_deleted_at", - "columnNames": [ - "deleted_at" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_address_deleted_at\" ON \"cart_address\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_address_deleted_at\" ON \"cart_address\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "cart_address_pkey", @@ -238,42 +236,42 @@ "nullable": false, "mappedType": "text" }, - "shipping_address_id": { - "name": "shipping_address_id", - "type": "text", + "metadata": { + "name": "metadata", + "type": "jsonb", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "text" + "mappedType": "json" }, - "billing_address_id": { - "name": "billing_address_id", - "type": "text", + "completed_at": { + "name": "completed_at", + "type": "timestamptz", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "text" + "length": 6, + "mappedType": "datetime" }, - "metadata": { - "name": "metadata", - "type": "jsonb", + "shipping_address_id": { + "name": "shipping_address_id", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "json" + "mappedType": "text" }, - "completed_at": { - "name": "completed_at", - "type": "timestamptz", + "billing_address_id": { + "name": "billing_address_id", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "length": 6, - "mappedType": "datetime" + "mappedType": "text" }, "created_at": { "name": "created_at", @@ -311,11 +309,33 @@ "name": "cart", "schema": "public", "indexes": [ + { + "keyName": "IDX_cart_shipping_address_id", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_address_id\" ON \"cart\" (shipping_address_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_cart_billing_address_id", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_billing_address_id\" ON \"cart\" (billing_address_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_cart_deleted_at", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_deleted_at\" ON \"cart\" (deleted_at) WHERE deleted_at IS NULL" + }, { "keyName": "IDX_cart_region_id", - "columnNames": [ - "region_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -323,9 +343,7 @@ }, { "keyName": "IDX_cart_customer_id", - "columnNames": [ - "customer_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -333,9 +351,7 @@ }, { "keyName": "IDX_cart_sales_channel_id", - "columnNames": [ - "sales_channel_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -343,9 +359,7 @@ }, { "keyName": "IDX_cart_curency_code", - "columnNames": [ - "currency_code" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -353,9 +367,7 @@ }, { "keyName": "IDX_cart_shipping_address_id", - "columnNames": [ - "shipping_address_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -363,24 +375,12 @@ }, { "keyName": "IDX_cart_billing_address_id", - "columnNames": [ - "billing_address_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_billing_address_id\" ON \"cart\" (billing_address_id) WHERE deleted_at IS NULL AND billing_address_id IS NOT NULL" }, - { - "keyName": "IDX_cart_deleted_at", - "columnNames": [ - "deleted_at" - ], - "composite": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_deleted_at\" ON \"cart\" (deleted_at) WHERE deleted_at IS NOT NULL" - }, { "keyName": "cart_pkey", "columnNames": [ @@ -432,15 +432,6 @@ "nullable": false, "mappedType": "text" }, - "cart_id": { - "name": "cart_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, "title": { "name": "title", "type": "text", @@ -549,15 +540,6 @@ "nullable": true, "mappedType": "text" }, - "product_handle": { - "name": "product_handle", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, "variant_sku": { "name": "variant_sku", "type": "text", @@ -601,6 +583,7 @@ "autoincrement": false, "primary": false, "nullable": false, + "default": "true", "mappedType": "boolean" }, "is_discountable": { @@ -610,6 +593,7 @@ "autoincrement": false, "primary": false, "nullable": false, + "default": "true", "mappedType": "boolean" }, "is_tax_inclusive": { @@ -619,6 +603,7 @@ "autoincrement": false, "primary": false, "nullable": false, + "default": "false", "mappedType": "boolean" }, "compare_at_unit_price": { @@ -630,8 +615,17 @@ "nullable": true, "mappedType": "decimal" }, - "raw_compare_at_unit_price": { - "name": "raw_compare_at_unit_price", + "unit_price": { + "name": "unit_price", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "metadata": { + "name": "metadata", "type": "jsonb", "unsigned": false, "autoincrement": false, @@ -639,31 +633,31 @@ "nullable": true, "mappedType": "json" }, - "unit_price": { - "name": "unit_price", - "type": "numeric", + "cart_id": { + "name": "cart_id", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": false, - "mappedType": "decimal" + "mappedType": "text" }, - "raw_unit_price": { - "name": "raw_unit_price", + "raw_compare_at_unit_price": { + "name": "raw_compare_at_unit_price", "type": "jsonb", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": false, + "nullable": true, "mappedType": "json" }, - "metadata": { - "name": "metadata", + "raw_unit_price": { + "name": "raw_unit_price", "type": "jsonb", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": true, + "nullable": false, "mappedType": "json" }, "created_at": { @@ -702,11 +696,25 @@ "name": "cart_line_item", "schema": "public", "indexes": [ + { + "keyName": "IDX_cart_line_item_cart_id", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_cart_id\" ON \"cart_line_item\" (cart_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_cart_line_item_deleted_at", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_deleted_at\" ON \"cart_line_item\" (deleted_at) WHERE deleted_at IS NULL" + }, { "keyName": "IDX_line_item_cart_id", - "columnNames": [ - "cart_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -714,9 +722,7 @@ }, { "keyName": "IDX_line_item_variant_id", - "columnNames": [ - "variant_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -724,9 +730,7 @@ }, { "keyName": "IDX_line_item_product_id", - "columnNames": [ - "product_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -734,24 +738,12 @@ }, { "keyName": "IDX_line_item_product_type_id", - "columnNames": [ - "product_type_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_product_type_id\" ON \"cart_line_item\" (product_type_id) WHERE deleted_at IS NULL AND product_type_id IS NOT NULL" }, - { - "keyName": "IDX_cart_line_item_deleted_at", - "columnNames": [ - "deleted_at" - ], - "composite": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_deleted_at\" ON \"cart_line_item\" (deleted_at) WHERE deleted_at IS NOT NULL" - }, { "keyName": "cart_line_item_pkey", "columnNames": [ @@ -774,6 +766,7 @@ "id" ], "referencedTableName": "public.cart", + "deleteRule": "cascade", "updateRule": "cascade" } } @@ -816,17 +809,17 @@ "nullable": false, "mappedType": "decimal" }, - "raw_amount": { - "name": "raw_amount", - "type": "jsonb", + "provider_id": { + "name": "provider_id", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": false, - "mappedType": "json" + "nullable": true, + "mappedType": "text" }, - "provider_id": { - "name": "provider_id", + "promotion_id": { + "name": "promotion_id", "type": "text", "unsigned": false, "autoincrement": false, @@ -843,6 +836,24 @@ "nullable": true, "mappedType": "json" }, + "item_id": { + "name": "item_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "raw_amount": { + "name": "raw_amount", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, "created_at": { "name": "created_at", "type": "timestamptz", @@ -865,24 +876,6 @@ "default": "now()", "mappedType": "datetime" }, - "item_id": { - "name": "item_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "promotion_id": { - "name": "promotion_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, "deleted_at": { "name": "deleted_at", "type": "timestamptz", @@ -898,34 +891,36 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_adjustment_item_id", - "columnNames": [ - "item_id" - ], + "keyName": "IDX_cart_line_item_adjustment_item_id", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_adjustment_item_id\" ON \"cart_line_item_adjustment\" (item_id) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_adjustment_item_id\" ON \"cart_line_item_adjustment\" (item_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_cart_line_item_adjustment_deleted_at", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_adjustment_deleted_at\" ON \"cart_line_item_adjustment\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_line_item_adjustment_promotion_id", - "columnNames": [ - "promotion_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_adjustment_promotion_id\" ON \"cart_line_item_adjustment\" (promotion_id) WHERE deleted_at IS NULL AND promotion_id IS NOT NULL" }, { - "keyName": "IDX_cart_line_item_adjustment_deleted_at", - "columnNames": [ - "deleted_at" - ], + "keyName": "IDX_adjustment_item_id", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_adjustment_deleted_at\" ON \"cart_line_item_adjustment\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_adjustment_item_id\" ON \"cart_line_item_adjustment\" (item_id) WHERE deleted_at IS NULL" }, { "keyName": "cart_line_item_adjustment_pkey", @@ -937,13 +932,7 @@ "unique": true } ], - "checks": [ - { - "name": "cart_line_item_adjustment_check", - "expression": "amount >= 0", - "definition": "check ((amount >= 0))" - } - ], + "checks": [], "foreignKeys": { "cart_line_item_adjustment_item_id_foreign": { "constraintName": "cart_line_item_adjustment_item_id_foreign", @@ -955,6 +944,7 @@ "id" ], "referencedTableName": "public.cart_line_item", + "deleteRule": "cascade", "updateRule": "cascade" } } @@ -990,12 +980,12 @@ }, "rate": { "name": "rate", - "type": "numeric", + "type": "integer", "unsigned": false, "autoincrement": false, "primary": false, "nullable": false, - "mappedType": "decimal" + "mappedType": "integer" }, "provider_id": { "name": "provider_id", @@ -1006,14 +996,23 @@ "nullable": true, "mappedType": "text" }, - "metadata": { - "name": "metadata", - "type": "jsonb", + "tax_rate_id": { + "name": "tax_rate_id", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "json" + "mappedType": "text" + }, + "item_id": { + "name": "item_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" }, "created_at": { "name": "created_at", @@ -1037,24 +1036,6 @@ "default": "now()", "mappedType": "datetime" }, - "item_id": { - "name": "item_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "tax_rate_id": { - "name": "tax_rate_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, "deleted_at": { "name": "deleted_at", "type": "timestamptz", @@ -1070,34 +1051,36 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_tax_line_item_id", - "columnNames": [ - "item_id" - ], + "keyName": "IDX_cart_line_item_tax_line_item_id", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_tax_line_item_id\" ON \"cart_line_item_tax_line\" (item_id) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_tax_line_item_id\" ON \"cart_line_item_tax_line\" (item_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_cart_line_item_tax_line_deleted_at", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_tax_line_deleted_at\" ON \"cart_line_item_tax_line\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_line_item_tax_line_tax_rate_id", - "columnNames": [ - "tax_rate_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_tax_line_tax_rate_id\" ON \"cart_line_item_tax_line\" (tax_rate_id) WHERE deleted_at IS NULL AND tax_rate_id IS NOT NULL" }, { - "keyName": "IDX_cart_line_item_tax_line_deleted_at", - "columnNames": [ - "deleted_at" - ], + "keyName": "IDX_tax_line_item_id", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_tax_line_deleted_at\" ON \"cart_line_item_tax_line\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_tax_line_item_id\" ON \"cart_line_item_tax_line\" (item_id) WHERE deleted_at IS NULL" }, { "keyName": "cart_line_item_tax_line_pkey", @@ -1121,6 +1104,7 @@ "id" ], "referencedTableName": "public.cart_line_item", + "deleteRule": "cascade", "updateRule": "cascade" } } @@ -1136,15 +1120,6 @@ "nullable": false, "mappedType": "text" }, - "cart_id": { - "name": "cart_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, "name": { "name": "name", "type": "text", @@ -1172,15 +1147,6 @@ "nullable": false, "mappedType": "decimal" }, - "raw_amount": { - "name": "raw_amount", - "type": "jsonb", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "json" - }, "is_tax_inclusive": { "name": "is_tax_inclusive", "type": "boolean", @@ -1218,6 +1184,24 @@ "nullable": true, "mappedType": "json" }, + "cart_id": { + "name": "cart_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "raw_amount": { + "name": "raw_amount", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, "created_at": { "name": "created_at", "type": "timestamptz", @@ -1255,34 +1239,36 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_shipping_method_cart_id", - "columnNames": [ - "cart_id" - ], + "keyName": "IDX_cart_shipping_method_cart_id", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_cart_id\" ON \"cart_shipping_method\" (cart_id) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_cart_id\" ON \"cart_shipping_method\" (cart_id) WHERE deleted_at IS NULL" }, { - "keyName": "IDX_shipping_method_option_id", - "columnNames": [ - "shipping_option_id" - ], + "keyName": "IDX_cart_shipping_method_deleted_at", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_option_id\" ON \"cart_shipping_method\" (shipping_option_id) WHERE deleted_at IS NULL AND shipping_option_id IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_deleted_at\" ON \"cart_shipping_method\" (deleted_at) WHERE deleted_at IS NULL" }, { - "keyName": "IDX_cart_shipping_method_deleted_at", - "columnNames": [ - "deleted_at" - ], + "keyName": "IDX_shipping_method_cart_id", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_deleted_at\" ON \"cart_shipping_method\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_cart_id\" ON \"cart_shipping_method\" (cart_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_shipping_method_option_id", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_option_id\" ON \"cart_shipping_method\" (shipping_option_id) WHERE deleted_at IS NULL AND shipping_option_id IS NOT NULL" }, { "keyName": "cart_shipping_method_pkey", @@ -1294,13 +1280,7 @@ "unique": true } ], - "checks": [ - { - "name": "cart_shipping_method_check", - "expression": "amount >= 0", - "definition": "check ((amount >= 0))" - } - ], + "checks": [], "foreignKeys": { "cart_shipping_method_cart_id_foreign": { "constraintName": "cart_shipping_method_cart_id_foreign", @@ -1312,6 +1292,7 @@ "id" ], "referencedTableName": "public.cart", + "deleteRule": "cascade", "updateRule": "cascade" } } @@ -1354,17 +1335,26 @@ "nullable": false, "mappedType": "decimal" }, - "raw_amount": { - "name": "raw_amount", + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "metadata": { + "name": "metadata", "type": "jsonb", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": false, + "nullable": true, "mappedType": "json" }, - "provider_id": { - "name": "provider_id", + "promotion_id": { + "name": "promotion_id", "type": "text", "unsigned": false, "autoincrement": false, @@ -1372,13 +1362,22 @@ "nullable": true, "mappedType": "text" }, - "metadata": { - "name": "metadata", + "shipping_method_id": { + "name": "shipping_method_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "raw_amount": { + "name": "raw_amount", "type": "jsonb", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": true, + "nullable": false, "mappedType": "json" }, "created_at": { @@ -1403,24 +1402,6 @@ "default": "now()", "mappedType": "datetime" }, - "shipping_method_id": { - "name": "shipping_method_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "promotion_id": { - "name": "promotion_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, "deleted_at": { "name": "deleted_at", "type": "timestamptz", @@ -1436,34 +1417,36 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_adjustment_shipping_method_id", - "columnNames": [ - "shipping_method_id" - ], + "keyName": "IDX_cart_shipping_method_adjustment_shipping_method_id", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_adjustment_shipping_method_id\" ON \"cart_shipping_method_adjustment\" (shipping_method_id) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_adjustment_shipping_method_id\" ON \"cart_shipping_method_adjustment\" (shipping_method_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_cart_shipping_method_adjustment_deleted_at", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_adjustment_deleted_at\" ON \"cart_shipping_method_adjustment\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_shipping_method_adjustment_promotion_id", - "columnNames": [ - "promotion_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_adjustment_promotion_id\" ON \"cart_shipping_method_adjustment\" (promotion_id) WHERE deleted_at IS NULL AND promotion_id IS NOT NULL" }, { - "keyName": "IDX_cart_shipping_method_adjustment_deleted_at", - "columnNames": [ - "deleted_at" - ], + "keyName": "IDX_adjustment_shipping_method_id", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_adjustment_deleted_at\" ON \"cart_shipping_method_adjustment\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_adjustment_shipping_method_id\" ON \"cart_shipping_method_adjustment\" (shipping_method_id) WHERE deleted_at IS NULL" }, { "keyName": "cart_shipping_method_adjustment_pkey", @@ -1522,12 +1505,12 @@ }, "rate": { "name": "rate", - "type": "numeric", + "type": "integer", "unsigned": false, "autoincrement": false, "primary": false, "nullable": false, - "mappedType": "decimal" + "mappedType": "integer" }, "provider_id": { "name": "provider_id", @@ -1538,6 +1521,15 @@ "nullable": true, "mappedType": "text" }, + "tax_rate_id": { + "name": "tax_rate_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, "metadata": { "name": "metadata", "type": "jsonb", @@ -1547,6 +1539,15 @@ "nullable": true, "mappedType": "json" }, + "shipping_method_id": { + "name": "shipping_method_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, "created_at": { "name": "created_at", "type": "timestamptz", @@ -1569,24 +1570,6 @@ "default": "now()", "mappedType": "datetime" }, - "shipping_method_id": { - "name": "shipping_method_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "tax_rate_id": { - "name": "tax_rate_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, "deleted_at": { "name": "deleted_at", "type": "timestamptz", @@ -1602,34 +1585,36 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_tax_line_shipping_method_id", - "columnNames": [ - "shipping_method_id" - ], + "keyName": "IDX_cart_shipping_method_tax_line_shipping_method_id", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_tax_line_shipping_method_id\" ON \"cart_shipping_method_tax_line\" (shipping_method_id) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_tax_line_shipping_method_id\" ON \"cart_shipping_method_tax_line\" (shipping_method_id) WHERE deleted_at IS NULL" }, { - "keyName": "IDX_shipping_method_tax_line_tax_rate_id", - "columnNames": [ - "tax_rate_id" - ], + "keyName": "IDX_cart_shipping_method_tax_line_deleted_at", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_tax_line_tax_rate_id\" ON \"cart_shipping_method_tax_line\" (tax_rate_id) WHERE deleted_at IS NULL AND tax_rate_id IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_tax_line_deleted_at\" ON \"cart_shipping_method_tax_line\" (deleted_at) WHERE deleted_at IS NULL" }, { - "keyName": "IDX_cart_shipping_method_tax_line_deleted_at", - "columnNames": [ - "deleted_at" - ], + "keyName": "IDX_tax_line_shipping_method_id", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_tax_line_shipping_method_id\" ON \"cart_shipping_method_tax_line\" (shipping_method_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_shipping_method_tax_line_tax_rate_id", + "columnNames": [], "composite": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_tax_line_deleted_at\" ON \"cart_shipping_method_tax_line\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_tax_line_tax_rate_id\" ON \"cart_shipping_method_tax_line\" (tax_rate_id) WHERE deleted_at IS NULL AND tax_rate_id IS NOT NULL" }, { "keyName": "cart_shipping_method_tax_line_pkey", diff --git a/packages/modules/cart/src/migrations/Migration20241202080921.ts b/packages/modules/cart/src/migrations/Migration20241202080921.ts new file mode 100644 index 0000000000000..27ba5ef3508b0 --- /dev/null +++ b/packages/modules/cart/src/migrations/Migration20241202080921.ts @@ -0,0 +1,163 @@ +import { Migration } from "@mikro-orm/migrations" + +export class Migration20241202080921 extends Migration { + async up(): Promise { + this.addSql( + 'alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item_adjustment" drop constraint if exists "cart_line_item_adjustment_item_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item_tax_line" drop constraint if exists "cart_line_item_tax_line_item_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item" alter column "requires_shipping" type boolean using ("requires_shipping"::boolean);' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "requires_shipping" set default true;' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_discountable" type boolean using ("is_discountable"::boolean);' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_discountable" set default true;' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" type boolean using ("is_tax_inclusive"::boolean);' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" set default false;' + ) + this.addSql( + 'alter table if exists "cart_line_item" drop column if exists "product_handle";' + ) + this.addSql( + 'alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_cart_id" ON "cart_line_item" (cart_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "cart_line_item_adjustment" drop constraint if exists cart_line_item_adjustment_check;' + ) + this.addSql( + 'alter table if exists "cart_line_item_adjustment" add constraint "cart_line_item_adjustment_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_adjustment_item_id" ON "cart_line_item_adjustment" (item_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "cart_line_item_tax_line" alter column "rate" type integer using ("rate"::integer);' + ) + this.addSql( + 'alter table if exists "cart_line_item_tax_line" drop column if exists "metadata";' + ) + this.addSql( + 'alter table if exists "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_tax_line_item_id" ON "cart_line_item_tax_line" (item_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method" drop constraint if exists cart_shipping_method_check;' + ) + this.addSql( + 'alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_cart_id" ON "cart_shipping_method" (cart_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_adjustment_shipping_method_id" ON "cart_shipping_method_adjustment" (shipping_method_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method_tax_line" alter column "rate" type integer using ("rate"::integer);' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_tax_line_shipping_method_id" ON "cart_shipping_method_tax_line" (shipping_method_id) WHERE deleted_at IS NULL;' + ) + } + + async down(): Promise { + this.addSql( + 'alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item_adjustment" drop constraint if exists "cart_line_item_adjustment_item_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item_tax_line" drop constraint if exists "cart_line_item_tax_line_item_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item" add column if not exists "product_handle" text null;' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" drop default;' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" type boolean using ("is_tax_inclusive"::boolean);' + ) + this.addSql('drop index if exists "IDX_cart_line_item_cart_id";') + this.addSql( + 'alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;' + ) + + this.addSql('drop index if exists "IDX_cart_line_item_adjustment_item_id";') + this.addSql( + 'alter table if exists "cart_line_item_adjustment" add constraint "cart_line_item_adjustment_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade;' + ) + this.addSql( + 'alter table if exists "cart_line_item_adjustment" add constraint cart_line_item_adjustment_check check(amount >= 0);' + ) + + this.addSql( + 'alter table if exists "cart_line_item_tax_line" add column if not exists "metadata" jsonb null;' + ) + this.addSql( + 'alter table if exists "cart_line_item_tax_line" alter column "rate" type numeric using ("rate"::numeric);' + ) + this.addSql('drop index if exists "IDX_cart_line_item_tax_line_item_id";') + this.addSql( + 'alter table if exists "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade;' + ) + + this.addSql('drop index if exists "IDX_cart_shipping_method_cart_id";') + this.addSql( + 'alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;' + ) + this.addSql( + 'alter table if exists "cart_shipping_method" add constraint cart_shipping_method_check check(amount >= 0);' + ) + + this.addSql( + 'drop index if exists "IDX_cart_shipping_method_adjustment_shipping_method_id";' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method_tax_line" alter column "rate" type numeric using ("rate"::numeric);' + ) + this.addSql( + 'drop index if exists "IDX_cart_shipping_method_tax_line_shipping_method_id";' + ) + } +} diff --git a/packages/modules/cart/src/models/address.ts b/packages/modules/cart/src/models/address.ts index 270ba6dbf3bde..0c02cd8dddcf6 100644 --- a/packages/modules/cart/src/models/address.ts +++ b/packages/modules/cart/src/models/address.ts @@ -1,97 +1,100 @@ -import { DAL } from "@medusajs/framework/types" -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Entity, - Filter, - OnInit, - OptionalProps, - PrimaryKey, - Property, -} from "@mikro-orm/core" - -type OptionalAddressProps = DAL.SoftDeletableModelDateColumns - -const PgIndex = createPsqlIndexStatementHelper({ - tableName: "cart_address", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}) - -@Entity({ tableName: "cart_address" }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class Address { - [OptionalProps]: OptionalAddressProps - - @PrimaryKey({ columnType: "text" }) - id!: string - - @Property({ columnType: "text", nullable: true }) - customer_id: string | null = null - - @Property({ columnType: "text", nullable: true }) - company: string | null = null - - @Property({ columnType: "text", nullable: true }) - first_name: string | null = null - - @Property({ columnType: "text", nullable: true }) - last_name: string | null = null - - @Property({ columnType: "text", nullable: true }) - address_1: string | null = null - - @Property({ columnType: "text", nullable: true }) - address_2: string | null = null - - @Property({ columnType: "text", nullable: true }) - city: string | null = null - - @Property({ columnType: "text", nullable: true }) - country_code: string | null = null - - @Property({ columnType: "text", nullable: true }) - province: string | null = null - - @Property({ columnType: "text", nullable: true }) - postal_code: string | null = null - - @Property({ columnType: "text", nullable: true }) - phone: string | null = null - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null = null - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - updated_at: Date - - @PgIndex.MikroORMIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "caaddr") +import { model } from "@medusajs/framework/utils" +import Cart from "./cart" + +const Address = model.define( + { tableName: "cart_address", name: "Address" }, + { + id: model.id({ prefix: "caaddr" }).primaryKey(), + customer_id: model.text().nullable(), + company: model.text().nullable(), + first_name: model.text().nullable(), + last_name: model.text().nullable(), + address_1: model.text().nullable(), + address_2: model.text().nullable(), + city: model.text().nullable(), + country_code: model.text().nullable(), + province: model.text().nullable(), + postal_code: model.text().nullable(), + phone: model.text().nullable(), + metadata: model.json().nullable(), + carts: model.hasMany(() => Cart, { + mappedBy: "shipping_address", + }), } +) - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "caaddr") - } -} +// @Entity({ tableName: "cart_address" }) +// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) +// export default class Address { +// [OptionalProps]: OptionalAddressProps + +// @PrimaryKey({ columnType: "text" }) +// id!: string + +// @Property({ columnType: "text", nullable: true }) +// customer_id: string | null = null + +// @Property({ columnType: "text", nullable: true }) +// company: string | null = null + +// @Property({ columnType: "text", nullable: true }) +// first_name: string | null = null + +// @Property({ columnType: "text", nullable: true }) +// last_name: string | null = null + +// @Property({ columnType: "text", nullable: true }) +// address_1: string | null = null + +// @Property({ columnType: "text", nullable: true }) +// address_2: string | null = null + +// @Property({ columnType: "text", nullable: true }) +// city: string | null = null + +// @Property({ columnType: "text", nullable: true }) +// country_code: string | null = null + +// @Property({ columnType: "text", nullable: true }) +// province: string | null = null + +// @Property({ columnType: "text", nullable: true }) +// postal_code: string | null = null + +// @Property({ columnType: "text", nullable: true }) +// phone: string | null = null + +// @Property({ columnType: "jsonb", nullable: true }) +// metadata: Record | null = null + +// @Property({ +// onCreate: () => new Date(), +// columnType: "timestamptz", +// defaultRaw: "now()", +// }) +// created_at: Date + +// @Property({ +// onCreate: () => new Date(), +// onUpdate: () => new Date(), +// columnType: "timestamptz", +// defaultRaw: "now()", +// }) +// updated_at: Date + +// @PgIndex.MikroORMIndex() +// @Property({ columnType: "timestamptz", nullable: true }) +// deleted_at: Date | null = null + +// @BeforeCreate() +// onCreate() { +// this.id = generateEntityId(this.id, "caaddr") +// } + +// @OnInit() +// onInit() { +// this.id = generateEntityId(this.id, "caaddr") +// } +// } + +export default Address diff --git a/packages/modules/cart/src/models/adjustment-line.ts b/packages/modules/cart/src/models/adjustment-line.ts index 55b913c79404e..60d312fb62851 100644 --- a/packages/modules/cart/src/models/adjustment-line.ts +++ b/packages/modules/cart/src/models/adjustment-line.ts @@ -1,49 +1,46 @@ -import { DAL } from "@medusajs/framework/types" -import { BigNumber, MikroOrmBigNumberProperty } from "@medusajs/framework/utils" -import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core" +// import { model } from "@medusajs/framework/utils" -type OptionalAdjustmentLineProps = DAL.SoftDeletableModelDateColumns +// const AdjustmentLine = model.define("AdjustmentLine", { +// id: model.id().primaryKey(), +// description: model.text().nullable(), +// code: model.text().nullable(), +// amount: model.bigNumber(), +// provider_id: model.text().nullable(), +// metadata: model.json().nullable(), +// }) /** * As per the Mikro ORM docs, superclasses should use the abstract class definition * Source: https://mikro-orm.io/docs/inheritance-mapping */ -export default abstract class AdjustmentLine { - [OptionalProps]: OptionalAdjustmentLineProps - - @PrimaryKey({ columnType: "text" }) - id: string - - @Property({ columnType: "text", nullable: true }) - description: string | null = null - - @Property({ columnType: "text", nullable: true }) - code: string | null = null - - @MikroOrmBigNumberProperty() - amount: BigNumber | number - - @Property({ columnType: "jsonb" }) - raw_amount: Record - - @Property({ columnType: "text", nullable: true }) - provider_id: string | null = null - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null = null - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - updated_at: Date -} +// export default abstract class AdjustmentLine { +// [OptionalProps]: OptionalAdjustmentLineProps +// @PrimaryKey({ columnType: "text" }) +// id: string +// @Property({ columnType: "text", nullable: true }) +// description: string | null = null +// @Property({ columnType: "text", nullable: true }) +// code: string | null = null +// @MikroOrmBigNumberProperty() +// amount: BigNumber | number +// @Property({ columnType: "jsonb" }) +// raw_amount: Record +// @Property({ columnType: "text", nullable: true }) +// provider_id: string | null = null +// @Property({ columnType: "jsonb", nullable: true }) +// metadata: Record | null = null +// @Property({ +// onCreate: () => new Date(), +// columnType: "timestamptz", +// defaultRaw: "now()", +// }) +// created_at: Date +// @Property({ +// onCreate: () => new Date(), +// onUpdate: () => new Date(), +// columnType: "timestamptz", +// defaultRaw: "now()", +// }) +// updated_at: Date +// } +// export default AdjustmentLine diff --git a/packages/modules/cart/src/models/cart.ts b/packages/modules/cart/src/models/cart.ts index 9df5ba729675a..5a43588b5cd55 100644 --- a/packages/modules/cart/src/models/cart.ts +++ b/packages/modules/cart/src/models/cart.ts @@ -1,181 +1,154 @@ -import { DAL } from "@medusajs/framework/types" -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Cascade, - Collection, - Entity, - Filter, - ManyToOne, - OneToMany, - OnInit, - OptionalProps, - PrimaryKey, - Property, - Rel, -} from "@mikro-orm/core" +import { model } from "@medusajs/framework/utils" import Address from "./address" import LineItem from "./line-item" import ShippingMethod from "./shipping-method" -type OptionalCartProps = - | "shipping_address" - | "billing_address" - | DAL.SoftDeletableModelDateColumns - -const RegionIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_cart_region_id", - tableName: "cart", - columns: "region_id", - where: "deleted_at IS NULL AND region_id IS NOT NULL", -}).MikroORMIndex - -const CustomerIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_cart_customer_id", - tableName: "cart", - columns: "customer_id", - where: "deleted_at IS NULL AND customer_id IS NOT NULL", -}).MikroORMIndex - -const SalesChannelIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_cart_sales_channel_id", - tableName: "cart", - columns: "sales_channel_id", - where: "deleted_at IS NULL AND sales_channel_id IS NOT NULL", -}).MikroORMIndex - -const CurrencyCodeIndex = createPsqlIndexStatementHelper({ - name: "IDX_cart_curency_code", - tableName: "cart", - columns: "currency_code", - where: "deleted_at IS NULL", -}).MikroORMIndex - -const ShippingAddressIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_cart_shipping_address_id", - tableName: "cart", - columns: "shipping_address_id", - where: "deleted_at IS NULL AND shipping_address_id IS NOT NULL", -}).MikroORMIndex - -const BillingAddressIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_cart_billing_address_id", - tableName: "cart", - columns: "billing_address_id", - where: "deleted_at IS NULL AND billing_address_id IS NOT NULL", -}).MikroORMIndex - -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex - -@Entity({ tableName: "cart" }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class Cart { - [OptionalProps]?: OptionalCartProps - - @PrimaryKey({ columnType: "text" }) - id: string - - @RegionIdIndex() - @Property({ columnType: "text", nullable: true }) - region_id: string | null = null - - @CustomerIdIndex() - @Property({ columnType: "text", nullable: true }) - customer_id: string | null = null - - @SalesChannelIdIndex() - @Property({ columnType: "text", nullable: true }) - sales_channel_id: string | null = null - - @Property({ columnType: "text", nullable: true }) - email: string | null = null - - @CurrencyCodeIndex() - @Property({ columnType: "text" }) - currency_code: string - - @ShippingAddressIdIndex() - @ManyToOne({ - entity: () => Address, - columnType: "text", - fieldName: "shipping_address_id", - mapToPk: true, - nullable: true, +const Cart = model + .define("Cart", { + id: model.id({ prefix: "cart" }).primaryKey(), + region_id: model.text().nullable(), + customer_id: model.text().nullable(), + sales_channel_id: model.text().nullable(), + email: model.text().nullable(), + currency_code: model.text(), + metadata: model.json().nullable(), + completed_at: model.dateTime().nullable(), + shipping_address: model + .belongsTo(() => Address, { + mappedBy: "carts", + }) + .nullable(), + billing_address: model + .belongsTo(() => Address, { + mappedBy: "carts", + }) + .nullable(), + items: model.hasMany(() => LineItem, { + mappedBy: "cart", + }), + shipping_methods: model.hasMany(() => ShippingMethod, { + mappedBy: "cart", + }), }) - shipping_address_id: string | null - - @ManyToOne(() => Address, { - cascade: [Cascade.PERSIST], - nullable: true, + .cascades({ + delete: ["items", "shipping_methods"], }) - shipping_address: Rel
| null - - @BillingAddressIdIndex() - @ManyToOne({ - entity: () => Address, - columnType: "text", - fieldName: "billing_address_id", - mapToPk: true, - nullable: true, - }) - billing_address_id: string | null - - @ManyToOne(() => Address, { - cascade: [Cascade.PERSIST], - nullable: true, - }) - billing_address: Rel
| null - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null = null - - @OneToMany(() => LineItem, (lineItem) => lineItem.cart, { - cascade: [Cascade.PERSIST, "soft-remove"] as any, - }) - items = new Collection>(this) - - @OneToMany(() => ShippingMethod, (shippingMethod) => shippingMethod.cart, { - cascade: [Cascade.PERSIST, "soft-remove"] as any, - }) - shipping_methods = new Collection>(this) - - @Property({ columnType: "timestamptz", nullable: true }) - completed_at: Date | null = null - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - updated_at: Date - - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "cart") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "cart") - } -} + .indexes([ + { + name: "IDX_cart_region_id", + on: ["region_id"], + where: "deleted_at IS NULL AND region_id IS NOT NULL", + }, + { + name: "IDX_cart_customer_id", + on: ["customer_id"], + where: "deleted_at IS NULL AND customer_id IS NOT NULL", + }, + { + name: "IDX_cart_sales_channel_id", + on: ["sales_channel_id"], + where: "deleted_at IS NULL AND sales_channel_id IS NOT NULL", + }, + { + name: "IDX_cart_curency_code", + on: ["currency_code"], + where: "deleted_at IS NULL", + }, + { + name: "IDX_cart_shipping_address_id", + on: ["shipping_address_id"], + where: "deleted_at IS NULL AND shipping_address_id IS NOT NULL", + }, + { + name: "IDX_cart_billing_address_id", + on: ["billing_address_id"], + where: "deleted_at IS NULL AND billing_address_id IS NOT NULL", + }, + ]) + +// @Entity({ tableName: "cart" }) +// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) +// export default class Cart { +// [OptionalProps]?: OptionalCartProps +// @PrimaryKey({ columnType: "text" }) +// id: string +// @RegionIdIndex() +// @Property({ columnType: "text", nullable: true }) +// region_id: string | null = null +// @CustomerIdIndex() +// @Property({ columnType: "text", nullable: true }) +// customer_id: string | null = null +// @SalesChannelIdIndex() +// @Property({ columnType: "text", nullable: true }) +// sales_channel_id: string | null = null +// @Property({ columnType: "text", nullable: true }) +// email: string | null = null +// @CurrencyCodeIndex() +// @Property({ columnType: "text" }) +// currency_code: string +// @ShippingAddressIdIndex() +// @ManyToOne({ +// entity: () => Address, +// columnType: "text", +// fieldName: "shipping_address_id", +// mapToPk: true, +// nullable: true, +// }) +// shipping_address_id: string | null +// @ManyToOne(() => Address, { +// cascade: [Cascade.PERSIST], +// nullable: true, +// }) +// shipping_address: Rel
| null +// @BillingAddressIdIndex() +// @ManyToOne({ +// entity: () => Address, +// columnType: "text", +// fieldName: "billing_address_id", +// mapToPk: true, +// nullable: true, +// }) +// billing_address_id: string | null +// @ManyToOne(() => Address, { +// cascade: [Cascade.PERSIST], +// nullable: true, +// }) +// billing_address: Rel
| null +// @Property({ columnType: "jsonb", nullable: true }) +// metadata: Record | null = null +// @OneToMany(() => LineItem, (lineItem) => lineItem.cart, { +// cascade: [Cascade.PERSIST, "soft-remove"] as any, +// }) +// items = new Collection>(this) +// @OneToMany(() => ShippingMethod, (shippingMethod) => shippingMethod.cart, { +// cascade: [Cascade.PERSIST, "soft-remove"] as any, +// }) +// shipping_methods = new Collection>(this) +// @Property({ columnType: "timestamptz", nullable: true }) +// completed_at: Date | null = null +// @Property({ +// onCreate: () => new Date(), +// columnType: "timestamptz", +// defaultRaw: "now()", +// }) +// created_at: Date +// @Property({ +// onCreate: () => new Date(), +// onUpdate: () => new Date(), +// columnType: "timestamptz", +// defaultRaw: "now()", +// }) +// updated_at: Date +// @DeletedAtIndex() +// @Property({ columnType: "timestamptz", nullable: true }) +// deleted_at: Date | null = null +// @BeforeCreate() +// onCreate() { +// this.id = generateEntityId(this.id, "cart") +// } +// @OnInit() +// onInit() { +// this.id = generateEntityId(this.id, "cart") +// } +// } +export default Cart diff --git a/packages/modules/cart/src/models/line-item-adjustment.ts b/packages/modules/cart/src/models/line-item-adjustment.ts index f0c816d67d51f..3e87c94ebd7ad 100644 --- a/packages/modules/cart/src/models/line-item-adjustment.ts +++ b/packages/modules/cart/src/models/line-item-adjustment.ts @@ -1,74 +1,64 @@ -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Check, - Entity, - Filter, - ManyToOne, - OnInit, - Property, - Rel, -} from "@mikro-orm/core" -import AdjustmentLine from "./adjustment-line" +import { model } from "@medusajs/framework/utils" import LineItem from "./line-item" -const LineItemIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_adjustment_item_id", - tableName: "cart_line_item_adjustment", - columns: "item_id", - where: "deleted_at IS NULL", -}).MikroORMIndex +const LineItemAdjustment = model + .define( + { name: "LineItemAdjustment", tableName: "cart_line_item_adjustment" }, + { + id: model.id({ prefix: "caliadj" }).primaryKey(), + description: model.text().nullable(), + code: model.text().nullable(), + amount: model.bigNumber(), + provider_id: model.text().nullable(), + promotion_id: model.text().nullable(), + metadata: model.json().nullable(), + item: model.belongsTo(() => LineItem, { + mappedBy: "adjustments", + }), + } + ) + .indexes([ + { + name: "IDX_line_item_adjustment_promotion_id", + on: ["promotion_id"], + where: "deleted_at IS NULL AND promotion_id IS NOT NULL", + }, + { + name: "IDX_adjustment_item_id", + on: ["item_id"], + where: "deleted_at IS NULL", + }, + ]) -const PromotionIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_line_item_adjustment_promotion_id", - tableName: "cart_line_item_adjustment", - columns: "promotion_id", - where: "deleted_at IS NULL AND promotion_id IS NOT NULL", -}).MikroORMIndex - -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart_line_item_adjustment", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex - -@Entity({ tableName: "cart_line_item_adjustment" }) -@Check({ - expression: (columns) => `${columns.amount} >= 0`, -}) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class LineItemAdjustment extends AdjustmentLine { - @ManyToOne({ entity: () => LineItem, persist: false }) - item: Rel - - @LineItemIdIndex() - @ManyToOne({ - entity: () => LineItem, - columnType: "text", - fieldName: "item_id", - mapToPk: true, - }) - item_id: string - - @PromotionIdIndex() - @Property({ columnType: "text", nullable: true }) - promotion_id: string | null = null - - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "caliadj") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "caliadj") - } -} +// @Entity({ tableName: "cart_line_item_adjustment" }) +// @Check({ +// expression: (columns) => `${columns.amount} >= 0`, +// }) +// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) +// export default class LineItemAdjustment extends AdjustmentLine { +// @ManyToOne({ entity: () => LineItem, persist: false }) +// item: Rel +// @LineItemIdIndex() +// @ManyToOne({ +// entity: () => LineItem, +// columnType: "text", +// fieldName: "item_id", +// mapToPk: true, +// }) +// item_id: string +// @PromotionIdIndex() +// @Property({ columnType: "text", nullable: true }) +// promotion_id: string | null = null +// @DeletedAtIndex() +// @Property({ columnType: "timestamptz", nullable: true }) +// deleted_at: Date | null = null +// @BeforeCreate() +// onCreate() { +// this.id = generateEntityId(this.id, "caliadj") +// } +// @OnInit() +// onInit() { +// this.id = generateEntityId(this.id, "caliadj") +// } +// } +export default LineItemAdjustment diff --git a/packages/modules/cart/src/models/line-item-tax-line.ts b/packages/modules/cart/src/models/line-item-tax-line.ts index 4a773c07c9495..09ce5153a64da 100644 --- a/packages/modules/cart/src/models/line-item-tax-line.ts +++ b/packages/modules/cart/src/models/line-item-tax-line.ts @@ -1,70 +1,68 @@ -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Entity, - Filter, - ManyToOne, - OnInit, - Property, - Rel, -} from "@mikro-orm/core" +import { model } from "@medusajs/framework/utils" import LineItem from "./line-item" -import TaxLine from "./tax-line" -const LineItemIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_tax_line_item_id", - tableName: "cart_line_item_tax_line", - columns: "item_id", - where: "deleted_at IS NULL", -}).MikroORMIndex +const LineItemTaxLine = model + .define( + { + name: "LineItemTaxLine", + tableName: "cart_line_item_tax_line", + }, + { + id: model.id({ prefix: "calitxl" }).primaryKey(), + description: model.text().nullable(), + code: model.text(), + rate: model.number(), + provider_id: model.text().nullable(), + tax_rate_id: model.text().nullable(), + item: model.belongsTo(() => LineItem, { + mappedBy: "tax_lines", + }), + } + ) + .indexes([ + { + name: "IDX_line_item_tax_line_tax_rate_id", + on: ["tax_rate_id"], + where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL", + }, + { + name: "IDX_tax_line_item_id", + on: ["item_id"], + where: "deleted_at IS NULL", + }, + ]) -const TaxRateIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_line_item_tax_line_tax_rate_id", - tableName: "cart_line_item_tax_line", - columns: "tax_rate_id", - where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL", -}).MikroORMIndex +// @Entity({ tableName: "cart_line_item_tax_line" }) +// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) +// export default class LineItemTaxLine extends TaxLine { +// @ManyToOne({ entity: () => LineItem, persist: false }) +// item: Rel -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart_line_item_tax_line", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex +// @LineItemIdIndex() +// @ManyToOne({ +// entity: () => LineItem, +// columnType: "text", +// fieldName: "item_id", +// mapToPk: true, +// }) +// item_id: string -@Entity({ tableName: "cart_line_item_tax_line" }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class LineItemTaxLine extends TaxLine { - @ManyToOne({ entity: () => LineItem, persist: false }) - item: Rel +// @TaxRateIdIndex() +// @Property({ columnType: "text", nullable: true }) +// tax_rate_id: string | null = null - @LineItemIdIndex() - @ManyToOne({ - entity: () => LineItem, - columnType: "text", - fieldName: "item_id", - mapToPk: true, - }) - item_id: string +// @DeletedAtIndex() +// @Property({ columnType: "timestamptz", nullable: true }) +// deleted_at: Date | null = null - @TaxRateIdIndex() - @Property({ columnType: "text", nullable: true }) - tax_rate_id: string | null = null +// @BeforeCreate() +// onCreate() { +// this.id = generateEntityId(this.id, "calitxl") +// } - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "calitxl") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "calitxl") - } -} +// @OnInit() +// onInit() { +// this.id = generateEntityId(this.id, "calitxl") +// } +// } +export default LineItemTaxLine diff --git a/packages/modules/cart/src/models/line-item.ts b/packages/modules/cart/src/models/line-item.ts index 86d9792a0c391..e0c7e292db93c 100644 --- a/packages/modules/cart/src/models/line-item.ts +++ b/packages/modules/cart/src/models/line-item.ts @@ -1,205 +1,173 @@ -import { BigNumberRawValue, DAL } from "@medusajs/framework/types" -import { - BigNumber, - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, - MikroOrmBigNumberProperty, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Cascade, - Collection, - Entity, - Filter, - ManyToOne, - OneToMany, - OnInit, - OptionalProps, - PrimaryKey, - Property, - Rel, -} from "@mikro-orm/core" +import { model } from "@medusajs/framework/utils" import Cart from "./cart" -import LineItemAdjustment from "./line-item-adjustment" import LineItemTaxLine from "./line-item-tax-line" +import LineItemAdjustment from "./line-item-adjustment" -type OptionalLineItemProps = - | "is_discoutable" - | "is_tax_inclusive" - | "compare_at_unit_price" - | "requires_shipping" - | "cart" - | DAL.SoftDeletableModelDateColumns - -const CartIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_line_item_cart_id", - tableName: "cart_line_item", - columns: "cart_id", - where: "deleted_at IS NULL", -}).MikroORMIndex - -const VariantIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_line_item_variant_id", - tableName: "cart_line_item", - columns: "variant_id", - where: "deleted_at IS NULL AND variant_id IS NOT NULL", -}).MikroORMIndex - -const ProductIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_line_item_product_id", - tableName: "cart_line_item", - columns: "product_id", - where: "deleted_at IS NULL AND product_id IS NOT NULL", -}).MikroORMIndex - -const ProductTypeIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_line_item_product_type_id", - tableName: "cart_line_item", - columns: "product_type_id", - where: "deleted_at IS NULL AND product_type_id IS NOT NULL", -}).MikroORMIndex - -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart_line_item", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex - -@Entity({ tableName: "cart_line_item" }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class LineItem { - [OptionalProps]?: OptionalLineItemProps - - @PrimaryKey({ columnType: "text" }) - id: string - - @CartIdIndex() - @ManyToOne({ - entity: () => Cart, - columnType: "text", - fieldName: "cart_id", - mapToPk: true, - }) - cart_id: string - - @ManyToOne({ entity: () => Cart, persist: false }) - cart: Rel - - @Property({ columnType: "text" }) - title: string - - @Property({ columnType: "text", nullable: true }) - subtitle: string | null = null - - @Property({ columnType: "text", nullable: true }) - thumbnail: string | null = null - - @Property({ columnType: "integer" }) - quantity: number - - @VariantIdIndex() - @Property({ columnType: "text", nullable: true }) - variant_id: string | null = null - - @ProductIdIndex() - @Property({ columnType: "text", nullable: true }) - product_id: string | null = null - - @Property({ columnType: "text", nullable: true }) - product_title: string | null = null - - @Property({ columnType: "text", nullable: true }) - product_description: string | null = null - - @Property({ columnType: "text", nullable: true }) - product_subtitle: string | null = null - - @Property({ columnType: "text", nullable: true }) - product_type: string | null = null - - @ProductTypeIdIndex() - @Property({ columnType: "text", nullable: true }) - product_type_id: string | null = null - - @Property({ columnType: "text", nullable: true }) - product_collection: string | null = null - - @Property({ columnType: "text", nullable: true }) - product_handle: string | null = null - - @Property({ columnType: "text", nullable: true }) - variant_sku: string | null = null - - @Property({ columnType: "text", nullable: true }) - variant_barcode: string | null = null - - @Property({ columnType: "text", nullable: true }) - variant_title: string | null = null - - @Property({ columnType: "jsonb", nullable: true }) - variant_option_values: Record | null = null - - @Property({ columnType: "boolean" }) - requires_shipping: boolean = true - - @Property({ columnType: "boolean" }) - is_discountable: boolean = true - - @Property({ columnType: "boolean" }) - is_tax_inclusive: boolean = false - - @MikroOrmBigNumberProperty({ nullable: true }) - compare_at_unit_price?: BigNumber | number | null = null - - @Property({ columnType: "jsonb", nullable: true }) - raw_compare_at_unit_price: BigNumberRawValue | null = null - - @MikroOrmBigNumberProperty() - unit_price: BigNumber | number - - @Property({ columnType: "jsonb" }) - raw_unit_price: BigNumberRawValue - - @OneToMany(() => LineItemTaxLine, (taxLine) => taxLine.item, { - cascade: [Cascade.PERSIST, "soft-remove"] as any, - }) - tax_lines = new Collection>(this) - - @OneToMany(() => LineItemAdjustment, (adjustment) => adjustment.item, { - cascade: [Cascade.PERSIST, "soft-remove"] as any, - }) - adjustments = new Collection>(this) - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null = null - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", +const LineItem = model + .define( + { name: "LineItem", tableName: "cart_line_item" }, + { + id: model.id({ prefix: "cali" }).primaryKey(), + title: model.text(), + subtitle: model.text().nullable(), + thumbnail: model.text().nullable(), + quantity: model.number(), + variant_id: model.text().nullable(), + product_id: model.text().nullable(), + product_title: model.text().nullable(), + product_description: model.text().nullable(), + product_subtitle: model.text().nullable(), + product_type: model.text().nullable(), + product_type_id: model.text().nullable(), + product_collection: model.text().nullable(), + variant_sku: model.text().nullable(), + variant_barcode: model.text().nullable(), + variant_title: model.text().nullable(), + variant_option_values: model.json().nullable(), + requires_shipping: model.boolean().default(true), + is_discountable: model.boolean().default(true), + is_tax_inclusive: model.boolean().default(false), + compare_at_unit_price: model.bigNumber().nullable(), + unit_price: model.bigNumber(), + metadata: model.json().nullable(), + adjustments: model.hasMany(() => LineItemAdjustment, { + mappedBy: "item", + }), + tax_lines: model.hasMany(() => LineItemTaxLine, { + mappedBy: "item", + }), + cart: model.belongsTo(() => Cart, { + mappedBy: "items", + }), + } + ) + .indexes([ + { + name: "IDX_line_item_cart_id", + on: ["cart_id"], + where: "deleted_at IS NULL", + }, + { + name: "IDX_line_item_variant_id", + on: ["variant_id"], + where: "deleted_at IS NULL AND variant_id IS NOT NULL", + }, + { + name: "IDX_line_item_product_id", + on: ["product_id"], + where: "deleted_at IS NULL AND product_id IS NOT NULL", + }, + { + name: "IDX_line_item_product_type_id", + on: ["product_type_id"], + where: "deleted_at IS NULL AND product_type_id IS NOT NULL", + }, + ]) + .cascades({ + delete: ["adjustments", "tax_lines"], }) - updated_at: Date - - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "cali") - } - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "cali") - } -} +// @Entity({ tableName: "cart_line_item" }) +// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) +// export default class LineItem { +// [OptionalProps]?: OptionalLineItemProps +// @PrimaryKey({ columnType: "text" }) +// id: string +// @CartIdIndex() +// @ManyToOne({ +// entity: () => Cart, +// columnType: "text", +// fieldName: "cart_id", +// mapToPk: true, +// }) +// cart_id: string +// @ManyToOne({ entity: () => Cart, persist: false }) +// cart: Rel +// @Property({ columnType: "text" }) +// title: string +// @Property({ columnType: "text", nullable: true }) +// subtitle: string | null = null +// @Property({ columnType: "text", nullable: true }) +// thumbnail: string | null = null +// @Property({ columnType: "integer" }) +// quantity: number +// @VariantIdIndex() +// @Property({ columnType: "text", nullable: true }) +// variant_id: string | null = null +// @ProductIdIndex() +// @Property({ columnType: "text", nullable: true }) +// product_id: string | null = null +// @Property({ columnType: "text", nullable: true }) +// product_title: string | null = null +// @Property({ columnType: "text", nullable: true }) +// product_description: string | null = null +// @Property({ columnType: "text", nullable: true }) +// product_subtitle: string | null = null +// @Property({ columnType: "text", nullable: true }) +// product_type: string | null = null +// @ProductTypeIdIndex() +// @Property({ columnType: "text", nullable: true }) +// product_type_id: string | null = null +// @Property({ columnType: "text", nullable: true }) +// product_collection: string | null = null +// @Property({ columnType: "text", nullable: true }) +// product_handle: string | null = null +// @Property({ columnType: "text", nullable: true }) +// variant_sku: string | null = null +// @Property({ columnType: "text", nullable: true }) +// variant_barcode: string | null = null +// @Property({ columnType: "text", nullable: true }) +// variant_title: string | null = null +// @Property({ columnType: "jsonb", nullable: true }) +// variant_option_values: Record | null = null +// @Property({ columnType: "boolean" }) +// requires_shipping: boolean = true +// @Property({ columnType: "boolean" }) +// is_discountable: boolean = true +// @Property({ columnType: "boolean" }) +// is_tax_inclusive: boolean = false +// @MikroOrmBigNumberProperty({ nullable: true }) +// compare_at_unit_price?: BigNumber | number | null = null +// @Property({ columnType: "jsonb", nullable: true }) +// raw_compare_at_unit_price: BigNumberRawValue | null = null +// @MikroOrmBigNumberProperty() +// unit_price: BigNumber | number +// @Property({ columnType: "jsonb" }) +// raw_unit_price: BigNumberRawValue +// @OneToMany(() => LineItemTaxLine, (taxLine) => taxLine.item, { +// cascade: [Cascade.PERSIST, "soft-remove"] as any, +// }) +// tax_lines = new Collection>(this) +// @OneToMany(() => LineItemAdjustment, (adjustment) => adjustment.item, { +// cascade: [Cascade.PERSIST, "soft-remove"] as any, +// }) +// adjustments = new Collection>(this) +// @Property({ columnType: "jsonb", nullable: true }) +// metadata: Record | null = null +// @Property({ +// onCreate: () => new Date(), +// columnType: "timestamptz", +// defaultRaw: "now()", +// }) +// created_at: Date +// @Property({ +// onCreate: () => new Date(), +// onUpdate: () => new Date(), +// columnType: "timestamptz", +// defaultRaw: "now()", +// }) +// updated_at: Date +// @DeletedAtIndex() +// @Property({ columnType: "timestamptz", nullable: true }) +// deleted_at: Date | null = null +// @BeforeCreate() +// onCreate() { +// this.id = generateEntityId(this.id, "cali") +// } +// @OnInit() +// onInit() { +// this.id = generateEntityId(this.id, "cali") +// } +// } + +export default LineItem diff --git a/packages/modules/cart/src/models/shipping-method-adjustment.ts b/packages/modules/cart/src/models/shipping-method-adjustment.ts index 8e9702741d8e5..a608c314c8585 100644 --- a/packages/modules/cart/src/models/shipping-method-adjustment.ts +++ b/packages/modules/cart/src/models/shipping-method-adjustment.ts @@ -1,70 +1,69 @@ -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Entity, - Filter, - ManyToOne, - OnInit, - Property, - Rel, -} from "@mikro-orm/core" -import AdjustmentLine from "./adjustment-line" +import { model } from "@medusajs/framework/utils" import ShippingMethod from "./shipping-method" -const ShippingMethodIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_adjustment_shipping_method_id", - tableName: "cart_shipping_method_adjustment", - columns: "shipping_method_id", - where: "deleted_at IS NULL", -}).MikroORMIndex +const ShippingMethodAdjustment = model + .define( + { + name: "ShippingMethodAdjustment", + tableName: "cart_shipping_method_adjustment", + }, + { + id: model.id({ prefix: "casmadj" }).primaryKey(), + description: model.text().nullable(), + code: model.text().nullable(), + amount: model.bigNumber(), + provider_id: model.text().nullable(), + metadata: model.json().nullable(), + promotion_id: model.text().nullable(), + shipping_method: model.belongsTo(() => ShippingMethod, { + mappedBy: "adjustments", + }), + } + ) + .indexes([ + { + name: "IDX_shipping_method_adjustment_promotion_id", + on: ["promotion_id"], + where: "deleted_at IS NULL AND promotion_id IS NOT NULL", + }, + { + name: "IDX_adjustment_shipping_method_id", + on: ["shipping_method_id"], + where: "deleted_at IS NULL", + }, + ]) -const PromotionIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_shipping_method_adjustment_promotion_id", - tableName: "cart_shipping_method_adjustment", - columns: "promotion_id", - where: "deleted_at IS NULL AND promotion_id IS NOT NULL", -}).MikroORMIndex +// @Entity({ tableName: "cart_shipping_method_adjustment" }) +// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) +// export default class ShippingMethodAdjustment extends AdjustmentLine { +// @ManyToOne({ entity: () => ShippingMethod, persist: false }) +// shipping_method: Rel -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart_shipping_method_adjustment", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex +// @ShippingMethodIdIndex() +// @ManyToOne({ +// entity: () => ShippingMethod, +// columnType: "text", +// fieldName: "shipping_method_id", +// mapToPk: true, +// }) +// shipping_method_id: string -@Entity({ tableName: "cart_shipping_method_adjustment" }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class ShippingMethodAdjustment extends AdjustmentLine { - @ManyToOne({ entity: () => ShippingMethod, persist: false }) - shipping_method: Rel +// @PromotionIdIndex() +// @Property({ columnType: "text", nullable: true }) +// promotion_id: string | null = null - @ShippingMethodIdIndex() - @ManyToOne({ - entity: () => ShippingMethod, - columnType: "text", - fieldName: "shipping_method_id", - mapToPk: true, - }) - shipping_method_id: string +// @DeletedAtIndex() +// @Property({ columnType: "timestamptz", nullable: true }) +// deleted_at: Date | null = null - @PromotionIdIndex() - @Property({ columnType: "text", nullable: true }) - promotion_id: string | null = null +// @BeforeCreate() +// onCreate() { +// this.id = generateEntityId(this.id, "casmadj") +// } - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "casmadj") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "casmadj") - } -} +// @OnInit() +// onInit() { +// this.id = generateEntityId(this.id, "casmadj") +// } +// } +export default ShippingMethodAdjustment diff --git a/packages/modules/cart/src/models/shipping-method-tax-line.ts b/packages/modules/cart/src/models/shipping-method-tax-line.ts index 697dd156b707f..da141494743fa 100644 --- a/packages/modules/cart/src/models/shipping-method-tax-line.ts +++ b/packages/modules/cart/src/models/shipping-method-tax-line.ts @@ -1,70 +1,69 @@ -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Entity, - Filter, - ManyToOne, - OnInit, - Property, - Rel, -} from "@mikro-orm/core" +import { model } from "@medusajs/framework/utils" import ShippingMethod from "./shipping-method" -import TaxLine from "./tax-line" -const ShippingMethodIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_tax_line_shipping_method_id", - tableName: "cart_shipping_method_tax_line", - columns: "shipping_method_id", - where: "deleted_at IS NULL", -}).MikroORMIndex +const ShippingMethodTaxLine = model + .define( + { + name: "ShippingMethodTaxLine", + tableName: "cart_shipping_method_tax_line", + }, + { + id: model.id({ prefix: "casmtxl" }).primaryKey(), + description: model.text().nullable(), + code: model.text(), + rate: model.number(), + provider_id: model.text().nullable(), + tax_rate_id: model.text().nullable(), + metadata: model.json().nullable(), + shipping_method: model.belongsTo(() => ShippingMethod, { + mappedBy: "tax_lines", + }), + } + ) + .indexes([ + { + name: "IDX_tax_line_shipping_method_id", + on: ["shipping_method_id"], + where: "deleted_at IS NULL", + }, + { + name: "IDX_shipping_method_tax_line_tax_rate_id", + on: ["tax_rate_id"], + where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL", + }, + ]) -const TaxRateIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_shipping_method_tax_line_tax_rate_id", - tableName: "cart_shipping_method_tax_line", - columns: "tax_rate_id", - where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL", -}).MikroORMIndex +// @Entity({ tableName: "cart_shipping_method_tax_line" }) +// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) +// export default class ShippingMethodTaxLine extends TaxLine { +// @ManyToOne({ entity: () => ShippingMethod, persist: false }) +// shipping_method: Rel -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart_shipping_method_tax_line", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex +// @ShippingMethodIdIndex() +// @ManyToOne({ +// entity: () => ShippingMethod, +// columnType: "text", +// fieldName: "shipping_method_id", +// mapToPk: true, +// }) +// shipping_method_id: string -@Entity({ tableName: "cart_shipping_method_tax_line" }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class ShippingMethodTaxLine extends TaxLine { - @ManyToOne({ entity: () => ShippingMethod, persist: false }) - shipping_method: Rel +// @TaxRateIdIndex() +// @Property({ columnType: "text", nullable: true }) +// tax_rate_id: string | null = null - @ShippingMethodIdIndex() - @ManyToOne({ - entity: () => ShippingMethod, - columnType: "text", - fieldName: "shipping_method_id", - mapToPk: true, - }) - shipping_method_id: string +// @DeletedAtIndex() +// @Property({ columnType: "timestamptz", nullable: true }) +// deleted_at: Date | null = null - @TaxRateIdIndex() - @Property({ columnType: "text", nullable: true }) - tax_rate_id: string | null = null +// @BeforeCreate() +// onCreate() { +// this.id = generateEntityId(this.id, "casmtxl") +// } - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "casmtxl") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "casmtxl") - } -} +// @OnInit() +// onInit() { +// this.id = generateEntityId(this.id, "casmtxl") +// } +// } +export default ShippingMethodTaxLine diff --git a/packages/modules/cart/src/models/shipping-method.ts b/packages/modules/cart/src/models/shipping-method.ts index 6da5721656166..b0d88a80b392e 100644 --- a/packages/modules/cart/src/models/shipping-method.ts +++ b/packages/modules/cart/src/models/shipping-method.ts @@ -1,145 +1,138 @@ -import { BigNumberRawValue, DAL } from "@medusajs/framework/types" -import { - BigNumber, - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, - MikroOrmBigNumberProperty, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Cascade, - Check, - Collection, - Entity, - Filter, - ManyToOne, - OneToMany, - OnInit, - OptionalProps, - PrimaryKey, - Property, - Rel, -} from "@mikro-orm/core" +import { model } from "@medusajs/framework/utils" import Cart from "./cart" import ShippingMethodAdjustment from "./shipping-method-adjustment" import ShippingMethodTaxLine from "./shipping-method-tax-line" -type OptionalShippingMethodProps = - | "cart" - | "is_tax_inclusive" - | DAL.SoftDeletableModelDateColumns - -const CartIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_shipping_method_cart_id", - tableName: "cart_shipping_method", - columns: "cart_id", - where: "deleted_at IS NULL", -}).MikroORMIndex - -const ShippingOptionIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_shipping_method_option_id", - tableName: "cart_shipping_method", - columns: "shipping_option_id", - where: "deleted_at IS NULL AND shipping_option_id IS NOT NULL", -}).MikroORMIndex - -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart_shipping_method", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex - -@Entity({ tableName: "cart_shipping_method" }) -@Check({ expression: (columns) => `${columns.amount} >= 0` }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class ShippingMethod { - [OptionalProps]?: OptionalShippingMethodProps - - @PrimaryKey({ columnType: "text" }) - id: string - - @CartIdIndex() - @ManyToOne({ - entity: () => Cart, - columnType: "text", - fieldName: "cart_id", - mapToPk: true, - }) - cart_id: string - - @ManyToOne({ entity: () => Cart, persist: false }) - cart: Rel - - @Property({ columnType: "text" }) - name: string - - @Property({ columnType: "jsonb", nullable: true }) - description: string | null = null - - @MikroOrmBigNumberProperty() - amount: BigNumber | number - - @Property({ columnType: "jsonb" }) - raw_amount: BigNumberRawValue - - @Property({ columnType: "boolean" }) - is_tax_inclusive: boolean = false - - @ShippingOptionIdIndex() - @Property({ columnType: "text", nullable: true }) - shipping_option_id: string | null = null - - @Property({ columnType: "jsonb", nullable: true }) - data: Record | null = null - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null = null - - @OneToMany( - () => ShippingMethodTaxLine, - (taxLine) => taxLine.shipping_method, +const ShippingMethod = model + .define( { - cascade: [Cascade.PERSIST, "soft-remove"] as any, - } - ) - tax_lines = new Collection>(this) - - @OneToMany( - () => ShippingMethodAdjustment, - (adjustment) => adjustment.shipping_method, + name: "ShippingMethod", + tableName: "cart_shipping_method", + }, { - cascade: [Cascade.PERSIST, "soft-remove"] as any, + id: model.id({ prefix: "casm" }).primaryKey(), + name: model.text(), + description: model.json().nullable(), + amount: model.bigNumber(), + is_tax_inclusive: model.boolean().default(false), + shipping_option_id: model.text().nullable(), + data: model.json().nullable(), + metadata: model.json().nullable(), + cart: model.belongsTo(() => Cart, { + mappedBy: "shipping_methods", + }), + tax_lines: model.hasMany(() => ShippingMethodTaxLine, { + mappedBy: "shipping_method", + }), + adjustments: model.hasMany(() => ShippingMethodAdjustment, { + mappedBy: "shipping_method", + }), } ) - adjustments = new Collection>(this) - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - updated_at: Date - - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "casm") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "casm") - } -} + .indexes([ + { + name: "IDX_shipping_method_cart_id", + on: ["cart_id"], + where: "deleted_at IS NULL", + }, + { + name: "IDX_shipping_method_option_id", + on: ["shipping_option_id"], + where: "deleted_at IS NULL AND shipping_option_id IS NOT NULL", + }, + ]) + +// @Entity({ tableName: "cart_shipping_method" }) +// @Check({ expression: (columns) => `${columns.amount} >= 0` }) +// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) +// export default class ShippingMethod { +// [OptionalProps]?: OptionalShippingMethodProps + +// @PrimaryKey({ columnType: "text" }) +// id: string + +// @CartIdIndex() +// @ManyToOne({ +// entity: () => Cart, +// columnType: "text", +// fieldName: "cart_id", +// mapToPk: true, +// }) +// cart_id: string + +// @ManyToOne({ entity: () => Cart, persist: false }) +// cart: Rel + +// @Property({ columnType: "text" }) +// name: string + +// @Property({ columnType: "jsonb", nullable: true }) +// description: string | null = null + +// @MikroOrmBigNumberProperty() +// amount: BigNumber | number + +// @Property({ columnType: "jsonb" }) +// raw_amount: BigNumberRawValue + +// @Property({ columnType: "boolean" }) +// is_tax_inclusive: boolean = false + +// @ShippingOptionIdIndex() +// @Property({ columnType: "text", nullable: true }) +// shipping_option_id: string | null = null + +// @Property({ columnType: "jsonb", nullable: true }) +// data: Record | null = null + +// @Property({ columnType: "jsonb", nullable: true }) +// metadata: Record | null = null + +// @OneToMany( +// () => ShippingMethodTaxLine, +// (taxLine) => taxLine.shipping_method, +// { +// cascade: [Cascade.PERSIST, "soft-remove"] as any, +// } +// ) +// tax_lines = new Collection>(this) + +// @OneToMany( +// () => ShippingMethodAdjustment, +// (adjustment) => adjustment.shipping_method, +// { +// cascade: [Cascade.PERSIST, "soft-remove"] as any, +// } +// ) +// adjustments = new Collection>(this) + +// @Property({ +// onCreate: () => new Date(), +// columnType: "timestamptz", +// defaultRaw: "now()", +// }) +// created_at: Date + +// @Property({ +// onCreate: () => new Date(), +// onUpdate: () => new Date(), +// columnType: "timestamptz", +// defaultRaw: "now()", +// }) +// updated_at: Date + +// @DeletedAtIndex() +// @Property({ columnType: "timestamptz", nullable: true }) +// deleted_at: Date | null = null + +// @BeforeCreate() +// onCreate() { +// this.id = generateEntityId(this.id, "casm") +// } + +// @OnInit() +// onInit() { +// this.id = generateEntityId(this.id, "casm") +// } +// } +export default ShippingMethod diff --git a/packages/modules/cart/src/models/tax-line.ts b/packages/modules/cart/src/models/tax-line.ts index b99ab74cb88ab..bc9d4e8477649 100644 --- a/packages/modules/cart/src/models/tax-line.ts +++ b/packages/modules/cart/src/models/tax-line.ts @@ -1,45 +1,54 @@ -import { DAL } from "@medusajs/framework/types" -import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core" - -type OptionalTaxLineProps = DAL.SoftDeletableModelDateColumns +// import { model } from "@medusajs/framework/utils" + +// const TaxLine = model.define( +// { name: "TaxLine" }, +// { +// id: model.id().primaryKey(), +// description: model.text().nullable(), +// code: model.text(), +// rate: model.number(), +// provider_id: model.text().nullable(), +// metadata: model.json().nullable(), +// } +// ) /** * As per the Mikro ORM docs, superclasses should use the abstract class definition * Source: https://mikro-orm.io/docs/inheritance-mapping */ -export default abstract class TaxLine { - [OptionalProps]?: OptionalTaxLineProps - - @PrimaryKey({ columnType: "text" }) - id: string - - @Property({ columnType: "text", nullable: true }) - description?: string | null - - @Property({ columnType: "text" }) - code: string - - @Property({ columnType: "numeric", serializer: Number }) - rate: number - - @Property({ columnType: "text", nullable: true }) - provider_id?: string | null - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null = null - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - updated_at: Date -} +// export default abstract class TaxLine { +// [OptionalProps]?: OptionalTaxLineProps + +// @PrimaryKey({ columnType: "text" }) +// id: string + +// @Property({ columnType: "text", nullable: true }) +// description?: string | null + +// @Property({ columnType: "text" }) +// code: string + +// @Property({ columnType: "numeric", serializer: Number }) +// rate: number + +// @Property({ columnType: "text", nullable: true }) +// provider_id?: string | null + +// @Property({ columnType: "jsonb", nullable: true }) +// metadata: Record | null = null + +// @Property({ +// onCreate: () => new Date(), +// columnType: "timestamptz", +// defaultRaw: "now()", +// }) +// created_at: Date + +// @Property({ +// onCreate: () => new Date(), +// onUpdate: () => new Date(), +// columnType: "timestamptz", +// defaultRaw: "now()", +// }) +// updated_at: Date +// } diff --git a/packages/modules/cart/src/services/cart-module.ts b/packages/modules/cart/src/services/cart-module.ts index d727707d77cb8..25f155e0e87c7 100644 --- a/packages/modules/cart/src/services/cart-module.ts +++ b/packages/modules/cart/src/services/cart-module.ts @@ -5,6 +5,7 @@ import { DAL, FindConfig, ICartModuleService, + InferEntityType, InternalModuleDeclaration, ModulesSdkTypes, } from "@medusajs/framework/types" @@ -76,14 +77,30 @@ export default class CartModuleService implements ICartModuleService { protected baseRepository_: DAL.RepositoryService - protected cartService_: ModulesSdkTypes.IMedusaInternalService - protected addressService_: ModulesSdkTypes.IMedusaInternalService
- protected lineItemService_: ModulesSdkTypes.IMedusaInternalService - protected shippingMethodAdjustmentService_: ModulesSdkTypes.IMedusaInternalService - protected shippingMethodService_: ModulesSdkTypes.IMedusaInternalService - protected lineItemAdjustmentService_: ModulesSdkTypes.IMedusaInternalService - protected lineItemTaxLineService_: ModulesSdkTypes.IMedusaInternalService - protected shippingMethodTaxLineService_: ModulesSdkTypes.IMedusaInternalService + protected cartService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected addressService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected lineItemService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected shippingMethodAdjustmentService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected shippingMethodService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected lineItemAdjustmentService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected lineItemTaxLineService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected shippingMethodTaxLineService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > constructor( { @@ -274,7 +291,7 @@ export default class CartModuleService @MedusaContext() sharedContext: Context = {} ) { const lineItemsToCreate: CreateLineItemDTO[] = [] - const createdCarts: Cart[] = [] + const createdCarts: InferEntityType[] = [] for (const { items, ...cart } of data) { const [created] = await this.cartService_.create([cart], sharedContext) @@ -397,7 +414,7 @@ export default class CartModuleService data?: CartTypes.CreateLineItemDTO[] | CartTypes.CreateLineItemDTO, @MedusaContext() sharedContext: Context = {} ): Promise { - let items: LineItem[] = [] + let items: InferEntityType[] = [] if (isString(cartIdOrData)) { items = await this.addLineItems_( cartIdOrData, @@ -422,7 +439,7 @@ export default class CartModuleService cartId: string, items: CartTypes.CreateLineItemDTO[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise[]> { const cart = await this.retrieveCart( cartId, { select: ["id"] }, @@ -443,7 +460,7 @@ export default class CartModuleService protected async addLineItemsBulk_( data: CreateLineItemDTO[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise[]> { return await this.lineItemService_.create(data, sharedContext) } @@ -471,7 +488,7 @@ export default class CartModuleService data?: CartTypes.UpdateLineItemDTO | Partial, @MedusaContext() sharedContext: Context = {} ): Promise { - let items: LineItem[] = [] + let items: InferEntityType[] = [] if (isString(lineItemIdOrDataOrSelector)) { const item = await this.updateLineItem_( lineItemIdOrDataOrSelector, @@ -511,7 +528,7 @@ export default class CartModuleService lineItemId: string, data: Partial, @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise> { const [item] = await this.lineItemService_.update( [{ id: lineItemId, ...data }], sharedContext @@ -524,7 +541,7 @@ export default class CartModuleService protected async updateLineItemsWithSelector_( updates: CartTypes.UpdateLineItemWithSelectorDTO[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise[]> { let toUpdate: UpdateLineItemDTO[] = [] for (const { selector, data } of updates) { const items = await this.listLineItems({ ...selector }, {}, sharedContext) @@ -639,7 +656,7 @@ export default class CartModuleService ): Promise< CartTypes.CartShippingMethodDTO[] | CartTypes.CartShippingMethodDTO > { - let methods: ShippingMethod[] + let methods: InferEntityType[] if (isString(cartIdOrData)) { methods = await this.addShippingMethods_( cartIdOrData, @@ -664,7 +681,7 @@ export default class CartModuleService cartId: string, data: CartTypes.CreateShippingMethodForSingleCartDTO[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise[]> { const cart = await this.retrieveCart( cartId, { select: ["id"] }, @@ -685,7 +702,7 @@ export default class CartModuleService protected async addShippingMethodsBulk_( data: CartTypes.CreateShippingMethodDTO[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise[]> { return await this.shippingMethodService_.create( data as unknown as CreateShippingMethodDTO[], sharedContext @@ -713,7 +730,7 @@ export default class CartModuleService adjustments?: CartTypes.CreateLineItemAdjustmentDTO[], @MedusaContext() sharedContext: Context = {} ): Promise { - let addedAdjustments: LineItemAdjustment[] = [] + let addedAdjustments: InferEntityType[] = [] if (isString(cartIdOrData)) { const cart = await this.retrieveCart( cartIdOrData, @@ -888,7 +905,8 @@ export default class CartModuleService | CartTypes.ShippingMethodAdjustmentDTO[] | CartTypes.ShippingMethodAdjustmentDTO > { - let addedAdjustments: ShippingMethodAdjustment[] = [] + let addedAdjustments: InferEntityType[] = + [] if (isString(cartIdOrData)) { const cart = await this.retrieveCart( cartIdOrData, @@ -961,7 +979,7 @@ export default class CartModuleService | CartTypes.CreateLineItemTaxLineDTO, @MedusaContext() sharedContext: Context = {} ): Promise { - let addedTaxLines: LineItemTaxLine[] + let addedTaxLines: InferEntityType[] if (isString(cartIdOrData)) { // existence check await this.retrieveCart(cartIdOrData, { select: ["id"] }, sharedContext) @@ -1077,7 +1095,7 @@ export default class CartModuleService ): Promise< CartTypes.ShippingMethodTaxLineDTO[] | CartTypes.ShippingMethodTaxLineDTO > { - let addedTaxLines: ShippingMethodTaxLine[] + let addedTaxLines: InferEntityType[] if (isString(cartIdOrData)) { // existence check await this.retrieveCart(cartIdOrData, { select: ["id"] }, sharedContext) From 99c384a3cd52000c5246aceb242e2c14f0eff052 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Mon, 2 Dec 2024 14:55:39 +0530 Subject: [PATCH 02/12] fix: add missing product_handle column --- .../cart/src/migrations/.snapshot-medusa-cart.json | 9 +++++++++ ...ation20241202080921.ts => Migration20241202092453.ts} | 8 +------- packages/modules/cart/src/models/line-item.ts | 1 + 3 files changed, 11 insertions(+), 7 deletions(-) rename packages/modules/cart/src/migrations/{Migration20241202080921.ts => Migration20241202092453.ts} (96%) diff --git a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json index 7f91d43aa0df6..ba11404addc34 100644 --- a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json +++ b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json @@ -540,6 +540,15 @@ "nullable": true, "mappedType": "text" }, + "product_handle": { + "name": "product_handle", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, "variant_sku": { "name": "variant_sku", "type": "text", diff --git a/packages/modules/cart/src/migrations/Migration20241202080921.ts b/packages/modules/cart/src/migrations/Migration20241202092453.ts similarity index 96% rename from packages/modules/cart/src/migrations/Migration20241202080921.ts rename to packages/modules/cart/src/migrations/Migration20241202092453.ts index 27ba5ef3508b0..ee600a0eb0949 100644 --- a/packages/modules/cart/src/migrations/Migration20241202080921.ts +++ b/packages/modules/cart/src/migrations/Migration20241202092453.ts @@ -1,6 +1,6 @@ import { Migration } from "@mikro-orm/migrations" -export class Migration20241202080921 extends Migration { +export class Migration20241202092453 extends Migration { async up(): Promise { this.addSql( 'alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";' @@ -36,9 +36,6 @@ export class Migration20241202080921 extends Migration { this.addSql( 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" set default false;' ) - this.addSql( - 'alter table if exists "cart_line_item" drop column if exists "product_handle";' - ) this.addSql( 'alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' ) @@ -108,9 +105,6 @@ export class Migration20241202080921 extends Migration { 'alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";' ) - this.addSql( - 'alter table if exists "cart_line_item" add column if not exists "product_handle" text null;' - ) this.addSql( 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" drop default;' ) diff --git a/packages/modules/cart/src/models/line-item.ts b/packages/modules/cart/src/models/line-item.ts index e0c7e292db93c..08f6ada67ed85 100644 --- a/packages/modules/cart/src/models/line-item.ts +++ b/packages/modules/cart/src/models/line-item.ts @@ -20,6 +20,7 @@ const LineItem = model product_type: model.text().nullable(), product_type_id: model.text().nullable(), product_collection: model.text().nullable(), + product_handle: model.text().nullable(), variant_sku: model.text().nullable(), variant_barcode: model.text().nullable(), variant_title: model.text().nullable(), From c3ab06f24e2401ea58956f85f2b4b21ada7bc350 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Mon, 2 Dec 2024 15:36:12 +0530 Subject: [PATCH 03/12] refactor: remove dead code --- packages/modules/cart/src/models/address.ts | 74 ------------- .../cart/src/models/adjustment-line.ts | 46 -------- packages/modules/cart/src/models/cart.ts | 85 --------------- .../cart/src/models/line-item-adjustment.ts | 31 ------ .../cart/src/models/line-item-tax-line.ts | 33 ------ packages/modules/cart/src/models/line-item.ts | 103 ------------------ .../src/models/shipping-method-adjustment.ts | 33 ------ .../src/models/shipping-method-tax-line.ts | 33 ------ .../cart/src/models/shipping-method.ts | 93 ---------------- packages/modules/cart/src/models/tax-line.ts | 54 --------- 10 files changed, 585 deletions(-) delete mode 100644 packages/modules/cart/src/models/adjustment-line.ts delete mode 100644 packages/modules/cart/src/models/tax-line.ts diff --git a/packages/modules/cart/src/models/address.ts b/packages/modules/cart/src/models/address.ts index 0c02cd8dddcf6..9db78f0bcfced 100644 --- a/packages/modules/cart/src/models/address.ts +++ b/packages/modules/cart/src/models/address.ts @@ -23,78 +23,4 @@ const Address = model.define( } ) -// @Entity({ tableName: "cart_address" }) -// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -// export default class Address { -// [OptionalProps]: OptionalAddressProps - -// @PrimaryKey({ columnType: "text" }) -// id!: string - -// @Property({ columnType: "text", nullable: true }) -// customer_id: string | null = null - -// @Property({ columnType: "text", nullable: true }) -// company: string | null = null - -// @Property({ columnType: "text", nullable: true }) -// first_name: string | null = null - -// @Property({ columnType: "text", nullable: true }) -// last_name: string | null = null - -// @Property({ columnType: "text", nullable: true }) -// address_1: string | null = null - -// @Property({ columnType: "text", nullable: true }) -// address_2: string | null = null - -// @Property({ columnType: "text", nullable: true }) -// city: string | null = null - -// @Property({ columnType: "text", nullable: true }) -// country_code: string | null = null - -// @Property({ columnType: "text", nullable: true }) -// province: string | null = null - -// @Property({ columnType: "text", nullable: true }) -// postal_code: string | null = null - -// @Property({ columnType: "text", nullable: true }) -// phone: string | null = null - -// @Property({ columnType: "jsonb", nullable: true }) -// metadata: Record | null = null - -// @Property({ -// onCreate: () => new Date(), -// columnType: "timestamptz", -// defaultRaw: "now()", -// }) -// created_at: Date - -// @Property({ -// onCreate: () => new Date(), -// onUpdate: () => new Date(), -// columnType: "timestamptz", -// defaultRaw: "now()", -// }) -// updated_at: Date - -// @PgIndex.MikroORMIndex() -// @Property({ columnType: "timestamptz", nullable: true }) -// deleted_at: Date | null = null - -// @BeforeCreate() -// onCreate() { -// this.id = generateEntityId(this.id, "caaddr") -// } - -// @OnInit() -// onInit() { -// this.id = generateEntityId(this.id, "caaddr") -// } -// } - export default Address diff --git a/packages/modules/cart/src/models/adjustment-line.ts b/packages/modules/cart/src/models/adjustment-line.ts deleted file mode 100644 index 60d312fb62851..0000000000000 --- a/packages/modules/cart/src/models/adjustment-line.ts +++ /dev/null @@ -1,46 +0,0 @@ -// import { model } from "@medusajs/framework/utils" - -// const AdjustmentLine = model.define("AdjustmentLine", { -// id: model.id().primaryKey(), -// description: model.text().nullable(), -// code: model.text().nullable(), -// amount: model.bigNumber(), -// provider_id: model.text().nullable(), -// metadata: model.json().nullable(), -// }) - -/** - * As per the Mikro ORM docs, superclasses should use the abstract class definition - * Source: https://mikro-orm.io/docs/inheritance-mapping - */ -// export default abstract class AdjustmentLine { -// [OptionalProps]: OptionalAdjustmentLineProps -// @PrimaryKey({ columnType: "text" }) -// id: string -// @Property({ columnType: "text", nullable: true }) -// description: string | null = null -// @Property({ columnType: "text", nullable: true }) -// code: string | null = null -// @MikroOrmBigNumberProperty() -// amount: BigNumber | number -// @Property({ columnType: "jsonb" }) -// raw_amount: Record -// @Property({ columnType: "text", nullable: true }) -// provider_id: string | null = null -// @Property({ columnType: "jsonb", nullable: true }) -// metadata: Record | null = null -// @Property({ -// onCreate: () => new Date(), -// columnType: "timestamptz", -// defaultRaw: "now()", -// }) -// created_at: Date -// @Property({ -// onCreate: () => new Date(), -// onUpdate: () => new Date(), -// columnType: "timestamptz", -// defaultRaw: "now()", -// }) -// updated_at: Date -// } -// export default AdjustmentLine diff --git a/packages/modules/cart/src/models/cart.ts b/packages/modules/cart/src/models/cart.ts index 5a43588b5cd55..ccea5ea9bb8f9 100644 --- a/packages/modules/cart/src/models/cart.ts +++ b/packages/modules/cart/src/models/cart.ts @@ -66,89 +66,4 @@ const Cart = model }, ]) -// @Entity({ tableName: "cart" }) -// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -// export default class Cart { -// [OptionalProps]?: OptionalCartProps -// @PrimaryKey({ columnType: "text" }) -// id: string -// @RegionIdIndex() -// @Property({ columnType: "text", nullable: true }) -// region_id: string | null = null -// @CustomerIdIndex() -// @Property({ columnType: "text", nullable: true }) -// customer_id: string | null = null -// @SalesChannelIdIndex() -// @Property({ columnType: "text", nullable: true }) -// sales_channel_id: string | null = null -// @Property({ columnType: "text", nullable: true }) -// email: string | null = null -// @CurrencyCodeIndex() -// @Property({ columnType: "text" }) -// currency_code: string -// @ShippingAddressIdIndex() -// @ManyToOne({ -// entity: () => Address, -// columnType: "text", -// fieldName: "shipping_address_id", -// mapToPk: true, -// nullable: true, -// }) -// shipping_address_id: string | null -// @ManyToOne(() => Address, { -// cascade: [Cascade.PERSIST], -// nullable: true, -// }) -// shipping_address: Rel
| null -// @BillingAddressIdIndex() -// @ManyToOne({ -// entity: () => Address, -// columnType: "text", -// fieldName: "billing_address_id", -// mapToPk: true, -// nullable: true, -// }) -// billing_address_id: string | null -// @ManyToOne(() => Address, { -// cascade: [Cascade.PERSIST], -// nullable: true, -// }) -// billing_address: Rel
| null -// @Property({ columnType: "jsonb", nullable: true }) -// metadata: Record | null = null -// @OneToMany(() => LineItem, (lineItem) => lineItem.cart, { -// cascade: [Cascade.PERSIST, "soft-remove"] as any, -// }) -// items = new Collection>(this) -// @OneToMany(() => ShippingMethod, (shippingMethod) => shippingMethod.cart, { -// cascade: [Cascade.PERSIST, "soft-remove"] as any, -// }) -// shipping_methods = new Collection>(this) -// @Property({ columnType: "timestamptz", nullable: true }) -// completed_at: Date | null = null -// @Property({ -// onCreate: () => new Date(), -// columnType: "timestamptz", -// defaultRaw: "now()", -// }) -// created_at: Date -// @Property({ -// onCreate: () => new Date(), -// onUpdate: () => new Date(), -// columnType: "timestamptz", -// defaultRaw: "now()", -// }) -// updated_at: Date -// @DeletedAtIndex() -// @Property({ columnType: "timestamptz", nullable: true }) -// deleted_at: Date | null = null -// @BeforeCreate() -// onCreate() { -// this.id = generateEntityId(this.id, "cart") -// } -// @OnInit() -// onInit() { -// this.id = generateEntityId(this.id, "cart") -// } -// } export default Cart diff --git a/packages/modules/cart/src/models/line-item-adjustment.ts b/packages/modules/cart/src/models/line-item-adjustment.ts index 3e87c94ebd7ad..0c3bda98af6be 100644 --- a/packages/modules/cart/src/models/line-item-adjustment.ts +++ b/packages/modules/cart/src/models/line-item-adjustment.ts @@ -30,35 +30,4 @@ const LineItemAdjustment = model }, ]) -// @Entity({ tableName: "cart_line_item_adjustment" }) -// @Check({ -// expression: (columns) => `${columns.amount} >= 0`, -// }) -// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -// export default class LineItemAdjustment extends AdjustmentLine { -// @ManyToOne({ entity: () => LineItem, persist: false }) -// item: Rel -// @LineItemIdIndex() -// @ManyToOne({ -// entity: () => LineItem, -// columnType: "text", -// fieldName: "item_id", -// mapToPk: true, -// }) -// item_id: string -// @PromotionIdIndex() -// @Property({ columnType: "text", nullable: true }) -// promotion_id: string | null = null -// @DeletedAtIndex() -// @Property({ columnType: "timestamptz", nullable: true }) -// deleted_at: Date | null = null -// @BeforeCreate() -// onCreate() { -// this.id = generateEntityId(this.id, "caliadj") -// } -// @OnInit() -// onInit() { -// this.id = generateEntityId(this.id, "caliadj") -// } -// } export default LineItemAdjustment diff --git a/packages/modules/cart/src/models/line-item-tax-line.ts b/packages/modules/cart/src/models/line-item-tax-line.ts index 09ce5153a64da..ac4db925ca255 100644 --- a/packages/modules/cart/src/models/line-item-tax-line.ts +++ b/packages/modules/cart/src/models/line-item-tax-line.ts @@ -32,37 +32,4 @@ const LineItemTaxLine = model }, ]) -// @Entity({ tableName: "cart_line_item_tax_line" }) -// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -// export default class LineItemTaxLine extends TaxLine { -// @ManyToOne({ entity: () => LineItem, persist: false }) -// item: Rel - -// @LineItemIdIndex() -// @ManyToOne({ -// entity: () => LineItem, -// columnType: "text", -// fieldName: "item_id", -// mapToPk: true, -// }) -// item_id: string - -// @TaxRateIdIndex() -// @Property({ columnType: "text", nullable: true }) -// tax_rate_id: string | null = null - -// @DeletedAtIndex() -// @Property({ columnType: "timestamptz", nullable: true }) -// deleted_at: Date | null = null - -// @BeforeCreate() -// onCreate() { -// this.id = generateEntityId(this.id, "calitxl") -// } - -// @OnInit() -// onInit() { -// this.id = generateEntityId(this.id, "calitxl") -// } -// } export default LineItemTaxLine diff --git a/packages/modules/cart/src/models/line-item.ts b/packages/modules/cart/src/models/line-item.ts index 08f6ada67ed85..b37a13211d148 100644 --- a/packages/modules/cart/src/models/line-item.ts +++ b/packages/modules/cart/src/models/line-item.ts @@ -68,107 +68,4 @@ const LineItem = model delete: ["adjustments", "tax_lines"], }) -// @Entity({ tableName: "cart_line_item" }) -// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -// export default class LineItem { -// [OptionalProps]?: OptionalLineItemProps -// @PrimaryKey({ columnType: "text" }) -// id: string -// @CartIdIndex() -// @ManyToOne({ -// entity: () => Cart, -// columnType: "text", -// fieldName: "cart_id", -// mapToPk: true, -// }) -// cart_id: string -// @ManyToOne({ entity: () => Cart, persist: false }) -// cart: Rel -// @Property({ columnType: "text" }) -// title: string -// @Property({ columnType: "text", nullable: true }) -// subtitle: string | null = null -// @Property({ columnType: "text", nullable: true }) -// thumbnail: string | null = null -// @Property({ columnType: "integer" }) -// quantity: number -// @VariantIdIndex() -// @Property({ columnType: "text", nullable: true }) -// variant_id: string | null = null -// @ProductIdIndex() -// @Property({ columnType: "text", nullable: true }) -// product_id: string | null = null -// @Property({ columnType: "text", nullable: true }) -// product_title: string | null = null -// @Property({ columnType: "text", nullable: true }) -// product_description: string | null = null -// @Property({ columnType: "text", nullable: true }) -// product_subtitle: string | null = null -// @Property({ columnType: "text", nullable: true }) -// product_type: string | null = null -// @ProductTypeIdIndex() -// @Property({ columnType: "text", nullable: true }) -// product_type_id: string | null = null -// @Property({ columnType: "text", nullable: true }) -// product_collection: string | null = null -// @Property({ columnType: "text", nullable: true }) -// product_handle: string | null = null -// @Property({ columnType: "text", nullable: true }) -// variant_sku: string | null = null -// @Property({ columnType: "text", nullable: true }) -// variant_barcode: string | null = null -// @Property({ columnType: "text", nullable: true }) -// variant_title: string | null = null -// @Property({ columnType: "jsonb", nullable: true }) -// variant_option_values: Record | null = null -// @Property({ columnType: "boolean" }) -// requires_shipping: boolean = true -// @Property({ columnType: "boolean" }) -// is_discountable: boolean = true -// @Property({ columnType: "boolean" }) -// is_tax_inclusive: boolean = false -// @MikroOrmBigNumberProperty({ nullable: true }) -// compare_at_unit_price?: BigNumber | number | null = null -// @Property({ columnType: "jsonb", nullable: true }) -// raw_compare_at_unit_price: BigNumberRawValue | null = null -// @MikroOrmBigNumberProperty() -// unit_price: BigNumber | number -// @Property({ columnType: "jsonb" }) -// raw_unit_price: BigNumberRawValue -// @OneToMany(() => LineItemTaxLine, (taxLine) => taxLine.item, { -// cascade: [Cascade.PERSIST, "soft-remove"] as any, -// }) -// tax_lines = new Collection>(this) -// @OneToMany(() => LineItemAdjustment, (adjustment) => adjustment.item, { -// cascade: [Cascade.PERSIST, "soft-remove"] as any, -// }) -// adjustments = new Collection>(this) -// @Property({ columnType: "jsonb", nullable: true }) -// metadata: Record | null = null -// @Property({ -// onCreate: () => new Date(), -// columnType: "timestamptz", -// defaultRaw: "now()", -// }) -// created_at: Date -// @Property({ -// onCreate: () => new Date(), -// onUpdate: () => new Date(), -// columnType: "timestamptz", -// defaultRaw: "now()", -// }) -// updated_at: Date -// @DeletedAtIndex() -// @Property({ columnType: "timestamptz", nullable: true }) -// deleted_at: Date | null = null -// @BeforeCreate() -// onCreate() { -// this.id = generateEntityId(this.id, "cali") -// } -// @OnInit() -// onInit() { -// this.id = generateEntityId(this.id, "cali") -// } -// } - export default LineItem diff --git a/packages/modules/cart/src/models/shipping-method-adjustment.ts b/packages/modules/cart/src/models/shipping-method-adjustment.ts index a608c314c8585..935c791cc2b6b 100644 --- a/packages/modules/cart/src/models/shipping-method-adjustment.ts +++ b/packages/modules/cart/src/models/shipping-method-adjustment.ts @@ -33,37 +33,4 @@ const ShippingMethodAdjustment = model }, ]) -// @Entity({ tableName: "cart_shipping_method_adjustment" }) -// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -// export default class ShippingMethodAdjustment extends AdjustmentLine { -// @ManyToOne({ entity: () => ShippingMethod, persist: false }) -// shipping_method: Rel - -// @ShippingMethodIdIndex() -// @ManyToOne({ -// entity: () => ShippingMethod, -// columnType: "text", -// fieldName: "shipping_method_id", -// mapToPk: true, -// }) -// shipping_method_id: string - -// @PromotionIdIndex() -// @Property({ columnType: "text", nullable: true }) -// promotion_id: string | null = null - -// @DeletedAtIndex() -// @Property({ columnType: "timestamptz", nullable: true }) -// deleted_at: Date | null = null - -// @BeforeCreate() -// onCreate() { -// this.id = generateEntityId(this.id, "casmadj") -// } - -// @OnInit() -// onInit() { -// this.id = generateEntityId(this.id, "casmadj") -// } -// } export default ShippingMethodAdjustment diff --git a/packages/modules/cart/src/models/shipping-method-tax-line.ts b/packages/modules/cart/src/models/shipping-method-tax-line.ts index da141494743fa..80c1aa53c57dd 100644 --- a/packages/modules/cart/src/models/shipping-method-tax-line.ts +++ b/packages/modules/cart/src/models/shipping-method-tax-line.ts @@ -33,37 +33,4 @@ const ShippingMethodTaxLine = model }, ]) -// @Entity({ tableName: "cart_shipping_method_tax_line" }) -// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -// export default class ShippingMethodTaxLine extends TaxLine { -// @ManyToOne({ entity: () => ShippingMethod, persist: false }) -// shipping_method: Rel - -// @ShippingMethodIdIndex() -// @ManyToOne({ -// entity: () => ShippingMethod, -// columnType: "text", -// fieldName: "shipping_method_id", -// mapToPk: true, -// }) -// shipping_method_id: string - -// @TaxRateIdIndex() -// @Property({ columnType: "text", nullable: true }) -// tax_rate_id: string | null = null - -// @DeletedAtIndex() -// @Property({ columnType: "timestamptz", nullable: true }) -// deleted_at: Date | null = null - -// @BeforeCreate() -// onCreate() { -// this.id = generateEntityId(this.id, "casmtxl") -// } - -// @OnInit() -// onInit() { -// this.id = generateEntityId(this.id, "casmtxl") -// } -// } export default ShippingMethodTaxLine diff --git a/packages/modules/cart/src/models/shipping-method.ts b/packages/modules/cart/src/models/shipping-method.ts index b0d88a80b392e..cc69e70a2654e 100644 --- a/packages/modules/cart/src/models/shipping-method.ts +++ b/packages/modules/cart/src/models/shipping-method.ts @@ -42,97 +42,4 @@ const ShippingMethod = model }, ]) -// @Entity({ tableName: "cart_shipping_method" }) -// @Check({ expression: (columns) => `${columns.amount} >= 0` }) -// @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -// export default class ShippingMethod { -// [OptionalProps]?: OptionalShippingMethodProps - -// @PrimaryKey({ columnType: "text" }) -// id: string - -// @CartIdIndex() -// @ManyToOne({ -// entity: () => Cart, -// columnType: "text", -// fieldName: "cart_id", -// mapToPk: true, -// }) -// cart_id: string - -// @ManyToOne({ entity: () => Cart, persist: false }) -// cart: Rel - -// @Property({ columnType: "text" }) -// name: string - -// @Property({ columnType: "jsonb", nullable: true }) -// description: string | null = null - -// @MikroOrmBigNumberProperty() -// amount: BigNumber | number - -// @Property({ columnType: "jsonb" }) -// raw_amount: BigNumberRawValue - -// @Property({ columnType: "boolean" }) -// is_tax_inclusive: boolean = false - -// @ShippingOptionIdIndex() -// @Property({ columnType: "text", nullable: true }) -// shipping_option_id: string | null = null - -// @Property({ columnType: "jsonb", nullable: true }) -// data: Record | null = null - -// @Property({ columnType: "jsonb", nullable: true }) -// metadata: Record | null = null - -// @OneToMany( -// () => ShippingMethodTaxLine, -// (taxLine) => taxLine.shipping_method, -// { -// cascade: [Cascade.PERSIST, "soft-remove"] as any, -// } -// ) -// tax_lines = new Collection>(this) - -// @OneToMany( -// () => ShippingMethodAdjustment, -// (adjustment) => adjustment.shipping_method, -// { -// cascade: [Cascade.PERSIST, "soft-remove"] as any, -// } -// ) -// adjustments = new Collection>(this) - -// @Property({ -// onCreate: () => new Date(), -// columnType: "timestamptz", -// defaultRaw: "now()", -// }) -// created_at: Date - -// @Property({ -// onCreate: () => new Date(), -// onUpdate: () => new Date(), -// columnType: "timestamptz", -// defaultRaw: "now()", -// }) -// updated_at: Date - -// @DeletedAtIndex() -// @Property({ columnType: "timestamptz", nullable: true }) -// deleted_at: Date | null = null - -// @BeforeCreate() -// onCreate() { -// this.id = generateEntityId(this.id, "casm") -// } - -// @OnInit() -// onInit() { -// this.id = generateEntityId(this.id, "casm") -// } -// } export default ShippingMethod diff --git a/packages/modules/cart/src/models/tax-line.ts b/packages/modules/cart/src/models/tax-line.ts deleted file mode 100644 index bc9d4e8477649..0000000000000 --- a/packages/modules/cart/src/models/tax-line.ts +++ /dev/null @@ -1,54 +0,0 @@ -// import { model } from "@medusajs/framework/utils" - -// const TaxLine = model.define( -// { name: "TaxLine" }, -// { -// id: model.id().primaryKey(), -// description: model.text().nullable(), -// code: model.text(), -// rate: model.number(), -// provider_id: model.text().nullable(), -// metadata: model.json().nullable(), -// } -// ) - -/** - * As per the Mikro ORM docs, superclasses should use the abstract class definition - * Source: https://mikro-orm.io/docs/inheritance-mapping - */ -// export default abstract class TaxLine { -// [OptionalProps]?: OptionalTaxLineProps - -// @PrimaryKey({ columnType: "text" }) -// id: string - -// @Property({ columnType: "text", nullable: true }) -// description?: string | null - -// @Property({ columnType: "text" }) -// code: string - -// @Property({ columnType: "numeric", serializer: Number }) -// rate: number - -// @Property({ columnType: "text", nullable: true }) -// provider_id?: string | null - -// @Property({ columnType: "jsonb", nullable: true }) -// metadata: Record | null = null - -// @Property({ -// onCreate: () => new Date(), -// columnType: "timestamptz", -// defaultRaw: "now()", -// }) -// created_at: Date - -// @Property({ -// onCreate: () => new Date(), -// onUpdate: () => new Date(), -// columnType: "timestamptz", -// defaultRaw: "now()", -// }) -// updated_at: Date -// } From 2df1a2a1ec95a109be257a7f861244b1ec7f2ea5 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Mon, 2 Dec 2024 18:37:20 +0530 Subject: [PATCH 04/12] feat: re-generate migrations after implementing checks --- .../cart/src/migrations/.snapshot-medusa-cart.json | 8 +++++++- ...ration20241202092453.ts => Migration20241202130532.ts} | 8 +------- packages/modules/cart/src/models/shipping-method.ts | 1 + 3 files changed, 9 insertions(+), 8 deletions(-) rename packages/modules/cart/src/migrations/{Migration20241202092453.ts => Migration20241202130532.ts} (95%) diff --git a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json index ba11404addc34..909e38c66a1ca 100644 --- a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json +++ b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json @@ -1289,7 +1289,13 @@ "unique": true } ], - "checks": [], + "checks": [ + { + "name": "cart_shipping_method_check", + "expression": "amount >= 0", + "definition": "check ((amount >= 0))" + } + ], "foreignKeys": { "cart_shipping_method_cart_id_foreign": { "constraintName": "cart_shipping_method_cart_id_foreign", diff --git a/packages/modules/cart/src/migrations/Migration20241202092453.ts b/packages/modules/cart/src/migrations/Migration20241202130532.ts similarity index 95% rename from packages/modules/cart/src/migrations/Migration20241202092453.ts rename to packages/modules/cart/src/migrations/Migration20241202130532.ts index ee600a0eb0949..1d11eed3280db 100644 --- a/packages/modules/cart/src/migrations/Migration20241202092453.ts +++ b/packages/modules/cart/src/migrations/Migration20241202130532.ts @@ -1,6 +1,6 @@ import { Migration } from "@mikro-orm/migrations" -export class Migration20241202092453 extends Migration { +export class Migration20241202130532 extends Migration { async up(): Promise { this.addSql( 'alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";' @@ -66,9 +66,6 @@ export class Migration20241202092453 extends Migration { 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_tax_line_item_id" ON "cart_line_item_tax_line" (item_id) WHERE deleted_at IS NULL;' ) - this.addSql( - 'alter table if exists "cart_shipping_method" drop constraint if exists cart_shipping_method_check;' - ) this.addSql( 'alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' ) @@ -139,9 +136,6 @@ export class Migration20241202092453 extends Migration { this.addSql( 'alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;' ) - this.addSql( - 'alter table if exists "cart_shipping_method" add constraint cart_shipping_method_check check(amount >= 0);' - ) this.addSql( 'drop index if exists "IDX_cart_shipping_method_adjustment_shipping_method_id";' diff --git a/packages/modules/cart/src/models/shipping-method.ts b/packages/modules/cart/src/models/shipping-method.ts index cc69e70a2654e..b3a303c2c6d73 100644 --- a/packages/modules/cart/src/models/shipping-method.ts +++ b/packages/modules/cart/src/models/shipping-method.ts @@ -41,5 +41,6 @@ const ShippingMethod = model where: "deleted_at IS NULL AND shipping_option_id IS NOT NULL", }, ]) + .checks([(columns) => `${columns.amount} >= 0`]) export default ShippingMethod From a715fbab3afea2b766c4a8f809f8f5d9e0a813bf Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 4 Dec 2024 12:53:52 +0530 Subject: [PATCH 05/12] refactor: define cart -> address relationship via hasOne --- .../entity-builder/define-relationship.ts | 5 +++-- packages/modules/cart/src/models/address.ts | 4 ---- packages/modules/cart/src/models/cart.ts | 21 +++++++++---------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts b/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts index 10f8a79823fc6..822cb08b19336 100644 --- a/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts +++ b/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts @@ -143,11 +143,12 @@ export function defineHasOneRelationship( OneToOne({ entity: relatedModelName, nullable: relationship.nullable, - mappedBy: relationship.mappedBy || camelToSnakeCase(MikroORMEntity.name), + ...(relationship.mappedBy ? { mappedBy: relationship.mappedBy } : {}), + // mappedBy: relationship.mappedBy || camelToSnakeCase(MikroORMEntity.name), cascade: shouldRemoveRelated ? (["persist", "soft-remove"] as any) : undefined, - })(MikroORMEntity.prototype, relationship.name) + } as any)(MikroORMEntity.prototype, relationship.name) } /** diff --git a/packages/modules/cart/src/models/address.ts b/packages/modules/cart/src/models/address.ts index 9db78f0bcfced..34fa8885a009f 100644 --- a/packages/modules/cart/src/models/address.ts +++ b/packages/modules/cart/src/models/address.ts @@ -1,5 +1,4 @@ import { model } from "@medusajs/framework/utils" -import Cart from "./cart" const Address = model.define( { tableName: "cart_address", name: "Address" }, @@ -17,9 +16,6 @@ const Address = model.define( postal_code: model.text().nullable(), phone: model.text().nullable(), metadata: model.json().nullable(), - carts: model.hasMany(() => Cart, { - mappedBy: "shipping_address", - }), } ) diff --git a/packages/modules/cart/src/models/cart.ts b/packages/modules/cart/src/models/cart.ts index ccea5ea9bb8f9..9462107a8c620 100644 --- a/packages/modules/cart/src/models/cart.ts +++ b/packages/modules/cart/src/models/cart.ts @@ -13,16 +13,10 @@ const Cart = model currency_code: model.text(), metadata: model.json().nullable(), completed_at: model.dateTime().nullable(), - shipping_address: model - .belongsTo(() => Address, { - mappedBy: "carts", - }) - .nullable(), - billing_address: model - .belongsTo(() => Address, { - mappedBy: "carts", - }) - .nullable(), + shipping_address_id: model.text().nullable(), + billing_address_id: model.text().nullable(), + shipping_address: model.hasOne(() => Address).nullable(), + billing_address: model.hasOne(() => Address).nullable(), items: model.hasMany(() => LineItem, { mappedBy: "cart", }), @@ -31,7 +25,12 @@ const Cart = model }), }) .cascades({ - delete: ["items", "shipping_methods"], + delete: [ + "items", + "shipping_methods", + "shipping_address", + "billing_address", + ], }) .indexes([ { From 408f25331fa7cba7e44d1fe26f69f155cea47e31 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 4 Dec 2024 13:21:40 +0530 Subject: [PATCH 06/12] refactor: add support for explicitly unsetting the mappedBy --- .../dml/helpers/entity-builder/define-relationship.ts | 10 ++++++++-- packages/core/utils/src/dml/relations/base.ts | 4 +++- packages/modules/cart/src/models/cart.ts | 8 ++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts b/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts index 822cb08b19336..0a8a126f8c0a6 100644 --- a/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts +++ b/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts @@ -139,12 +139,18 @@ export function defineHasOneRelationship( cascades: EntityCascades ) { const shouldRemoveRelated = !!cascades.delete?.includes(relationship.name) + let mappedBy: string | undefined + + if ("mappedBy" in relationship) { + mappedBy = relationship.mappedBy + } else { + mappedBy = camelToSnakeCase(MikroORMEntity.name) + } OneToOne({ entity: relatedModelName, nullable: relationship.nullable, - ...(relationship.mappedBy ? { mappedBy: relationship.mappedBy } : {}), - // mappedBy: relationship.mappedBy || camelToSnakeCase(MikroORMEntity.name), + ...(mappedBy ? { mappedBy } : {}), cascade: shouldRemoveRelated ? (["persist", "soft-remove"] as any) : undefined, diff --git a/packages/core/utils/src/dml/relations/base.ts b/packages/core/utils/src/dml/relations/base.ts index e29e74f2058c6..08c40ec0bc47b 100644 --- a/packages/core/utils/src/dml/relations/base.ts +++ b/packages/core/utils/src/dml/relations/base.ts @@ -71,11 +71,13 @@ export abstract class BaseRelationship implements RelationshipType { return { name: relationshipName, nullable: false, - mappedBy: this.options.mappedBy, options: this.options, searchable: this.#searchable, entity: this.#referencedEntity, type: this.type, + ...("mappedBy" in this.options + ? { mappedBy: this.options.mappedBy } + : {}), } } } diff --git a/packages/modules/cart/src/models/cart.ts b/packages/modules/cart/src/models/cart.ts index 9462107a8c620..6cd6173a484d4 100644 --- a/packages/modules/cart/src/models/cart.ts +++ b/packages/modules/cart/src/models/cart.ts @@ -15,8 +15,12 @@ const Cart = model completed_at: model.dateTime().nullable(), shipping_address_id: model.text().nullable(), billing_address_id: model.text().nullable(), - shipping_address: model.hasOne(() => Address).nullable(), - billing_address: model.hasOne(() => Address).nullable(), + shipping_address: model + .hasOne(() => Address, { mappedBy: undefined }) + .nullable(), + billing_address: model + .hasOne(() => Address, { mappedBy: undefined }) + .nullable(), items: model.hasMany(() => LineItem, { mappedBy: "cart", }), From 904828d472242219ccd8bbf80225ee99df013917 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 4 Dec 2024 14:27:28 +0530 Subject: [PATCH 07/12] feat: allow hasOne relationship to contain foreign key --- .../entity-builder/define-relationship.ts | 36 ++++++++++++++----- packages/modules/cart/src/models/cart.ts | 16 +++++---- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts b/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts index 0a8a126f8c0a6..3509acb1ed177 100644 --- a/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts +++ b/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts @@ -147,14 +147,34 @@ export function defineHasOneRelationship( mappedBy = camelToSnakeCase(MikroORMEntity.name) } - OneToOne({ - entity: relatedModelName, - nullable: relationship.nullable, - ...(mappedBy ? { mappedBy } : {}), - cascade: shouldRemoveRelated - ? (["persist", "soft-remove"] as any) - : undefined, - } as any)(MikroORMEntity.prototype, relationship.name) + if (relationship.options.foreignKey) { + const foreignKeyName = camelToSnakeCase(`${relationship.name}Id`) + + OneToOne({ + entity: relatedModelName, + nullable: relationship.nullable, + ...(mappedBy ? { mappedBy } : {}), + cascade: shouldRemoveRelated + ? (["persist", "soft-remove"] as any) + : undefined, + } as any)(MikroORMEntity.prototype, relationship.name) + + Property({ + type: "string", + columnType: "text", + nullable: relationship.nullable, + persist: true, + })(MikroORMEntity.prototype, foreignKeyName) + } else { + OneToOne({ + entity: relatedModelName, + nullable: relationship.nullable, + ...(mappedBy ? { mappedBy } : {}), + cascade: shouldRemoveRelated + ? (["persist", "soft-remove"] as any) + : undefined, + } as any)(MikroORMEntity.prototype, relationship.name) + } } /** diff --git a/packages/modules/cart/src/models/cart.ts b/packages/modules/cart/src/models/cart.ts index 6cd6173a484d4..95f49044cc8f7 100644 --- a/packages/modules/cart/src/models/cart.ts +++ b/packages/modules/cart/src/models/cart.ts @@ -13,13 +13,17 @@ const Cart = model currency_code: model.text(), metadata: model.json().nullable(), completed_at: model.dateTime().nullable(), - shipping_address_id: model.text().nullable(), - billing_address_id: model.text().nullable(), shipping_address: model - .hasOne(() => Address, { mappedBy: undefined }) + .hasOne(() => Address, { + mappedBy: undefined, + foreignKey: true, + }) .nullable(), billing_address: model - .hasOne(() => Address, { mappedBy: undefined }) + .hasOne(() => Address, { + mappedBy: undefined, + foreignKey: true, + }) .nullable(), items: model.hasMany(() => LineItem, { mappedBy: "cart", @@ -59,12 +63,12 @@ const Cart = model }, { name: "IDX_cart_shipping_address_id", - on: ["shipping_address_id"], + on: ["shipping_address_id" as any], where: "deleted_at IS NULL AND shipping_address_id IS NOT NULL", }, { name: "IDX_cart_billing_address_id", - on: ["billing_address_id"], + on: ["billing_address_id" as any], where: "deleted_at IS NULL AND billing_address_id IS NOT NULL", }, ]) From 117920bd67e1b207cc0aa3681363e5ce4c4fad87 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 4 Dec 2024 14:44:59 +0530 Subject: [PATCH 08/12] refactor: regenerate migrations --- .../src/migrations/.snapshot-medusa-cart.json | 18 ++- .../src/migrations/Migration20241202130532.ts | 151 ------------------ .../src/migrations/Migration20241204091419.ts | 79 +++++++++ 3 files changed, 89 insertions(+), 159 deletions(-) delete mode 100644 packages/modules/cart/src/migrations/Migration20241202130532.ts create mode 100644 packages/modules/cart/src/migrations/Migration20241204091419.ts diff --git a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json index 909e38c66a1ca..227936f042570 100644 --- a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json +++ b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json @@ -310,20 +310,22 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_cart_shipping_address_id", - "columnNames": [], + "columnNames": [ + "shipping_address_id" + ], "composite": false, + "keyName": "cart_shipping_address_id_unique", "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_address_id\" ON \"cart\" (shipping_address_id) WHERE deleted_at IS NULL" + "unique": true }, { - "keyName": "IDX_cart_billing_address_id", - "columnNames": [], + "columnNames": [ + "billing_address_id" + ], "composite": false, + "keyName": "cart_billing_address_id_unique", "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_billing_address_id\" ON \"cart\" (billing_address_id) WHERE deleted_at IS NULL" + "unique": true }, { "keyName": "IDX_cart_deleted_at", diff --git a/packages/modules/cart/src/migrations/Migration20241202130532.ts b/packages/modules/cart/src/migrations/Migration20241202130532.ts deleted file mode 100644 index 1d11eed3280db..0000000000000 --- a/packages/modules/cart/src/migrations/Migration20241202130532.ts +++ /dev/null @@ -1,151 +0,0 @@ -import { Migration } from "@mikro-orm/migrations" - -export class Migration20241202130532 extends Migration { - async up(): Promise { - this.addSql( - 'alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";' - ) - - this.addSql( - 'alter table if exists "cart_line_item_adjustment" drop constraint if exists "cart_line_item_adjustment_item_id_foreign";' - ) - - this.addSql( - 'alter table if exists "cart_line_item_tax_line" drop constraint if exists "cart_line_item_tax_line_item_id_foreign";' - ) - - this.addSql( - 'alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";' - ) - - this.addSql( - 'alter table if exists "cart_line_item" alter column "requires_shipping" type boolean using ("requires_shipping"::boolean);' - ) - this.addSql( - 'alter table if exists "cart_line_item" alter column "requires_shipping" set default true;' - ) - this.addSql( - 'alter table if exists "cart_line_item" alter column "is_discountable" type boolean using ("is_discountable"::boolean);' - ) - this.addSql( - 'alter table if exists "cart_line_item" alter column "is_discountable" set default true;' - ) - this.addSql( - 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" type boolean using ("is_tax_inclusive"::boolean);' - ) - this.addSql( - 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" set default false;' - ) - this.addSql( - 'alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' - ) - this.addSql( - 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_cart_id" ON "cart_line_item" (cart_id) WHERE deleted_at IS NULL;' - ) - - this.addSql( - 'alter table if exists "cart_line_item_adjustment" drop constraint if exists cart_line_item_adjustment_check;' - ) - this.addSql( - 'alter table if exists "cart_line_item_adjustment" add constraint "cart_line_item_adjustment_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;' - ) - this.addSql( - 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_adjustment_item_id" ON "cart_line_item_adjustment" (item_id) WHERE deleted_at IS NULL;' - ) - - this.addSql( - 'alter table if exists "cart_line_item_tax_line" alter column "rate" type integer using ("rate"::integer);' - ) - this.addSql( - 'alter table if exists "cart_line_item_tax_line" drop column if exists "metadata";' - ) - this.addSql( - 'alter table if exists "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;' - ) - this.addSql( - 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_tax_line_item_id" ON "cart_line_item_tax_line" (item_id) WHERE deleted_at IS NULL;' - ) - - this.addSql( - 'alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' - ) - this.addSql( - 'CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_cart_id" ON "cart_shipping_method" (cart_id) WHERE deleted_at IS NULL;' - ) - - this.addSql( - 'CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_adjustment_shipping_method_id" ON "cart_shipping_method_adjustment" (shipping_method_id) WHERE deleted_at IS NULL;' - ) - - this.addSql( - 'alter table if exists "cart_shipping_method_tax_line" alter column "rate" type integer using ("rate"::integer);' - ) - this.addSql( - 'CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_tax_line_shipping_method_id" ON "cart_shipping_method_tax_line" (shipping_method_id) WHERE deleted_at IS NULL;' - ) - } - - async down(): Promise { - this.addSql( - 'alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";' - ) - - this.addSql( - 'alter table if exists "cart_line_item_adjustment" drop constraint if exists "cart_line_item_adjustment_item_id_foreign";' - ) - - this.addSql( - 'alter table if exists "cart_line_item_tax_line" drop constraint if exists "cart_line_item_tax_line_item_id_foreign";' - ) - - this.addSql( - 'alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";' - ) - - this.addSql( - 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" drop default;' - ) - this.addSql( - 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" type boolean using ("is_tax_inclusive"::boolean);' - ) - this.addSql('drop index if exists "IDX_cart_line_item_cart_id";') - this.addSql( - 'alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;' - ) - - this.addSql('drop index if exists "IDX_cart_line_item_adjustment_item_id";') - this.addSql( - 'alter table if exists "cart_line_item_adjustment" add constraint "cart_line_item_adjustment_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade;' - ) - this.addSql( - 'alter table if exists "cart_line_item_adjustment" add constraint cart_line_item_adjustment_check check(amount >= 0);' - ) - - this.addSql( - 'alter table if exists "cart_line_item_tax_line" add column if not exists "metadata" jsonb null;' - ) - this.addSql( - 'alter table if exists "cart_line_item_tax_line" alter column "rate" type numeric using ("rate"::numeric);' - ) - this.addSql('drop index if exists "IDX_cart_line_item_tax_line_item_id";') - this.addSql( - 'alter table if exists "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade;' - ) - - this.addSql('drop index if exists "IDX_cart_shipping_method_cart_id";') - this.addSql( - 'alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;' - ) - - this.addSql( - 'drop index if exists "IDX_cart_shipping_method_adjustment_shipping_method_id";' - ) - - this.addSql( - 'alter table if exists "cart_shipping_method_tax_line" alter column "rate" type numeric using ("rate"::numeric);' - ) - this.addSql( - 'drop index if exists "IDX_cart_shipping_method_tax_line_shipping_method_id";' - ) - } -} diff --git a/packages/modules/cart/src/migrations/Migration20241204091419.ts b/packages/modules/cart/src/migrations/Migration20241204091419.ts new file mode 100644 index 0000000000000..677a5be69aec8 --- /dev/null +++ b/packages/modules/cart/src/migrations/Migration20241204091419.ts @@ -0,0 +1,79 @@ +import { Migration } from '@mikro-orm/migrations'; + +export class Migration20241204091419 extends Migration { + + async up(): Promise { + this.addSql('alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";'); + + this.addSql('alter table if exists "cart_line_item_adjustment" drop constraint if exists "cart_line_item_adjustment_item_id_foreign";'); + + this.addSql('alter table if exists "cart_line_item_tax_line" drop constraint if exists "cart_line_item_tax_line_item_id_foreign";'); + + this.addSql('alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";'); + + this.addSql('alter table if exists "cart" add constraint "cart_shipping_address_id_unique" unique ("shipping_address_id");'); + this.addSql('alter table if exists "cart" add constraint "cart_billing_address_id_unique" unique ("billing_address_id");'); + + this.addSql('alter table if exists "cart_line_item" alter column "requires_shipping" type boolean using ("requires_shipping"::boolean);'); + this.addSql('alter table if exists "cart_line_item" alter column "requires_shipping" set default true;'); + this.addSql('alter table if exists "cart_line_item" alter column "is_discountable" type boolean using ("is_discountable"::boolean);'); + this.addSql('alter table if exists "cart_line_item" alter column "is_discountable" set default true;'); + this.addSql('alter table if exists "cart_line_item" alter column "is_tax_inclusive" type boolean using ("is_tax_inclusive"::boolean);'); + this.addSql('alter table if exists "cart_line_item" alter column "is_tax_inclusive" set default false;'); + this.addSql('alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;'); + this.addSql('CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_cart_id" ON "cart_line_item" (cart_id) WHERE deleted_at IS NULL;'); + + this.addSql('alter table if exists "cart_line_item_adjustment" drop constraint if exists cart_line_item_adjustment_check;'); + this.addSql('alter table if exists "cart_line_item_adjustment" add constraint "cart_line_item_adjustment_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;'); + this.addSql('CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_adjustment_item_id" ON "cart_line_item_adjustment" (item_id) WHERE deleted_at IS NULL;'); + + this.addSql('alter table if exists "cart_line_item_tax_line" alter column "rate" type integer using ("rate"::integer);'); + this.addSql('alter table if exists "cart_line_item_tax_line" drop column if exists "metadata";'); + this.addSql('alter table if exists "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;'); + this.addSql('CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_tax_line_item_id" ON "cart_line_item_tax_line" (item_id) WHERE deleted_at IS NULL;'); + + this.addSql('alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;'); + this.addSql('CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_cart_id" ON "cart_shipping_method" (cart_id) WHERE deleted_at IS NULL;'); + + this.addSql('CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_adjustment_shipping_method_id" ON "cart_shipping_method_adjustment" (shipping_method_id) WHERE deleted_at IS NULL;'); + + this.addSql('alter table if exists "cart_shipping_method_tax_line" alter column "rate" type integer using ("rate"::integer);'); + this.addSql('CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_tax_line_shipping_method_id" ON "cart_shipping_method_tax_line" (shipping_method_id) WHERE deleted_at IS NULL;'); + } + + async down(): Promise { + this.addSql('alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";'); + + this.addSql('alter table if exists "cart_line_item_adjustment" drop constraint if exists "cart_line_item_adjustment_item_id_foreign";'); + + this.addSql('alter table if exists "cart_line_item_tax_line" drop constraint if exists "cart_line_item_tax_line_item_id_foreign";'); + + this.addSql('alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";'); + + this.addSql('alter table if exists "cart" drop constraint if exists "cart_shipping_address_id_unique";'); + this.addSql('alter table if exists "cart" drop constraint if exists "cart_billing_address_id_unique";'); + + this.addSql('alter table if exists "cart_line_item" alter column "is_tax_inclusive" drop default;'); + this.addSql('alter table if exists "cart_line_item" alter column "is_tax_inclusive" type boolean using ("is_tax_inclusive"::boolean);'); + this.addSql('drop index if exists "IDX_cart_line_item_cart_id";'); + this.addSql('alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;'); + + this.addSql('drop index if exists "IDX_cart_line_item_adjustment_item_id";'); + this.addSql('alter table if exists "cart_line_item_adjustment" add constraint "cart_line_item_adjustment_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade;'); + this.addSql('alter table if exists "cart_line_item_adjustment" add constraint cart_line_item_adjustment_check check(amount >= 0);'); + + this.addSql('alter table if exists "cart_line_item_tax_line" add column if not exists "metadata" jsonb null;'); + this.addSql('alter table if exists "cart_line_item_tax_line" alter column "rate" type numeric using ("rate"::numeric);'); + this.addSql('drop index if exists "IDX_cart_line_item_tax_line_item_id";'); + this.addSql('alter table if exists "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade;'); + + this.addSql('drop index if exists "IDX_cart_shipping_method_cart_id";'); + this.addSql('alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;'); + + this.addSql('drop index if exists "IDX_cart_shipping_method_adjustment_shipping_method_id";'); + + this.addSql('alter table if exists "cart_shipping_method_tax_line" alter column "rate" type numeric using ("rate"::numeric);'); + this.addSql('drop index if exists "IDX_cart_shipping_method_tax_line_shipping_method_id";'); + } + +} From fed6782f51ef200a09cabeb4550ca1ea39c050e8 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 5 Dec 2024 14:34:38 +0530 Subject: [PATCH 09/12] refactor: remove custom setup for hasOne with FK --- .../entity-builder/define-relationship.ts | 36 +++++-------------- .../core/utils/src/dml/relations/index.ts | 1 + packages/modules/cart/src/models/cart.ts | 4 +-- 3 files changed, 11 insertions(+), 30 deletions(-) diff --git a/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts b/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts index 290d8b99d3090..4f2544ebd2aac 100644 --- a/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts +++ b/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts @@ -148,34 +148,14 @@ export function defineHasOneRelationship( mappedBy = camelToSnakeCase(MikroORMEntity.name) } - if (relationship.options.foreignKey) { - const foreignKeyName = camelToSnakeCase(`${relationship.name}Id`) - - OneToOne({ - entity: relatedModelName, - nullable: relationship.nullable, - ...(mappedBy ? { mappedBy } : {}), - cascade: shouldRemoveRelated - ? (["persist", "soft-remove"] as any) - : undefined, - } as any)(MikroORMEntity.prototype, relationship.name) - - Property({ - type: "string", - columnType: "text", - nullable: relationship.nullable, - persist: true, - })(MikroORMEntity.prototype, foreignKeyName) - } else { - OneToOne({ - entity: relatedModelName, - nullable: relationship.nullable, - ...(mappedBy ? { mappedBy } : {}), - cascade: shouldRemoveRelated - ? (["persist", "soft-remove"] as any) - : undefined, - } as any)(MikroORMEntity.prototype, relationship.name) - } + OneToOne({ + entity: relatedModelName, + nullable: relationship.nullable, + ...(mappedBy ? { mappedBy } : {}), + cascade: shouldRemoveRelated + ? (["persist", "soft-remove"] as any) + : undefined, + } as any)(MikroORMEntity.prototype, relationship.name) } /** diff --git a/packages/core/utils/src/dml/relations/index.ts b/packages/core/utils/src/dml/relations/index.ts index 8e94644d4c58a..cdf7a4a955ecf 100644 --- a/packages/core/utils/src/dml/relations/index.ts +++ b/packages/core/utils/src/dml/relations/index.ts @@ -4,3 +4,4 @@ export * from "./has-many" export * from "./has-one" export * from "./many-to-many" export * from "./nullable" +export * from "./has-one-fk" diff --git a/packages/modules/cart/src/models/cart.ts b/packages/modules/cart/src/models/cart.ts index 95f49044cc8f7..0f40460cf39fa 100644 --- a/packages/modules/cart/src/models/cart.ts +++ b/packages/modules/cart/src/models/cart.ts @@ -63,12 +63,12 @@ const Cart = model }, { name: "IDX_cart_shipping_address_id", - on: ["shipping_address_id" as any], + on: ["shipping_address_id"], where: "deleted_at IS NULL AND shipping_address_id IS NOT NULL", }, { name: "IDX_cart_billing_address_id", - on: ["billing_address_id" as any], + on: ["billing_address_id"], where: "deleted_at IS NULL AND billing_address_id IS NOT NULL", }, ]) From 0076bc0758284635bd6e1a37ea7c6f1c09837088 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 5 Dec 2024 15:24:10 +0530 Subject: [PATCH 10/12] refactor: implement feedback --- .../src/migrations/.snapshot-medusa-cart.json | 17 +- .../src/migrations/Migration20241204091419.ts | 79 ---------- .../src/migrations/Migration20241205095237.ts | 146 ++++++++++++++++++ .../cart/src/models/line-item-adjustment.ts | 1 + .../cart/src/models/line-item-tax-line.ts | 1 + 5 files changed, 164 insertions(+), 80 deletions(-) delete mode 100644 packages/modules/cart/src/migrations/Migration20241204091419.ts create mode 100644 packages/modules/cart/src/migrations/Migration20241205095237.ts diff --git a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json index 227936f042570..af920bf4f77a9 100644 --- a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json +++ b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json @@ -943,7 +943,13 @@ "unique": true } ], - "checks": [], + "checks": [ + { + "name": "cart_line_item_adjustment_check", + "expression": "amount >= 0", + "definition": "check ((amount >= 0))" + } + ], "foreignKeys": { "cart_line_item_adjustment_item_id_foreign": { "constraintName": "cart_line_item_adjustment_item_id_foreign", @@ -1007,6 +1013,15 @@ "nullable": true, "mappedType": "text" }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, "tax_rate_id": { "name": "tax_rate_id", "type": "text", diff --git a/packages/modules/cart/src/migrations/Migration20241204091419.ts b/packages/modules/cart/src/migrations/Migration20241204091419.ts deleted file mode 100644 index 677a5be69aec8..0000000000000 --- a/packages/modules/cart/src/migrations/Migration20241204091419.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { Migration } from '@mikro-orm/migrations'; - -export class Migration20241204091419 extends Migration { - - async up(): Promise { - this.addSql('alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";'); - - this.addSql('alter table if exists "cart_line_item_adjustment" drop constraint if exists "cart_line_item_adjustment_item_id_foreign";'); - - this.addSql('alter table if exists "cart_line_item_tax_line" drop constraint if exists "cart_line_item_tax_line_item_id_foreign";'); - - this.addSql('alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";'); - - this.addSql('alter table if exists "cart" add constraint "cart_shipping_address_id_unique" unique ("shipping_address_id");'); - this.addSql('alter table if exists "cart" add constraint "cart_billing_address_id_unique" unique ("billing_address_id");'); - - this.addSql('alter table if exists "cart_line_item" alter column "requires_shipping" type boolean using ("requires_shipping"::boolean);'); - this.addSql('alter table if exists "cart_line_item" alter column "requires_shipping" set default true;'); - this.addSql('alter table if exists "cart_line_item" alter column "is_discountable" type boolean using ("is_discountable"::boolean);'); - this.addSql('alter table if exists "cart_line_item" alter column "is_discountable" set default true;'); - this.addSql('alter table if exists "cart_line_item" alter column "is_tax_inclusive" type boolean using ("is_tax_inclusive"::boolean);'); - this.addSql('alter table if exists "cart_line_item" alter column "is_tax_inclusive" set default false;'); - this.addSql('alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;'); - this.addSql('CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_cart_id" ON "cart_line_item" (cart_id) WHERE deleted_at IS NULL;'); - - this.addSql('alter table if exists "cart_line_item_adjustment" drop constraint if exists cart_line_item_adjustment_check;'); - this.addSql('alter table if exists "cart_line_item_adjustment" add constraint "cart_line_item_adjustment_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;'); - this.addSql('CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_adjustment_item_id" ON "cart_line_item_adjustment" (item_id) WHERE deleted_at IS NULL;'); - - this.addSql('alter table if exists "cart_line_item_tax_line" alter column "rate" type integer using ("rate"::integer);'); - this.addSql('alter table if exists "cart_line_item_tax_line" drop column if exists "metadata";'); - this.addSql('alter table if exists "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;'); - this.addSql('CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_tax_line_item_id" ON "cart_line_item_tax_line" (item_id) WHERE deleted_at IS NULL;'); - - this.addSql('alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;'); - this.addSql('CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_cart_id" ON "cart_shipping_method" (cart_id) WHERE deleted_at IS NULL;'); - - this.addSql('CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_adjustment_shipping_method_id" ON "cart_shipping_method_adjustment" (shipping_method_id) WHERE deleted_at IS NULL;'); - - this.addSql('alter table if exists "cart_shipping_method_tax_line" alter column "rate" type integer using ("rate"::integer);'); - this.addSql('CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_tax_line_shipping_method_id" ON "cart_shipping_method_tax_line" (shipping_method_id) WHERE deleted_at IS NULL;'); - } - - async down(): Promise { - this.addSql('alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";'); - - this.addSql('alter table if exists "cart_line_item_adjustment" drop constraint if exists "cart_line_item_adjustment_item_id_foreign";'); - - this.addSql('alter table if exists "cart_line_item_tax_line" drop constraint if exists "cart_line_item_tax_line_item_id_foreign";'); - - this.addSql('alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";'); - - this.addSql('alter table if exists "cart" drop constraint if exists "cart_shipping_address_id_unique";'); - this.addSql('alter table if exists "cart" drop constraint if exists "cart_billing_address_id_unique";'); - - this.addSql('alter table if exists "cart_line_item" alter column "is_tax_inclusive" drop default;'); - this.addSql('alter table if exists "cart_line_item" alter column "is_tax_inclusive" type boolean using ("is_tax_inclusive"::boolean);'); - this.addSql('drop index if exists "IDX_cart_line_item_cart_id";'); - this.addSql('alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;'); - - this.addSql('drop index if exists "IDX_cart_line_item_adjustment_item_id";'); - this.addSql('alter table if exists "cart_line_item_adjustment" add constraint "cart_line_item_adjustment_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade;'); - this.addSql('alter table if exists "cart_line_item_adjustment" add constraint cart_line_item_adjustment_check check(amount >= 0);'); - - this.addSql('alter table if exists "cart_line_item_tax_line" add column if not exists "metadata" jsonb null;'); - this.addSql('alter table if exists "cart_line_item_tax_line" alter column "rate" type numeric using ("rate"::numeric);'); - this.addSql('drop index if exists "IDX_cart_line_item_tax_line_item_id";'); - this.addSql('alter table if exists "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade;'); - - this.addSql('drop index if exists "IDX_cart_shipping_method_cart_id";'); - this.addSql('alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;'); - - this.addSql('drop index if exists "IDX_cart_shipping_method_adjustment_shipping_method_id";'); - - this.addSql('alter table if exists "cart_shipping_method_tax_line" alter column "rate" type numeric using ("rate"::numeric);'); - this.addSql('drop index if exists "IDX_cart_shipping_method_tax_line_shipping_method_id";'); - } - -} diff --git a/packages/modules/cart/src/migrations/Migration20241205095237.ts b/packages/modules/cart/src/migrations/Migration20241205095237.ts new file mode 100644 index 0000000000000..1492715bbfcad --- /dev/null +++ b/packages/modules/cart/src/migrations/Migration20241205095237.ts @@ -0,0 +1,146 @@ +import { Migration } from "@mikro-orm/migrations" + +export class Migration20241205095237 extends Migration { + async up(): Promise { + this.addSql( + 'alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item_adjustment" drop constraint if exists "cart_line_item_adjustment_item_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item_tax_line" drop constraint if exists "cart_line_item_tax_line_item_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item" alter column "requires_shipping" type boolean using ("requires_shipping"::boolean);' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "requires_shipping" set default true;' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_discountable" type boolean using ("is_discountable"::boolean);' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_discountable" set default true;' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" type boolean using ("is_tax_inclusive"::boolean);' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" set default false;' + ) + this.addSql( + 'alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_cart_id" ON "cart_line_item" (cart_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "cart_line_item_adjustment" add constraint "cart_line_item_adjustment_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_adjustment_item_id" ON "cart_line_item_adjustment" (item_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "cart_line_item_tax_line" alter column "rate" type integer using ("rate"::integer);' + ) + this.addSql( + 'alter table if exists "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_tax_line_item_id" ON "cart_line_item_tax_line" (item_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_cart_id" ON "cart_shipping_method" (cart_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_adjustment_shipping_method_id" ON "cart_shipping_method_adjustment" (shipping_method_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method_tax_line" alter column "rate" type integer using ("rate"::integer);' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_tax_line_shipping_method_id" ON "cart_shipping_method_tax_line" (shipping_method_id) WHERE deleted_at IS NULL;' + ) + } + + async down(): Promise { + this.addSql( + 'alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item_adjustment" drop constraint if exists "cart_line_item_adjustment_item_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item_tax_line" drop constraint if exists "cart_line_item_tax_line_item_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart" drop constraint if exists "cart_shipping_address_id_unique";' + ) + this.addSql( + 'alter table if exists "cart" drop constraint if exists "cart_billing_address_id_unique";' + ) + + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" drop default;' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" type boolean using ("is_tax_inclusive"::boolean);' + ) + this.addSql('drop index if exists "IDX_cart_line_item_cart_id";') + this.addSql( + 'alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;' + ) + + this.addSql('drop index if exists "IDX_cart_line_item_adjustment_item_id";') + this.addSql( + 'alter table if exists "cart_line_item_adjustment" add constraint "cart_line_item_adjustment_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade;' + ) + + this.addSql( + 'alter table if exists "cart_line_item_tax_line" alter column "rate" type numeric using ("rate"::numeric);' + ) + this.addSql('drop index if exists "IDX_cart_line_item_tax_line_item_id";') + this.addSql( + 'alter table if exists "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade;' + ) + + this.addSql('drop index if exists "IDX_cart_shipping_method_cart_id";') + this.addSql( + 'alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;' + ) + + this.addSql( + 'drop index if exists "IDX_cart_shipping_method_adjustment_shipping_method_id";' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method_tax_line" alter column "rate" type numeric using ("rate"::numeric);' + ) + this.addSql( + 'drop index if exists "IDX_cart_shipping_method_tax_line_shipping_method_id";' + ) + } +} diff --git a/packages/modules/cart/src/models/line-item-adjustment.ts b/packages/modules/cart/src/models/line-item-adjustment.ts index 0c3bda98af6be..2a1cc8edc26fd 100644 --- a/packages/modules/cart/src/models/line-item-adjustment.ts +++ b/packages/modules/cart/src/models/line-item-adjustment.ts @@ -29,5 +29,6 @@ const LineItemAdjustment = model where: "deleted_at IS NULL", }, ]) + .checks([(columns) => `${columns.amount} >= 0`]) export default LineItemAdjustment diff --git a/packages/modules/cart/src/models/line-item-tax-line.ts b/packages/modules/cart/src/models/line-item-tax-line.ts index ac4db925ca255..bc6e0f9911339 100644 --- a/packages/modules/cart/src/models/line-item-tax-line.ts +++ b/packages/modules/cart/src/models/line-item-tax-line.ts @@ -13,6 +13,7 @@ const LineItemTaxLine = model code: model.text(), rate: model.number(), provider_id: model.text().nullable(), + metadata: model.json().nullable(), tax_rate_id: model.text().nullable(), item: model.belongsTo(() => LineItem, { mappedBy: "tax_lines", From dc9dfc62a415709dbf1d219f9874441ef4fec349 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 5 Dec 2024 17:33:05 +0530 Subject: [PATCH 11/12] Create sour-cats-sort.md --- .changeset/sour-cats-sort.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/sour-cats-sort.md diff --git a/.changeset/sour-cats-sort.md b/.changeset/sour-cats-sort.md new file mode 100644 index 0000000000000..bcdb2842123c0 --- /dev/null +++ b/.changeset/sour-cats-sort.md @@ -0,0 +1,6 @@ +--- +"@medusajs/cart": patch +"@medusajs/utils": patch +--- + +refactor: migrate cart module to DML From b83214bda17180ad448aea0e109619d5dbc5edeb Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 5 Dec 2024 17:43:19 +0530 Subject: [PATCH 12/12] refactor: remove unneeded statements from migrations --- .../modules/cart/src/migrations/Migration20241205095237.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/modules/cart/src/migrations/Migration20241205095237.ts b/packages/modules/cart/src/migrations/Migration20241205095237.ts index 1492715bbfcad..2cf70fdfee223 100644 --- a/packages/modules/cart/src/migrations/Migration20241205095237.ts +++ b/packages/modules/cart/src/migrations/Migration20241205095237.ts @@ -96,13 +96,6 @@ export class Migration20241205095237 extends Migration { 'alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";' ) - this.addSql( - 'alter table if exists "cart" drop constraint if exists "cart_shipping_address_id_unique";' - ) - this.addSql( - 'alter table if exists "cart" drop constraint if exists "cart_billing_address_id_unique";' - ) - this.addSql( 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" drop default;' )