diff --git a/.changeset/unlucky-brooms-provide.md b/.changeset/unlucky-brooms-provide.md new file mode 100644 index 0000000000000..bce8edf1de9da --- /dev/null +++ b/.changeset/unlucky-brooms-provide.md @@ -0,0 +1,6 @@ +--- +"@medusajs/types": patch +"@medusajs/utils": patch +--- + +BigNumberField decorator diff --git a/packages/order/src/migrations/.snapshot-medusa-order.json b/packages/order/src/migrations/.snapshot-medusa-order.json new file mode 100644 index 0000000000000..6700f36b1ddce --- /dev/null +++ b/packages/order/src/migrations/.snapshot-medusa-order.json @@ -0,0 +1,2138 @@ +{ + "namespaces": ["public"], + "name": "public", + "tables": [ + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "customer_id": { + "name": "customer_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "company": { + "name": "company", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "first_name": { + "name": "first_name", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "last_name": { + "name": "last_name", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "address_1": { + "name": "address_1", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "address_2": { + "name": "address_2", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "city": { + "name": "city", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "country_code": { + "name": "country_code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "province": { + "name": "province", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "postal_code": { + "name": "postal_code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "phone": { + "name": "phone", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + } + }, + "name": "order_address", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_order_address_customer_id", + "columnNames": ["customer_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_address_customer_id\" ON \"order_address\" (customer_id)" + }, + { + "keyName": "order_address_pkey", + "columnNames": ["id"], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": {} + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "region_id": { + "name": "region_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "customer_id": { + "name": "customer_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "original_order_id": { + "name": "original_order_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "version": { + "name": "version", + "type": "integer", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "1", + "mappedType": "integer" + }, + "sales_channel_id": { + "name": "sales_channel_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "status": { + "name": "status", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "'pending'", + "enumItems": [ + "pending", + "completed", + "draft", + "archived", + "canceled", + "requires_action" + ], + "mappedType": "enum" + }, + "email": { + "name": "email", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "currency_code": { + "name": "currency_code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "shipping_address_id": { + "name": "shipping_address_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "billing_address_id": { + "name": "billing_address_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "no_notification": { + "name": "no_notification", + "type": "boolean", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "boolean" + }, + "summary": { + "name": "summary", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "canceled_at": { + "name": "canceled_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + } + }, + "name": "order", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_order_region_id", + "columnNames": ["region_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_region_id\" ON \"order\" (region_id) WHERE deleted_at IS NOT NULL" + }, + { + "keyName": "IDX_order_customer_id", + "columnNames": ["customer_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_customer_id\" ON \"order\" (customer_id) WHERE deleted_at IS NOT NULL" + }, + { + "keyName": "IDX_order_original_order_id", + "columnNames": ["original_order_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_original_order_id\" ON \"order\" (original_order_id) WHERE deleted_at IS NOT NULL" + }, + { + "keyName": "IDX_order_customer_id", + "columnNames": ["sales_channel_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_customer_id\" ON \"order\" (customer_id) WHERE deleted_at IS NOT NULL" + }, + { + "keyName": "IDX_order_currency_code", + "columnNames": ["currency_code"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_currency_code\" ON \"order\" (currency_code) WHERE deleted_at IS NOT NULL" + }, + { + "keyName": "IDX_order_shipping_address_id", + "columnNames": ["shipping_address_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_address_id\" ON \"order\" (shipping_address_id) WHERE deleted_at IS NOT NULL" + }, + { + "keyName": "IDX_order_billing_address_id", + "columnNames": ["billing_address_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_billing_address_id\" ON \"order\" (billing_address_id) WHERE deleted_at IS NOT NULL" + }, + { + "keyName": "IDX_order_deleted_at", + "columnNames": ["deleted_at"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_deleted_at\" ON \"order\" (deleted_at) WHERE deleted_at IS NOT NULL" + }, + { + "keyName": "order_pkey", + "columnNames": ["id"], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "order_shipping_address_id_foreign": { + "constraintName": "order_shipping_address_id_foreign", + "columnNames": ["shipping_address_id"], + "localTableName": "public.order", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order_address", + "deleteRule": "set null", + "updateRule": "cascade" + }, + "order_billing_address_id_foreign": { + "constraintName": "order_billing_address_id_foreign", + "columnNames": ["billing_address_id"], + "localTableName": "public.order", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order_address", + "deleteRule": "set null", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "order_id": { + "name": "order_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "description": { + "name": "description", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "status": { + "name": "status", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "'pending'", + "enumItems": [ + "confirmed", + "declined", + "requested", + "pending", + "canceled" + ], + "mappedType": "enum" + }, + "internal_note": { + "name": "internal_note", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_by": { + "name": "created_by", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "requested_by": { + "name": "requested_by", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "requested_at": { + "name": "requested_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "confirmed_by": { + "name": "confirmed_by", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "confirmed_at": { + "name": "confirmed_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "declined_by": { + "name": "declined_by", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "declined_reason": { + "name": "declined_reason", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "declined_at": { + "name": "declined_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "canceled_by": { + "name": "canceled_by", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "canceled_at": { + "name": "canceled_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + } + }, + "name": "order_change", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_order_change_order_id", + "columnNames": ["order_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_change_order_id\" ON \"order_change\" (order_id)" + }, + { + "keyName": "IDX_order_change_status", + "columnNames": ["status"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_change_status\" ON \"order_change\" (status)" + }, + { + "keyName": "order_change_pkey", + "columnNames": ["id"], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "order_change_order_id_foreign": { + "constraintName": "order_change_order_id_foreign", + "columnNames": ["order_id"], + "localTableName": "public.order_change", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "order_change_id": { + "name": "order_change_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "reference": { + "name": "reference", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "reference_id": { + "name": "reference_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "action": { + "name": "action", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "internal_note": { + "name": "internal_note", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + } + }, + "name": "order_change_action", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_order_change_action_order_change_id", + "columnNames": ["order_change_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_change_action_order_change_id\" ON \"order_change_action\" (order_change_id)" + }, + { + "keyName": "IDX_order_change_action_reference_id", + "columnNames": ["reference_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_change_action_reference_id\" ON \"order_change_action\" (reference_id)" + }, + { + "keyName": "order_change_action_pkey", + "columnNames": ["id"], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "order_change_action_order_change_id_foreign": { + "constraintName": "order_change_action_order_change_id_foreign", + "columnNames": ["order_change_id"], + "localTableName": "public.order_change_action", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order_change", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "order_id": { + "name": "order_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "version": { + "name": "version", + "type": "integer", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "integer" + }, + "item_id": { + "name": "item_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "quantity": { + "name": "quantity", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_quantity": { + "name": "raw_quantity", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "fulfilled_quantity": { + "name": "fulfilled_quantity", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_fulfilled_quantity": { + "name": "raw_fulfilled_quantity", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "shipped_quantity": { + "name": "shipped_quantity", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_shipped_quantity": { + "name": "raw_shipped_quantity", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "return_requested_quantity": { + "name": "return_requested_quantity", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_return_requested_quantity": { + "name": "raw_return_requested_quantity", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "return_received_quantity": { + "name": "return_received_quantity", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_return_received_quantity": { + "name": "raw_return_received_quantity", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "return_dismissed_quantity": { + "name": "return_dismissed_quantity", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_return_dismissed_quantity": { + "name": "raw_return_dismissed_quantity", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "written_off_quantity": { + "name": "written_off_quantity", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_written_off_quantity": { + "name": "raw_written_off_quantity", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "summary": { + "name": "summary", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + } + }, + "name": "order_detail", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_order_detail_order_id_item_id_version", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_detail_order_id_item_id_version\" ON \"order_detail\" (order_id, item_id, version)" + }, + { + "keyName": "order_detail_pkey", + "columnNames": ["id"], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "order_detail_order_id_foreign": { + "constraintName": "order_detail_order_id_foreign", + "columnNames": ["order_id"], + "localTableName": "public.order_detail", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order", + "deleteRule": "cascade", + "updateRule": "cascade" + }, + "order_detail_item_id_foreign": { + "constraintName": "order_detail_item_id_foreign", + "columnNames": ["item_id"], + "localTableName": "public.order_detail", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order_line_item", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "totals_id": { + "name": "totals_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "title": { + "name": "title", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "subtitle": { + "name": "subtitle", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "thumbnail": { + "name": "thumbnail", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "variant_id": { + "name": "variant_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_id": { + "name": "product_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_title": { + "name": "product_title", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_description": { + "name": "product_description", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_subtitle": { + "name": "product_subtitle", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_type": { + "name": "product_type", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "product_collection": { + "name": "product_collection", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "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", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "variant_barcode": { + "name": "variant_barcode", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "variant_title": { + "name": "variant_title", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "variant_option_values": { + "name": "variant_option_values", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "requires_shipping": { + "name": "requires_shipping", + "type": "boolean", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "true", + "mappedType": "boolean" + }, + "is_discountable": { + "name": "is_discountable", + "type": "boolean", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "true", + "mappedType": "boolean" + }, + "is_tax_inclusive": { + "name": "is_tax_inclusive", + "type": "boolean", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "false", + "mappedType": "boolean" + }, + "compare_at_unit_price": { + "name": "compare_at_unit_price", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "decimal" + }, + "raw_compare_at_unit_price": { + "name": "raw_compare_at_unit_price", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "unit_price": { + "name": "unit_price", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_unit_price": { + "name": "raw_unit_price", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + } + }, + "name": "order_line_item", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_order_line_item_variant_id", + "columnNames": ["variant_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_line_item_variant_id\" ON \"order_line_item\" (variant_id)" + }, + { + "keyName": "IDX_order_line_item_product_id", + "columnNames": ["product_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_line_item_product_id\" ON \"order_line_item\" (product_id)" + }, + { + "keyName": "order_line_item_pkey", + "columnNames": ["id"], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "order_line_item_totals_id_foreign": { + "constraintName": "order_line_item_totals_id_foreign", + "columnNames": ["totals_id"], + "localTableName": "public.order_line_item", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order_detail", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "description": { + "name": "description", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "tax_rate_id": { + "name": "tax_rate_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "code": { + "name": "code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "rate": { + "name": "rate", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_rate": { + "name": "raw_rate", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "item_id": { + "name": "item_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + } + }, + "name": "order_line_item_tax_line", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_order_line_item_tax_line_item_id", + "columnNames": ["item_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_line_item_tax_line_item_id\" ON \"order_line_item_tax_line\" (item_id)" + }, + { + "keyName": "order_line_item_tax_line_pkey", + "columnNames": ["id"], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "order_line_item_tax_line_item_id_foreign": { + "constraintName": "order_line_item_tax_line_item_id_foreign", + "columnNames": ["item_id"], + "localTableName": "public.order_line_item_tax_line", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order_line_item", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "description": { + "name": "description", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "promotion_id": { + "name": "promotion_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "code": { + "name": "code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_amount": { + "name": "raw_amount", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "item_id": { + "name": "item_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + } + }, + "name": "order_line_item_adjustment", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_order_line_item_adjustment_item_id", + "columnNames": ["item_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_line_item_adjustment_item_id\" ON \"order_line_item_adjustment\" (item_id)" + }, + { + "keyName": "order_line_item_adjustment_pkey", + "columnNames": ["id"], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [ + { + "name": "order_line_item_adjustment_check", + "expression": "amount >= 0", + "definition": "check ((amount >= 0))" + } + ], + "foreignKeys": { + "order_line_item_adjustment_item_id_foreign": { + "constraintName": "order_line_item_adjustment_item_id_foreign", + "columnNames": ["item_id"], + "localTableName": "public.order_line_item_adjustment", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order_line_item", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "order_id": { + "name": "order_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "name": { + "name": "name", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "description": { + "name": "description", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "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", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "false", + "mappedType": "boolean" + }, + "shipping_option_id": { + "name": "shipping_option_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "data": { + "name": "data", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + } + }, + "name": "order_shipping_method", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_order_shipping_method_order_id", + "columnNames": ["order_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_method_order_id\" ON \"order_shipping_method\" (order_id)" + }, + { + "keyName": "IDX_order_shipping_method_shipping_option_id", + "columnNames": ["shipping_option_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_method_shipping_option_id\" ON \"order_shipping_method\" (shipping_option_id)" + }, + { + "keyName": "order_shipping_method_pkey", + "columnNames": ["id"], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [ + { + "name": "order_shipping_method_check", + "expression": "amount >= 0", + "definition": "check ((amount >= 0))" + } + ], + "foreignKeys": { + "order_shipping_method_order_id_foreign": { + "constraintName": "order_shipping_method_order_id_foreign", + "columnNames": ["order_id"], + "localTableName": "public.order_shipping_method", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "description": { + "name": "description", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "promotion_id": { + "name": "promotion_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "code": { + "name": "code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_amount": { + "name": "raw_amount", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "shipping_method_id": { + "name": "shipping_method_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + } + }, + "name": "order_shipping_method_adjustment", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_order_shipping_method_adjustment_shipping_method_id", + "columnNames": ["shipping_method_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_method_adjustment_shipping_method_id\" ON \"order_shipping_method_adjustment\" (shipping_method_id)" + }, + { + "keyName": "order_shipping_method_adjustment_pkey", + "columnNames": ["id"], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "order_shipping_method_adjustment_shipping_method_id_foreign": { + "constraintName": "order_shipping_method_adjustment_shipping_method_id_foreign", + "columnNames": ["shipping_method_id"], + "localTableName": "public.order_shipping_method_adjustment", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order_shipping_method", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "description": { + "name": "description", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "tax_rate_id": { + "name": "tax_rate_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "code": { + "name": "code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "rate": { + "name": "rate", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_rate": { + "name": "raw_rate", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "shipping_method_id": { + "name": "shipping_method_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + } + }, + "name": "order_shipping_method_tax_line", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_order_shipping_method_tax_line_shipping_method_id", + "columnNames": ["shipping_method_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_method_tax_line_shipping_method_id\" ON \"order_shipping_method_tax_line\" (shipping_method_id)" + }, + { + "keyName": "order_shipping_method_tax_line_pkey", + "columnNames": ["id"], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "order_shipping_method_tax_line_shipping_method_id_foreign": { + "constraintName": "order_shipping_method_tax_line_shipping_method_id_foreign", + "columnNames": ["shipping_method_id"], + "localTableName": "public.order_shipping_method_tax_line", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order_shipping_method", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "order_id": { + "name": "order_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "raw_amount": { + "name": "raw_amount", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, + "currency_code": { + "name": "currency_code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "reference": { + "name": "reference", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "reference_id": { + "name": "reference_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + } + }, + "name": "order_transaction", + "schema": "public", + "indexes": [ + { + "keyName": "IDX_order_transaction_order_id", + "columnNames": ["order_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_transaction_order_id\" ON \"order_transaction\" (order_id)" + }, + { + "keyName": "IDX_order_transaction_currency_code", + "columnNames": ["currency_code"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_transaction_currency_code\" ON \"order_transaction\" (currency_code)" + }, + { + "keyName": "IDX_order_transaction_reference_id", + "columnNames": ["reference_id"], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_transaction_reference_id\" ON \"order_transaction\" (reference_id)" + }, + { + "keyName": "order_transaction_pkey", + "columnNames": ["id"], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "order_transaction_order_id_foreign": { + "constraintName": "order_transaction_order_id_foreign", + "columnNames": ["order_id"], + "localTableName": "public.order_transaction", + "referencedColumnNames": ["id"], + "referencedTableName": "public.order", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + } + ] +} diff --git a/packages/order/src/migrations/Migration20240219102530.ts b/packages/order/src/migrations/Migration20240219102530.ts new file mode 100644 index 0000000000000..2ae6791b5fa33 --- /dev/null +++ b/packages/order/src/migrations/Migration20240219102530.ts @@ -0,0 +1,460 @@ +import { generatePostgresAlterColummnIfExistStatement } from "@medusajs/utils" +import { Migration } from "@mikro-orm/migrations" + +export class Migration20240219102530 extends Migration { + async up(): Promise { + const sql = ` + CREATE TABLE IF NOT EXISTS "order_address" ( + "id" TEXT NOT NULL, + "customer_id" TEXT NULL, + "company" TEXT NULL, + "first_name" TEXT NULL, + "last_name" TEXT NULL, + "address_1" TEXT NULL, + "address_2" TEXT NULL, + "city" TEXT NULL, + "country_code" TEXT NULL, + "province" TEXT NULL, + "postal_code" TEXT NULL, + "phone" TEXT NULL, + "metadata" JSONB NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + CONSTRAINT "order_address_pkey" PRIMARY KEY ("id") + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_address_customer_id" ON "order_address" ( + customer_id + ); + + CREATE TABLE IF NOT EXISTS "order" ( + "id" TEXT NOT NULL, + "region_id" TEXT NULL, + "customer_id" TEXT NULL, + "original_order_id" TEXT NULL, + "version" INTEGER NOT NULL DEFAULT 1, + "sales_channel_id" TEXT NULL, + "status" text check ( + "status" IN ( + 'pending', + 'completed', + 'draft', + 'archived', + 'canceled', + 'requires_action' + ) + ) NOT NULL DEFAULT 'pending', + "email" text NULL, + "currency_code" text NOT NULL, + "shipping_address_id" text NULL, + "billing_address_id" text NULL, + "no_notification" boolean NULL, + "summary" jsonb NOT NULL, + "metadata" jsonb NULL, + "created_at" timestamptz NOT NULL DEFAULT now(), + "updated_at" timestamptz NOT NULL DEFAULT now(), + "deleted_at" timestamptz NULL, + "canceled_at" timestamptz NULL, + CONSTRAINT "order_pkey" PRIMARY KEY ("id") + ); + + ALTER TABLE "order" + ADD COLUMN if NOT exists "deleted_at" timestamptz NULL; + + ALTER TABLE "order" + ADD COLUMN if NOT exists "original_order_id" text NULL; + + ALTER TABLE "order" DROP constraint if EXISTS "FK_6ff7e874f01b478c115fdd462eb" CASCADE; + + ALTER TABLE "order" DROP constraint if EXISTS "FK_19b0c6293443d1b464f604c3316" CASCADE; + + ALTER TABLE "order" DROP constraint if EXISTS "FK_717a141f96b76d794d409f38129" CASCADE; + + ALTER TABLE "order" DROP constraint if EXISTS "FK_727b872f86c7378474a8fa46147" CASCADE; + + ALTER TABLE "order" DROP constraint if EXISTS "FK_5568d3b9ce9f7abeeb37511ecf2" CASCADE; + + ALTER TABLE "order" DROP constraint if EXISTS "FK_c99a206eb11ad45f6b7f04f2dcc" CASCADE; + + ALTER TABLE "order" DROP constraint if EXISTS "FK_cd7812c96209c5bdd48a6b858b0" CASCADE; + + ALTER TABLE "order" DROP constraint if EXISTS "FK_e1fcce2b18dbcdbe0a5ba9a68b8" CASCADE; + + ALTER TABLE "order" DROP constraint if EXISTS "REL_c99a206eb11ad45f6b7f04f2dc" CASCADE; + + ALTER TABLE "order" DROP constraint if EXISTS "UQ_727b872f86c7378474a8fa46147" CASCADE; + + DROP INDEX if exists "IDX_19b0c6293443d1b464f604c331"; + + DROP INDEX if exists "IDX_579e01fb94f4f58db480857e05"; + + DROP INDEX if exists "IDX_5568d3b9ce9f7abeeb37511ecf"; + + DROP INDEX if exists "IDX_c99a206eb11ad45f6b7f04f2dc"; + + DROP INDEX if exists "IDX_cd7812c96209c5bdd48a6b858b"; + + DROP INDEX if exists "IDX_e1fcce2b18dbcdbe0a5ba9a68b"; + + ${generatePostgresAlterColummnIfExistStatement( + "order", + ["fulfillment_status", "payment_status", "display_id"], + "DROP NOT NULL" + )} + + CREATE INDEX IF NOT EXISTS "IDX_order_region_id" ON "order" ( + region_id + ) + WHERE deleted_at IS NOT NULL; + + CREATE INDEX IF NOT EXISTS "IDX_order_customer_id" ON "order" ( + customer_id + ) + WHERE deleted_at IS NOT NULL; + + CREATE INDEX IF NOT EXISTS "IDX_order_original_order_id" ON "order" ( + original_order_id + ) + WHERE deleted_at IS NOT NULL; + + CREATE INDEX IF NOT EXISTS "IDX_order_customer_id" ON "order" ( + customer_id + ) + WHERE deleted_at IS NOT NULL; + + CREATE INDEX IF NOT EXISTS "IDX_order_currency_code" ON "order" ( + currency_code + ) + WHERE deleted_at IS NOT NULL; + + CREATE INDEX IF NOT EXISTS "IDX_order_shipping_address_id" ON "order" ( + shipping_address_id + ) + WHERE deleted_at IS NOT NULL; + + CREATE INDEX IF NOT EXISTS "IDX_order_billing_address_id" ON "order" ( + billing_address_id + ) + WHERE deleted_at IS NOT NULL; + + CREATE INDEX IF NOT EXISTS "IDX_order_deleted_at" ON "order" ( + deleted_at + ) + WHERE deleted_at IS NOT NULL; + + CREATE TABLE IF NOT EXISTS "order_change" ( + "id" TEXT NOT NULL, + "order_id" TEXT NOT NULL, + "description" TEXT NULL, + "status" text check ( + "status" IN ( + 'confirmed', + 'declined', + 'requested', + 'pending', + 'canceled' + ) + ) NOT NULL DEFAULT 'pending', + "internal_note" text NULL, + "created_by" text NOT NULL, + "requested_by" text NULL, + "requested_at" timestamptz NULL, + "confirmed_by" text NULL, + "confirmed_at" timestamptz NULL, + "declined_by" text NULL, + "declined_reason" text NULL, + "metadata" jsonb NULL, + "declined_at" timestamptz NULL, + "canceled_by" text NULL, + "canceled_at" timestamptz NULL, + "created_at" timestamptz NOT NULL DEFAULT now(), + "updated_at" timestamptz NOT NULL DEFAULT now(), + CONSTRAINT "order_change_pkey" PRIMARY KEY ("id") + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_change_order_id" ON "order_change" ( + order_id + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_change_status" ON "order_change" (status); + + CREATE TABLE IF NOT EXISTS "order_change_action" ( + "id" TEXT NOT NULL, + "order_change_id" TEXT NOT NULL, + "reference" TEXT NOT NULL, + "reference_id" TEXT NOT NULL, + "action" JSONB NOT NULL, + "metadata" JSONB NULL, + "internal_note" TEXT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + CONSTRAINT "order_change_action_pkey" PRIMARY KEY ("id") + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_change_action_order_change_id" ON "order_change_action" ( + order_change_id + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_change_action_reference_id" ON "order_change_action" ( + reference_id + ); + + CREATE TABLE IF NOT EXISTS "order_detail" ( + "id" TEXT NOT NULL, + "order_id" TEXT NOT NULL, + "version" INTEGER NOT NULL, + "item_id" TEXT NOT NULL, + "quantity" NUMERIC NOT NULL, + "raw_quantity" JSONB NOT NULL, + "fulfilled_quantity" NUMERIC NOT NULL, + "raw_fulfilled_quantity" JSONB NOT NULL, + "shipped_quantity" NUMERIC NOT NULL, + "raw_shipped_quantity" JSONB NOT NULL, + "return_requested_quantity" NUMERIC NOT NULL, + "raw_return_requested_quantity" JSONB NOT NULL, + "return_received_quantity" NUMERIC NOT NULL, + "raw_return_received_quantity" JSONB NOT NULL, + "return_dismissed_quantity" NUMERIC NOT NULL, + "raw_return_dismissed_quantity" JSONB NOT NULL, + "written_off_quantity" NUMERIC NOT NULL, + "raw_written_off_quantity" JSONB NOT NULL, + "summary" JSONB NOT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + CONSTRAINT "order_detail_pkey" PRIMARY KEY ("id") + ); + + CREATE UNIQUE INDEX IF NOT EXISTS "IDX_order_detail_order_id_item_id_version" ON "order_detail" ( + order_id, + item_id, + version + ); + + CREATE TABLE IF NOT EXISTS "order_line_item" ( + "id" TEXT NOT NULL, + "totals_id" TEXT NULL, + "title" TEXT NOT NULL, + "subtitle" TEXT NULL, + "thumbnail" TEXT NULL, + "variant_id" TEXT NULL, + "product_id" TEXT NULL, + "product_title" TEXT NULL, + "product_description" TEXT NULL, + "product_subtitle" TEXT NULL, + "product_type" TEXT NULL, + "product_collection" TEXT NULL, + "product_handle" TEXT NULL, + "variant_sku" TEXT NULL, + "variant_barcode" TEXT NULL, + "variant_title" TEXT NULL, + "variant_option_values" JSONB NULL, + "requires_shipping" BOOLEAN NOT NULL DEFAULT true, + "is_discountable" BOOLEAN NOT NULL DEFAULT true, + "is_tax_inclusive" BOOLEAN NOT NULL DEFAULT false, + "compare_at_unit_price" NUMERIC NULL, + "raw_compare_at_unit_price" JSONB NULL, + "unit_price" NUMERIC NOT NULL, + "raw_unit_price" JSONB NOT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + CONSTRAINT "order_line_item_pkey" PRIMARY KEY ("id") + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_line_item_variant_id" ON "order_line_item" ( + variant_id + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_line_item_product_id" ON "order_line_item" ( + product_id + ); + + CREATE TABLE IF NOT EXISTS "order_line_item_tax_line" ( + "id" TEXT NOT NULL, + "description" TEXT NULL, + "tax_rate_id" TEXT NULL, + "code" TEXT NOT NULL, + "rate" NUMERIC NOT NULL, + "raw_rate" JSONB NOT NULL, + "provider_id" TEXT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "item_id" TEXT NULL, + CONSTRAINT "order_line_item_tax_line_pkey" PRIMARY KEY ("id") + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_line_item_tax_line_item_id" ON "order_line_item_tax_line" (item_id); + + CREATE TABLE IF NOT EXISTS "order_line_item_adjustment" ( + "id" TEXT NOT NULL, + "description" TEXT NULL, + "promotion_id" TEXT NULL, + "code" TEXT NULL, + "amount" NUMERIC NOT NULL, + "raw_amount" JSONB NOT NULL, + "provider_id" TEXT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "item_id" TEXT NULL, + CONSTRAINT "order_line_item_adjustment_pkey" PRIMARY KEY ("id"), + CONSTRAINT order_line_item_adjustment_check CHECK (amount >= 0) + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_line_item_adjustment_item_id" ON "order_line_item_adjustment" (item_id); + + CREATE TABLE IF NOT EXISTS "order_shipping_method" ( + "id" TEXT NOT NULL, + "order_id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "description" JSONB NULL, + "amount" NUMERIC NOT NULL, + "raw_amount" JSONB NOT NULL, + "is_tax_inclusive" BOOLEAN NOT NULL DEFAULT false, + "shipping_option_id" TEXT NULL, + "data" JSONB NULL, + "metadata" JSONB NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + CONSTRAINT "order_shipping_method_pkey" PRIMARY KEY ("id"), + CONSTRAINT order_shipping_method_check CHECK (amount >= 0) + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_shipping_method_order_id" ON "order_shipping_method" ( + order_id + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_shipping_method_shipping_option_id" ON "order_shipping_method" ( + shipping_option_id + ); + + CREATE TABLE IF NOT EXISTS "order_shipping_method_adjustment" ( + "id" TEXT NOT NULL, + "description" TEXT NULL, + "promotion_id" TEXT NULL, + "code" TEXT NULL, + "amount" NUMERIC NOT NULL, + "raw_amount" JSONB NOT NULL, + "provider_id" TEXT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "shipping_method_id" TEXT NULL, + CONSTRAINT "order_shipping_method_adjustment_pkey" PRIMARY KEY ("id") + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_shipping_method_adjustment_shipping_method_id" ON "order_shipping_method_adjustment" ( + shipping_method_id + ); + + CREATE TABLE IF NOT EXISTS "order_shipping_method_tax_line" ( + "id" TEXT NOT NULL, + "description" TEXT NULL, + "tax_rate_id" TEXT NULL, + "code" TEXT NOT NULL, + "rate" NUMERIC NOT NULL, + "raw_rate" JSONB NOT NULL, + "provider_id" TEXT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "shipping_method_id" TEXT NULL, + CONSTRAINT "order_shipping_method_tax_line_pkey" PRIMARY KEY ("id") + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_shipping_method_tax_line_shipping_method_id" ON "order_shipping_method_tax_line" ( + shipping_method_id + ); + + CREATE TABLE IF NOT EXISTS "order_transaction" ( + "id" TEXT NOT NULL, + "order_id" TEXT NOT NULL, + "amount" NUMERIC NOT NULL, + "raw_amount" JSONB NOT NULL, + "currency_code" TEXT NOT NULL, + "reference" TEXT NULL, + "reference_id" TEXT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + "updated_at" TIMESTAMPTZ NOT NULL DEFAULT Now(), + CONSTRAINT "order_transaction_pkey" PRIMARY KEY ("id") + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_transaction_order_id" ON "order_transaction" ( + order_id + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_transaction_currency_code" ON "order_transaction" ( + currency_code + ); + + CREATE INDEX IF NOT EXISTS "IDX_order_transaction_reference_id" ON "order_transaction" ( + reference_id + ); + + ALTER TABLE if exists "order" + ADD CONSTRAINT "order_shipping_address_id_foreign" FOREIGN KEY ("shipping_address_id") REFERENCES "order_address" ("id") ON + UPDATE CASCADE ON + DELETE + SET NULL; + + ALTER TABLE if exists "order" + ADD CONSTRAINT "order_billing_address_id_foreign" FOREIGN KEY ("billing_address_id") REFERENCES "order_address" ("id") ON + UPDATE CASCADE ON + DELETE + SET NULL; + + ALTER TABLE if exists "order_change" + ADD CONSTRAINT "order_change_order_id_foreign" FOREIGN KEY ("order_id") REFERENCES "order" ("id") ON + UPDATE CASCADE ON + DELETE CASCADE; + + ALTER TABLE if exists "order_change_action" + ADD CONSTRAINT "order_change_action_order_change_id_foreign" FOREIGN KEY ("order_change_id") REFERENCES "order_change" ("id") ON + UPDATE CASCADE ON + DELETE CASCADE; + + ALTER TABLE if exists "order_detail" + ADD CONSTRAINT "order_detail_order_id_foreign" FOREIGN KEY ("order_id") REFERENCES "order" ("id") ON + UPDATE CASCADE ON + DELETE CASCADE; + + ALTER TABLE if exists "order_detail" + ADD CONSTRAINT "order_detail_item_id_foreign" FOREIGN KEY ("item_id") REFERENCES "order_line_item" ("id") ON + UPDATE CASCADE ON + DELETE CASCADE; + + ALTER TABLE if exists "order_line_item" + ADD CONSTRAINT "order_line_item_totals_id_foreign" FOREIGN KEY ("totals_id") REFERENCES "order_detail" ("id") ON + UPDATE CASCADE ON + DELETE CASCADE; + + ALTER TABLE if exists "order_line_item_tax_line" + ADD CONSTRAINT "order_line_item_tax_line_item_id_foreign" FOREIGN KEY ("item_id") REFERENCES "order_line_item" ("id") ON + UPDATE CASCADE ON + DELETE CASCADE; + + ALTER TABLE if exists "order_line_item_adjustment" + ADD CONSTRAINT "order_line_item_adjustment_item_id_foreign" FOREIGN KEY ("item_id") REFERENCES "order_line_item" ("id") ON + UPDATE CASCADE ON + DELETE CASCADE; + + ALTER TABLE if exists "order_shipping_method" + ADD CONSTRAINT "order_shipping_method_order_id_foreign" FOREIGN KEY ("order_id") REFERENCES "order" ("id") ON + UPDATE CASCADE ON + DELETE CASCADE; + + ALTER TABLE if exists "order_shipping_method_adjustment" + ADD CONSTRAINT "order_shipping_method_adjustment_shipping_method_id_foreign" FOREIGN KEY ("shipping_method_id") REFERENCES "order_shipping_method" ("id") ON + UPDATE CASCADE ON + DELETE CASCADE; + + ALTER TABLE if exists "order_shipping_method_tax_line" + ADD CONSTRAINT "order_shipping_method_tax_line_shipping_method_id_foreign" FOREIGN KEY ("shipping_method_id") REFERENCES "order_shipping_method" ("id") ON + UPDATE CASCADE ON + DELETE CASCADE; + + ALTER TABLE if exists "order_transaction" + ADD CONSTRAINT "order_transaction_order_id_foreign" FOREIGN KEY ("order_id") REFERENCES "order" ("id") ON + UPDATE CASCADE ON + DELETE CASCADE; + ` + + this.addSql(sql) + } +} diff --git a/packages/order/src/models/address.ts b/packages/order/src/models/address.ts index d2a4fdc9a8507..9b6e3d743ddf0 100644 --- a/packages/order/src/models/address.ts +++ b/packages/order/src/models/address.ts @@ -14,7 +14,7 @@ import { type OptionalAddressProps = DAL.EntityDateColumns -const customerIdIndex = createPsqlIndexStatementHelper({ +const CustomerIdIndex = createPsqlIndexStatementHelper({ tableName: "order_address", columns: "customer_id", }) @@ -27,7 +27,7 @@ export default class Address { id!: string @Property({ columnType: "text", nullable: true }) - @customerIdIndex.MikroORMIndex() + @CustomerIdIndex.MikroORMIndex() customer_id: string | null = null @Property({ columnType: "text", nullable: true }) diff --git a/packages/order/src/models/adjustment-line.ts b/packages/order/src/models/adjustment-line.ts index 8e117eb208422..6c988f912c00b 100644 --- a/packages/order/src/models/adjustment-line.ts +++ b/packages/order/src/models/adjustment-line.ts @@ -1,5 +1,5 @@ import { BigNumberRawValue, DAL } from "@medusajs/types" -import { BigNumber } from "@medusajs/utils" +import { BigNumber, BigNumberField } from "@medusajs/utils" import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core" type OptionalAdjustmentLineProps = DAL.EntityDateColumns @@ -27,6 +27,7 @@ export default abstract class AdjustmentLine { code: string | null = null @Property({ columnType: "numeric" }) + @BigNumberField() amount: BigNumber | number @Property({ columnType: "jsonb" }) diff --git a/packages/order/src/models/index.ts b/packages/order/src/models/index.ts index 4a8a606bfc6ab..a9cb844508bb6 100644 --- a/packages/order/src/models/index.ts +++ b/packages/order/src/models/index.ts @@ -3,6 +3,10 @@ export { default as LineItem } from "./line-item" export { default as LineItemAdjustment } from "./line-item-adjustment" export { default as LineItemTaxLine } from "./line-item-tax-line" export { default as Order } from "./order" +export { default as OrderChange } from "./order-change" +export { default as OrderChangeAction } from "./order-change-action" +export { default as OrderDetail } from "./order-detail" export { default as ShippingMethod } from "./shipping-method" export { default as ShippingMethodAdjustment } from "./shipping-method-adjustment" export { default as ShippingMethodTaxLine } from "./shipping-method-tax-line" +export { default as Transaction } from "./transaction" diff --git a/packages/order/src/models/line-item-adjustment.ts b/packages/order/src/models/line-item-adjustment.ts index bb40a89ce1b69..409550342fb53 100644 --- a/packages/order/src/models/line-item-adjustment.ts +++ b/packages/order/src/models/line-item-adjustment.ts @@ -1,4 +1,7 @@ -import { generateEntityId } from "@medusajs/utils" +import { + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, @@ -11,6 +14,11 @@ import { import AdjustmentLine from "./adjustment-line" import LineItem from "./line-item" +const ItemIdIndex = createPsqlIndexStatementHelper({ + tableName: "order_line_item_adjustment", + columns: "item_id", +}) + @Entity({ tableName: "order_line_item_adjustment" }) @Check({ expression: (columns) => `${columns.amount} >= 0`, @@ -18,12 +26,12 @@ import LineItem from "./line-item" export default class LineItemAdjustment extends AdjustmentLine { @ManyToOne({ entity: () => LineItem, - index: "IDX_order_line_item_adjustment_item_id", cascade: [Cascade.REMOVE, Cascade.PERSIST], }) item: LineItem @Property({ columnType: "text" }) + @ItemIdIndex.MikroORMIndex() item_id: string @BeforeCreate() diff --git a/packages/order/src/models/line-item-tax-line.ts b/packages/order/src/models/line-item-tax-line.ts index 52a81dd9036a2..eab06b87046de 100644 --- a/packages/order/src/models/line-item-tax-line.ts +++ b/packages/order/src/models/line-item-tax-line.ts @@ -1,4 +1,7 @@ -import { generateEntityId } from "@medusajs/utils" +import { + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, @@ -10,16 +13,21 @@ import { import LineItem from "./line-item" import TaxLine from "./tax-line" +const ItemIdIndex = createPsqlIndexStatementHelper({ + tableName: "order_line_item_tax_line", + columns: "item_id", +}) + @Entity({ tableName: "order_line_item_tax_line" }) export default class LineItemTaxLine extends TaxLine { @ManyToOne({ entity: () => LineItem, - index: "IDX_order_line_item_tax_line_item_id", cascade: [Cascade.REMOVE, Cascade.PERSIST], }) item: LineItem @Property({ columnType: "text" }) + @ItemIdIndex.MikroORMIndex() item_id: string @BeforeCreate() diff --git a/packages/order/src/models/line-item.ts b/packages/order/src/models/line-item.ts index 551262dc029e6..91da94c8ebfa5 100644 --- a/packages/order/src/models/line-item.ts +++ b/packages/order/src/models/line-item.ts @@ -1,12 +1,12 @@ import { BigNumberRawValue, DAL } from "@medusajs/types" import { BigNumber, + BigNumberField, createPsqlIndexStatementHelper, generateEntityId, } from "@medusajs/utils" import { BeforeCreate, - BeforeUpdate, Cascade, Collection, Entity, @@ -19,22 +19,21 @@ import { } from "@mikro-orm/core" import LineItemAdjustment from "./line-item-adjustment" import LineItemTaxLine from "./line-item-tax-line" -import Order from "./order" +import OrderDetail from "./order-detail" type OptionalLineItemProps = | "is_discoutable" | "is_tax_inclusive" | "compare_at_unit_price" | "requires_shipping" - | "order" | DAL.EntityDateColumns -const productIdIndex = createPsqlIndexStatementHelper({ +const ProductIdIndex = createPsqlIndexStatementHelper({ tableName: "order_line_item", columns: "product_id", }) -const variantIdIndex = createPsqlIndexStatementHelper({ +const VariantIdIndex = createPsqlIndexStatementHelper({ tableName: "order_line_item", columns: "variant_id", }) @@ -46,16 +45,12 @@ export default class LineItem { @PrimaryKey({ columnType: "text" }) id: string - @Property({ columnType: "text" }) - order_id: string - @ManyToOne({ - entity: () => Order, + entity: () => OrderDetail, onDelete: "cascade", - index: "IDX_order_line_item_order_id", cascade: [Cascade.REMOVE, Cascade.PERSIST], }) - order: Order + totals: OrderDetail @Property({ columnType: "text" }) title: string @@ -66,24 +61,18 @@ export default class LineItem { @Property({ columnType: "text", nullable: true }) thumbnail: string | null = null - @Property({ columnType: "numeric" }) - quantity: BigNumber | number - - @Property({ columnType: "jsonb" }) - raw_quantity: BigNumberRawValue - @Property({ columnType: "text", nullable: true, }) - @variantIdIndex.MikroORMIndex() + @VariantIdIndex.MikroORMIndex() variant_id: string | null = null @Property({ columnType: "text", nullable: true, }) - @productIdIndex.MikroORMIndex() + @ProductIdIndex.MikroORMIndex() product_id: string | null = null @Property({ columnType: "text", nullable: true }) @@ -126,12 +115,14 @@ export default class LineItem { is_tax_inclusive = false @Property({ columnType: "numeric", nullable: true }) + @BigNumberField({ nullable: true }) compare_at_unit_price?: BigNumber | number | null = null @Property({ columnType: "jsonb", nullable: true }) raw_compare_at_unit_price: BigNumberRawValue | null = null @Property({ columnType: "numeric" }) + @BigNumberField() unit_price: BigNumber | number @Property({ columnType: "jsonb" }) @@ -165,28 +156,10 @@ export default class LineItem { @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "ordli") - - const val = new BigNumber(this.raw_unit_price ?? this.unit_price) - - this.unit_price = val.numeric - this.raw_unit_price = val.raw! - } - - @BeforeUpdate() - onUpdate() { - const val = new BigNumber(this.raw_unit_price ?? this.unit_price) - - this.unit_price = val.numeric - this.raw_unit_price = val.raw as BigNumberRawValue } @OnInit() onInit() { this.id = generateEntityId(this.id, "ordli") - - const val = new BigNumber(this.raw_unit_price ?? this.unit_price) - - this.unit_price = val.numeric - this.raw_unit_price = val.raw! } } diff --git a/packages/order/src/models/order-change-action.ts b/packages/order/src/models/order-change-action.ts new file mode 100644 index 0000000000000..373e32fc67cd5 --- /dev/null +++ b/packages/order/src/models/order-change-action.ts @@ -0,0 +1,91 @@ +import { DAL } from "@medusajs/types" +import { + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" +import { + BeforeCreate, + Cascade, + Entity, + ManyToOne, + OnInit, + OptionalProps, + PrimaryKey, + Property, +} from "@mikro-orm/core" +import OrderChange from "./order-change" + +type OptionalLineItemProps = DAL.EntityDateColumns + +const OrderChangeIdIndex = createPsqlIndexStatementHelper({ + tableName: "order_change_action", + columns: "order_change_id", +}) + +const ReferenceIdIndex = createPsqlIndexStatementHelper({ + tableName: "order_change_action", + columns: "reference_id", +}) + +@Entity({ tableName: "order_change_action" }) +export default class OrderChangeAction { + [OptionalProps]?: OptionalLineItemProps + + @PrimaryKey({ columnType: "text" }) + id: string + + @Property({ columnType: "text" }) + @OrderChangeIdIndex.MikroORMIndex() + order_change_id: string + + @ManyToOne({ + entity: () => OrderChange, + fieldName: "order_change_id", + cascade: [Cascade.REMOVE, Cascade.PERSIST], + }) + order_change: OrderChange + + @Property({ columnType: "text" }) + reference: string + + @Property({ columnType: "text" }) + @ReferenceIdIndex.MikroORMIndex() + reference_id: string + + @Property({ columnType: "jsonb" }) + action: Record = {} + + @Property({ columnType: "jsonb", nullable: true }) + metadata: Record | null = null + + @Property({ + columnType: "text", + nullable: true, + }) + internal_note: string | 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 + + @BeforeCreate() + onCreate() { + this.id = generateEntityId(this.id, "ordchact") + } + + @OnInit() + onInit() { + this.id = generateEntityId(this.id, "ordchact") + } +} diff --git a/packages/order/src/models/order-change.ts b/packages/order/src/models/order-change.ts new file mode 100644 index 0000000000000..97c64313bb1fe --- /dev/null +++ b/packages/order/src/models/order-change.ts @@ -0,0 +1,140 @@ +import { DAL } from "@medusajs/types" +import { + OrderChangeStatus, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" +import { + BeforeCreate, + Cascade, + Collection, + Entity, + Enum, + ManyToOne, + OnInit, + OptionalProps, + PrimaryKey, + Property, +} from "@mikro-orm/core" +import { OneToMany } from "typeorm" +import Order from "./order" +import OrderChangeAction from "./order-change-action" + +type OptionalLineItemProps = DAL.EntityDateColumns + +const OrderIdIndex = createPsqlIndexStatementHelper({ + tableName: "order_change", + columns: "order_id", +}) + +const OrderChangeStatusIndex = createPsqlIndexStatementHelper({ + tableName: "order_change", + columns: "status", +}) + +@Entity({ tableName: "order_change" }) +export default class OrderChange { + [OptionalProps]?: OptionalLineItemProps + + @PrimaryKey({ columnType: "text" }) + id: string + + @Property({ columnType: "text" }) + @OrderIdIndex.MikroORMIndex() + order_id: string + + @ManyToOne({ + entity: () => Order, + fieldName: "order_id", + cascade: [Cascade.REMOVE, Cascade.PERSIST], + }) + order: Order + + @OneToMany(() => OrderChangeAction, (action) => action.order_change_id, { + cascade: [Cascade.REMOVE], + }) + actions = new Collection(this) + + @Property({ + columnType: "text", + nullable: true, + }) + description: string | null = null + + @Enum({ items: () => OrderChangeStatus, default: OrderChangeStatus.PENDING }) + @OrderChangeStatusIndex.MikroORMIndex() + status: OrderChangeStatus + + @Property({ columnType: "text", nullable: true }) + internal_note: string | null = null + + @Property({ columnType: "text" }) + created_by: string // customer, user, third party, etc. + + @Property({ columnType: "text", nullable: true }) + requested_by: string | null = null // customer or user ID + + @Property({ + columnType: "timestamptz", + nullable: true, + }) + requested_at?: Date + + @Property({ columnType: "text", nullable: true }) + confirmed_by: string | null = null // customer or user ID + + @Property({ + columnType: "timestamptz", + nullable: true, + }) + confirmed_at?: Date + + @Property({ columnType: "text", nullable: true }) + declined_by: string | null = null // customer or user ID + + @Property({ columnType: "text", nullable: true }) + declined_reason: string | null = null + + @Property({ columnType: "jsonb", nullable: true }) + metadata: Record | null = null + + @Property({ + columnType: "timestamptz", + nullable: true, + }) + declined_at?: Date + + @Property({ columnType: "text", nullable: true }) + canceled_by: string | null = null + + @Property({ + columnType: "timestamptz", + nullable: true, + }) + canceled_at?: Date + + @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 + + @BeforeCreate() + onCreate() { + this.id = generateEntityId(this.id, "ordch") + } + + @OnInit() + onInit() { + this.id = generateEntityId(this.id, "ordch") + } +} diff --git a/packages/order/src/models/order-detail.ts b/packages/order/src/models/order-detail.ts new file mode 100644 index 0000000000000..476bd01a89a20 --- /dev/null +++ b/packages/order/src/models/order-detail.ts @@ -0,0 +1,136 @@ +import { BigNumberRawValue, DAL } from "@medusajs/types" +import { + BigNumber, + BigNumberField, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" +import { + BeforeCreate, + Cascade, + Entity, + ManyToOne, + OnInit, + OptionalProps, + PrimaryKey, + Property, +} from "@mikro-orm/core" +import { ItemSummary } from "../types/common" +import LineItem from "./line-item" +import Order from "./order" + +type OptionalLineItemProps = DAL.EntityDateColumns + +const OrderItemVersionIndex = createPsqlIndexStatementHelper({ + tableName: "order_detail", + columns: ["order_id", "item_id", "version"], + unique: true, +}) + +@Entity({ tableName: "order_detail" }) +@OrderItemVersionIndex.MikroORMIndex() +export default class OrderDetail { + [OptionalProps]?: OptionalLineItemProps + + @PrimaryKey({ columnType: "text" }) + id: string + + @Property({ columnType: "text" }) + order_id: string + + @Property({ columnType: "integer" }) + version: number + @ManyToOne({ + entity: () => Order, + onDelete: "cascade", + cascade: [Cascade.REMOVE, Cascade.PERSIST], + }) + order: Order + + @Property({ columnType: "text" }) + item_id: string + + @ManyToOne({ + entity: () => LineItem, + onDelete: "cascade", + cascade: [Cascade.REMOVE, Cascade.PERSIST], + }) + item: LineItem + + @Property({ columnType: "numeric" }) + @BigNumberField() + quantity: BigNumber | number + + @Property({ columnType: "jsonb" }) + raw_quantity: BigNumberRawValue + + @Property({ columnType: "numeric" }) + @BigNumberField() + fulfilled_quantity: BigNumber | number + + @Property({ columnType: "jsonb" }) + raw_fulfilled_quantity: BigNumberRawValue + + @Property({ columnType: "numeric" }) + @BigNumberField() + shipped_quantity: BigNumber | number + + @Property({ columnType: "jsonb" }) + raw_shipped_quantity: BigNumberRawValue + + @Property({ columnType: "numeric" }) + @BigNumberField() + return_requested_quantity: BigNumber | number + + @Property({ columnType: "jsonb" }) + raw_return_requested_quantity: BigNumberRawValue + + @Property({ columnType: "numeric" }) + @BigNumberField() + return_received_quantity: BigNumber | number + + @Property({ columnType: "jsonb" }) + raw_return_received_quantity: BigNumberRawValue + + @Property({ columnType: "numeric" }) + @BigNumberField() + return_dismissed_quantity: BigNumber | number + + @Property({ columnType: "jsonb" }) + raw_return_dismissed_quantity: BigNumberRawValue + + @Property({ columnType: "numeric" }) + @BigNumberField() + written_off_quantity: BigNumber | number + + @Property({ columnType: "jsonb" }) + raw_written_off_quantity: BigNumberRawValue + + @Property({ columnType: "jsonb" }) + summary: ItemSummary | null = {} as ItemSummary + + @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 + + @BeforeCreate() + onCreate() { + this.id = generateEntityId(this.id, "ordlisum") + } + + @OnInit() + onInit() { + this.id = generateEntityId(this.id, "ordlisum") + } +} diff --git a/packages/order/src/models/order.ts b/packages/order/src/models/order.ts index 09e914742522f..c853b386aa0cf 100644 --- a/packages/order/src/models/order.ts +++ b/packages/order/src/models/order.ts @@ -17,45 +17,65 @@ import { PrimaryKey, Property, } from "@mikro-orm/core" +import { OrderSummary } from "../types/common" import Address from "./address" -import LineItem from "./line-item" +import OrderDetail from "./order-detail" import ShippingMethod from "./shipping-method" +import Transaction from "./transaction" type OptionalOrderProps = | "shipping_address" | "billing_address" | DAL.EntityDateColumns -const regionIdIndex = createPsqlIndexStatementHelper({ +const RegionIdIndex = createPsqlIndexStatementHelper({ tableName: "order", columns: "region_id", where: "deleted_at IS NOT NULL", }) -const customerIdIndex = createPsqlIndexStatementHelper({ +const CustomerIdIndex = createPsqlIndexStatementHelper({ tableName: "order", columns: "customer_id", where: "deleted_at IS NOT NULL", }) -const salesChannelIdIndex = createPsqlIndexStatementHelper({ +const SalesChannelIdIndex = createPsqlIndexStatementHelper({ tableName: "order", columns: "customer_id", where: "deleted_at IS NOT NULL", }) -const orderDeletedAtIndex = createPsqlIndexStatementHelper({ +const OrderDeletedAtIndex = createPsqlIndexStatementHelper({ tableName: "order", columns: "deleted_at", where: "deleted_at IS NOT NULL", }) -const currencyCodeIndex = createPsqlIndexStatementHelper({ +const OriginalOrderIdIndex = createPsqlIndexStatementHelper({ + tableName: "order", + columns: "original_order_id", + where: "deleted_at IS NOT NULL", +}) + +const CurrencyCodeIndex = createPsqlIndexStatementHelper({ tableName: "order", columns: "currency_code", where: "deleted_at IS NOT NULL", }) +const ShippingAddressIdIndex = createPsqlIndexStatementHelper({ + tableName: "order", + columns: "shipping_address_id", + where: "deleted_at IS NOT NULL", +}) + +const BillingAddressIdIndex = createPsqlIndexStatementHelper({ + tableName: "order", + columns: "billing_address_id", + where: "deleted_at IS NOT NULL", +}) + @Entity({ tableName: "order" }) export default class Order { [OptionalProps]?: OptionalOrderProps @@ -67,21 +87,34 @@ export default class Order { columnType: "text", nullable: true, }) - @regionIdIndex.MikroORMIndex() + @RegionIdIndex.MikroORMIndex() region_id: string | null = null @Property({ columnType: "text", nullable: true, }) - @customerIdIndex.MikroORMIndex() + @CustomerIdIndex.MikroORMIndex() customer_id: string | null = null @Property({ columnType: "text", nullable: true, }) - @salesChannelIdIndex.MikroORMIndex() + @OriginalOrderIdIndex.MikroORMIndex() + original_order_id: string | null = null + + @Property({ + columnType: "integer", + defaultRaw: "1", + }) + version: number = 1 + + @Property({ + columnType: "text", + nullable: true, + }) + @SalesChannelIdIndex.MikroORMIndex() sales_channel_id: string | null = null @Enum({ items: () => OrderStatus, default: OrderStatus.PENDING }) @@ -91,29 +124,29 @@ export default class Order { email: string | null = null @Property({ columnType: "text" }) - @currencyCodeIndex.MikroORMIndex() + @CurrencyCodeIndex.MikroORMIndex() currency_code: string @Property({ columnType: "text", nullable: true }) + @ShippingAddressIdIndex.MikroORMIndex() shipping_address_id?: string | null @ManyToOne({ entity: () => Address, fieldName: "shipping_address_id", nullable: true, - index: "IDX_order_shipping_address_id", cascade: [Cascade.PERSIST], }) shipping_address?: Address | null @Property({ columnType: "text", nullable: true }) + @BillingAddressIdIndex.MikroORMIndex() billing_address_id?: string | null @ManyToOne({ entity: () => Address, fieldName: "billing_address_id", nullable: true, - index: "IDX_order_billing_address_id", cascade: [Cascade.PERSIST], }) billing_address?: Address | null @@ -121,19 +154,27 @@ export default class Order { @Property({ columnType: "boolean", nullable: true }) no_notification: boolean | null = null + @Property({ columnType: "jsonb" }) + summary: OrderSummary | null = {} as OrderSummary + @Property({ columnType: "jsonb", nullable: true }) metadata: Record | null = null - @OneToMany(() => LineItem, (lineItem) => lineItem.order, { + @OneToMany(() => OrderDetail, (itemDetail) => itemDetail.order, { cascade: [Cascade.REMOVE], }) - items = new Collection(this) + items = new Collection(this) @OneToMany(() => ShippingMethod, (shippingMethod) => shippingMethod.order, { cascade: [Cascade.REMOVE], }) shipping_methods = new Collection(this) + @OneToMany(() => Transaction, (transaction) => transaction.order, { + cascade: [Cascade.REMOVE], + }) + transactions = new Collection(this) + @Property({ onCreate: () => new Date(), columnType: "timestamptz", @@ -150,7 +191,7 @@ export default class Order { updated_at: Date @Property({ columnType: "timestamptz", nullable: true }) - @orderDeletedAtIndex.MikroORMIndex() + @OrderDeletedAtIndex.MikroORMIndex() deleted_at: Date | null = null @Property({ columnType: "timestamptz", nullable: true }) diff --git a/packages/order/src/models/shipping-method-adjustment.ts b/packages/order/src/models/shipping-method-adjustment.ts index 5384bba84d35a..33b9c199106a9 100644 --- a/packages/order/src/models/shipping-method-adjustment.ts +++ b/packages/order/src/models/shipping-method-adjustment.ts @@ -1,4 +1,7 @@ -import { generateEntityId } from "@medusajs/utils" +import { + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, @@ -10,17 +13,22 @@ import { import AdjustmentLine from "./adjustment-line" import ShippingMethod from "./shipping-method" +const ShippingMethodIdIdIndex = createPsqlIndexStatementHelper({ + tableName: "order_shipping_method_adjustment", + columns: "shipping_method_id", +}) + @Entity({ tableName: "order_shipping_method_adjustment" }) export default class ShippingMethodAdjustment extends AdjustmentLine { @ManyToOne({ entity: () => ShippingMethod, fieldName: "shipping_method_id", - index: "IDX_order_shipping_method_adjustment_shipping_method_id", cascade: [Cascade.REMOVE, Cascade.PERSIST], }) shipping_method: ShippingMethod @Property({ columnType: "text" }) + @ShippingMethodIdIdIndex.MikroORMIndex() shipping_method_id: string @BeforeCreate() diff --git a/packages/order/src/models/shipping-method-tax-line.ts b/packages/order/src/models/shipping-method-tax-line.ts index c89ad24ed74db..2cea68d0f5a08 100644 --- a/packages/order/src/models/shipping-method-tax-line.ts +++ b/packages/order/src/models/shipping-method-tax-line.ts @@ -1,4 +1,7 @@ -import { generateEntityId } from "@medusajs/utils" +import { + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, @@ -10,17 +13,22 @@ import { import ShippingMethod from "./shipping-method" import TaxLine from "./tax-line" +const ShippingMethodIdIdIndex = createPsqlIndexStatementHelper({ + tableName: "order_shipping_method_tax_line", + columns: "shipping_method_id", +}) + @Entity({ tableName: "order_shipping_method_tax_line" }) export default class ShippingMethodTaxLine extends TaxLine { @ManyToOne({ entity: () => ShippingMethod, fieldName: "shipping_method_id", - index: "IDX_order_tax_line_shipping_method_id", cascade: [Cascade.REMOVE, Cascade.PERSIST], }) shipping_method: ShippingMethod @Property({ columnType: "text" }) + @ShippingMethodIdIdIndex.MikroORMIndex() shipping_method_id: string @BeforeCreate() diff --git a/packages/order/src/models/shipping-method.ts b/packages/order/src/models/shipping-method.ts index d878514cadd85..612f3e4edd988 100644 --- a/packages/order/src/models/shipping-method.ts +++ b/packages/order/src/models/shipping-method.ts @@ -1,6 +1,7 @@ import { BigNumberRawValue } from "@medusajs/types" import { BigNumber, + BigNumberField, createPsqlIndexStatementHelper, generateEntityId, } from "@medusajs/utils" @@ -16,16 +17,20 @@ import { PrimaryKey, Property, } from "@mikro-orm/core" -import { BeforeUpdate } from "typeorm" import Order from "./order" import ShippingMethodAdjustment from "./shipping-method-adjustment" import ShippingMethodTaxLine from "./shipping-method-tax-line" -const shippingOptionIdIndex = createPsqlIndexStatementHelper({ +const ShippingOptionIdIndex = createPsqlIndexStatementHelper({ tableName: "order_shipping_method", columns: "shipping_option_id", }) +const OrderIdIndex = createPsqlIndexStatementHelper({ + tableName: "order_shipping_method", + columns: "order_id", +}) + @Entity({ tableName: "order_shipping_method" }) @Check({ expression: (columns) => `${columns.amount} >= 0` }) export default class ShippingMethod { @@ -33,12 +38,12 @@ export default class ShippingMethod { id: string @Property({ columnType: "text" }) + @OrderIdIndex.MikroORMIndex() order_id: string @ManyToOne({ entity: () => Order, fieldName: "order_id", - index: "IDX_order_shipping_method_order_id", cascade: [Cascade.REMOVE, Cascade.PERSIST], }) order: Order @@ -50,6 +55,7 @@ export default class ShippingMethod { description: string | null = null @Property({ columnType: "numeric" }) + @BigNumberField() amount: BigNumber | number @Property({ columnType: "jsonb" }) @@ -62,7 +68,7 @@ export default class ShippingMethod { columnType: "text", nullable: true, }) - @shippingOptionIdIndex.MikroORMIndex() + @ShippingOptionIdIndex.MikroORMIndex() shipping_option_id: string | null = null @Property({ columnType: "jsonb", nullable: true }) @@ -107,28 +113,9 @@ export default class ShippingMethod { @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "ordsm") - - const val = new BigNumber(this.raw_amount ?? this.amount) - - this.amount = val.numeric - this.raw_amount = val.raw! } - - @BeforeUpdate() - onUpdate() { - const val = new BigNumber(this.raw_amount ?? this.amount) - - this.amount = val.numeric - this.raw_amount = val.raw as BigNumberRawValue - } - @OnInit() onInit() { this.id = generateEntityId(this.id, "ordsm") - - const val = new BigNumber(this.raw_amount ?? this.amount) - - this.amount = val.numeric - this.raw_amount = val.raw! } } diff --git a/packages/order/src/models/tax-line.ts b/packages/order/src/models/tax-line.ts index 296aafc577ed2..19637c915bbcc 100644 --- a/packages/order/src/models/tax-line.ts +++ b/packages/order/src/models/tax-line.ts @@ -1,5 +1,5 @@ import { BigNumberRawValue } from "@medusajs/types" -import { BigNumber } from "@medusajs/utils" +import { BigNumber, BigNumberField } from "@medusajs/utils" import { PrimaryKey, Property } from "@mikro-orm/core" /** @@ -23,6 +23,7 @@ export default abstract class TaxLine { code: string @Property({ columnType: "numeric" }) + @BigNumberField() rate: BigNumber | number @Property({ columnType: "jsonb" }) diff --git a/packages/order/src/models/transaction.ts b/packages/order/src/models/transaction.ts new file mode 100644 index 0000000000000..725e3a0aa4490 --- /dev/null +++ b/packages/order/src/models/transaction.ts @@ -0,0 +1,103 @@ +import { BigNumberRawValue, DAL } from "@medusajs/types" +import { + BigNumber, + BigNumberField, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" +import { + BeforeCreate, + Cascade, + Entity, + ManyToOne, + OnInit, + OptionalProps, + PrimaryKey, + Property, +} from "@mikro-orm/core" +import Order from "./order" + +type OptionalLineItemProps = DAL.EntityDateColumns + +const ReferenceIdIndex = createPsqlIndexStatementHelper({ + tableName: "order_transaction", + columns: "reference_id", +}) + +const OrderIdIndex = createPsqlIndexStatementHelper({ + tableName: "order_transaction", + columns: "order_id", +}) + +const CurrencyCodeIndex = createPsqlIndexStatementHelper({ + tableName: "order_transaction", + columns: "currency_code", +}) + +@Entity({ tableName: "order_transaction" }) +export default class Transaction { + [OptionalProps]?: OptionalLineItemProps + + @PrimaryKey({ columnType: "text" }) + id: string + + @Property({ columnType: "text" }) + @OrderIdIndex.MikroORMIndex() + order_id: string + + @ManyToOne({ + entity: () => Order, + fieldName: "order_id", + cascade: [Cascade.REMOVE, Cascade.PERSIST], + }) + order: Order + + @Property({ columnType: "numeric" }) + @BigNumberField() + amount: BigNumber | number + + @Property({ columnType: "jsonb" }) + raw_amount: BigNumberRawValue + + @Property({ columnType: "text" }) + @CurrencyCodeIndex.MikroORMIndex() + currency_code: string + + @Property({ + columnType: "text", + nullable: true, + }) + reference: string | null = null + + @Property({ + columnType: "text", + nullable: true, + }) + @ReferenceIdIndex.MikroORMIndex() + reference_id: string | 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 + + @BeforeCreate() + onCreate() { + this.id = generateEntityId(this.id, "ordtrx") + } + + @OnInit() + onInit() { + this.id = generateEntityId(this.id, "ordtrx") + } +} diff --git a/packages/order/src/services/order-module-service.ts b/packages/order/src/services/order-module-service.ts index e41bc2f785bad..f7d1701b5783c 100644 --- a/packages/order/src/services/order-module-service.ts +++ b/packages/order/src/services/order-module-service.ts @@ -23,9 +23,13 @@ import { LineItemAdjustment, LineItemTaxLine, Order, + OrderChange, + OrderChangeAction, + OrderDetail, ShippingMethod, ShippingMethodAdjustment, ShippingMethodTaxLine, + Transaction, } from "@models" import { CreateOrderLineItemDTO, @@ -48,6 +52,10 @@ type InjectedDependencies = { lineItemAdjustmentService: ModulesSdkTypes.InternalModuleService lineItemTaxLineService: ModulesSdkTypes.InternalModuleService shippingMethodTaxLineService: ModulesSdkTypes.InternalModuleService + transactionService: ModulesSdkTypes.InternalModuleService + orderChangeService: ModulesSdkTypes.InternalModuleService + orderChangeActionService: ModulesSdkTypes.InternalModuleService + orderDetailService: ModulesSdkTypes.InternalModuleService } const generateMethodForModels = [ @@ -58,6 +66,10 @@ const generateMethodForModels = [ ShippingMethod, ShippingMethodAdjustment, ShippingMethodTaxLine, + Transaction, + OrderChange, + OrderChangeAction, + OrderDetail, ] export default class OrderModuleService< @@ -68,7 +80,11 @@ export default class OrderModuleService< TLineItemTaxLine extends LineItemTaxLine = LineItemTaxLine, TShippingMethodAdjustment extends ShippingMethodAdjustment = ShippingMethodAdjustment, TShippingMethodTaxLine extends ShippingMethodTaxLine = ShippingMethodTaxLine, - TShippingMethod extends ShippingMethod = ShippingMethod + TShippingMethod extends ShippingMethod = ShippingMethod, + TTransaction extends Transaction = Transaction, + TOrderChange extends OrderChange = OrderChange, + TOrderChangeAction extends OrderChangeAction = OrderChangeAction, + TOrderDetail extends OrderDetail = OrderDetail > extends ModulesSdkUtils.abstractModuleServiceFactory< InjectedDependencies, @@ -83,6 +99,9 @@ export default class OrderModuleService< dto: OrderTypes.OrderShippingMethodAdjustmentDTO } ShippingMethodTaxLine: { dto: OrderTypes.OrderShippingMethodTaxLineDTO } + Transaction: { dto: OrderTypes.OrderTransactionDTO } + Change: { dto: OrderTypes.OrderChangeDTO } + ChangeAction: { dto: OrderTypes.OrderChangeActionDTO } } >(Order, generateMethodForModels, entityNameToLinkableKeysMap) implements IOrderModuleService @@ -96,6 +115,10 @@ export default class OrderModuleService< protected lineItemAdjustmentService_: ModulesSdkTypes.InternalModuleService protected lineItemTaxLineService_: ModulesSdkTypes.InternalModuleService protected shippingMethodTaxLineService_: ModulesSdkTypes.InternalModuleService + protected transactionService_: ModulesSdkTypes.InternalModuleService + protected orderChangeService_: ModulesSdkTypes.InternalModuleService + protected orderChangeActionService_: ModulesSdkTypes.InternalModuleService + protected orderDetailService_: ModulesSdkTypes.InternalModuleService constructor( { @@ -108,6 +131,10 @@ export default class OrderModuleService< lineItemAdjustmentService, shippingMethodTaxLineService, lineItemTaxLineService, + transactionService, + orderChangeService, + orderChangeActionService, + orderDetailService, }: InjectedDependencies, protected readonly moduleDeclaration: InternalModuleDeclaration ) { @@ -123,6 +150,10 @@ export default class OrderModuleService< this.lineItemAdjustmentService_ = lineItemAdjustmentService this.shippingMethodTaxLineService_ = shippingMethodTaxLineService this.lineItemTaxLineService_ = lineItemTaxLineService + this.transactionService_ = transactionService + this.orderChangeService_ = orderChangeService + this.orderChangeActionService_ = orderChangeActionService + this.orderDetailService_ = orderDetailService } __joinerConfig(): ModuleJoinerConfig { diff --git a/packages/order/src/types/UpdateOrderShippingMethodDTO.ts b/packages/order/src/types/UpdateOrderShippingMethodDTO.ts deleted file mode 100644 index 1004d333a8d46..0000000000000 --- a/packages/order/src/types/UpdateOrderShippingMethodDTO.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { BigNumberInput } from "@medusajs/types" - -export interface UpdateOrderShippingMethodDTO { - id: string - name?: string - amount?: BigNumberInput - data?: Record -} diff --git a/packages/order/src/types/address.ts b/packages/order/src/types/address.ts index 373fe7d573dfe..027d751460656 100644 --- a/packages/order/src/types/address.ts +++ b/packages/order/src/types/address.ts @@ -1,20 +1,11 @@ -export interface UpsertOrderAddressDTO { - customer_id?: string - company?: string - first_name?: string - last_name?: string - address_1?: string - address_2?: string - city?: string - country_code?: string - province?: string - postal_code?: string - phone?: string - metadata?: Record -} +import { OrderTypes } from "@medusajs/types" + +export type UpsertOrderAddressDTO = OrderTypes.UpsertOrderAddressDTO export interface UpdateOrderAddressDTO extends UpsertOrderAddressDTO { id: string } export interface CreateOrderAddressDTO extends UpsertOrderAddressDTO {} + +export type OrderAddressDTO = OrderTypes.OrderAddressDTO diff --git a/packages/order/src/types/common.ts b/packages/order/src/types/common.ts new file mode 100644 index 0000000000000..3eb338e720aa6 --- /dev/null +++ b/packages/order/src/types/common.ts @@ -0,0 +1,36 @@ +import { BigNumber } from "@medusajs/utils" + +export type OrderSummary = { + total: BigNumber + subtotal: BigNumber + total_tax: BigNumber + + ordered_total: BigNumber + fulfilled_total: BigNumber + returned_total: BigNumber + return_request_total: BigNumber + write_off_total: BigNumber + projected_total: BigNumber + + net_total: BigNumber + net_subtotal: BigNumber + net_total_tax: BigNumber + + future_total: BigNumber + future_subtotal: BigNumber + future_total_tax: BigNumber + future_projected_total: BigNumber + + balance: BigNumber + future_balance: BigNumber +} + +export type ItemSummary = { + returnable_quantity: BigNumber + ordered_quantity: BigNumber + fulfilled_quantity: BigNumber + return_requested_quantity: BigNumber + return_received_quantity: BigNumber + return_dismissed_quantity: BigNumber + written_off_quantity: BigNumber +} diff --git a/packages/order/src/types/index.ts b/packages/order/src/types/index.ts index 7b57d247b91d8..0c9046c7a43fe 100644 --- a/packages/order/src/types/index.ts +++ b/packages/order/src/types/index.ts @@ -8,6 +8,7 @@ export * from "./order" export * from "./shipping-method" export * from "./shipping-method-adjustment" export * from "./shipping-method-tax-line" +export * from "./transaction" export type InitializeModuleInjectableDependencies = { logger?: Logger diff --git a/packages/order/src/types/line-item-adjustment.ts b/packages/order/src/types/line-item-adjustment.ts index c7ddd8f201c77..361883a534f68 100644 --- a/packages/order/src/types/line-item-adjustment.ts +++ b/packages/order/src/types/line-item-adjustment.ts @@ -1,13 +1,7 @@ -import { BigNumberInput } from "@medusajs/types" +import { OrderTypes } from "@medusajs/types" -export interface CreateOrderLineItemAdjustmentDTO { - item_id: string - amount: BigNumberInput - code?: string - description?: string - promotion_id?: string - provider_id?: string -} +export type CreateOrderLineItemAdjustmentDTO = + OrderTypes.CreateOrderLineItemAdjustmentDTO export interface UpdateOrderLineItemAdjustmentDTO extends Partial { diff --git a/packages/order/src/types/order.ts b/packages/order/src/types/order.ts index 562d92bccd683..92be80ab9dc24 100644 --- a/packages/order/src/types/order.ts +++ b/packages/order/src/types/order.ts @@ -17,6 +17,7 @@ export interface CreateOrderDTO { export interface UpdateOrderDTO { id: string + version?: number region_id?: string customer_id?: string sales_channel_id?: string diff --git a/packages/order/src/types/shipping-method-adjustment.ts b/packages/order/src/types/shipping-method-adjustment.ts index e3651c61bb6df..75bfadc6ead3c 100644 --- a/packages/order/src/types/shipping-method-adjustment.ts +++ b/packages/order/src/types/shipping-method-adjustment.ts @@ -1,19 +1,7 @@ -import { BigNumberInput } from "@medusajs/types" +import { OrderTypes } from "@medusajs/types" -export interface CreateOrderShippingMethodAdjustmentDTO { - shipping_method_id: string - code: string - amount: BigNumberInput - description?: string - promotion_id?: string - provider_id?: string -} +export type CreateOrderShippingMethodAdjustmentDTO = + OrderTypes.CreateOrderShippingMethodAdjustmentDTO -export interface UpdateOrderShippingMethodAdjustmentDTO { - id: string - code?: string - amount?: BigNumberInput - description?: string - promotion_id?: string - provider_id?: string -} +export type UpdateOrderShippingMethodAdjustmentDTO = + OrderTypes.UpdateOrderShippingMethodAdjustmentDTO diff --git a/packages/order/src/types/tax-line.ts b/packages/order/src/types/tax-line.ts index 7fdb8bd27a20c..ccf6bd89f9545 100644 --- a/packages/order/src/types/tax-line.ts +++ b/packages/order/src/types/tax-line.ts @@ -1,18 +1,5 @@ -import { BigNumberInput } from "@medusajs/types" +import { OrderTypes } from "@medusajs/types" -export interface UpdateOrderTaxLineDTO { - id: string - description?: string - tax_rate_id?: string - code?: string - rate?: BigNumberInput - provider_id?: string -} +export type CreateOrderTaxLineDTO = OrderTypes.CreateOrderTaxLineDTO -export interface CreateOrderTaxLineDTO { - description?: string - tax_rate_id?: string - code: string - rate: BigNumberInput - provider_id?: string -} +export type UpdateOrderTaxLineDTO = OrderTypes.UpdateOrderTaxLineDTO diff --git a/packages/order/src/types/transaction.ts b/packages/order/src/types/transaction.ts new file mode 100644 index 0000000000000..1b508a0e3a505 --- /dev/null +++ b/packages/order/src/types/transaction.ts @@ -0,0 +1,3 @@ +import { OrderTypes } from "@medusajs/types" + +export type OrderTransactionDTO = OrderTypes.OrderTransactionDTO diff --git a/packages/types/src/order/common.ts b/packages/types/src/order/common.ts index b695d795ec23a..806c060a448b5 100644 --- a/packages/types/src/order/common.ts +++ b/packages/types/src/order/common.ts @@ -1,5 +1,41 @@ import { BaseFilterable } from "../dal" import { OperatorMap } from "../dal/utils" +import { BigNumberRawValue } from "../totals" + +type OrderSummary = { + total: number + subtotal: number + total_tax: number + + ordered_total: number + fulfilled_total: number + returned_total: number + return_request_total: number + write_off_total: number + projected_total: number + + net_total: number + net_subtotal: number + net_total_tax: number + + future_total: number + future_subtotal: number + future_total_tax: number + future_projected_total: number + + balance: number + future_balance: number +} + +type ItemSummary = { + returnable_quantity: number + ordered_quantity: number + fulfilled_quantity: number + return_requested_quantity: number + return_received_quantity: number + return_dismissed_quantity: number + written_off_quantity: number +} export interface OrderAdjustmentLineDTO { /** @@ -251,10 +287,6 @@ export interface OrderShippingMethodDTO { */ updated_at: Date | string - original_total: number - original_subtotal: number - original_tax_total: number - total: number subtotal: number tax_total: number @@ -280,127 +312,204 @@ export interface OrderLineItemTotalsDTO { export interface OrderLineItemDTO extends OrderLineItemTotalsDTO { /** - * The ID of the line item. + * The ID of the order line item. */ id: string + /** - * The title of the line item. + * The title of the order line item. */ title: string + /** - * The subtitle of the line item. - */ - subtitle?: string - /** - * The url of the line item thumbnail. + * The subtitle of the order line item. */ - thumbnail?: string + subtitle?: string | null + /** - * The line item quantity + * The thumbnail of the order line item. */ - quantity: number + thumbnail?: string | null + /** - * The product ID of the line item. + * The ID of the variant associated with the order line item. */ - product_id?: string + variant_id?: string | null + /** - * The product title of the line item. + * The ID of the product associated with the order line item. */ - product_title?: string + product_id?: string | null + /** - * The product description of the line item. + * The title of the product associated with the order line item. */ - product_description?: string + product_title?: string | null + /** - * The product subtitle of the line item. + * The description of the product associated with the order line item. */ - product_subtitle?: string + product_description?: string | null + /** - * The product type of the line item. + * The subtitle of the product associated with the order line item. */ - product_type?: string + product_subtitle?: string | null + /** - * The product collection of the line item. + * The type of the product associated with the order line item. */ - product_collection?: string + product_type?: string | null + /** - * The product handle of the line item. + * The collection of the product associated with the order line item. */ - product_handle?: string + product_collection?: string | null + /** - * The variant ID of the line item. + * The handle of the product associated with the order line item. */ - variant_id?: string + product_handle?: string | null + /** - * The variant sku of the line item. + * The SKU (stock keeping unit) of the variant associated with the order line item. */ - variant_sku?: string + variant_sku?: string | null + /** - * The variant barcode of the line item. + * The barcode of the variant associated with the order line item. */ - variant_barcode?: string + variant_barcode?: string | null + /** - * The variant title of the line item. + * The title of the variant associated with the order line item. */ - variant_title?: string + variant_title?: string | null + /** - * The variant option values of the line item. + * The option values of the variant associated with the order line item. */ - variant_option_values?: Record + variant_option_values?: Record | null + /** - * Whether the line item requires shipping or not + * Indicates whether the order line item requires shipping. */ requires_shipping: boolean + /** - * Whether the line item is discountable or not + * Indicates whether the order line item is discountable. */ is_discountable: boolean + /** - * Whether the line item price is tax inclusive or not + * Indicates whether the order line item price is tax inclusive. */ is_tax_inclusive: boolean + /** - * The original price of the item before an adjustment or a sale. + * The compare at unit price of the order line item. */ compare_at_unit_price?: number + + /** + * The raw compare at unit price of the order line item. + */ + raw_compare_at_unit_price?: BigNumberRawValue + /** - * The price of the item + * The unit price of the order line item. */ unit_price: number + /** - * The associated tax lines. - * - * @expandable + * The raw unit price of the order line item. */ - tax_lines?: OrderLineItemTaxLineDTO[] + raw_unit_price: BigNumberRawValue + /** - * The associated adjustments. - * - * @expandable + * The quantity of the order line item. */ - adjustments?: OrderLineItemAdjustmentDTO[] + quantity: number + /** - * The associated order. - * - * @expandable + * The raw quantity of the order line item. */ - order: OrderDTO + raw_quantity: BigNumberRawValue + /** - * The ID of the associated order. + * The fulfilled quantity of the order line item. */ - order_id: string + fulfilled_quantity: number + /** - * Holds custom data in key-value pairs. + * The raw fulfilled quantity of the order line item. */ - metadata?: Record | null + raw_fulfilled_quantity: BigNumberRawValue + + /** + * The shipped quantity of the order line item. + */ + shipped_quantity: number + + /** + * The raw shipped quantity of the order line item. + */ + raw_shipped_quantity: BigNumberRawValue + + /** + * The quantity of return requested for the order line item. + */ + return_requested_quantity: number + + /** + * The raw quantity of return requested for the order line item. + */ + raw_return_requested_quantity: BigNumberRawValue + + /** + * The quantity of return received for the order line item. + */ + return_received_quantity: number + + /** + * The raw quantity of return received for the order line item. + */ + raw_return_received_quantity: BigNumberRawValue + + /** + * The quantity of return dismissed for the order line item. + */ + return_dismissed_quantity: number + + /** + * The raw quantity of return dismissed for the order line item. + */ + raw_return_dismissed_quantity: BigNumberRawValue + + /** + * The quantity of written off for the order line item. + */ + written_off_quantity: number + + /** + * The raw quantity of written off for the order line item. + */ + raw_written_off_quantity: BigNumberRawValue + + /** + * The summary of the order line item. + */ + summary?: ItemSummary + /** - * When the line item was created. + * The date when the order line item was created. */ - created_at?: Date + created_at: Date + /** - * When the line item was updated. + * The date when the order line item was last updated. */ - updated_at?: Date + updated_at: Date } export interface OrderDTO { @@ -452,6 +561,10 @@ export interface OrderDTO { * @expandable */ shipping_methods?: OrderShippingMethodDTO[] + /** + * The summary of the order totals. + */ + summary?: OrderSummary /** * Holds custom data in key-value pairs. */ @@ -464,36 +577,165 @@ export interface OrderDTO { * When the order was updated. */ updated_at?: string | Date +} - original_item_total: number - original_item_subtotal: number - original_item_tax_total: number - - item_total: number - item_subtotal: number - item_tax_total: number - - original_total: number - original_subtotal: number - original_tax_total: number - - total: number - subtotal: number - tax_total: number - discount_total: number - raw_discount_total: any - discount_tax_total: number - - gift_card_total: number - gift_card_tax_total: number +export interface OrderChangeDTO { + /** + * The ID of the order change + */ + id: string + /** + * The ID of the associated order + */ + order_id: string + /** + * The associated order + * + * @expandable + */ + order: OrderDTO + /** + * The status of the order change + */ + status: string + /** + * The requested by of the order change + */ + requested_by: string | null + /** + * When the order change was requested + */ + requested_at: Date | string | null + /** + * The confirmed by of the order change + */ + confirmed_by: string | null + /** + * When the order change was confirmed + */ + confirmed_at: Date | string | null + /** + * The declined by of the order change + */ + declined_by: string | null + /** + * The declined reason of the order change + */ + declined_reason: string | null + /** + * The metadata of the order change + */ + metadata: Record | null + /** + * When the order change was declined + */ + declined_at: Date | string | null + /** + * The canceled by of the order change + */ + canceled_by: string | null + /** + * When the order change was canceled + */ + canceled_at: Date | string | null + /** + * When the order change was created + */ + created_at: Date | string + /** + * When the order change was updated + */ + updated_at: Date | string +} - shipping_total: number - shipping_subtotal: number - shipping_tax_total: number +export interface OrderChangeActionDTO { + /** + * The ID of the order change action + */ + id: string + /** + * The ID of the associated order change + */ + order_change_id: string + /** + * The associated order change + * + * @expandable + */ + order_change: OrderChangeDTO + /** + * The reference of the order change action + */ + reference: string + /** + * The ID of the reference + */ + reference_id: string + /** + * The action of the order change action + */ + action: Record + /** + * The metadata of the order change action + */ + metadata?: Record | null + /** + * The internal note of the order change action + */ + internal_note: string | null + /** + * When the order change action was created + */ + created_at: Date | string + /** + * When the order change action was updated + */ + updated_at: Date | string +} - original_shipping_total: number - original_shipping_subtotal: number - original_shipping_tax_total: number +export interface OrderTransactionDTO { + /** + * The ID of the transaction + */ + id: string + /** + * The ID of the associated order + */ + order_id: string + /** + * The associated order + * + * @expandable + */ + order: OrderDTO + /** + * The amount of the transaction + */ + amount: number + /** + * The currency code of the transaction + */ + currency_code: string + /** + * The reference of the transaction + */ + reference: string + /** + * The ID of the reference + */ + reference_id: string + /** + * The metadata of the transaction + */ + metadata: Record | null + /** + * When the transaction was created + */ + created_at: Date | string + /** + * When the transaction was updated + */ + updated_at: Date | string } export interface FilterableOrderProps @@ -568,3 +810,31 @@ export interface FilterableOrderShippingMethodTaxLineProps shipping_method_id?: string | string[] shipping_method?: FilterableOrderShippingMethodProps } + +export interface FilterableOrderChangeProps + extends BaseFilterable { + id?: string | string[] + order_id?: string | string[] + status?: string | string[] + requested_by?: string | string[] + confirmed_by?: string | string[] + declined_by?: string | string[] + canceled_by?: string | string[] +} + +export interface FilterableOrderChangeActionProps + extends BaseFilterable { + id?: string | string[] + order_change_id?: string | string[] + reference?: string | string[] + reference_id?: string | string[] +} + +export interface FilterableOrderTransactionProps + extends BaseFilterable { + id?: string | string[] + order_id?: string | string[] + currency_code?: string | string[] + reference?: string | string[] + reference_id?: string | string[] +} diff --git a/packages/types/src/order/mutations.ts b/packages/types/src/order/mutations.ts index 5c7ada5b92af0..16933a3d98b1a 100644 --- a/packages/types/src/order/mutations.ts +++ b/packages/types/src/order/mutations.ts @@ -1,3 +1,4 @@ +import { BigNumberInput } from "../totals" import { OrderLineItemDTO } from "./common" /** ADDRESS START */ @@ -63,7 +64,7 @@ export interface UpdateOrderDTO { /** ADJUSTMENT START */ export interface CreateOrderAdjustmentDTO { code: string - amount: number + amount: BigNumberInput description?: string promotion_id?: string provider_id?: string @@ -72,7 +73,7 @@ export interface CreateOrderAdjustmentDTO { export interface UpdateOrderAdjustmentDTO { id: string code?: string - amount: number + amount: BigNumberInput description?: string promotion_id?: string provider_id?: string @@ -92,7 +93,7 @@ export interface UpsertOrderLineItemAdjustmentDTO { id?: string item_id: string code?: string - amount?: number + amount?: BigNumberInput description?: string promotion_id?: string provider_id?: string @@ -106,7 +107,7 @@ export interface CreateOrderTaxLineDTO { description?: string tax_rate_id?: string code: string - rate: number + rate: BigNumberInput provider_id?: string } @@ -115,7 +116,7 @@ export interface UpdateOrderTaxLineDTO { description?: string tax_rate_id?: string code?: string - rate?: number + rate?: BigNumberInput provider_id?: string } @@ -139,7 +140,7 @@ export interface CreateOrderLineItemDTO { order_id?: string - quantity: number + quantity: BigNumberInput product_id?: string product_title?: string @@ -159,8 +160,8 @@ export interface CreateOrderLineItemDTO { is_discountable?: boolean is_tax_inclusive?: boolean - compare_at_unit_price?: number - unit_price: number | string + compare_at_unit_price?: BigNumberInput + unit_price: BigNumberInput tax_lines?: CreateOrderTaxLineDTO[] adjustments?: CreateOrderAdjustmentDTO[] @@ -183,8 +184,8 @@ export interface UpdateOrderLineItemDTO id: string title?: string - quantity?: number - unit_price?: number + quantity?: BigNumberInput + unit_price?: BigNumberInput tax_lines?: UpdateOrderTaxLineDTO[] | CreateOrderTaxLineDTO[] adjustments?: UpdateOrderAdjustmentDTO[] | CreateOrderAdjustmentDTO[] @@ -197,7 +198,7 @@ export interface UpdateOrderLineItemDTO export interface CreateOrderShippingMethodDTO { name: string order_id: string - amount: number + amount: BigNumberInput data?: Record tax_lines?: CreateOrderTaxLineDTO[] adjustments?: CreateOrderAdjustmentDTO[] @@ -205,7 +206,7 @@ export interface CreateOrderShippingMethodDTO { export interface CreateOrderShippingMethodForSingleOrderDTO { name: string - amount: number + amount: BigNumberInput data?: Record tax_lines?: CreateOrderTaxLineDTO[] adjustments?: CreateOrderAdjustmentDTO[] @@ -214,7 +215,7 @@ export interface CreateOrderShippingMethodForSingleOrderDTO { export interface UpdateOrderShippingMethodDTO { id: string name?: string - amount?: number + amount?: BigNumberInput data?: Record tax_lines?: UpdateOrderTaxLineDTO[] | CreateOrderTaxLineDTO[] adjustments?: CreateOrderAdjustmentDTO[] | UpdateOrderAdjustmentDTO[] @@ -223,7 +224,7 @@ export interface UpdateOrderShippingMethodDTO { export interface CreateOrderShippingMethodAdjustmentDTO { shipping_method_id: string code: string - amount: number + amount: BigNumberInput description?: string promotion_id?: string provider_id?: string @@ -232,10 +233,86 @@ export interface CreateOrderShippingMethodAdjustmentDTO { export interface UpdateOrderShippingMethodAdjustmentDTO { id: string code?: string - amount?: number + amount?: BigNumberInput description?: string promotion_id?: string provider_id?: string } /** SHIPPING METHODS END */ + +/** ORDER CHANGE START */ + +export interface CreateOrderChangeDTO { + order_id: string + status: string + description?: string + internal_note?: string + requested_by?: string + requested_at?: Date + confirmed_by?: string + confirmed_at?: Date + declined_by?: string + declined_reason?: string + declined_at?: Date + canceled_by?: string + metadata?: Record +} + +export interface UpdateOrderChangeDTO { + id: string + status?: string + description?: string + internal_note?: string + requested_by?: string + requested_at?: Date + confirmed_by?: string + confirmed_at?: Date + declined_by?: string + declined_reason?: string + declined_at?: Date + canceled_by?: string + metadata?: Record +} + +/** ORDER CHANGE END */ + +/** ORDER CHANGE ACTION START */ + +export interface CreateOrderChangeActionDTO { + order_change_id: string + reference: string + reference_id: string + action: Record + internal_note?: string + metadata?: Record +} + +export interface UpdateOrderChangeActionDTO { + id: string + reference?: string + reference_id?: string + action?: Record + internal_note?: string + metadata?: Record +} + +/** ORDER TRANSACTION START */ + +export interface CreateOrderTransactionDTO { + order_id: string + amount: BigNumberInput + currency_code: string + reference?: string + reference_id?: string + metadata?: Record +} + +export interface UpdateOrderTransactionDTO { + id: string + amount?: BigNumberInput + currency_code?: string + reference?: string + reference_id?: string + metadata?: Record +} diff --git a/packages/utils/src/dal/index.ts b/packages/utils/src/dal/index.ts index 2d87ea47cfaed..95092cd0a3ce3 100644 --- a/packages/utils/src/dal/index.ts +++ b/packages/utils/src/dal/index.ts @@ -1,3 +1,4 @@ +export * from "./mikro-orm/big-number-field" export * from "./mikro-orm/mikro-orm-create-connection" export * from "./mikro-orm/mikro-orm-repository" export * from "./mikro-orm/mikro-orm-soft-deletable-filter" diff --git a/packages/utils/src/dal/mikro-orm/big-number-field.ts b/packages/utils/src/dal/mikro-orm/big-number-field.ts new file mode 100644 index 0000000000000..940aedba01dc2 --- /dev/null +++ b/packages/utils/src/dal/mikro-orm/big-number-field.ts @@ -0,0 +1,78 @@ +import { BigNumber } from "../../totals/big-number" + +const bigNumberFields = new WeakMap< + object, + { prop: string; options: { nullable?: boolean } }[] +>() + +export function BigNumberField(options: { nullable?: boolean } = {}) { + return function (target: any, prop: string) { + const entity = target.constructor + if (!bigNumberFields.has(entity)) { + bigNumberFields.set(entity, []) + } + + if (prop.startsWith("raw_")) { + const suggestedPropName = prop.replace("raw_", "") + throw new Error( + `BigNumberField decorator has to be used on the property "${suggestedPropName}" and ${prop} typed as BigNumberRawValue.` + ) + } + + bigNumberFields.get(entity)?.push({ prop, options }) + + if (!entity.prototype.__bigNumberInitialized) { + entity.prototype.__bigNumberInitialized = true + + registerGlobalHook(entity) + } + } +} + +function registerGlobalHook(entity: any) { + const originalOnInit = entity.prototype.onInit + const originalOnCreate = entity.prototype.onCreate + const originalOnUpdate = entity.prototype.onUpdate + + entity.prototype.onInit = function (...args: any[]) { + initializeBigNumberFields(this) + + if (originalOnInit) { + originalOnInit.apply(this, args) + } + } + + entity.prototype.onCreate = function (...args: any[]) { + initializeBigNumberFields(this) + + if (originalOnCreate) { + originalOnCreate.apply(this, args) + } + } + + entity.prototype.onUpdate = function (...args: any[]) { + initializeBigNumberFields(this) + + if (originalOnUpdate) { + originalOnUpdate.apply(this, args) + } + } +} + +function initializeBigNumberFields(entity: any) { + const fields = bigNumberFields.get(entity.constructor) ?? [] + + for (const field of fields) { + const { prop, options } = field + + const rawValue = entity[`raw_${prop}`] + const value = entity[prop] + if (options.nullable && rawValue === null && value === null) { + return + } + + const val = new BigNumber(rawValue ?? value) + entity[prop] = val.numeric + entity[`raw_${prop}`] = val.raw + } +} diff --git a/packages/utils/src/order/status.ts b/packages/utils/src/order/status.ts index 8ed93b743efb9..b8886ae9a6b7f 100644 --- a/packages/utils/src/order/status.ts +++ b/packages/utils/src/order/status.ts @@ -29,3 +29,31 @@ export enum OrderStatus { */ REQUIRES_ACTION = "requires_action", } + +/** + * @enum + * + * The order change's status. + */ +export enum OrderChangeStatus { + /** + * The order change is confirmed. + */ + CONFIRMED = "confirmed", + /** + * The order change is declined. + */ + DECLINED = "declined", + /** + * The order change is requested. + */ + REQUESTED = "requested", + /** + * The order change is pending. + */ + PENDING = "pending", + /** + * The order change is canceled. + */ + CANCELED = "canceled", +}