feat(order): order change actions engine (#6467)

This commit is contained in:
Carlos R. L. Rodrigues
2024-02-29 16:22:14 -03:00
committed by GitHub
parent cdb01e073b
commit 03ca5c814e
70 changed files with 5727 additions and 992 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ 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 OrderDetail } from "./order-detail"
export { default as OrderItem } from "./order-item"
export { default as ShippingMethod } from "./shipping-method"
export { default as ShippingMethodAdjustment } from "./shipping-method-adjustment"
export { default as ShippingMethodTaxLine } from "./shipping-method-tax-line"
@@ -5,11 +5,9 @@ import {
import {
BeforeCreate,
Cascade,
Check,
Entity,
ManyToOne,
OnInit,
Property,
} from "@mikro-orm/core"
import AdjustmentLine from "./adjustment-line"
import LineItem from "./line-item"
@@ -20,27 +18,31 @@ const ItemIdIndex = createPsqlIndexStatementHelper({
})
@Entity({ tableName: "order_line_item_adjustment" })
@Check<LineItemAdjustment>({
expression: (columns) => `${columns.amount} >= 0`,
})
export default class LineItemAdjustment extends AdjustmentLine {
@ManyToOne({
entity: () => LineItem,
cascade: [Cascade.REMOVE, Cascade.PERSIST],
@ManyToOne(() => LineItem, {
persist: false,
})
item: LineItem
@Property({ columnType: "text" })
@ManyToOne({
entity: () => LineItem,
columnType: "text",
fieldName: "item_id",
cascade: [Cascade.REMOVE],
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
}
}
@@ -8,7 +8,6 @@ import {
Entity,
ManyToOne,
OnInit,
Property,
} from "@mikro-orm/core"
import LineItem from "./line-item"
import TaxLine from "./tax-line"
@@ -20,23 +19,31 @@ const ItemIdIndex = createPsqlIndexStatementHelper({
@Entity({ tableName: "order_line_item_tax_line" })
export default class LineItemTaxLine extends TaxLine {
@ManyToOne({
entity: () => LineItem,
cascade: [Cascade.REMOVE, Cascade.PERSIST],
@ManyToOne(() => LineItem, {
fieldName: "item_id",
persist: false,
})
item: LineItem
@Property({ columnType: "text" })
@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
}
}
+4 -13
View File
@@ -1,25 +1,23 @@
import { BigNumberRawValue, DAL } from "@medusajs/types"
import {
BigNumber,
MikroOrmBigNumberProperty,
createPsqlIndexStatementHelper,
generateEntityId,
MikroOrmBigNumberProperty,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
ManyToOne,
OneToMany,
OnInit,
OneToMany,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import LineItemAdjustment from "./line-item-adjustment"
import LineItemTaxLine from "./line-item-tax-line"
import OrderDetail from "./order-detail"
type OptionalLineItemProps =
| "is_discoutable"
@@ -45,13 +43,6 @@ export default class LineItem {
@PrimaryKey({ columnType: "text" })
id: string
@ManyToOne({
entity: () => OrderDetail,
onDelete: "cascade",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
})
totals: OrderDetail
@Property({ columnType: "text" })
title: string
@@ -131,12 +122,12 @@ export default class LineItem {
raw_unit_price: BigNumberRawValue
@OneToMany(() => LineItemTaxLine, (taxLine) => taxLine.item, {
cascade: [Cascade.REMOVE],
cascade: [Cascade.PERSIST],
})
tax_lines = new Collection<LineItemTaxLine>(this)
@OneToMany(() => LineItemAdjustment, (adjustment) => adjustment.item, {
cascade: [Cascade.REMOVE],
cascade: [Cascade.PERSIST],
})
adjustments = new Collection<LineItemAdjustment>(this)
@@ -22,26 +22,31 @@ const OrderChangeIdIndex = createPsqlIndexStatementHelper({
columns: "order_change_id",
})
const ReferenceIdIndex = createPsqlIndexStatementHelper({
const ReferenceIndex = createPsqlIndexStatementHelper({
tableName: "order_change_action",
columns: "reference_id",
columns: ["reference", "reference_id"],
})
@Entity({ tableName: "order_change_action" })
@ReferenceIndex.MikroORMIndex()
export default class OrderChangeAction {
[OptionalProps]?: OptionalLineItemProps
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
@ManyToOne({
entity: () => OrderChange,
columnType: "text",
fieldName: "order_change_id",
cascade: [Cascade.REMOVE],
mapToPk: true,
})
@OrderChangeIdIndex.MikroORMIndex()
order_change_id: string
@ManyToOne({
entity: () => OrderChange,
fieldName: "order_change_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
@ManyToOne(() => OrderChange, {
persist: false,
})
order_change: OrderChange
@@ -49,7 +54,6 @@ export default class OrderChangeAction {
reference: string
@Property({ columnType: "text" })
@ReferenceIdIndex.MikroORMIndex()
reference_id: string
@Property({ columnType: "jsonb" })
@@ -82,10 +86,12 @@ export default class OrderChangeAction {
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordchact")
this.order_change_id ??= this.order_change?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordchact")
this.order_change_id ??= this.order_change?.id
}
}
+21 -5
View File
@@ -32,24 +32,38 @@ const OrderChangeStatusIndex = createPsqlIndexStatementHelper({
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
@Property({ columnType: "text" })
@ManyToOne({
entity: () => Order,
columnType: "text",
fieldName: "order_id",
cascade: [Cascade.REMOVE],
mapToPk: true,
})
@OrderIdIndex.MikroORMIndex()
order_id: string
@ManyToOne({
entity: () => Order,
fieldName: "order_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
@ManyToOne(() => Order, {
persist: false,
})
order: Order
@Property({ columnType: "integer" })
@VersionIndex.MikroORMIndex()
version: number
@OneToMany(() => OrderChangeAction, (action) => action.order_change_id, {
cascade: [Cascade.REMOVE],
})
@@ -131,10 +145,12 @@ export default class OrderChange {
@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
}
}
@@ -1,13 +1,12 @@
import { BigNumberRawValue, DAL } from "@medusajs/types"
import {
BigNumber,
MikroOrmBigNumberProperty,
createPsqlIndexStatementHelper,
generateEntityId,
MikroOrmBigNumberProperty,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Entity,
ManyToOne,
OnInit,
@@ -15,45 +14,62 @@ import {
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { ItemSummary } from "../types/common"
import LineItem from "./line-item"
import Order from "./order"
type OptionalLineItemProps = DAL.EntityDateColumns
const OrderItemVersionIndex = createPsqlIndexStatementHelper({
tableName: "order_detail",
columns: ["order_id", "item_id", "version"],
unique: true,
const OrderIdIndex = createPsqlIndexStatementHelper({
tableName: "order_item",
columns: ["order_id"],
})
@Entity({ tableName: "order_detail" })
@OrderItemVersionIndex.MikroORMIndex()
export default class OrderDetail {
const OrderVersionIndex = createPsqlIndexStatementHelper({
tableName: "order_item",
columns: ["version"],
})
const ItemIdIndex = createPsqlIndexStatementHelper({
tableName: "order_item",
columns: ["item_id"],
})
@Entity({ tableName: "order_item" })
export default class OrderItem {
[OptionalProps]?: OptionalLineItemProps
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
@ManyToOne({
entity: () => Order,
mapToPk: true,
fieldName: "order_id",
columnType: "text",
})
@OrderIdIndex.MikroORMIndex()
order_id: string
@Property({ columnType: "integer" })
@OrderVersionIndex.MikroORMIndex()
version: number
@ManyToOne({
entity: () => Order,
onDelete: "cascade",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
@ManyToOne(() => Order, {
persist: false,
})
order: Order
@Property({ columnType: "text" })
item_id: string
@ManyToOne({
entity: () => LineItem,
onDelete: "cascade",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
fieldName: "item_id",
mapToPk: true,
columnType: "text",
})
@ItemIdIndex.MikroORMIndex()
item_id: string
@ManyToOne(() => LineItem, {
persist: false,
})
item: LineItem
@@ -64,43 +80,43 @@ export default class OrderDetail {
raw_quantity: BigNumberRawValue
@MikroOrmBigNumberProperty()
fulfilled_quantity: BigNumber | number
fulfilled_quantity: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_fulfilled_quantity: BigNumberRawValue
@MikroOrmBigNumberProperty()
shipped_quantity: BigNumber | number
shipped_quantity: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_shipped_quantity: BigNumberRawValue
@MikroOrmBigNumberProperty()
return_requested_quantity: BigNumber | number
return_requested_quantity: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_return_requested_quantity: BigNumberRawValue
@MikroOrmBigNumberProperty()
return_received_quantity: BigNumber | number
return_received_quantity: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_return_received_quantity: BigNumberRawValue
@MikroOrmBigNumberProperty()
return_dismissed_quantity: BigNumber | number
return_dismissed_quantity: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_return_dismissed_quantity: BigNumberRawValue
@MikroOrmBigNumberProperty()
written_off_quantity: BigNumber | number
written_off_quantity: BigNumber | number = 0
@Property({ columnType: "jsonb" })
raw_written_off_quantity: BigNumberRawValue
@Property({ columnType: "jsonb" })
summary: ItemSummary | null = {} as ItemSummary
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@Property({
onCreate: () => new Date(),
@@ -119,11 +135,15 @@ export default class OrderDetail {
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordlisum")
this.id = generateEntityId(this.id, "orditem")
this.order_id ??= this.order?.id
this.item_id ??= this.item?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordlisum")
this.id = generateEntityId(this.id, "orditem")
this.order_id ??= this.order?.id
this.item_id ??= this.item?.id
}
}
+6 -6
View File
@@ -19,7 +19,7 @@ import {
} from "@mikro-orm/core"
import { OrderSummary } from "../types/common"
import Address from "./address"
import OrderDetail from "./order-detail"
import OrderItem from "./order-item"
import ShippingMethod from "./shipping-method"
import Transaction from "./transaction"
@@ -160,18 +160,18 @@ export default class Order {
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@OneToMany(() => OrderDetail, (itemDetail) => itemDetail.order, {
cascade: [Cascade.REMOVE],
@OneToMany(() => OrderItem, (itemDetail) => itemDetail.order, {
cascade: [Cascade.PERSIST],
})
items = new Collection<OrderDetail>(this)
items = new Collection<OrderItem>(this)
@OneToMany(() => ShippingMethod, (shippingMethod) => shippingMethod.order, {
cascade: [Cascade.REMOVE],
cascade: [Cascade.PERSIST],
})
shipping_methods = new Collection<ShippingMethod>(this)
@OneToMany(() => Transaction, (transaction) => transaction.order, {
cascade: [Cascade.REMOVE],
cascade: [Cascade.PERSIST],
})
transactions = new Collection<Transaction>(this)
@@ -8,7 +8,6 @@ import {
Entity,
ManyToOne,
OnInit,
Property,
} from "@mikro-orm/core"
import AdjustmentLine from "./adjustment-line"
import ShippingMethod from "./shipping-method"
@@ -20,24 +19,30 @@ const ShippingMethodIdIdIndex = createPsqlIndexStatementHelper({
@Entity({ tableName: "order_shipping_method_adjustment" })
export default class ShippingMethodAdjustment extends AdjustmentLine {
@ManyToOne({
entity: () => ShippingMethod,
fieldName: "shipping_method_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
@ManyToOne(() => ShippingMethod, {
persist: false,
})
shipping_method: ShippingMethod
@Property({ columnType: "text" })
@ManyToOne({
entity: () => ShippingMethod,
columnType: "text",
fieldName: "shipping_method_id",
mapToPk: true,
cascade: [Cascade.REMOVE],
})
@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
}
}
@@ -8,7 +8,6 @@ import {
Entity,
ManyToOne,
OnInit,
Property,
} from "@mikro-orm/core"
import ShippingMethod from "./shipping-method"
import TaxLine from "./tax-line"
@@ -20,24 +19,30 @@ const ShippingMethodIdIdIndex = createPsqlIndexStatementHelper({
@Entity({ tableName: "order_shipping_method_tax_line" })
export default class ShippingMethodTaxLine extends TaxLine {
@ManyToOne({
entity: () => ShippingMethod,
fieldName: "shipping_method_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
@ManyToOne(() => ShippingMethod, {
persist: false,
})
shipping_method: ShippingMethod
@Property({ columnType: "text" })
@ManyToOne({
entity: () => ShippingMethod,
fieldName: "shipping_method_id",
columnType: "text",
mapToPk: true,
cascade: [Cascade.REMOVE],
})
@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
}
}
+25 -6
View File
@@ -8,7 +8,6 @@ import {
import {
BeforeCreate,
Cascade,
Check,
Collection,
Entity,
ManyToOne,
@@ -31,23 +30,41 @@ const OrderIdIndex = createPsqlIndexStatementHelper({
columns: "order_id",
})
const OrderVersionIndex = createPsqlIndexStatementHelper({
tableName: "order_shipping_method",
columns: ["order_id", "version"],
})
@Entity({ tableName: "order_shipping_method" })
@Check<ShippingMethod>({ expression: (columns) => `${columns.amount} >= 0` })
@OrderVersionIndex.MikroORMIndex()
export default class ShippingMethod {
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
@ManyToOne({
entity: () => Order,
columnType: "text",
fieldName: "order_id",
mapToPk: true,
cascade: [Cascade.REMOVE],
})
@OrderIdIndex.MikroORMIndex()
order_id: string
@ManyToOne({
entity: () => Order,
fieldName: "order_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
cascade: [Cascade.REMOVE],
persist: false,
})
order: Order
@Property({
columnType: "integer",
defaultRaw: "1",
})
version: number = 1
@Property({ columnType: "text" })
name: string
@@ -80,7 +97,7 @@ export default class ShippingMethod {
() => ShippingMethodTaxLine,
(taxLine) => taxLine.shipping_method,
{
cascade: [Cascade.REMOVE],
cascade: [Cascade.PERSIST],
}
)
tax_lines = new Collection<ShippingMethodTaxLine>(this)
@@ -89,7 +106,7 @@ export default class ShippingMethod {
() => ShippingMethodAdjustment,
(adjustment) => adjustment.shipping_method,
{
cascade: [Cascade.REMOVE],
cascade: [Cascade.PERSIST],
}
)
adjustments = new Collection<ShippingMethodAdjustment>(this)
@@ -112,9 +129,11 @@ export default class ShippingMethod {
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordsm")
this.order_id ??= this.order?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordsm")
this.order_id ??= this.order?.id
}
}
+12 -6
View File
@@ -1,9 +1,9 @@
import { BigNumberRawValue, DAL } from "@medusajs/types"
import {
BigNumber,
MikroOrmBigNumberProperty,
createPsqlIndexStatementHelper,
generateEntityId,
MikroOrmBigNumberProperty,
} from "@medusajs/utils"
import {
BeforeCreate,
@@ -41,14 +41,18 @@ export default class Transaction {
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
@ManyToOne({
entity: () => Order,
columnType: "text",
fieldName: "order_id",
cascade: [Cascade.REMOVE],
mapToPk: true,
})
@OrderIdIndex.MikroORMIndex()
order_id: string
@ManyToOne({
entity: () => Order,
fieldName: "order_id",
cascade: [Cascade.REMOVE, Cascade.PERSIST],
@ManyToOne(() => Order, {
persist: false,
})
order: Order
@@ -93,10 +97,12 @@ export default class Transaction {
@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
}
}