Skip to content

Commit

Permalink
fix models
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Jan 4, 2024
1 parent ee02ba2 commit 0653093
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 175 deletions.
18 changes: 10 additions & 8 deletions packages/cart/src/models/address.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { DALUtils, generateEntityId } from "@medusajs/utils"
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Entity,
Filter,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Property
} from "@mikro-orm/core"


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

@Entity({ tableName: "cart_address" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Address {
[OptionalProps]: OptionalAddressProps

@PrimaryKey({ columnType: "text" })
id: string
id!: string

@Property({ columnType: "text", nullable: true })
customer_id?: string | null
Expand Down Expand Up @@ -65,9 +70,6 @@ export default class Address {
})
updated_at: Date

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

@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "caaddr")
Expand Down
10 changes: 6 additions & 4 deletions packages/cart/src/models/adjustment-line.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { PrimaryKey, Property } from "@mikro-orm/core"
import { DAL } from "@medusajs/types"
import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core"

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

export default class AdjustmentLine {
[OptionalProps]: OptionalAdjustmentLineProps

@PrimaryKey({ columnType: "text" })
id: string

Expand Down Expand Up @@ -33,7 +38,4 @@ export default class AdjustmentLine {
defaultRaw: "now()",
})
updated_at: Date

@Property({ columnType: "timestamptz", nullable: true })
deleted_at?: Date | null
}
89 changes: 45 additions & 44 deletions packages/cart/src/models/cart.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { DALUtils, generateEntityId } from "@medusajs/utils"
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Collection,
Entity,
Filter,
ManyToOne,
OnInit,
OneToMany,
OneToOne,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import Address from "./address"
import LineItem from "./line-item"
import ShippingMethod from "./shipping-method"

type OptionalCartProps =
| "shipping_address"
| "billing_address"
| DAL.EntityDateColumns // TODO: To be revisited when more clear

@Entity({ tableName: "cart" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Cart {
@PrimaryKey({ columnType: "text" })
id: string
Expand All @@ -35,66 +39,66 @@ export default class Cart {
@Property({ columnType: "text" })
currency_code: string

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

@ManyToOne(() => Address, {
nullable: true,
fieldName: "shipping_address_id",
@OneToOne({
entity: () => Address,
joinColumn: "shipping_address_id",
orphanRemoval: true,
})
shipping_address?: Address | null

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

@ManyToOne(() => Address, {
nullable: true,
fieldName: "billing_address_id",
@OneToOne({
entity: () => Address,
joinColumn: "billing_address_id",
orphanRemoval: true,
})
billing_address?: Address | null

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

@OneToMany(() => LineItem, (lineItem) => lineItem.cart, {
cascade: ["soft-remove"] as any,
orphanRemoval: true,
})
items = new Collection<LineItem>(this)

@OneToMany(() => ShippingMethod, (shippingMethod) => shippingMethod.cart, {
cascade: ["soft-remove"] as any,
orphanRemoval: true,
})
shipping_methods = new Collection<ShippingMethod>(this)

compare_at_item_total?: number | null
compare_at_item_subtotal?: number | null
compare_at_item_tax_total?: number | null
/** 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
// original_item_total: number
// original_item_subtotal: number
// original_item_tax_total: number

item_total: number
item_subtotal: number
item_tax_total: number
// item_total: number
// item_subtotal: number
// item_tax_total: number

original_total: number
original_subtotal: number
original_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
// total: number
// subtotal: number
// tax_total: number
// discount_total: number
// discount_tax_total: number

shipping_total: number
shipping_subtotal: number
shipping_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
// original_shipping_total: number
// original_shipping_subtotal: number
// original_shipping_tax_total: number

/** COMPUTED PROPERTIES - END */

@Property({
onCreate: () => new Date(),
Expand All @@ -111,9 +115,6 @@ export default class Cart {
})
updated_at: Date

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

@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "cart")
Expand Down
13 changes: 4 additions & 9 deletions packages/cart/src/models/line-item-adjustment-line.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { DALUtils, generateEntityId } from "@medusajs/utils"
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
PrimaryKey
OnInit
} from "@mikro-orm/core"
import AdjustmentLine from "./adjustment-line"
import LineItem from "./line-item"

@Entity({ tableName: "cart_line_item_adjustment_line" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class LineItemAdjustmentLine extends AdjustmentLine {
@PrimaryKey({ columnType: "text" })
line_item_id: string

@ManyToOne(() => LineItem, {
fieldName: "item_id",
joinColumn: "line_item",
fieldName: "line_item_id",
})
line_item: LineItem

Expand Down
16 changes: 3 additions & 13 deletions packages/cart/src/models/line-item-tax-line.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import { DALUtils, generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
PrimaryKey
} from "@mikro-orm/core"
import { generateEntityId } from "@medusajs/utils"
import { BeforeCreate, Entity, ManyToOne, OnInit } from "@mikro-orm/core"
import LineItem from "./line-item"
import TaxLine from "./tax-line"

@Entity({ tableName: "cart_line_item_tax_line" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class LineItemTaxLine extends TaxLine {
@PrimaryKey({ columnType: "text" })
line_item_id: string

@ManyToOne(() => LineItem, {
joinColumn: "line_item",
fieldName: "line_item_id",
})
line_item: LineItem
Expand Down
Loading

0 comments on commit 0653093

Please sign in to comment.