chore(cart): Clean up data models (#6256)

Clean up data models in Cart Module according to latest established conventions
This commit is contained in:
Oli Juhl authored and GitHub committed 2024-01-29 18:22:13 +00:00
1 parent 87390704be
commit 1c27f7cb34
8 files changed
+79 -141

No files matched your search

+13 -14
View File
@@ -6,10 +6,9 @@ import {
OnInit, OnInit,
OptionalProps, OptionalProps,
PrimaryKey, PrimaryKey,
Property Property,
} from "@mikro-orm/core" } from "@mikro-orm/core"
type OptionalAddressProps = DAL.EntityDateColumns // TODO: To be revisited when more clear type OptionalAddressProps = DAL.EntityDateColumns // TODO: To be revisited when more clear
@Entity({ tableName: "cart_address" }) @Entity({ tableName: "cart_address" })
@@ -20,40 +19,40 @@ export default class Address {
id!: string id!: string
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
customer_id?: string | null customer_id: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
company?: string | null company: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
first_name?: string | null first_name: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
last_name?: string | null last_name: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
address_1?: string | null address_1: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
address_2?: string | null address_2: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
city?: string | null city: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
country_code?: string | null country_code: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
province?: string | null province: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
postal_code?: string | null postal_code: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
phone?: string | null phone: string | null = null
@Property({ columnType: "jsonb", nullable: true }) @Property({ columnType: "jsonb", nullable: true })
metadata?: Record<string, unknown> | null metadata: Record<string, unknown> | null = null
@Property({ @Property({
onCreate: () => new Date(), onCreate: () => new Date(),
+16 -47
View File
@@ -5,7 +5,6 @@ import {
Cascade, Cascade,
Collection, Collection,
Entity, Entity,
Index,
ManyToOne, ManyToOne,
OnInit, OnInit,
OneToMany, OneToMany,
@@ -34,50 +33,54 @@ export default class Cart {
nullable: true, nullable: true,
index: "IDX_cart_region_id", index: "IDX_cart_region_id",
}) })
region_id?: string | null region_id: string | null = null
@Property({ @Property({
columnType: "text", columnType: "text",
nullable: true, nullable: true,
index: "IDX_cart_customer_id", index: "IDX_cart_customer_id",
}) })
customer_id?: string | null customer_id: string | null = null
@Property({ @Property({
columnType: "text", columnType: "text",
nullable: true, nullable: true,
index: "IDX_cart_sales_channel_id", index: "IDX_cart_sales_channel_id",
}) })
sales_channel_id?: string | null sales_channel_id: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
email?: string | null email: string | null = null
@Property({ columnType: "text", index: "IDX_cart_curency_code" }) @Property({ columnType: "text", index: "IDX_cart_curency_code" })
currency_code: string currency_code: string
@Index({ name: "IDX_cart_shipping_address_id" })
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
shipping_address_id?: string | null shipping_address_id?: string | null
@ManyToOne(() => Address, { @ManyToOne({
entity: () => Address,
fieldName: "shipping_address_id", fieldName: "shipping_address_id",
nullable: true, nullable: true,
index: "IDX_cart_shipping_address_id",
cascade: [Cascade.PERSIST],
}) })
shipping_address?: Address | null shipping_address?: Address | null
@Index({ name: "IDX_cart_billing_address_id" })
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
billing_address_id?: string | null billing_address_id?: string | null
@ManyToOne(() => Address, { @ManyToOne({
entity: () => Address,
fieldName: "billing_address_id", fieldName: "billing_address_id",
nullable: true, nullable: true,
index: "IDX_cart_billing_address_id",
cascade: [Cascade.PERSIST],
}) })
billing_address?: Address | null billing_address?: Address | null
@Property({ columnType: "jsonb", nullable: true }) @Property({ columnType: "jsonb", nullable: true })
metadata?: Record<string, unknown> | null metadata: Record<string, unknown> | null = null
@OneToMany(() => LineItem, (lineItem) => lineItem.cart, { @OneToMany(() => LineItem, (lineItem) => lineItem.cart, {
cascade: [Cascade.REMOVE], cascade: [Cascade.REMOVE],
@@ -89,46 +92,12 @@ export default class Cart {
}) })
shipping_methods = new Collection<ShippingMethod>(this) shipping_methods = new Collection<ShippingMethod>(this)
/** COMPUTED PROPERTIES - START */
// compare_at_item_total?: number
// compare_at_item_subtotal?: number
// compare_at_item_tax_total?: number
// 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
// discount_tax_total: number
// shipping_total: number
// shipping_subtotal: number
// shipping_tax_total: number
// original_shipping_total: number
// original_shipping_subtotal: number
// original_shipping_tax_total: number
/** COMPUTED PROPERTIES - END */
@Property({ @Property({
onCreate: () => new Date(), onCreate: () => new Date(),
columnType: "timestamptz", columnType: "timestamptz",
defaultRaw: "now()", defaultRaw: "now()",
}) })
created_at?: Date created_at: Date
@Property({ @Property({
onCreate: () => new Date(), onCreate: () => new Date(),
@@ -136,10 +105,10 @@ export default class Cart {
columnType: "timestamptz", columnType: "timestamptz",
defaultRaw: "now()", defaultRaw: "now()",
}) })
updated_at?: Date updated_at: Date
@Property({ columnType: "timestamptz", nullable: true }) @Property({ columnType: "timestamptz", nullable: true })
deleted_at?: Date deleted_at: Date | null = null
@BeforeCreate() @BeforeCreate()
onCreate() { onCreate() {
@@ -1,6 +1,7 @@
import { generateEntityId } from "@medusajs/utils" import { generateEntityId } from "@medusajs/utils"
import { import {
BeforeCreate, BeforeCreate,
Cascade,
Check, Check,
Entity, Entity,
ManyToOne, ManyToOne,
@@ -15,12 +16,12 @@ import LineItem from "./line-item"
expression: (columns) => `${columns.amount} >= 0`, expression: (columns) => `${columns.amount} >= 0`,
}) })
export default class LineItemAdjustment extends AdjustmentLine { export default class LineItemAdjustment extends AdjustmentLine {
@ManyToOne(() => LineItem, { @ManyToOne({
onDelete: "cascade", entity: () => LineItem,
nullable: true,
index: "IDX_adjustment_item_id", index: "IDX_adjustment_item_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
}) })
item?: LineItem | null item: LineItem
@Property({ columnType: "text" }) @Property({ columnType: "text" })
item_id: string item_id: string
@@ -1,6 +1,7 @@
import { generateEntityId } from "@medusajs/utils" import { generateEntityId } from "@medusajs/utils"
import { import {
BeforeCreate, BeforeCreate,
Cascade,
Entity, Entity,
ManyToOne, ManyToOne,
OnInit, OnInit,
@@ -11,12 +12,12 @@ import TaxLine from "./tax-line"
@Entity({ tableName: "cart_line_item_tax_line" }) @Entity({ tableName: "cart_line_item_tax_line" })
export default class LineItemTaxLine extends TaxLine { export default class LineItemTaxLine extends TaxLine {
@ManyToOne(() => LineItem, { @ManyToOne({
onDelete: "cascade", entity: () => LineItem,
nullable: true,
index: "IDX_tax_line_item_id", index: "IDX_tax_line_item_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST]
}) })
item: LineItem | null item: LineItem
@Property({ columnType: "text" }) @Property({ columnType: "text" })
item_id: string item_id: string
+22 -42
View File
@@ -35,21 +35,22 @@ export default class LineItem {
@Property({ columnType: "text" }) @Property({ columnType: "text" })
cart_id: string cart_id: string
@ManyToOne(() => Cart, { @ManyToOne({
entity: () => Cart,
onDelete: "cascade", onDelete: "cascade",
index: "IDX_line_item_cart_id", index: "IDX_line_item_cart_id",
nullable: true, cascade: [Cascade.REMOVE, Cascade.PERSIST],
}) })
cart?: Cart | null cart: Cart
@Property({ columnType: "text" }) @Property({ columnType: "text" })
title: string title: string
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
subtitle: string | null subtitle: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
thumbnail?: string | null thumbnail: string | null = null
@Property({ columnType: "integer" }) @Property({ columnType: "integer" })
quantity: number quantity: number
@@ -59,44 +60,44 @@ export default class LineItem {
nullable: true, nullable: true,
index: "IDX_line_item_variant_id", index: "IDX_line_item_variant_id",
}) })
variant_id?: string | null variant_id: string | null = null
@Property({ @Property({
columnType: "text", columnType: "text",
nullable: true, nullable: true,
index: "IDX_line_item_product_id", index: "IDX_line_item_product_id",
}) })
product_id?: string | null product_id: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
product_title?: string | null product_title: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
product_description?: string | null product_description: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
product_subtitle?: string | null product_subtitle: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
product_type?: string | null product_type: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
product_collection?: string | null product_collection: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
product_handle?: string | null product_handle: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
variant_sku?: string | null variant_sku: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
variant_barcode?: string | null variant_barcode: string | null = null
@Property({ columnType: "text", nullable: true }) @Property({ columnType: "text", nullable: true })
variant_title?: string | null variant_title: string | null = null
@Property({ columnType: "jsonb", nullable: true }) @Property({ columnType: "jsonb", nullable: true })
variant_option_values?: Record<string, unknown> | null variant_option_values: Record<string, unknown> | null = null
@Property({ columnType: "boolean" }) @Property({ columnType: "boolean" })
requires_shipping = true requires_shipping = true
@@ -108,8 +109,9 @@ export default class LineItem {
is_tax_inclusive = false is_tax_inclusive = false
@Property({ columnType: "numeric", nullable: true }) @Property({ columnType: "numeric", nullable: true })
compare_at_unit_price?: number compare_at_unit_price: number | null = null
// TODO: Rework when BigNumber has been introduced
@Property({ columnType: "numeric", serializer: Number }) @Property({ columnType: "numeric", serializer: Number })
@Check({ expression: "unit_price >= 0" }) // TODO: Validate that numeric types work with the expression @Check({ expression: "unit_price >= 0" }) // TODO: Validate that numeric types work with the expression
unit_price: number unit_price: number
@@ -124,34 +126,12 @@ export default class LineItem {
}) })
adjustments = new Collection<LineItemAdjustment>(this) adjustments = new Collection<LineItemAdjustment>(this)
/** COMPUTED PROPERTIES - START */
// compare_at_total?: number
// compare_at_subtotal?: number
// compare_at_tax_total?: number
// original_total: number
// original_subtotal: number
// original_tax_total: number
// item_total: number
// item_subtotal: number
// item_tax_total: number
// total: number
// subtotal: number
// tax_total: number
// discount_total: number
// discount_tax_total: number
/** COMPUTED PROPERTIES - END */
@Property({ @Property({
onCreate: () => new Date(), onCreate: () => new Date(),
columnType: "timestamptz", columnType: "timestamptz",
defaultRaw: "now()", defaultRaw: "now()",
}) })
created_at?: Date created_at: Date
@Property({ @Property({
onCreate: () => new Date(), onCreate: () => new Date(),
@@ -159,7 +139,7 @@ export default class LineItem {
columnType: "timestamptz", columnType: "timestamptz",
defaultRaw: "now()", defaultRaw: "now()",
}) })
updated_at?: Date updated_at: Date
@BeforeCreate() @BeforeCreate()
onCreate() { onCreate() {
@@ -1,6 +1,7 @@
import { generateEntityId } from "@medusajs/utils" import { generateEntityId } from "@medusajs/utils"
import { import {
BeforeCreate, BeforeCreate,
Cascade,
Entity, Entity,
ManyToOne, ManyToOne,
OnInit, OnInit,
@@ -11,12 +12,12 @@ import ShippingMethod from "./shipping-method"
@Entity({ tableName: "cart_shipping_method_adjustment" }) @Entity({ tableName: "cart_shipping_method_adjustment" })
export default class ShippingMethodAdjustment extends AdjustmentLine { export default class ShippingMethodAdjustment extends AdjustmentLine {
@ManyToOne(() => ShippingMethod, { @ManyToOne({
onDelete: "cascade", entity: () => ShippingMethod,
nullable: true,
index: "IDX_adjustment_shipping_method_id", index: "IDX_adjustment_shipping_method_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
}) })
shipping_method: ShippingMethod | null shipping_method: ShippingMethod
@Property({ columnType: "text" }) @Property({ columnType: "text" })
shipping_method_id: string shipping_method_id: string
@@ -1,6 +1,7 @@
import { generateEntityId } from "@medusajs/utils" import { generateEntityId } from "@medusajs/utils"
import { import {
BeforeCreate, BeforeCreate,
Cascade,
Entity, Entity,
ManyToOne, ManyToOne,
OnInit, OnInit,
@@ -11,12 +12,12 @@ import TaxLine from "./tax-line"
@Entity({ tableName: "cart_shipping_method_tax_line" }) @Entity({ tableName: "cart_shipping_method_tax_line" })
export default class ShippingMethodTaxLine extends TaxLine { export default class ShippingMethodTaxLine extends TaxLine {
@ManyToOne(() => ShippingMethod, { @ManyToOne({
onDelete: "cascade", entity: () => ShippingMethod,
nullable: true,
index: "IDX_tax_line_shipping_method_id", index: "IDX_tax_line_shipping_method_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
}) })
shipping_method: ShippingMethod | null shipping_method: ShippingMethod
@Property({ columnType: "text" }) @Property({ columnType: "text" })
shipping_method_id: string shipping_method_id: string
+8 -22
View File
@@ -24,18 +24,18 @@ export default class ShippingMethod {
@Property({ columnType: "text" }) @Property({ columnType: "text" })
cart_id: string cart_id: string
@ManyToOne(() => Cart, { @ManyToOne({
onDelete: "cascade", entity: () => Cart,
index: "IDX_shipping_method_cart_id", index: "IDX_shipping_method_cart_id",
nullable: true, cascade: [Cascade.REMOVE, Cascade.PERSIST],
}) })
cart?: Cart | null cart: Cart
@Property({ columnType: "text" }) @Property({ columnType: "text" })
name: string name: string
@Property({ columnType: "jsonb", nullable: true }) @Property({ columnType: "jsonb", nullable: true })
description?: string | null description: string | null = null
@Property({ columnType: "numeric", serializer: Number }) @Property({ columnType: "numeric", serializer: Number })
amount: number amount: number
@@ -48,13 +48,13 @@ export default class ShippingMethod {
nullable: true, nullable: true,
index: "IDX_shipping_method_option_id", index: "IDX_shipping_method_option_id",
}) })
shipping_option_id?: string | null shipping_option_id: string | null = null
@Property({ columnType: "jsonb", nullable: true }) @Property({ columnType: "jsonb", nullable: true })
data?: Record<string, unknown> | null data: Record<string, unknown> | null = null
@Property({ columnType: "jsonb", nullable: true }) @Property({ columnType: "jsonb", nullable: true })
metadata?: Record<string, unknown> | null metadata: Record<string, unknown> | null = null
@OneToMany( @OneToMany(
() => ShippingMethodTaxLine, () => ShippingMethodTaxLine,
@@ -74,20 +74,6 @@ export default class ShippingMethod {
) )
adjustments = new Collection<ShippingMethodAdjustment>(this) adjustments = new Collection<ShippingMethodAdjustment>(this)
/** COMPUTED PROPERTIES - START */
// original_total: number
// original_subtotal: number
// original_tax_total: number
// total: number
// subtotal: number
// tax_total: number
// discount_total: number
// discount_tax_total: number
/** COMPUTED PROPERTIES - END */
@Property({ @Property({
onCreate: () => new Date(), onCreate: () => new Date(),
columnType: "timestamptz", columnType: "timestamptz",