chore(): Reorganize modules (#7210)

**What**
Move all modules to the modules directory
This commit is contained in:
Adrien de Peretti
2024-05-02 17:33:34 +02:00
committed by GitHub
parent 7a351eef09
commit 4eae25e1ef
870 changed files with 91 additions and 62 deletions

View File

@@ -0,0 +1,90 @@
import { DAL } from "@medusajs/types"
import {
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Entity,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
type OptionalAddressProps = DAL.EntityDateColumns
const CustomerIdIndex = createPsqlIndexStatementHelper({
tableName: "order_address",
columns: "customer_id",
})
@Entity({ tableName: "order_address" })
export default class Address {
[OptionalProps]: OptionalAddressProps
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text", nullable: true })
@CustomerIdIndex.MikroORMIndex()
customer_id: string | null = null
@Property({ columnType: "text", nullable: true })
company: string | null = null
@Property({ columnType: "text", nullable: true })
first_name: string | null = null
@Property({ columnType: "text", nullable: true })
last_name: string | null = null
@Property({ columnType: "text", nullable: true })
address_1: string | null = null
@Property({ columnType: "text", nullable: true })
address_2: string | null = null
@Property({ columnType: "text", nullable: true })
city: string | null = null
@Property({ columnType: "text", nullable: true })
country_code: string | null = null
@Property({ columnType: "text", nullable: true })
province: string | null = null
@Property({ columnType: "text", nullable: true })
postal_code: string | null = null
@Property({ columnType: "text", nullable: true })
phone: string | null = null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | 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, "ordaddr")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordaddr")
}
}

View File

