Feat(order): post purchase support (#7666)

This commit is contained in:
Carlos R. L. Rodrigues
2024-06-10 18:44:51 -03:00
committed by GitHub
parent 39ddba2491
commit 37426939da
43 changed files with 1645 additions and 476 deletions

View File

@@ -8,6 +8,7 @@ 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 Return } from "./return"
export { default as ReturnReason } from "./return-reason"
export { default as ShippingMethod } from "./shipping-method"
export { default as ShippingMethodAdjustment } from "./shipping-method-adjustment"

View File

@@ -14,6 +14,7 @@ import {
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { Return } from "@models"
import Order from "./order"
import OrderChange from "./order-change"
@@ -22,16 +23,31 @@ type OptionalLineItemProps = DAL.EntityDateColumns
const OrderChangeIdIndex = createPsqlIndexStatementHelper({
tableName: "order_change_action",
columns: "order_change_id",
where: "deleted_at IS NOT NULL",
})
const OrderIdIndex = createPsqlIndexStatementHelper({
tableName: "order_change_action",
columns: "order_id",
where: "deleted_at IS NOT NULL",
})
const ReturnIdIndex = createPsqlIndexStatementHelper({
tableName: "order_change_action",
columns: "return_id",
where: "return_id IS NOT NULL AND deleted_at IS NOT NULL",
})
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "order_change_action",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const ActionOrderingIndex = createPsqlIndexStatementHelper({
tableName: "order_change_action",
columns: "ordering",
where: "deleted_at IS NOT NULL",
})
@Entity({ tableName: "order_change_action" })
@@ -62,6 +78,21 @@ export default class OrderChangeAction {
})
order: Order | null = null
@ManyToOne({
entity: () => Return,
mapToPk: true,
fieldName: "return_id",
columnType: "text",
nullable: true,
})
@ReturnIdIndex.MikroORMIndex()
return_id: string | null = null
@ManyToOne(() => Return, {
persist: false,
})
return: Return
@Property({ columnType: "integer", nullable: true })
version: number | null = null
@@ -133,6 +164,10 @@ export default class OrderChangeAction {
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
@DeletedAtIndex.MikroORMIndex()
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordchact")

View File

@@ -1,6 +1,5 @@
import { DAL } from "@medusajs/types"
import {
OrderChangeStatus,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
@@ -17,6 +16,8 @@ import {
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { Return } from "@models"
import { OrderChangeStatus, OrderChangeType } from "@types"
import Order from "./order"
import OrderChangeAction from "./order-change-action"
@@ -25,16 +26,37 @@ type OptionalLineItemProps = DAL.EntityDateColumns
const OrderIdIndex = createPsqlIndexStatementHelper({
tableName: "order_change",
columns: "order_id",
where: "deleted_at IS NOT NULL",
})
const ReturnIdIndex = createPsqlIndexStatementHelper({
tableName: "order_change",
columns: "return_id",
where: "return_id IS NOT NULL AND deleted_at IS NOT NULL",
})
const OrderChangeStatusIndex = createPsqlIndexStatementHelper({
tableName: "order_change",
columns: "status",
where: "deleted_at IS NOT NULL",
})
const OrderChangeTypeIndex = createPsqlIndexStatementHelper({
tableName: "order_change",
columns: "change_type",
where: "deleted_at IS NOT NULL",
})
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "order_change",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const VersionIndex = createPsqlIndexStatementHelper({
tableName: "order_change",
columns: ["order_id", "version"],
where: "deleted_at IS NOT NULL",
})
@Entity({ tableName: "order_change" })
@@ -60,10 +82,29 @@ export default class OrderChange {
})
order: Order
@ManyToOne({
entity: () => Return,
mapToPk: true,
fieldName: "return_id",
columnType: "text",
nullable: true,
})
@ReturnIdIndex.MikroORMIndex()
return_id: string | null = null
@ManyToOne(() => Return, {
persist: false,
})
return: Return
@Property({ columnType: "integer" })
@VersionIndex.MikroORMIndex()
version: number
@Enum({ items: () => OrderChangeType, nullable: true })
@OrderChangeTypeIndex.MikroORMIndex()
change_type: OrderChangeType | null = null
@OneToMany(() => OrderChangeAction, (action) => action.order_change, {
cascade: [Cascade.PERSIST, "sotf-remove" as Cascade],
})
@@ -142,6 +183,10 @@ export default class OrderChange {
})
updated_at: Date
@Property({ columnType: "timestamptz", nullable: true })
@DeletedAtIndex.MikroORMIndex()
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordch")

View File

@@ -14,6 +14,7 @@ import {
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { Return } from "@models"
import LineItem from "./line-item"
import Order from "./order"
@@ -25,6 +26,12 @@ const OrderIdIndex = createPsqlIndexStatementHelper({
where: "deleted_at IS NOT NULL",
})
const ReturnIdIndex = createPsqlIndexStatementHelper({
tableName: "order_item",
columns: "return_id",
where: "return_id IS NOT NULL AND deleted_at IS NOT NULL",
})
const OrderVersionIndex = createPsqlIndexStatementHelper({
tableName: "order_item",
columns: ["version"],
@@ -59,15 +66,30 @@ export default class OrderItem {
@OrderIdIndex.MikroORMIndex()
order_id: string
@Property({ columnType: "integer" })
@OrderVersionIndex.MikroORMIndex()
version: number
@ManyToOne(() => Order, {
persist: false,
})
order: Order
@ManyToOne({
entity: () => Return,
mapToPk: true,
fieldName: "return_id",
columnType: "text",
nullable: true,
})
@ReturnIdIndex.MikroORMIndex()
return_id: string | null = null
@ManyToOne(() => Return, {
persist: false,
})
return: Return
@Property({ columnType: "integer" })
@OrderVersionIndex.MikroORMIndex()
version: number
@ManyToOne({
entity: () => LineItem,
fieldName: "item_id",

View File

@@ -12,6 +12,7 @@ import {
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { Return } from "@models"
import Order from "./order"
import ShippingMethod from "./shipping-method"
@@ -23,6 +24,12 @@ const OrderIdIndex = createPsqlIndexStatementHelper({
where: "deleted_at IS NOT NULL",
})
const ReturnIdIndex = createPsqlIndexStatementHelper({
tableName: "order_shipping",
columns: "return_id",
where: "return_id IS NOT NULL AND deleted_at IS NOT NULL",
})
const OrderVersionIndex = createPsqlIndexStatementHelper({
tableName: "order_shipping",
columns: ["version"],
@@ -57,15 +64,30 @@ export default class OrderShippingMethod {
@OrderIdIndex.MikroORMIndex()
order_id: string
@Property({ columnType: "integer" })
@OrderVersionIndex.MikroORMIndex()
version: number
@ManyToOne(() => Order, {
persist: false,
})
order: Order
@ManyToOne({
entity: () => Return,
mapToPk: true,
fieldName: "return_id",
columnType: "text",
nullable: true,
})
@ReturnIdIndex.MikroORMIndex()
return_id: string | null = null
@ManyToOne(() => Return, {
persist: false,
})
return: Return
@Property({ columnType: "integer" })
@OrderVersionIndex.MikroORMIndex()
version: number
@ManyToOne({
entity: () => ShippingMethod,
fieldName: "shipping_method_id",

View File

@@ -0,0 +1,184 @@
import { BigNumberRawValue, DAL } from "@medusajs/types"
import {
BigNumber,
MikroOrmBigNumberProperty,
ReturnStatus,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Enum,
ManyToOne,
OnInit,
OneToMany,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { OrderItem, OrderShippingMethod } from "@models"
import Order from "./order"
type OptionalReturnProps = DAL.EntityDateColumns
const DisplayIdIndex = createPsqlIndexStatementHelper({
tableName: "return",
columns: "display_id",
where: "deleted_at IS NOT NULL",
})
const ReturnDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "return",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const OrderIdIndex = createPsqlIndexStatementHelper({
tableName: "return",
columns: ["order_id"],
where: "deleted_at IS NOT NULL",
})
const ClaimIdIndex = createPsqlIndexStatementHelper({
tableName: "return",
columns: ["claim_id"],
where: "claim_id IS NOT NULL AND deleted_at IS NOT NULL",
})
const ExchageIdIndex = createPsqlIndexStatementHelper({
tableName: "return",
columns: ["exchange_id"],
where: "exchange_id IS NOT NULL AND deleted_at IS NOT NULL",
})
@Entity({ tableName: "return" })
export default class Return {
[OptionalProps]?: OptionalReturnProps
@PrimaryKey({ columnType: "text" })
id: string
@ManyToOne({
entity: () => Order,
mapToPk: true,
fieldName: "order_id",
columnType: "text",
})
@OrderIdIndex.MikroORMIndex()
order_id: string
@ManyToOne(() => Order, {
persist: false,
})
order: Order
/*
@ManyToOne({
entity: () => Claim,
mapToPk: true,
fieldName: "claim_id",
columnType: "text",
nullable: true,
})
@ClaimIdIndex.MikroORMIndex()
claim_id: string | null
@ManyToOne(() => Claim, {
persist: false,
})
claim: Claim | null
@ManyToOne({
entity: () => Exchange,
mapToPk: true,
fieldName: "exchange_id",
columnType: "text",
nullable: true,
})
@ExchangeIdIndex.MikroORMIndex()
exchange_id: string | null
@ManyToOne(() => Exchange, {
persist: false,
})
exchange: Exchange | null
*/
@Property({
columnType: "integer",
})
order_version: number
@Property({ autoincrement: true, primary: false })
@DisplayIdIndex.MikroORMIndex()
display_id: number
@Enum({ items: () => ReturnStatus, default: ReturnStatus.REQUESTED })
status: ReturnStatus = ReturnStatus.REQUESTED
@Property({ columnType: "boolean", nullable: true })
no_notification: boolean | null = null
@MikroOrmBigNumberProperty({
nullable: true,
})
refund_amount: BigNumber | number
@Property({ columnType: "jsonb", nullable: true })
raw_refund_amount: BigNumberRawValue
@OneToMany(() => OrderItem, (itemDetail) => itemDetail.return, {
cascade: [Cascade.PERSIST],
})
items = new Collection<OrderItem>(this)
@OneToMany(
() => OrderShippingMethod,
(shippingMethod) => shippingMethod.return,
{
cascade: [Cascade.PERSIST],
}
)
shipping_methods = new Collection<OrderShippingMethod>(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
@Property({ columnType: "timestamptz", nullable: true })
@ReturnDeletedAtIndex.MikroORMIndex()
deleted_at: Date | null = null
@Property({ columnType: "timestamptz", nullable: true })
received_at: Date | null = null
@Property({ columnType: "timestamptz", nullable: true })
canceled_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "return")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "return")
}
}

View File

@@ -14,6 +14,7 @@ import {
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { Return } from "@models"
import Order from "./order"
type OptionalLineItemProps = DAL.EntityDateColumns
@@ -21,16 +22,25 @@ type OptionalLineItemProps = DAL.EntityDateColumns
const ReferenceIdIndex = createPsqlIndexStatementHelper({
tableName: "order_transaction",
columns: "reference_id",
where: "deleted_at IS NOT NULL",
})
const OrderIdIndex = createPsqlIndexStatementHelper({
tableName: "order_transaction",
columns: "order_id",
where: "deleted_at IS NOT NULL",
})
const ReturnIdIndex = createPsqlIndexStatementHelper({
tableName: "order_transaction",
columns: "return_id",
where: "return_id IS NOT NULL AND deleted_at IS NOT NULL",
})
const CurrencyCodeIndex = createPsqlIndexStatementHelper({
tableName: "order_transaction",
columns: "currency_code",
where: "deleted_at IS NOT NULL",
})
const DeletedAtIndex = createPsqlIndexStatementHelper({
@@ -68,6 +78,21 @@ export default class Transaction {
})
order: Order
@ManyToOne({
entity: () => Return,
mapToPk: true,
fieldName: "return_id",
columnType: "text",
nullable: true,
})
@ReturnIdIndex.MikroORMIndex()
return_id: string | null = null
@ManyToOne(() => Return, {
persist: false,
})
return: Return
@Property({
columnType: "integer",
defaultRaw: "1",