chore(cart): Make all cart entities soft-deletable (#6475)

This commit is contained in:
Oli Juhl
2024-02-23 08:59:02 +01:00
committed by GitHub
parent 3fc2aea752
commit d9636f4631
13 changed files with 763 additions and 214 deletions
+16 -2
View File
@@ -1,17 +1,23 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Entity,
Filter,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
type OptionalAddressProps = DAL.EntityDateColumns // TODO: To be revisited when more clear
type OptionalAddressProps = DAL.SoftDeletableEntityDateColumns
@Entity({ tableName: "cart_address" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Address {
[OptionalProps]: OptionalAddressProps
@@ -69,6 +75,14 @@ export default class Address {
})
updated_at: Date
@createPsqlIndexStatementHelper({
tableName: "cart_address",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "caaddr")
+1 -7
View File
@@ -1,7 +1,7 @@
import { DAL } from "@medusajs/types"
import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core"
type OptionalAdjustmentLineProps = DAL.EntityDateColumns // TODO: To be revisited when more clear
type OptionalAdjustmentLineProps = DAL.SoftDeletableEntityDateColumns
/**
* As per the Mikro ORM docs, superclasses should use the abstract class definition
@@ -16,12 +16,6 @@ export default abstract class AdjustmentLine {
@Property({ columnType: "text", nullable: true })
description: string | null = null
@Property({
columnType: "text",
nullable: true,
})
promotion_id: string | null = null
@Property({ columnType: "text", nullable: true })
code: string | null = null
+47 -15
View File
@@ -1,10 +1,15 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Filter,
ManyToOne,
OnInit,
OneToMany,
@@ -19,29 +24,40 @@ import ShippingMethod from "./shipping-method"
type OptionalCartProps =
| "shipping_address"
| "billing_address"
| DAL.EntityDateColumns
| DAL.SoftDeletableEntityDateColumns
@Entity({ tableName: "cart" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Cart {
[OptionalProps]?: OptionalCartProps
@PrimaryKey({ columnType: "text" })
id: string
@Property({
columnType: "text",
nullable: true,
index: "IDX_cart_region_id",
})
@createPsqlIndexStatementHelper({
name: "IDX_cart_region_id",
tableName: "cart",
columns: "region_id",
where: "deleted_at IS NULL AND region_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
region_id: string | null = null
@Property({
columnType: "text",
nullable: true,
index: "IDX_cart_customer_id",
})
@createPsqlIndexStatementHelper({
name: "IDX_cart_customer_id",
tableName: "cart",
columns: "customer_id",
where: "deleted_at IS NULL AND customer_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
customer_id: string | null = null
@createPsqlIndexStatementHelper({
name: "IDX_cart_sales_channel_id",
tableName: "cart",
columns: "sales_channel_id",
where: "deleted_at IS NULL AND sales_channel_id IS NOT NULL",
}).MikroORMIndex()
@Property({
columnType: "text",
nullable: true,
@@ -55,6 +71,12 @@ export default class Cart {
@Property({ columnType: "text", index: "IDX_cart_curency_code" })
currency_code: string
@createPsqlIndexStatementHelper({
name: "IDX_cart_shipping_address_id",
tableName: "cart",
columns: "shipping_address_id",
where: "deleted_at IS NULL AND shipping_address_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
shipping_address_id?: string | null
@@ -62,11 +84,16 @@ export default class Cart {
entity: () => Address,
fieldName: "shipping_address_id",
nullable: true,
index: "IDX_cart_shipping_address_id",
cascade: [Cascade.PERSIST],
})
shipping_address?: Address | null
@createPsqlIndexStatementHelper({
name: "IDX_cart_billing_address_id",
tableName: "cart",
columns: "billing_address_id",
where: "deleted_at IS NULL AND billing_address_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
billing_address_id?: string | null
@@ -83,12 +110,12 @@ export default class Cart {
metadata: Record<string, unknown> | null = null
@OneToMany(() => LineItem, (lineItem) => lineItem.cart, {
cascade: [Cascade.REMOVE],
cascade: [Cascade.PERSIST, "soft-remove"] as any,
})
items = new Collection<LineItem>(this)
@OneToMany(() => ShippingMethod, (shippingMethod) => shippingMethod.cart, {
cascade: [Cascade.REMOVE],
cascade: [Cascade.PERSIST, "soft-remove"] as any,
})
shipping_methods = new Collection<ShippingMethod>(this)
@@ -107,6 +134,11 @@ export default class Cart {
})
updated_at: Date
@createPsqlIndexStatementHelper({
tableName: "cart",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@@ -1,9 +1,14 @@
import { generateEntityId } from "@medusajs/utils"
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Check,
Entity,
Filter,
ManyToOne,
OnInit,
Property,
@@ -15,17 +20,40 @@ import LineItem from "./line-item"
@Check<LineItemAdjustment>({
expression: (columns) => `${columns.amount} >= 0`,
})
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class LineItemAdjustment extends AdjustmentLine {
@ManyToOne({
entity: () => LineItem,
index: "IDX_adjustment_item_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any,
})
item: LineItem
@createPsqlIndexStatementHelper({
name: "IDX_adjustment_item_id",
tableName: "cart_line_item_adjustment",
columns: "item_id",
where: "deleted_at IS NULL",
}).MikroORMIndex()
@Property({ columnType: "text" })
item_id: string
@createPsqlIndexStatementHelper({
name: "IDX_line_item_adjustment_promotion_id",
tableName: "cart_line_item_adjustment",
columns: "promotion_id",
where: "deleted_at IS NULL and promotion_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
promotion_id: string | null = null
@createPsqlIndexStatementHelper({
tableName: "cart_line_item_adjustment",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "caliadj")
+31 -3
View File
@@ -1,8 +1,13 @@
import { generateEntityId } from "@medusajs/utils"
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Entity,
Filter,
ManyToOne,
OnInit,
Property,
@@ -11,17 +16,40 @@ 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 {
@ManyToOne({
entity: () => LineItem,
index: "IDX_tax_line_item_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST]
cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any,
})
item: LineItem
@createPsqlIndexStatementHelper({
name: "IDX_tax_line_item_id",
tableName: "cart_line_item_tax_line",
columns: "item_id",
where: "deleted_at IS NULL",
}).MikroORMIndex()
@Property({ columnType: "text" })
item_id: string
@createPsqlIndexStatementHelper({
name: "IDX_line_item_tax_line_tax_rate_id",
tableName: "cart_line_item_tax_line",
columns: "tax_rate_id",
where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
tax_rate_id: string | null = null
@createPsqlIndexStatementHelper({
tableName: "cart_line_item_tax_line",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "calitxl")
+40 -17
View File
@@ -1,11 +1,17 @@
import { BigNumberRawValue, DAL } from "@medusajs/types"
import { BigNumber, generateEntityId } from "@medusajs/utils"
import {
BigNumber,
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
BeforeUpdate,
Cascade,
Collection,
Entity,
Filter,
ManyToOne,
OnInit,
OneToMany,
@@ -23,23 +29,28 @@ type OptionalLineItemProps =
| "compare_at_unit_price"
| "requires_shipping"
| "cart"
| DAL.EntityDateColumns
| DAL.SoftDeletableEntityDateColumns
@Entity({ tableName: "cart_line_item" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class LineItem {
[OptionalProps]?: OptionalLineItemProps
@PrimaryKey({ columnType: "text" })
id: string
@createPsqlIndexStatementHelper({
name: "IDX_line_item_cart_id",
tableName: "cart_line_item",
columns: "cart_id",
where: "deleted_at IS NULL",
}).MikroORMIndex()
@Property({ columnType: "text" })
cart_id: string
@ManyToOne({
entity: () => Cart,
onDelete: "cascade",
index: "IDX_line_item_cart_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any,
})
cart: Cart
@@ -55,18 +66,22 @@ export default class LineItem {
@Property({ columnType: "integer" })
quantity: number
@Property({
columnType: "text",
nullable: true,
index: "IDX_line_item_variant_id",
})
@createPsqlIndexStatementHelper({
name: "IDX_line_item_variant_id",
tableName: "cart_line_item",
columns: "variant_id",
where: "deleted_at IS NULL AND variant_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
variant_id: string | null = null
@Property({
columnType: "text",
nullable: true,
index: "IDX_line_item_product_id",
})
@createPsqlIndexStatementHelper({
name: "IDX_line_item_product_id",
tableName: "cart_line_item",
columns: "product_id",
where: "deleted_at IS NULL AND product_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
product_id: string | null = null
@Property({ columnType: "text", nullable: true })
@@ -121,12 +136,12 @@ export default class LineItem {
raw_unit_price: BigNumberRawValue
@OneToMany(() => LineItemTaxLine, (taxLine) => taxLine.item, {
cascade: [Cascade.REMOVE],
cascade: [Cascade.PERSIST, "soft-remove"] as any,
})
tax_lines = new Collection<LineItemTaxLine>(this)
@OneToMany(() => LineItemAdjustment, (adjustment) => adjustment.item, {
cascade: [Cascade.REMOVE],
cascade: [Cascade.PERSIST, "soft-remove"] as any,
})
adjustments = new Collection<LineItemAdjustment>(this)
@@ -145,6 +160,14 @@ export default class LineItem {
})
updated_at: Date
@createPsqlIndexStatementHelper({
tableName: "cart_line_item",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "cali")
@@ -1,8 +1,13 @@
import { generateEntityId } from "@medusajs/utils"
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Entity,
Filter,
ManyToOne,
OnInit,
Property,
@@ -11,17 +16,40 @@ import AdjustmentLine from "./adjustment-line"
import ShippingMethod from "./shipping-method"
@Entity({ tableName: "cart_shipping_method_adjustment" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class ShippingMethodAdjustment extends AdjustmentLine {
@ManyToOne({
entity: () => ShippingMethod,
index: "IDX_adjustment_shipping_method_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any,
})
shipping_method: ShippingMethod
@createPsqlIndexStatementHelper({
name: "IDX_adjustment_shipping_method_id",
tableName: "cart_shipping_method_adjustment",
columns: "shipping_method_id",
where: "deleted_at IS NULL",
}).MikroORMIndex()
@Property({ columnType: "text" })
shipping_method_id: string
@createPsqlIndexStatementHelper({
name: "IDX_shipping_method_adjustment_promotion_id",
tableName: "cart_shipping_method_adjustment",
columns: "promotion_id",
where: "deleted_at IS NULL and promotion_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
promotion_id: string | null = null
@createPsqlIndexStatementHelper({
tableName: "cart_shipping_method_adjustment",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "casmadj")
@@ -1,8 +1,13 @@
import { generateEntityId } from "@medusajs/utils"
import {
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Entity,
Filter,
ManyToOne,
OnInit,
Property,
@@ -11,17 +16,40 @@ import ShippingMethod from "./shipping-method"
import TaxLine from "./tax-line"
@Entity({ tableName: "cart_shipping_method_tax_line" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class ShippingMethodTaxLine extends TaxLine {
@ManyToOne({
entity: () => ShippingMethod,
index: "IDX_tax_line_shipping_method_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any,
})
shipping_method: ShippingMethod
@createPsqlIndexStatementHelper({
name: "IDX_tax_line_shipping_method_id",
tableName: "cart_shipping_method_tax_line",
columns: "shipping_method_id",
where: "deleted_at IS NULL",
}).MikroORMIndex()
@Property({ columnType: "text" })
shipping_method_id: string
@createPsqlIndexStatementHelper({
name: "IDX_shipping_method_tax_line_tax_rate_id",
tableName: "cart_shipping_method_tax_line",
columns: "tax_rate_id",
where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
tax_rate_id: string | null = null
@createPsqlIndexStatementHelper({
tableName: "cart_shipping_method_tax_line",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "casmtxl")
+41 -11
View File
@@ -1,14 +1,21 @@
import { BigNumberRawValue } from "@medusajs/types"
import { BigNumber, generateEntityId } from "@medusajs/utils"
import { BigNumberRawValue, DAL } from "@medusajs/types"
import {
BigNumber,
DALUtils,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Check,
Collection,
Entity,
Filter,
ManyToOne,
OnInit,
OneToMany,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
@@ -17,19 +24,32 @@ import Cart from "./cart"
import ShippingMethodAdjustment from "./shipping-method-adjustment"
import ShippingMethodTaxLine from "./shipping-method-tax-line"
type OptionalShippingMethodProps =
| "cart"
| "is_tax_inclusive"
| DAL.SoftDeletableEntityDateColumns
@Entity({ tableName: "cart_shipping_method" })
@Check<ShippingMethod>({ expression: (columns) => `${columns.amount} >= 0` })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class ShippingMethod {
[OptionalProps]?: OptionalShippingMethodProps
@PrimaryKey({ columnType: "text" })
id: string
@createPsqlIndexStatementHelper({
name: "IDX_shipping_method_cart_id",
tableName: "cart_shipping_method",
columns: "cart_id",
where: "deleted_at IS NULL",
}).MikroORMIndex()
@Property({ columnType: "text" })
cart_id: string
@ManyToOne({
entity: () => Cart,
index: "IDX_shipping_method_cart_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any,
})
cart: Cart
@@ -48,11 +68,13 @@ export default class ShippingMethod {
@Property({ columnType: "boolean" })
is_tax_inclusive = false
@Property({
columnType: "text",
nullable: true,
index: "IDX_shipping_method_option_id",
})
@createPsqlIndexStatementHelper({
name: "IDX_shipping_method_option_id",
tableName: "cart_shipping_method",
columns: "shipping_option_id",
where: "deleted_at IS NULL AND shipping_option_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
shipping_option_id: string | null = null
@Property({ columnType: "jsonb", nullable: true })
@@ -65,7 +87,7 @@ export default class ShippingMethod {
() => ShippingMethodTaxLine,
(taxLine) => taxLine.shipping_method,
{
cascade: [Cascade.REMOVE],
cascade: [Cascade.PERSIST, "soft-remove"] as any,
}
)
tax_lines = new Collection<ShippingMethodTaxLine>(this)
@@ -74,7 +96,7 @@ export default class ShippingMethod {
() => ShippingMethodAdjustment,
(adjustment) => adjustment.shipping_method,
{
cascade: [Cascade.REMOVE],
cascade: [Cascade.PERSIST, "soft-remove"] as any,
}
)
adjustments = new Collection<ShippingMethodAdjustment>(this)
@@ -94,6 +116,14 @@ export default class ShippingMethod {
})
updated_at: Date
@createPsqlIndexStatementHelper({
tableName: "cart_shipping_method",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "casm")
+6 -7
View File
@@ -1,22 +1,21 @@
import { PrimaryKey, Property } from "@mikro-orm/core"
import { DAL } from "@medusajs/types"
import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core"
type OptionalTaxLineProps = DAL.SoftDeletableEntityDateColumns
/**
* As per the Mikro ORM docs, superclasses should use the abstract class definition
* Source: https://mikro-orm.io/docs/inheritance-mapping
*/
export default abstract class TaxLine {
[OptionalProps]?: OptionalTaxLineProps
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text", nullable: true })
description?: string | null
@Property({
columnType: "text",
nullable: true,
})
tax_rate_id?: string | null
@Property({ columnType: "text" })
code: string