@@ -0,0 +1,52 @@
import { BigNumberRawValue, DAL } from "@medusajs/types"
import { BigNumber, MikroOrmBigNumberProperty } from "@medusajs/utils"
import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core"
type OptionalAdjustmentLineProps = DAL.EntityDateColumns
/**
* 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 AdjustmentLine {
[OptionalProps]: OptionalAdjustmentLineProps
@PrimaryKey({ columnType: "text" })
id: string
@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
@MikroOrmBigNumberProperty()
amount: BigNumber | number
@Property({ columnType: "jsonb" })
raw_amount: BigNumberRawValue
@Property({ columnType: "text", nullable: true })
provider_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
}

View File

@@ -0,0 +1,14 @@
export { default as Address } from "./address"
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 OrderItem } from "./order-item"
export { default as OrderShippingMethod } from "./order-shipping-method"
export { default as OrderSummary } from "./order-summary"
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"

View File

@@ -0,0 +1,42 @@
import {
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import { BeforeCreate, Entity, ManyToOne, OnInit } from "@mikro-orm/core"
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" })
export default class LineItemAdjustment extends AdjustmentLine {
@ManyToOne(() => LineItem, {
persist: false,
})
item: LineItem
@ManyToOne({
entity: () => LineItem,
columnType: "text",
fieldName: "item_id",
onDelete: "cascade",
mapToPk: true,
})
@ItemIdIndex.MikroORMIndex()
item_id: string
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordliadj")
this.item_id ??= this.item?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordliadj")
this.item_id ??= this.item?.id
}
}

View File

@@ -0,0 +1,49 @@
import {
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Entity,
ManyToOne,
OnInit,
} from "@mikro-orm/core"
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(() => LineItem, {
fieldName: "item_id",
persist: false,
})
item: LineItem
@ManyToOne({
entity: () => LineItem,
columnType: "text",
fieldName: "item_id",
cascade: [Cascade.PERSIST, Cascade.REMOVE],
mapToPk: true,
})
@ItemIdIndex.MikroORMIndex()
item_id: string
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordlitxl")
this.item_id ??= this.item?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordlitxl")
this.item_id ??= this.item?.id
}
}

View File

@@ -0,0 +1,161 @@
import { BigNumberRawValue, DAL } from "@medusajs/types"
import {
BigNumber,
MikroOrmBigNumberProperty,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
OnInit,
OneToMany,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import LineItemAdjustment from "./line-item-adjustment"
import LineItemTaxLine from "./line-item-tax-line"
type OptionalLineItemProps =
| "is_discoutable"
| "is_tax_inclusive"
| "compare_at_unit_price"
| "requires_shipping"
| DAL.EntityDateColumns
const ProductIdIndex = createPsqlIndexStatementHelper({
tableName: "order_line_item",
columns: "product_id",
})
const VariantIdIndex = createPsqlIndexStatementHelper({
tableName: "order_line_item",
columns: "variant_id",
})
@Entity({ tableName: "order_line_item" })
export default class LineItem {
[OptionalProps]?: OptionalLineItemProps
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
title: string
@Property({ columnType: "text", nullable: true })
subtitle: string | null = null
@Property({ columnType: "text", nullable: true })
thumbnail: string | null = null
@Property({
columnType: "text",
nullable: true,
})
@VariantIdIndex.MikroORMIndex()
variant_id: string | null = null
@Property({
columnType: "text",
nullable: true,
})
@ProductIdIndex.MikroORMIndex()
product_id: string | null = null
@Property({ columnType: "text", nullable: true })
product_title: string | null = null
@Property({ columnType: "text", nullable: true })
product_description: string | null = null
@Property({ columnType: "text", nullable: true })
product_subtitle: string | null = null
@Property({ columnType: "text", nullable: true })
product_type: string | null = null
@Property({ columnType: "text", nullable: true })
product_collection: string | null = null
@Property({ columnType: "text", nullable: true })
product_handle: string | null = null
@Property({ columnType: "text", nullable: true })
variant_sku: string | null = null
@Property({ columnType: "text", nullable: true })
variant_barcode: string | null = null
@Property({ columnType: "text", nullable: true })
variant_title: string | null = null
@Property({ columnType: "jsonb", nullable: true })
variant_option_values: Record<string, unknown> | null = null
@Property({ columnType: "boolean" })
requires_shipping = true
@Property({ columnType: "boolean" })
is_discountable = true
@Property({ columnType: "boolean" })
is_tax_inclusive = false
@MikroOrmBigNumberProperty({
nullable: true,
})
compare_at_unit_price?: BigNumber | number | null = null
@Property({ columnType: "jsonb", nullable: true })
raw_compare_at_unit_price: BigNumberRawValue | null = null
@MikroOrmBigNumberProperty({
nullable: true,
})
unit_price: BigNumber | number
@Property({ columnType: "jsonb" })
raw_unit_price: BigNumberRawValue
@OneToMany(() => LineItemTaxLine, (taxLine) => taxLine.item, {
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
})
tax_lines = new Collection<LineItemTaxLine>(this)
@OneToMany(() => LineItemAdjustment, (adjustment) => adjustment.item, {
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
})
adjustments = new Collection<LineItemAdjustment>(this)
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | 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, "ordli")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordli")
}
}

View File

@@ -0,0 +1,151 @@
import { BigNumberRawValue, DAL } from "@medusajs/types"
import {
BigNumber,
MikroOrmBigNumberProperty,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Entity,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import Order from "./order"
import OrderChange from "./order-change"
type OptionalLineItemProps = DAL.EntityDateColumns
const OrderChangeIdIndex = createPsqlIndexStatementHelper({
tableName: "order_change_action",
columns: "order_change_id",
})
const OrderIdIndex = createPsqlIndexStatementHelper({
tableName: "order_change_action",
columns: "order_id",
})
const ActionOrderingIndex = createPsqlIndexStatementHelper({
tableName: "order_change_action",
columns: "ordering",
})
@Entity({ tableName: "order_change_action" })
export default class OrderChangeAction {
[OptionalProps]?: OptionalLineItemProps
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "integer", autoincrement: true })
@ActionOrderingIndex.MikroORMIndex()
ordering: number
@ManyToOne({
entity: () => Order,
columnType: "text",
fieldName: "order_id",
onDelete: "cascade",
mapToPk: true,
nullable: true,
})
@OrderIdIndex.MikroORMIndex()
order_id: string | null = null
@ManyToOne(() => Order, {
persist: false,
nullable: true,
})
order: Order | null = null
@Property({ columnType: "integer", nullable: true })
version: number | null = null
@ManyToOne({
entity: () => OrderChange,
columnType: "text",
fieldName: "order_change_id",
onDelete: "cascade",
mapToPk: true,
nullable: true,
})
@OrderChangeIdIndex.MikroORMIndex()
order_change_id: string | null
@ManyToOne(() => OrderChange, {
persist: false,
nullable: true,
})
order_change: OrderChange | null = null
@Property({
columnType: "text",
nullable: true,
})
reference: string | null = null
@Property({
columnType: "text",
nullable: true,
})
reference_id: string | null = null
@Property({ columnType: "text" })
action: string
@Property({ columnType: "jsonb" })
details: Record<string, unknown> = {}
@MikroOrmBigNumberProperty({ nullable: true })
amount: BigNumber | number | null = null
@Property({ columnType: "jsonb", nullable: true })
raw_amount: BigNumberRawValue | null = null
@Property({
columnType: "text",
nullable: true,
})
internal_note: string | null = null
@Property({
columnType: "boolean",
defaultRaw: "false",
})
applied: boolean = false
@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")
this.order_id ??= this.order?.id ?? this.order_change?.order_id ?? null
this.order_change_id ??= this.order_change?.id ?? null
this.version ??= this.order_change?.version ?? null
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordchact")
this.order_id ??= this.order?.id ?? this.order_change?.order_id ?? null
this.order_change_id ??= this.order_change?.id ?? null
this.version ??= this.order_change?.version ?? null
}
}

View File

@@ -0,0 +1,156 @@
import { DAL } from "@medusajs/types"
import {
OrderChangeStatus,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Enum,
ManyToOne,
OnInit,
OneToMany,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
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",
})
const VersionIndex = createPsqlIndexStatementHelper({
tableName: "order_change",
columns: ["order_id", "version"],
})
@Entity({ tableName: "order_change" })
@VersionIndex.MikroORMIndex()
export default class OrderChange {
[OptionalProps]?: OptionalLineItemProps
@PrimaryKey({ columnType: "text" })
id: string
@ManyToOne({
entity: () => Order,
columnType: "text",
fieldName: "order_id",
onDelete: "cascade",
mapToPk: true,
})
@OrderIdIndex.MikroORMIndex()
order_id: string
@ManyToOne(() => Order, {
persist: false,
})
order: Order
@Property({ columnType: "integer" })
@VersionIndex.MikroORMIndex()
version: number
@OneToMany(() => OrderChangeAction, (action) => action.order_change, {
cascade: [Cascade.PERSIST, "sotf-remove" as Cascade],
})
actions = new Collection<OrderChangeAction>(this)
@Property({
columnType: "text",
nullable: true,
})
description: string | null = null
@Enum({ items: () => OrderChangeStatus, default: OrderChangeStatus.PENDING })
@OrderChangeStatusIndex.MikroORMIndex()
status: OrderChangeStatus = OrderChangeStatus.PENDING
@Property({ columnType: "text", nullable: true })
internal_note: string | null = null
@Property({ columnType: "text", nullable: true })
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 | null = null
@Property({ columnType: "text", nullable: true })
confirmed_by: string | null = null // customer or user ID
@Property({
columnType: "timestamptz",
nullable: true,
})
confirmed_at: Date | null = null
@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<string, unknown> | 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")
this.order_id ??= this.order?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordch")
this.order_id ??= this.order?.id
}
}

View File

@@ -0,0 +1,164 @@
import { BigNumberRawValue, DAL } from "@medusajs/types"
import {
BigNumber,
MikroOrmBigNumberProperty,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Entity,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import LineItem from "./line-item"
import Order from "./order"
type OptionalLineItemProps = DAL.EntityDateColumns
const OrderIdIndex = createPsqlIndexStatementHelper({
tableName: "order_item",
columns: ["order_id"],
where: "deleted_at IS NOT NULL",
})
const OrderVersionIndex = createPsqlIndexStatementHelper({
tableName: "order_item",
columns: ["version"],
where: "deleted_at IS NOT NULL",
})
const ItemIdIndex = createPsqlIndexStatementHelper({
tableName: "order_item",
columns: ["item_id"],
where: "deleted_at IS NOT NULL",
})
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "order",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
@Entity({ tableName: "order_item" })
export default class OrderItem {
[OptionalProps]?: OptionalLineItemProps
@PrimaryKey({ columnType: "text" })
id: string
@ManyToOne({
entity: () => Order,
mapToPk: true,
fieldName: "order_id",
columnType: "text",
})
@OrderIdIndex.MikroORMIndex()
order_id: string
@Property({ columnType: "integer" })
@OrderVersionIndex.MikroORMIndex()
version: number
@ManyToOne(() => Order, {
persist: false,
})
order: Order
@ManyToOne({
entity: () => LineItem,
fieldName: "item_id",
mapToPk: true,
columnType: "text",
})
@ItemIdIndex.MikroORMIndex()
item_id: string
@ManyToOne(() => LineItem, {
persist: false,
})
item: LineItem
@MikroOrmBigNumberProperty()
quantity: BigNumber | number
@Property({ columnType: "jsonb" })
raw_quantity: BigNumberRawValue
@MikroOrmBigNumberProperty()
fulfilled_quantity: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_fulfilled_quantity: BigNumberRawValue
@MikroOrmBigNumberProperty()
shipped_quantity: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_shipped_quantity: BigNumberRawValue
@MikroOrmBigNumberProperty()
return_requested_quantity: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_return_requested_quantity: BigNumberRawValue
@MikroOrmBigNumberProperty()
return_received_quantity: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_return_received_quantity: BigNumberRawValue
@MikroOrmBigNumberProperty()
return_dismissed_quantity: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_return_dismissed_quantity: BigNumberRawValue
@MikroOrmBigNumberProperty()
written_off_quantity: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_written_off_quantity: BigNumberRawValue
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | 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
@Property({ columnType: "timestamptz", nullable: true })
@DeletedAtIndex.MikroORMIndex()
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "orditem")
this.order_id ??= this.order?.id
this.item_id ??= this.item?.id
this.version ??= this.order?.version
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "orditem")
this.order_id ??= this.order?.id
this.item_id ??= this.item?.id
this.version ??= this.order?.version
}
}

View File

@@ -0,0 +1,117 @@
import { DAL } from "@medusajs/types"
import {
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Entity,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import Order from "./order"
import ShippingMethod from "./shipping-method"
type OptionalShippingMethodProps = DAL.EntityDateColumns
const OrderIdIndex = createPsqlIndexStatementHelper({
tableName: "order_shipping",
columns: ["order_id"],
where: "deleted_at IS NOT NULL",
})
const OrderVersionIndex = createPsqlIndexStatementHelper({
tableName: "order_shipping",
columns: ["version"],
where: "deleted_at IS NOT NULL",
})
const ItemIdIndex = createPsqlIndexStatementHelper({
tableName: "order_shipping",
columns: ["shipping_method_id"],
where: "deleted_at IS NOT NULL",
})
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "order",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
@Entity({ tableName: "order_shipping" })
export default class OrderShippingMethod {
[OptionalProps]?: OptionalShippingMethodProps
@PrimaryKey({ columnType: "text" })
id: string
@ManyToOne({
entity: () => Order,
mapToPk: true,
fieldName: "order_id",
columnType: "text",
})
@OrderIdIndex.MikroORMIndex()
order_id: string
@Property({ columnType: "integer" })
@OrderVersionIndex.MikroORMIndex()
version: number
@ManyToOne(() => Order, {
persist: false,
})
order: Order
@ManyToOne({
entity: () => ShippingMethod,
fieldName: "shipping_method_id",
mapToPk: true,
columnType: "text",
})
@ItemIdIndex.MikroORMIndex()
shipping_method_id: string
@ManyToOne(() => ShippingMethod, {
persist: false,
})
shipping_method: ShippingMethod
@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
@Property({ columnType: "timestamptz", nullable: true })
@DeletedAtIndex.MikroORMIndex()
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordspmv")
this.order_id ??= this.order?.id
this.shipping_method_id ??= this.shipping_method?.id
this.version ??= this.order?.version
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordspmv")
this.order_id ??= this.order?.id
this.shipping_method_id ??= this.shipping_method?.id
this.version ??= this.order?.version
}
}

View File

@@ -0,0 +1,112 @@
import {
BigNumber,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Entity,
ManyToOne,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { Order } from "@models"
type OrderSummaryTotals = {
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
}
const OrderIdVersionIndex = createPsqlIndexStatementHelper({
tableName: "order_summary",
columns: ["order_id", "version"],
where: "deleted_at IS NOT NULL",
})
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "order",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
@Entity({ tableName: "order_summary" })
@OrderIdVersionIndex.MikroORMIndex()
export default class OrderSummary {
@PrimaryKey({ columnType: "text" })
id: string
@ManyToOne({
entity: () => Order,
columnType: "text",
fieldName: "order_id",
mapToPk: true,
onDelete: "cascade",
})
order_id: string
@ManyToOne(() => Order, {
persist: false,
})
order: Order
@Property({
columnType: "integer",
defaultRaw: "1",
})
version: number = 1
@Property({ columnType: "jsonb" })
totals: OrderSummaryTotals | null = {} as OrderSummaryTotals
@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
@Property({ columnType: "timestamptz", nullable: true })
@DeletedAtIndex.MikroORMIndex()
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordsum")
this.order_id ??= this.order?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordsum")
this.order_id ??= this.order?.id
}
}

View File

@@ -0,0 +1,214 @@
import { DAL } from "@medusajs/types"
import {
OrderStatus,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Enum,
ManyToOne,
OnInit,
OneToMany,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import Address from "./address"
import OrderItem from "./order-item"
import OrderShippingMethod from "./order-shipping-method"
import OrderSummary from "./order-summary"
import Transaction from "./transaction"
type OptionalOrderProps =
| "shipping_address"
| "billing_address"
| DAL.EntityDateColumns
const RegionIdIndex = createPsqlIndexStatementHelper({
tableName: "order",
columns: "region_id",
where: "deleted_at IS NOT NULL",
})
const CustomerIdIndex = createPsqlIndexStatementHelper({
tableName: "order",
columns: "customer_id",
where: "deleted_at IS NOT NULL",
})
const SalesChannelIdIndex = createPsqlIndexStatementHelper({
tableName: "order",
columns: "customer_id",
where: "deleted_at IS NOT NULL",
})
const OrderDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "order",
columns: "deleted_at",
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",
})
const IsDraftOrderIndex = createPsqlIndexStatementHelper({
tableName: "order",
columns: "is_draft_order",
where: "deleted_at IS NOT NULL",
})
@Entity({ tableName: "order" })
export default class Order {
[OptionalProps]?: OptionalOrderProps
@PrimaryKey({ columnType: "text" })
id: string
@Property({
columnType: "text",
nullable: true,
})
@RegionIdIndex.MikroORMIndex()
region_id: string | null = null
@Property({
columnType: "text",
nullable: true,
})
@CustomerIdIndex.MikroORMIndex()
customer_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 })
status: OrderStatus
@Property({
columnType: "boolean",
})
@IsDraftOrderIndex.MikroORMIndex()
is_draft_order = false
@Property({ columnType: "text", nullable: true })
email: string | null = null
@Property({ columnType: "text" })
@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,
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,
cascade: [Cascade.PERSIST],
})
billing_address?: Address | null
@Property({ columnType: "boolean", nullable: true })
no_notification: boolean | null = null
@OneToMany(() => OrderSummary, (summary) => summary.order, {
cascade: [Cascade.PERSIST],
})
summary = new Collection<OrderSummary>(this)
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@OneToMany(() => OrderItem, (itemDetail) => itemDetail.order, {
cascade: [Cascade.PERSIST],
})
items = new Collection<OrderItem>(this)
@OneToMany(
() => OrderShippingMethod,
(shippingMethod) => shippingMethod.order,
{
cascade: [Cascade.PERSIST],
}
)
shipping_methods = new Collection<OrderShippingMethod>(this)
@OneToMany(() => Transaction, (transaction) => transaction.order, {
cascade: [Cascade.PERSIST],
})
transactions = new Collection<Transaction>(this)
@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
@Property({ columnType: "timestamptz", nullable: true })
@OrderDeletedAtIndex.MikroORMIndex()
deleted_at: Date | null = null
@Property({ columnType: "timestamptz", nullable: true })
canceled_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "order")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "order")
}
}

View File

@@ -0,0 +1,42 @@
import {
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import { BeforeCreate, Entity, ManyToOne, OnInit } from "@mikro-orm/core"
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(() => ShippingMethod, {
persist: false,
})
shipping_method: ShippingMethod
@ManyToOne({
entity: () => ShippingMethod,
columnType: "text",
fieldName: "shipping_method_id",
mapToPk: true,
onDelete: "cascade",
})
@ShippingMethodIdIdIndex.MikroORMIndex()
shipping_method_id: string
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordsmadj")
this.shipping_method_id ??= this.shipping_method?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordsmadj")
this.shipping_method_id ??= this.shipping_method?.id
}
}

View File

@@ -0,0 +1,42 @@
import {
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import { BeforeCreate, Entity, ManyToOne, OnInit } from "@mikro-orm/core"
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(() => ShippingMethod, {
persist: false,
})
shipping_method: ShippingMethod
@ManyToOne({
entity: () => ShippingMethod,
fieldName: "shipping_method_id",
columnType: "text",
mapToPk: true,
onDelete: "cascade",
})
@ShippingMethodIdIdIndex.MikroORMIndex()
shipping_method_id: string
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordsmtxl")
this.shipping_method_id ??= this.shipping_method?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordsmtxl")
this.shipping_method_id ??= this.shipping_method?.id
}
}

View File

@@ -0,0 +1,100 @@
import { BigNumberRawValue } from "@medusajs/types"
import {
BigNumber,
createPsqlIndexStatementHelper,
generateEntityId,
MikroOrmBigNumberProperty,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
OneToMany,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import ShippingMethodAdjustment from "./shipping-method-adjustment"
import ShippingMethodTaxLine from "./shipping-method-tax-line"
const ShippingOptionIdIndex = createPsqlIndexStatementHelper({
tableName: "order_shipping_method",
columns: "shipping_option_id",
})
@Entity({ tableName: "order_shipping_method" })
export default class ShippingMethod {
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
name: string
@Property({ columnType: "jsonb", nullable: true })
description: string | null = null
@MikroOrmBigNumberProperty()
amount: BigNumber | number
@Property({ columnType: "jsonb" })
raw_amount: BigNumberRawValue
@Property({ columnType: "boolean" })
is_tax_inclusive = false
@Property({
columnType: "text",
nullable: true,
})
@ShippingOptionIdIndex.MikroORMIndex()
shipping_option_id: string | null = null
@Property({ columnType: "jsonb", nullable: true })
data: Record<string, unknown> | null = null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@OneToMany(
() => ShippingMethodTaxLine,
(taxLine) => taxLine.shipping_method,
{
cascade: [Cascade.PERSIST],
}
)
tax_lines = new Collection<ShippingMethodTaxLine>(this)
@OneToMany(
() => ShippingMethodAdjustment,
(adjustment) => adjustment.shipping_method,
{
cascade: [Cascade.PERSIST],
}
)
adjustments = new Collection<ShippingMethodAdjustment>(this)
@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, "ordsm")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordsm")
}
}

View File

@@ -0,0 +1,48 @@
import { BigNumberRawValue } from "@medusajs/types"
import {BigNumber, MikroOrmBigNumberProperty} from "@medusajs/utils"
import { PrimaryKey, Property } from "@mikro-orm/core"
/**
* 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 {
@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
@MikroOrmBigNumberProperty()
rate: BigNumber | number
@Property({ columnType: "jsonb" })
raw_rate: BigNumberRawValue
@Property({ columnType: "text", nullable: true })
provider_id?: string | 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
}

View File

@@ -0,0 +1,107 @@
import { BigNumberRawValue, DAL } from "@medusajs/types"
import {
BigNumber,
MikroOrmBigNumberProperty,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
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
@ManyToOne({
entity: () => Order,
columnType: "text",
fieldName: "order_id",
onDelete: "cascade",
mapToPk: true,
})
@OrderIdIndex.MikroORMIndex()
order_id: string
@ManyToOne(() => Order, {
persist: false,
})
order: Order
@MikroOrmBigNumberProperty()
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")
this.order_id ??= this.order?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordtrx")
this.order_id ??= this.order?.id
}
}