Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cart): Clean up data models #6256

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions packages/cart/src/models/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import {
OnInit,
OptionalProps,
PrimaryKey,
Property
Property,
} from "@mikro-orm/core"


type OptionalAddressProps = DAL.EntityDateColumns // TODO: To be revisited when more clear

@Entity({ tableName: "cart_address" })
Expand All @@ -20,40 +19,40 @@ export default class Address {
id!: string

@Property({ columnType: "text", nullable: true })
customer_id?: string | null
customer_id: string | null = null

@Property({ columnType: "text", nullable: true })
company?: string | null
company: string | null = null

@Property({ columnType: "text", nullable: true })
first_name?: string | null
first_name: string | null = null

@Property({ columnType: "text", nullable: true })
last_name?: string | null
last_name: string | null = null

@Property({ columnType: "text", nullable: true })
address_1?: string | null
address_1: string | null = null

@Property({ columnType: "text", nullable: true })
address_2?: string | null
address_2: string | null = null

@Property({ columnType: "text", nullable: true })
city?: string | null
city: string | null = null

@Property({ columnType: "text", nullable: true })
country_code?: string | null
country_code: string | null = null

@Property({ columnType: "text", nullable: true })
province?: string | null
province: string | null = null

@Property({ columnType: "text", nullable: true })
postal_code?: string | null
postal_code: string | null = null

@Property({ columnType: "text", nullable: true })
phone?: string | null
phone: string | null = null

@Property({ columnType: "jsonb", nullable: true })
metadata?: Record<string, unknown> | null
metadata: Record<string, unknown> | null = null

@Property({
onCreate: () => new Date(),
Expand Down
63 changes: 16 additions & 47 deletions packages/cart/src/models/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Cascade,
Collection,
Entity,
Index,
ManyToOne,
OnInit,
OneToMany,
Expand Down Expand Up @@ -34,50 +33,54 @@ export default class Cart {
nullable: true,
index: "IDX_cart_region_id",
})
region_id?: string | null
region_id: string | null = null

@Property({
columnType: "text",
nullable: true,
index: "IDX_cart_customer_id",
})
customer_id?: string | null
customer_id: string | null = null

@Property({
columnType: "text",
nullable: true,
index: "IDX_cart_sales_channel_id",
})
sales_channel_id?: string | null
sales_channel_id: string | null = null

@Property({ columnType: "text", nullable: true })
email?: string | null
email: string | null = null

@Property({ columnType: "text", index: "IDX_cart_curency_code" })
currency_code: string

@Index({ name: "IDX_cart_shipping_address_id" })
@Property({ columnType: "text", nullable: true })
shipping_address_id?: string | null

@ManyToOne(() => Address, {
@ManyToOne({
entity: () => Address,
fieldName: "shipping_address_id",
nullable: true,
index: "IDX_cart_shipping_address_id",
cascade: [Cascade.PERSIST],
})
shipping_address?: Address | null

@Index({ name: "IDX_cart_billing_address_id" })
@Property({ columnType: "text", nullable: true })
billing_address_id?: string | null

@ManyToOne(() => Address, {
@ManyToOne({
entity: () => Address,
fieldName: "billing_address_id",
nullable: true,
index: "IDX_cart_billing_address_id",
cascade: [Cascade.PERSIST],
})
billing_address?: Address | null

@Property({ columnType: "jsonb", nullable: true })
metadata?: Record<string, unknown> | null
metadata: Record<string, unknown> | null = null

@OneToMany(() => LineItem, (lineItem) => lineItem.cart, {
cascade: [Cascade.REMOVE],
Expand All @@ -89,57 +92,23 @@ export default class Cart {
})
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({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at?: Date
created_at: Date

@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at?: Date
updated_at: Date

@Property({ columnType: "timestamptz", nullable: true })
deleted_at?: Date
deleted_at: Date | null = null

@BeforeCreate()
onCreate() {
Expand Down
9 changes: 5 additions & 4 deletions packages/cart/src/models/line-item-adjustment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Check,
Entity,
ManyToOne,
Expand All @@ -15,12 +16,12 @@ import LineItem from "./line-item"
expression: (columns) => `${columns.amount} >= 0`,
})
export default class LineItemAdjustment extends AdjustmentLine {
@ManyToOne(() => LineItem, {
onDelete: "cascade",
nullable: true,
@ManyToOne({
entity: () => LineItem,
index: "IDX_adjustment_item_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
})
item?: LineItem | null
item: LineItem

@Property({ columnType: "text" })
item_id: string
Expand Down
9 changes: 5 additions & 4 deletions packages/cart/src/models/line-item-tax-line.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Entity,
ManyToOne,
OnInit,
Expand All @@ -11,12 +12,12 @@ import TaxLine from "./tax-line"

@Entity({ tableName: "cart_line_item_tax_line" })
export default class LineItemTaxLine extends TaxLine {
@ManyToOne(() => LineItem, {
onDelete: "cascade",
nullable: true,
@ManyToOne({
entity: () => LineItem,
index: "IDX_tax_line_item_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST]
})
item: LineItem | null
item: LineItem

@Property({ columnType: "text" })
item_id: string
Expand Down
64 changes: 22 additions & 42 deletions packages/cart/src/models/line-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,22 @@ export default class LineItem {
@Property({ columnType: "text" })
cart_id: string

@ManyToOne(() => Cart, {
@ManyToOne({
entity: () => Cart,
onDelete: "cascade",
index: "IDX_line_item_cart_id",
nullable: true,
cascade: [Cascade.REMOVE, Cascade.PERSIST],
})
cart?: Cart | null
cart: Cart

@Property({ columnType: "text" })
title: string

@Property({ columnType: "text", nullable: true })
subtitle: string | null
subtitle: string | null = null

@Property({ columnType: "text", nullable: true })
thumbnail?: string | null
thumbnail: string | null = null

@Property({ columnType: "integer" })
quantity: number
Expand All @@ -59,44 +60,44 @@ export default class LineItem {
nullable: true,
index: "IDX_line_item_variant_id",
})
variant_id?: string | null
variant_id: string | null = null

@Property({
columnType: "text",
nullable: true,
index: "IDX_line_item_product_id",
})
product_id?: string | null
product_id: string | null = null

@Property({ columnType: "text", nullable: true })
product_title?: string | null
product_title: string | null = null

@Property({ columnType: "text", nullable: true })
product_description?: string | null
product_description: string | null = null

@Property({ columnType: "text", nullable: true })
product_subtitle?: string | null
product_subtitle: string | null = null

@Property({ columnType: "text", nullable: true })
product_type?: string | null
product_type: string | null = null

@Property({ columnType: "text", nullable: true })
product_collection?: string | null
product_collection: string | null = null

@Property({ columnType: "text", nullable: true })
product_handle?: string | null
product_handle: string | null = null

@Property({ columnType: "text", nullable: true })
variant_sku?: string | null
variant_sku: string | null = null

@Property({ columnType: "text", nullable: true })
variant_barcode?: string | null
variant_barcode: string | null = null

@Property({ columnType: "text", nullable: true })
variant_title?: string | null
variant_title: string | null = null

@Property({ columnType: "jsonb", nullable: true })
variant_option_values?: Record<string, unknown> | null
variant_option_values: Record<string, unknown> | null = null

@Property({ columnType: "boolean" })
requires_shipping = true
Expand All @@ -108,8 +109,9 @@ export default class LineItem {
is_tax_inclusive = false

@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 })
@Check({ expression: "unit_price >= 0" }) // TODO: Validate that numeric types work with the expression
unit_price: number
Expand All @@ -124,42 +126,20 @@ export default class LineItem {
})
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({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at?: Date
created_at: Date

@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at?: Date
updated_at: Date

@BeforeCreate()
onCreate() {
Expand Down
Loading
Loading