feat(order): create claim and exchange (#7734)
This commit is contained in:
committed by
GitHub
parent
e0b14519f1
commit
cfa983001b
@@ -19,31 +19,6 @@ export class Migration20240604100512 extends Migration {
|
||||
|
||||
|
||||
|
||||
ALTER TABLE "order_item"
|
||||
ADD COLUMN if NOT exists "return_id" TEXT NULL;
|
||||
|
||||
ALTER TABLE "order_item"
|
||||
ADD COLUMN if NOT exists "claim_id" TEXT NULL;
|
||||
|
||||
ALTER TABLE "order_item"
|
||||
ADD COLUMN if NOT exists "exchange_id" TEXT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "IDX_order_item_return_id" ON "order_item" (
|
||||
return_id
|
||||
)
|
||||
WHERE return_id IS NOT NULL AND deleted_at IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "IDX_order_item_claim_id" ON "order_item" (
|
||||
claim_id
|
||||
)
|
||||
WHERE claim_id IS NOT NULL AND deleted_at IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "IDX_order_item_exchange_id" ON "order_item" (
|
||||
exchange_id
|
||||
)
|
||||
WHERE exchange_id IS NOT NULL AND deleted_at IS NOT NULL;
|
||||
|
||||
|
||||
|
||||
ALTER TABLE "order_transaction"
|
||||
ADD COLUMN if NOT exists "return_id" TEXT NULL;
|
||||
@@ -205,6 +180,36 @@ export class Migration20240604100512 extends Migration {
|
||||
WHERE deleted_at IS NOT NULL;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "return_item" (
|
||||
"id" TEXT NOT NULL,
|
||||
"return_id" TEXT NOT NULL,
|
||||
"reason_id" TEXT NULL,
|
||||
"item_id" TEXT NOT NULL,
|
||||
"quantity" NUMERIC NOT NULL,
|
||||
"raw_quantity" JSONB NOT NULL,
|
||||
"received_quantity" NUMERIC NOT NULL DEFAULT 0,
|
||||
"raw_received_quantity" JSONB NOT NULL,
|
||||
"note" TEXT NULL,
|
||||
"metadata" JSONB NULL,
|
||||
"created_at" timestamptz NOT NULL DEFAULT now(),
|
||||
"updated_at" timestamptz NOT NULL DEFAULT now(),
|
||||
"deleted_at" timestamptz NULL,
|
||||
CONSTRAINT "return_item_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "IDX_return_item_deleted_at" ON "return_item" ("deleted_at")
|
||||
WHERE deleted_at IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "IDX_return_item_return_id" ON "return_item" ("return_id")
|
||||
WHERE deleted_at IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "IDX_return_item_item_id" ON "return_item" ("item_id")
|
||||
WHERE deleted_at IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "IDX_return_item_reason_id" ON "return_item" ("reason_id")
|
||||
WHERE deleted_at IS NOT NULL;
|
||||
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "order_exchange" (
|
||||
"id" TEXT NOT NULL,
|
||||
@@ -213,8 +218,6 @@ export class Migration20240604100512 extends Migration {
|
||||
"order_version" INTEGER NOT NULL,
|
||||
"display_id" SERIAL,
|
||||
"no_notification" BOOLEAN NULL,
|
||||
"refund_amount" NUMERIC NULL,
|
||||
"raw_refund_amount" JSONB NULL,
|
||||
"allow_backorder" BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
"difference_due" NUMERIC NULL,
|
||||
"raw_difference_due" JSONB NULL,
|
||||
@@ -239,6 +242,30 @@ export class Migration20240604100512 extends Migration {
|
||||
WHERE return_id IS NOT NULL AND deleted_at IS NOT NULL;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "order_exchange_item" (
|
||||
"id" TEXT NOT NULL,
|
||||
"exchange_id" TEXT NOT NULL,
|
||||
"item_id" TEXT NOT NULL,
|
||||
"quantity" NUMERIC NOT NULL,
|
||||
"raw_quantity" JSONB NOT NULL,
|
||||
"note" TEXT NULL,
|
||||
"metadata" JSONB NULL,
|
||||
"created_at" timestamptz NOT NULL DEFAULT now(),
|
||||
"updated_at" timestamptz NOT NULL DEFAULT now(),
|
||||
"deleted_at" timestamptz NULL,
|
||||
CONSTRAINT "order_exchange_item_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "IDX_order_exchange_item_deleted_at" ON "order_exchange_item" ("deleted_at")
|
||||
WHERE deleted_at IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "IDX_order_exchange_item_exchange_id" ON "order_exchange_item" ("exchange_id")
|
||||
WHERE deleted_at IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "IDX_order_exchange_item_item_id" ON "order_exchange_item" ("item_id")
|
||||
WHERE deleted_at IS NOT NULL;
|
||||
|
||||
|
||||
|
||||
CREATE TYPE order_claim_type_enum AS ENUM (
|
||||
'refund',
|
||||
@@ -288,7 +315,10 @@ export class Migration20240604100512 extends Migration {
|
||||
"id" TEXT NOT NULL,
|
||||
"claim_id" TEXT NOT NULL,
|
||||
"item_id" TEXT NOT NULL,
|
||||
"reason" claim_reason_enum NOT NULL,
|
||||
"is_additional_item" BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
"reason" claim_reason_enum NULL,
|
||||
"quantity" NUMERIC NOT NULL,
|
||||
"raw_quantity" JSONB NOT NULL,
|
||||
"note" TEXT NULL,
|
||||
"metadata" JSONB NULL,
|
||||
"created_at" timestamptz NOT NULL DEFAULT now(),
|
||||
|
||||
@@ -76,11 +76,13 @@ export default class ClaimItemImage {
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "ordclaimimg")
|
||||
this.id = generateEntityId(this.id, "climg")
|
||||
this.claim_item_id = this.item?.id
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "ordclaimimg")
|
||||
this.id = generateEntityId(this.id, "climg")
|
||||
this.claim_item_id = this.item?.id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { BigNumberRawValue, DAL } from "@medusajs/types"
|
||||
import {
|
||||
ClaimReason,
|
||||
MikroOrmBigNumberProperty,
|
||||
createPsqlIndexStatementHelper,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
@@ -26,11 +27,19 @@ type OptionalLineItemProps = DAL.EntityDateColumns
|
||||
const ClaimIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_claim_item",
|
||||
columns: "claim_id",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const ItemIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_claim_item",
|
||||
columns: "item_id",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const DeletedAtIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_claim_item_image",
|
||||
columns: "deleted_at",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
@Entity({ tableName: "order_claim_item" })
|
||||
@@ -45,8 +54,14 @@ export default class OrderClaimItem {
|
||||
})
|
||||
images = new Collection<ClaimItemImage>(this)
|
||||
|
||||
@Enum({ items: () => ClaimReason })
|
||||
reason: ClaimReason
|
||||
@Enum({ items: () => ClaimReason, nullable: true })
|
||||
reason: ClaimReason | null = null
|
||||
|
||||
@MikroOrmBigNumberProperty()
|
||||
quantity: Number | number
|
||||
|
||||
@Property({ columnType: "jsonb" })
|
||||
raw_quantity: BigNumberRawValue
|
||||
|
||||
@ManyToOne(() => Claim, {
|
||||
columnType: "text",
|
||||
@@ -76,6 +91,9 @@ export default class OrderClaimItem {
|
||||
})
|
||||
item: LineItem
|
||||
|
||||
@Property({ columnType: "boolean", default: false })
|
||||
is_additional_item: boolean = false
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
note: string
|
||||
|
||||
@@ -97,13 +115,19 @@ export default class OrderClaimItem {
|
||||
})
|
||||
updated_at: Date
|
||||
|
||||
@Property({ columnType: "timestamptz", nullable: true })
|
||||
@DeletedAtIndex.MikroORMIndex()
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "ordclaimitem")
|
||||
this.id = generateEntityId(this.id, "claitem")
|
||||
this.claim_id = this.claim?.id
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "ordclaimitem")
|
||||
this.id = generateEntityId(this.id, "claitem")
|
||||
this.claim_id = this.claim?.id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,9 @@ import {
|
||||
} from "@mikro-orm/core"
|
||||
import ClaimItem from "./claim-item"
|
||||
import Order from "./order"
|
||||
import OrderItem from "./order-item"
|
||||
import OrderShippingMethod from "./order-shipping-method"
|
||||
import Return from "./return"
|
||||
import Transaction from "./transaction"
|
||||
|
||||
type OptionalOrderClaimProps = DAL.EntityDateColumns
|
||||
|
||||
@@ -110,12 +110,12 @@ export default class OrderClaim {
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
raw_refund_amount: BigNumberRawValue
|
||||
|
||||
@OneToMany(() => OrderItem, (itemDetail) => itemDetail.claim, {
|
||||
@OneToMany(() => ClaimItem, (item) => item.claim, {
|
||||
cascade: [Cascade.PERSIST],
|
||||
})
|
||||
items = new Collection<OrderItem>(this)
|
||||
additional_items = new Collection<ClaimItem>(this)
|
||||
|
||||
@OneToMany(() => ClaimItem, (itemDetail) => itemDetail.claim, {
|
||||
@OneToMany(() => ClaimItem, (item) => item.claim, {
|
||||
cascade: [Cascade.PERSIST],
|
||||
})
|
||||
claim_items = new Collection<ClaimItem>(this)
|
||||
@@ -129,6 +129,11 @@ export default class OrderClaim {
|
||||
)
|
||||
shipping_methods = new Collection<OrderShippingMethod>(this)
|
||||
|
||||
@OneToMany(() => Transaction, (transaction) => transaction.claim, {
|
||||
cascade: [Cascade.PERSIST],
|
||||
})
|
||||
transactions = new Collection<Transaction>(this)
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@@ -156,11 +161,11 @@ export default class OrderClaim {
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "ordclaim")
|
||||
this.id = generateEntityId(this.id, "claim")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "ordclaim")
|
||||
this.id = generateEntityId(this.id, "claim")
|
||||
}
|
||||
}
|
||||
|
||||
116
packages/modules/order/src/models/exchange-item.ts
Normal file
116
packages/modules/order/src/models/exchange-item.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import { BigNumberRawValue, DAL } from "@medusajs/types"
|
||||
import {
|
||||
MikroOrmBigNumberProperty,
|
||||
createPsqlIndexStatementHelper,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import Exchange from "./exchange"
|
||||
import LineItem from "./line-item"
|
||||
|
||||
type OptionalLineItemProps = DAL.EntityDateColumns
|
||||
|
||||
const ExchangeIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_exchange_item",
|
||||
columns: "exchange_id",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const ItemIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_exchange_item",
|
||||
columns: "item_id",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const DeletedAtIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_claim_item_image",
|
||||
columns: "deleted_at",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
@Entity({ tableName: "order_exchange_item" })
|
||||
export default class OrderExchangeItem {
|
||||
[OptionalProps]?: OptionalLineItemProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@MikroOrmBigNumberProperty()
|
||||
quantity: Number | number
|
||||
|
||||
@Property({ columnType: "jsonb" })
|
||||
raw_quantity: BigNumberRawValue
|
||||
|
||||
@ManyToOne(() => Exchange, {
|
||||
columnType: "text",
|
||||
fieldName: "exchange_id",
|
||||
mapToPk: true,
|
||||
onDelete: "cascade",
|
||||
})
|
||||
@ExchangeIdIndex.MikroORMIndex()
|
||||
exchange_id: string
|
||||
|
||||
@ManyToOne(() => Exchange, {
|
||||
persist: false,
|
||||
})
|
||||
exchange: Exchange
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => LineItem,
|
||||
fieldName: "item_id",
|
||||
mapToPk: true,
|
||||
columnType: "text",
|
||||
})
|
||||
@ItemIdIndex.MikroORMIndex()
|
||||
item_id: string
|
||||
|
||||
@ManyToOne(() => LineItem, {
|
||||
persist: false,
|
||||
})
|
||||
item: LineItem
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
note: string
|
||||
|
||||
@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, "oexcitem")
|
||||
this.exchange_id = this.exchange?.id
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "oexcitem")
|
||||
this.exchange_id = this.exchange?.id
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,8 @@ import {
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import { ExchangeItem, Transaction } from "@models"
|
||||
import Order from "./order"
|
||||
import OrderItem from "./order-item"
|
||||
import OrderShippingMethod from "./order-shipping-method"
|
||||
import Return from "./return"
|
||||
|
||||
@@ -96,14 +96,6 @@ export default class OrderExchange {
|
||||
@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
|
||||
|
||||
@MikroOrmBigNumberProperty({
|
||||
nullable: true,
|
||||
})
|
||||
@@ -115,10 +107,10 @@ export default class OrderExchange {
|
||||
@Property({ columnType: "boolean", default: false })
|
||||
allow_backorder: boolean = false
|
||||
|
||||
@OneToMany(() => OrderItem, (itemDetail) => itemDetail.exchange, {
|
||||
@OneToMany(() => ExchangeItem, (item) => item.exchange, {
|
||||
cascade: [Cascade.PERSIST],
|
||||
})
|
||||
items = new Collection<OrderItem>(this)
|
||||
additional_items = new Collection<ExchangeItem>(this)
|
||||
|
||||
@OneToMany(
|
||||
() => OrderShippingMethod,
|
||||
@@ -129,6 +121,11 @@ export default class OrderExchange {
|
||||
)
|
||||
shipping_methods = new Collection<OrderShippingMethod>(this)
|
||||
|
||||
@OneToMany(() => Transaction, (transaction) => transaction.exchange, {
|
||||
cascade: [Cascade.PERSIST],
|
||||
})
|
||||
transactions = new Collection<Transaction>(this)
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@@ -156,11 +153,11 @@ export default class OrderExchange {
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "ordexchange")
|
||||
this.id = generateEntityId(this.id, "oexc")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "ordexchange")
|
||||
this.id = generateEntityId(this.id, "oexc")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
export { default as Address } from "./address"
|
||||
export { default as Claim } from "./claim"
|
||||
export { default as OrderClaim } from "./claim"
|
||||
export { default as ClaimItem } from "./claim-item"
|
||||
export { default as ClaimItemImage } from "./claim-item-image"
|
||||
export { default as Exchange } from "./exchange"
|
||||
export { default as OrderExchange } from "./exchange"
|
||||
export { default as ExchangeItem } from "./exchange-item"
|
||||
export { default as LineItem } from "./line-item"
|
||||
export { default as LineItemAdjustment } from "./line-item-adjustment"
|
||||
export { default as LineItemTaxLine } from "./line-item-tax-line"
|
||||
@@ -13,6 +14,7 @@ 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 ReturnItem } from "./return-item"
|
||||
export { default as ReturnReason } from "./return-reason"
|
||||
export { default as ShippingMethod } from "./shipping-method"
|
||||
export { default as ShippingMethodAdjustment } from "./shipping-method-adjustment"
|
||||
|
||||
@@ -14,6 +14,8 @@ import {
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import OrderClaim from "./claim"
|
||||
import OrderExchange from "./exchange"
|
||||
import Order from "./order"
|
||||
import OrderChange from "./order-change"
|
||||
import Return from "./return"
|
||||
@@ -38,6 +40,18 @@ const ReturnIdIndex = createPsqlIndexStatementHelper({
|
||||
where: "return_id IS NOT NULL AND deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const OrderClaimIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_change_action",
|
||||
columns: "claim_id",
|
||||
where: "claim_id IS NOT NULL AND deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const OrderExchangeIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_change_action",
|
||||
columns: "exchange_id",
|
||||
where: "exchange_id IS NOT NULL AND deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const DeletedAtIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_change_action",
|
||||
columns: "deleted_at",
|
||||
@@ -93,6 +107,36 @@ export default class OrderChangeAction {
|
||||
})
|
||||
return: Return
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => OrderClaim,
|
||||
mapToPk: true,
|
||||
fieldName: "claim_id",
|
||||
columnType: "text",
|
||||
nullable: true,
|
||||
})
|
||||
@OrderClaimIdIndex.MikroORMIndex()
|
||||
claim_id: string | null = null
|
||||
|
||||
@ManyToOne(() => OrderClaim, {
|
||||
persist: false,
|
||||
})
|
||||
claim: OrderClaim
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => OrderExchange,
|
||||
mapToPk: true,
|
||||
fieldName: "exchange_id",
|
||||
columnType: "text",
|
||||
nullable: true,
|
||||
})
|
||||
@OrderExchangeIdIndex.MikroORMIndex()
|
||||
exchange_id: string | null = null
|
||||
|
||||
@ManyToOne(() => OrderExchange, {
|
||||
persist: false,
|
||||
})
|
||||
exchange: OrderExchange
|
||||
|
||||
@Property({ columnType: "integer", nullable: true })
|
||||
version: number | null = null
|
||||
|
||||
@@ -172,6 +216,10 @@ export default class OrderChangeAction {
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "ordchact")
|
||||
this.order_id ??= this.order?.id ?? this.order_change?.order_id ?? null
|
||||
this.return_id ??= this.return?.id ?? this.order_change?.return_id ?? null
|
||||
this.claim_id ??= this.claim?.id ?? this.order_change?.claim_id ?? null
|
||||
this.exchange_id ??=
|
||||
this.exchange?.id ?? this.order_change?.exchange_id ?? null
|
||||
this.order_change_id ??= this.order_change?.id ?? null
|
||||
this.version ??= this.order_change?.version ?? null
|
||||
}
|
||||
@@ -180,6 +228,10 @@ export default class OrderChangeAction {
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "ordchact")
|
||||
this.order_id ??= this.order?.id ?? this.order_change?.order_id ?? null
|
||||
this.return_id ??= this.return?.id ?? this.order_change?.return_id ?? null
|
||||
this.claim_id ??= this.claim?.id ?? this.order_change?.claim_id ?? null
|
||||
this.exchange_id ??=
|
||||
this.exchange?.id ?? this.order_change?.exchange_id ?? null
|
||||
this.order_change_id ??= this.order_change?.id ?? null
|
||||
this.version ??= this.order_change?.version ?? null
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ import {
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import { OrderChangeStatus, OrderChangeType } from "@types"
|
||||
import OrderClaim from "./claim"
|
||||
import OrderExchange from "./exchange"
|
||||
import Order from "./order"
|
||||
import OrderChangeAction from "./order-change-action"
|
||||
import Return from "./return"
|
||||
@@ -35,6 +37,18 @@ const ReturnIdIndex = createPsqlIndexStatementHelper({
|
||||
where: "return_id IS NOT NULL AND deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const OrderClaimIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_change",
|
||||
columns: "claim_id",
|
||||
where: "claim_id IS NOT NULL AND deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const OrderExchangeIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_change",
|
||||
columns: "exchange_id",
|
||||
where: "exchange_id IS NOT NULL AND deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const OrderChangeStatusIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_change",
|
||||
columns: "status",
|
||||
@@ -97,6 +111,36 @@ export default class OrderChange {
|
||||
})
|
||||
return: Return
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => OrderClaim,
|
||||
mapToPk: true,
|
||||
fieldName: "claim_id",
|
||||
columnType: "text",
|
||||
nullable: true,
|
||||
})
|
||||
@OrderClaimIdIndex.MikroORMIndex()
|
||||
claim_id: string | null = null
|
||||
|
||||
@ManyToOne(() => OrderClaim, {
|
||||
persist: false,
|
||||
})
|
||||
claim: OrderClaim
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => OrderExchange,
|
||||
mapToPk: true,
|
||||
fieldName: "exchange_id",
|
||||
columnType: "text",
|
||||
nullable: true,
|
||||
})
|
||||
@OrderExchangeIdIndex.MikroORMIndex()
|
||||
exchange_id: string | null = null
|
||||
|
||||
@ManyToOne(() => OrderExchange, {
|
||||
persist: false,
|
||||
})
|
||||
exchange: OrderExchange
|
||||
|
||||
@Property({ columnType: "integer" })
|
||||
@VersionIndex.MikroORMIndex()
|
||||
version: number
|
||||
@@ -191,11 +235,17 @@ export default class OrderChange {
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "ordch")
|
||||
this.order_id ??= this.order?.id
|
||||
this.return_id ??= this.return?.id
|
||||
this.claim_id ??= this.claim?.id
|
||||
this.exchange_id ??= this.exchange?.id
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "ordch")
|
||||
this.order_id ??= this.order?.id
|
||||
this.return_id ??= this.return?.id
|
||||
this.claim_id ??= this.claim?.id
|
||||
this.exchange_id ??= this.exchange?.id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,11 +14,8 @@ import {
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import Claim from "./claim"
|
||||
import Exchange from "./exchange"
|
||||
import LineItem from "./line-item"
|
||||
import Order from "./order"
|
||||
import Return from "./return"
|
||||
|
||||
type OptionalLineItemProps = DAL.EntityDateColumns
|
||||
|
||||
@@ -28,24 +25,6 @@ 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 ExchangeIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_item",
|
||||
columns: ["exchange_id"],
|
||||
where: "exchange_id IS NOT NULL AND deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const ClaimIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_item",
|
||||
columns: ["claim_id"],
|
||||
where: "claim_id IS NOT NULL AND deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const OrderVersionIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_item",
|
||||
columns: ["version"],
|
||||
@@ -85,51 +64,6 @@ export default class OrderItem {
|
||||
})
|
||||
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
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => Exchange,
|
||||
mapToPk: true,
|
||||
fieldName: "exchange_id",
|
||||
columnType: "text",
|
||||
nullable: true,
|
||||
})
|
||||
@ExchangeIdIndex.MikroORMIndex()
|
||||
exchange_id: string | null
|
||||
|
||||
@ManyToOne(() => Exchange, {
|
||||
persist: false,
|
||||
})
|
||||
exchange: Exchange
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => Claim,
|
||||
mapToPk: true,
|
||||
fieldName: "claim_id",
|
||||
columnType: "text",
|
||||
nullable: true,
|
||||
})
|
||||
@ClaimIdIndex.MikroORMIndex()
|
||||
claim_id: string | null
|
||||
|
||||
@ManyToOne(() => Claim, {
|
||||
persist: false,
|
||||
})
|
||||
claim: Claim
|
||||
|
||||
@Property({ columnType: "integer" })
|
||||
@OrderVersionIndex.MikroORMIndex()
|
||||
version: number
|
||||
@@ -216,9 +150,6 @@ export default class OrderItem {
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "orditem")
|
||||
this.order_id ??= this.order?.id
|
||||
this.return_id ??= this.return?.id
|
||||
this.exchange_id ??= this.exchange?.id
|
||||
this.claim_id ??= this.claim?.id
|
||||
this.item_id ??= this.item?.id
|
||||
this.version ??= this.order?.version
|
||||
}
|
||||
@@ -227,9 +158,6 @@ export default class OrderItem {
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "orditem")
|
||||
this.order_id ??= this.order?.id
|
||||
this.return_id ??= this.return?.id
|
||||
this.exchange_id ??= this.exchange?.id
|
||||
this.claim_id ??= this.claim?.id
|
||||
this.item_id ??= this.item?.id
|
||||
this.version ??= this.order?.version
|
||||
}
|
||||
|
||||
@@ -170,6 +170,7 @@ export default class OrderShippingMethod {
|
||||
this.id = generateEntityId(this.id, "ordspmv")
|
||||
this.order_id ??= this.order?.id
|
||||
this.return_id ??= this.return?.id
|
||||
this.claim_id ??= this.claim?.id
|
||||
this.exchange_id ??= this.exchange?.id
|
||||
this.shipping_method_id ??= this.shipping_method?.id
|
||||
this.version ??= this.order?.version
|
||||
@@ -180,6 +181,7 @@ export default class OrderShippingMethod {
|
||||
this.id = generateEntityId(this.id, "ordspmv")
|
||||
this.order_id ??= this.order?.id
|
||||
this.return_id ??= this.return?.id
|
||||
this.claim_id ??= this.claim?.id
|
||||
this.exchange_id ??= this.exchange?.id
|
||||
this.shipping_method_id ??= this.shipping_method?.id
|
||||
this.version ??= this.order?.version
|
||||
|
||||
143
packages/modules/order/src/models/return-item.ts
Normal file
143
packages/modules/order/src/models/return-item.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import { BigNumberRawValue, DAL } from "@medusajs/types"
|
||||
import {
|
||||
MikroOrmBigNumberProperty,
|
||||
createPsqlIndexStatementHelper,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import LineItem from "./line-item"
|
||||
import Return from "./return"
|
||||
import ReturnReason from "./return-reason"
|
||||
|
||||
type OptionalLineItemProps = DAL.EntityDateColumns
|
||||
|
||||
const ReturnIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "return_item",
|
||||
columns: "return_id",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const ReturnReasonIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "return_item",
|
||||
columns: "reason_id",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const ItemIdIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "return_item",
|
||||
columns: "item_id",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
const DeletedAtIndex = createPsqlIndexStatementHelper({
|
||||
tableName: "order_claim_item_image",
|
||||
columns: "deleted_at",
|
||||
where: "deleted_at IS NOT NULL",
|
||||
})
|
||||
|
||||
@Entity({ tableName: "return_item" })
|
||||
export default class ReturnItem {
|
||||
[OptionalProps]?: OptionalLineItemProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@ManyToOne(() => ReturnReason, {
|
||||
columnType: "text",
|
||||
fieldName: "reason_id",
|
||||
mapToPk: true,
|
||||
nullable: true,
|
||||
})
|
||||
@ReturnReasonIdIndex.MikroORMIndex()
|
||||
reason_id: string | null = null
|
||||
|
||||
@ManyToOne(() => ReturnReason, {
|
||||
persist: false,
|
||||
})
|
||||
reason: ReturnReason
|
||||
|
||||
@MikroOrmBigNumberProperty()
|
||||
quantity: Number | number
|
||||
|
||||
@Property({ columnType: "jsonb" })
|
||||
raw_quantity: BigNumberRawValue
|
||||
|
||||
@MikroOrmBigNumberProperty()
|
||||
received_quantity: Number | number = 0
|
||||
|
||||
@Property({ columnType: "jsonb" })
|
||||
raw_received_quantity: BigNumberRawValue
|
||||
|
||||
@ManyToOne(() => Return, {
|
||||
columnType: "text",
|
||||
fieldName: "return_id",
|
||||
mapToPk: true,
|
||||
onDelete: "cascade",
|
||||
})
|
||||
@ReturnIdIndex.MikroORMIndex()
|
||||
return_id: string
|
||||
|
||||
@ManyToOne(() => Return, {
|
||||
persist: false,
|
||||
})
|
||||
return: Return
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => LineItem,
|
||||
fieldName: "item_id",
|
||||
mapToPk: true,
|
||||
columnType: "text",
|
||||
})
|
||||
@ItemIdIndex.MikroORMIndex()
|
||||
item_id: string
|
||||
|
||||
@ManyToOne(() => LineItem, {
|
||||
persist: false,
|
||||
})
|
||||
item: LineItem
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
note: string
|
||||
|
||||
@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, "retitem")
|
||||
this.return_id = this.return?.id
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "retitem")
|
||||
this.return_id = this.return?.id
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import { ReturnItem, Transaction } from "@models"
|
||||
import Claim from "./claim"
|
||||
import Exchange from "./exchange"
|
||||
import Order from "./order"
|
||||
@@ -126,7 +127,7 @@ export default class Return {
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
raw_refund_amount: BigNumberRawValue
|
||||
|
||||
@OneToMany(() => OrderItem, (itemDetail) => itemDetail.return, {
|
||||
@OneToMany(() => ReturnItem, (itemDetail) => itemDetail.return, {
|
||||
cascade: [Cascade.PERSIST],
|
||||
})
|
||||
items = new Collection<OrderItem>(this)
|
||||
@@ -140,6 +141,11 @@ export default class Return {
|
||||
)
|
||||
shipping_methods = new Collection<OrderShippingMethod>(this)
|
||||
|
||||
@OneToMany(() => Transaction, (transaction) => transaction.return, {
|
||||
cascade: [Cascade.PERSIST],
|
||||
})
|
||||
transactions = new Collection<Transaction>(this)
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
|
||||
@@ -189,11 +189,17 @@ export default class Transaction {
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "ordtrx")
|
||||
this.order_id ??= this.order?.id
|
||||
this.return_id ??= this.return?.id
|
||||
this.claim_id ??= this.claim?.id
|
||||
this.exchange_id ??= this.exchange?.id
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "ordtrx")
|
||||
this.order_id ??= this.order?.id
|
||||
this.return_id ??= this.return?.id
|
||||
this.claim_id ??= this.claim?.id
|
||||
this.exchange_id ??= this.exchange?.id
|
||||
}
|
||||
}
|
||||
|
||||
9
packages/modules/order/src/repositories/claim.ts
Normal file
9
packages/modules/order/src/repositories/claim.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { OrderClaim } from "@models"
|
||||
import { setFindMethods } from "../utils/base-repository-find"
|
||||
|
||||
export class OrderClaimRepository extends DALUtils.mikroOrmBaseRepositoryFactory<OrderClaim>(
|
||||
OrderClaim
|
||||
) {}
|
||||
|
||||
setFindMethods(OrderClaimRepository, OrderClaim)
|
||||
9
packages/modules/order/src/repositories/exchange.ts
Normal file
9
packages/modules/order/src/repositories/exchange.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { OrderExchange } from "@models"
|
||||
import { setFindMethods } from "../utils/base-repository-find"
|
||||
|
||||
export class OrderExchangeRepository extends DALUtils.mikroOrmBaseRepositoryFactory<OrderExchange>(
|
||||
OrderExchange
|
||||
) {}
|
||||
|
||||
setFindMethods(OrderExchangeRepository, OrderExchange)
|
||||
@@ -1,3 +1,4 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
export { OrderClaimRepository } from "./claim"
|
||||
export { OrderRepository } from "./order"
|
||||
export { ReturnRepository } from "./return"
|
||||
|
||||
@@ -71,8 +71,8 @@ describe("Order Exchange - Actions", function () {
|
||||
},
|
||||
{
|
||||
action: ChangeActionType.ITEM_ADD,
|
||||
reference_id: "item_555",
|
||||
details: {
|
||||
reference_id: "item_555",
|
||||
unit_price: 50,
|
||||
quantity: 1,
|
||||
},
|
||||
|
||||
365
packages/modules/order/src/services/actions/create-claim.ts
Normal file
365
packages/modules/order/src/services/actions/create-claim.ts
Normal file
@@ -0,0 +1,365 @@
|
||||
import {
|
||||
Context,
|
||||
CreateOrderChangeActionDTO,
|
||||
OrderTypes,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ClaimType,
|
||||
ReturnStatus,
|
||||
getShippingMethodsTotals,
|
||||
isString,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { ClaimItem, OrderClaim, Return, ReturnItem } from "@models"
|
||||
import { OrderChangeType } from "@types"
|
||||
import { ChangeActionType } from "../../utils"
|
||||
|
||||
function createClaimAndReturnEntities(em, data, order) {
|
||||
const claimReference = em.create(OrderClaim, {
|
||||
order_id: data.order_id,
|
||||
order_version: order.version,
|
||||
type: data.type as ClaimType,
|
||||
no_notification: data.no_notification,
|
||||
refund_amount: (data.refund_amount as unknown) ?? null,
|
||||
})
|
||||
|
||||
const returnReference =
|
||||
data.type === ClaimType.REPLACE
|
||||
? em.create(Return, {
|
||||
order_id: data.order_id,
|
||||
order_version: order.version,
|
||||
status: ReturnStatus.REQUESTED,
|
||||
claim_id: claimReference.id,
|
||||
refund_amount: (data.refund_amount as unknown) ?? null,
|
||||
})
|
||||
: undefined
|
||||
|
||||
claimReference.return_id = returnReference?.id
|
||||
|
||||
return { claimReference, returnReference }
|
||||
}
|
||||
|
||||
function createReturnItem(em, item, claimReference, returnReference, actions) {
|
||||
actions.push({
|
||||
action: ChangeActionType.RETURN_ITEM,
|
||||
reference: "return",
|
||||
reference_id: returnReference.id,
|
||||
details: {
|
||||
reference_id: item.id,
|
||||
return_id: returnReference.id,
|
||||
claim_id: claimReference.id,
|
||||
quantity: item.quantity,
|
||||
metadata: item.metadata,
|
||||
},
|
||||
})
|
||||
|
||||
return em.create(ReturnItem, {
|
||||
item_id: item.id,
|
||||
return_id: returnReference.id,
|
||||
quantity: item.quantity,
|
||||
note: item.note,
|
||||
metadata: item.metadata,
|
||||
})
|
||||
}
|
||||
|
||||
function createClaimAndReturnItems(
|
||||
em,
|
||||
data,
|
||||
claimReference,
|
||||
returnReference,
|
||||
actions
|
||||
) {
|
||||
const returnItems: ReturnItem[] = []
|
||||
const claimItems = data.claim_items?.map((item) => {
|
||||
actions.push({
|
||||
action: ChangeActionType.WRITE_OFF_ITEM,
|
||||
reference: "claim",
|
||||
reference_id: claimReference.id,
|
||||
details: {
|
||||
reference_id: item.id,
|
||||
claim_id: claimReference.id,
|
||||
quantity: item.quantity,
|
||||
metadata: item.metadata,
|
||||
},
|
||||
})
|
||||
|
||||
returnItems.push(
|
||||
returnReference
|
||||
? createReturnItem(em, item, claimReference, returnReference, actions)
|
||||
: undefined
|
||||
)
|
||||
|
||||
return em.create(ClaimItem, {
|
||||
item_id: item.id,
|
||||
reason: item.reason,
|
||||
quantity: item.quantity,
|
||||
note: item.note,
|
||||
metadata: item.metadata,
|
||||
})
|
||||
})
|
||||
|
||||
return [claimItems, returnItems]
|
||||
}
|
||||
|
||||
async function processAdditionalItems(
|
||||
em,
|
||||
service,
|
||||
data,
|
||||
order,
|
||||
claimReference,
|
||||
actions,
|
||||
sharedContext
|
||||
) {
|
||||
const itemsToAdd: any[] = []
|
||||
const additionalNewItems: ClaimItem[] = []
|
||||
const additionalItems: ClaimItem[] = []
|
||||
data.additional_items?.forEach((item) => {
|
||||
const hasItem = item.id
|
||||
? order.items.find((o) => o.item.id === item.id)
|
||||
: false
|
||||
|
||||
if (hasItem) {
|
||||
actions.push({
|
||||
action: ChangeActionType.ITEM_ADD,
|
||||
claim_id: claimReference.id,
|
||||
internal_note: item.internal_note,
|
||||
reference: "claim",
|
||||
reference_id: claimReference.id,
|
||||
details: {
|
||||
reference_id: item.id,
|
||||
claim_id: claimReference.id,
|
||||
quantity: item.quantity,
|
||||
metadata: item.metadata,
|
||||
},
|
||||
})
|
||||
|
||||
additionalItems.push(
|
||||
em.create(ClaimItem, {
|
||||
item_id: item.id,
|
||||
quantity: item.quantity,
|
||||
note: item.note,
|
||||
metadata: item.metadata,
|
||||
is_additional_item: true,
|
||||
})
|
||||
)
|
||||
} else {
|
||||
itemsToAdd.push(item)
|
||||
|
||||
additionalNewItems.push(
|
||||
em.create(ClaimItem, {
|
||||
quantity: item.quantity,
|
||||
note: item.note,
|
||||
metadata: item.metadata,
|
||||
is_additional_item: true,
|
||||
})
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
const createItems = await service.lineItemService_.create(
|
||||
itemsToAdd,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
createItems.forEach((item, index) => {
|
||||
const addedItem = itemsToAdd[index]
|
||||
additionalNewItems[index].item_id = item.id
|
||||
actions.push({
|
||||
action: ChangeActionType.ITEM_ADD,
|
||||
claim_id: claimReference.id,
|
||||
internal_note: addedItem.internal_note,
|
||||
reference: "claim",
|
||||
reference_id: claimReference.id,
|
||||
details: {
|
||||
reference_id: item.id,
|
||||
claim_id: claimReference.id,
|
||||
quantity: addedItem.quantity,
|
||||
metadata: addedItem.metadata,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
return additionalNewItems.concat(additionalItems)
|
||||
}
|
||||
|
||||
async function processShippingMethods(
|
||||
service,
|
||||
data,
|
||||
claimReference,
|
||||
actions,
|
||||
sharedContext
|
||||
) {
|
||||
for (const shippingMethod of data.shipping_methods ?? []) {
|
||||
let shippingMethodId
|
||||
|
||||
if (!isString(shippingMethod)) {
|
||||
const methods = await service.createShippingMethods(
|
||||
[
|
||||
{
|
||||
order_id: data.order_id,
|
||||
claim_id: claimReference.id,
|
||||
...shippingMethod,
|
||||
},
|
||||
],
|
||||
sharedContext
|
||||
)
|
||||
shippingMethodId = methods[0].id
|
||||
} else {
|
||||
shippingMethodId = shippingMethod
|
||||
}
|
||||
|
||||
const method = await service.retrieveShippingMethod(
|
||||
shippingMethodId,
|
||||
{ relations: ["tax_lines", "adjustments"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const calculatedAmount = getShippingMethodsTotals([method as any], {})[
|
||||
method.id
|
||||
]
|
||||
|
||||
actions.push({
|
||||
action: ChangeActionType.SHIPPING_ADD,
|
||||
reference: "order_shipping_method",
|
||||
reference_id: shippingMethodId,
|
||||
claim_id: claimReference.id,
|
||||
amount: calculatedAmount.total,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async function processReturnShipping(
|
||||
service,
|
||||
data,
|
||||
claimReference,
|
||||
returnReference,
|
||||
actions,
|
||||
sharedContext
|
||||
) {
|
||||
if (!returnReference) {
|
||||
return
|
||||
}
|
||||
|
||||
if (data.return_shipping) {
|
||||
let returnShippingMethodId
|
||||
|
||||
if (!isString(data.return_shipping)) {
|
||||
const methods = await service.createShippingMethods(
|
||||
[
|
||||
{
|
||||
order_id: data.order_id,
|
||||
claim_id: claimReference.id,
|
||||
return_id: returnReference.id,
|
||||
...data.return_shipping,
|
||||
},
|
||||
],
|
||||
sharedContext
|
||||
)
|
||||
returnShippingMethodId = methods[0].id
|
||||
} else {
|
||||
returnShippingMethodId = data.return_shipping
|
||||
}
|
||||
|
||||
const method = await service.retrieveShippingMethod(
|
||||
returnShippingMethodId,
|
||||
{ relations: ["tax_lines", "adjustments"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const calculatedAmount = getShippingMethodsTotals([method as any], {})[
|
||||
method.id
|
||||
]
|
||||
|
||||
actions.push({
|
||||
action: ChangeActionType.SHIPPING_ADD,
|
||||
reference: "order_shipping_method",
|
||||
reference_id: returnShippingMethodId,
|
||||
return_id: returnReference.id,
|
||||
claim_id: claimReference.id,
|
||||
amount: calculatedAmount.total,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export async function createClaim(
|
||||
this: any,
|
||||
data: OrderTypes.CreateOrderClaimDTO,
|
||||
sharedContext?: Context
|
||||
) {
|
||||
const order = await this.orderService_.retrieve(
|
||||
data.order_id,
|
||||
{ relations: ["items"] },
|
||||
sharedContext
|
||||
)
|
||||
const actions: CreateOrderChangeActionDTO[] = []
|
||||
const em = sharedContext!.transactionManager as any
|
||||
const { claimReference, returnReference } = createClaimAndReturnEntities(
|
||||
em,
|
||||
data,
|
||||
order
|
||||
)
|
||||
|
||||
const [claimItems, returnItems] = createClaimAndReturnItems(
|
||||
em,
|
||||
data,
|
||||
claimReference,
|
||||
returnReference,
|
||||
actions
|
||||
)
|
||||
|
||||
claimReference.claim_items = claimItems
|
||||
|
||||
if (returnReference) {
|
||||
returnReference.items = returnItems
|
||||
}
|
||||
|
||||
claimReference.additional_items = await processAdditionalItems(
|
||||
em,
|
||||
this,
|
||||
data,
|
||||
order,
|
||||
claimReference,
|
||||
actions,
|
||||
sharedContext
|
||||
)
|
||||
await processShippingMethods(
|
||||
this,
|
||||
data,
|
||||
claimReference,
|
||||
actions,
|
||||
sharedContext
|
||||
)
|
||||
await processReturnShipping(
|
||||
this,
|
||||
data,
|
||||
claimReference,
|
||||
returnReference,
|
||||
actions,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const change = await this.createOrderChange_(
|
||||
{
|
||||
order_id: data.order_id,
|
||||
claim_id: claimReference.id,
|
||||
return_id: returnReference.id,
|
||||
change_type: OrderChangeType.CLAIM,
|
||||
reference: "claim",
|
||||
reference_id: claimReference.id,
|
||||
description: data.description,
|
||||
internal_note: data.internal_note,
|
||||
created_by: data.created_by,
|
||||
metadata: data.metadata,
|
||||
actions,
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
await promiseAll([
|
||||
this.createReturns([returnReference], sharedContext),
|
||||
this.createOrderClaims([claimReference], sharedContext),
|
||||
this.confirmOrderChange(change[0].id, sharedContext),
|
||||
])
|
||||
|
||||
return claimReference
|
||||
}
|
||||
316
packages/modules/order/src/services/actions/create-exchange.ts
Normal file
316
packages/modules/order/src/services/actions/create-exchange.ts
Normal file
@@ -0,0 +1,316 @@
|
||||
import {
|
||||
Context,
|
||||
CreateOrderChangeActionDTO,
|
||||
OrderTypes,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ReturnStatus,
|
||||
getShippingMethodsTotals,
|
||||
isString,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { ExchangeItem, OrderExchange, Return, ReturnItem } from "@models"
|
||||
import { OrderChangeType } from "@types"
|
||||
import { ChangeActionType } from "../../utils"
|
||||
|
||||
function createExchangeAndReturnEntities(em, data, order) {
|
||||
const exchangeReference = em.create(OrderExchange, {
|
||||
order_id: data.order_id,
|
||||
order_version: order.version,
|
||||
no_notification: data.no_notification,
|
||||
allow_backorder: data.allow_backorder,
|
||||
difference_due: data.difference_due,
|
||||
})
|
||||
|
||||
const returnReference = em.create(Return, {
|
||||
order_id: data.order_id,
|
||||
order_version: order.version,
|
||||
status: ReturnStatus.REQUESTED,
|
||||
exchange_id: exchangeReference.id,
|
||||
refund_amount: (data.refund_amount as unknown) ?? null,
|
||||
})
|
||||
|
||||
exchangeReference.return_id = returnReference.id
|
||||
|
||||
return { exchangeReference, returnReference }
|
||||
}
|
||||
|
||||
function createReturnItems(
|
||||
em,
|
||||
data,
|
||||
exchangeReference,
|
||||
returnReference,
|
||||
actions
|
||||
) {
|
||||
return data.return_items?.map((item) => {
|
||||
actions.push({
|
||||
action: ChangeActionType.RETURN_ITEM,
|
||||
reference: "return",
|
||||
reference_id: returnReference.id,
|
||||
details: {
|
||||
reference_id: item.id,
|
||||
return_id: returnReference.id,
|
||||
exchange_id: exchangeReference.id,
|
||||
quantity: item.quantity,
|
||||
metadata: item.metadata,
|
||||
},
|
||||
})
|
||||
|
||||
return em.create(ReturnItem, {
|
||||
item_id: item.id,
|
||||
return_id: returnReference.id,
|
||||
reason: item.reason,
|
||||
quantity: item.quantity,
|
||||
note: item.note,
|
||||
metadata: item.metadata,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function processAdditionalItems(
|
||||
em,
|
||||
service,
|
||||
data,
|
||||
order,
|
||||
exchangeReference,
|
||||
actions,
|
||||
sharedContext
|
||||
) {
|
||||
const itemsToAdd: any[] = []
|
||||
const additionalNewItems: ExchangeItem[] = []
|
||||
const additionalItems: ExchangeItem[] = []
|
||||
data.additional_items?.forEach((item) => {
|
||||
const hasItem = item.id
|
||||
? order.items.find((o) => o.item.id === item.id)
|
||||
: false
|
||||
|
||||
if (hasItem) {
|
||||
actions.push({
|
||||
action: ChangeActionType.ITEM_ADD,
|
||||
exchange_id: exchangeReference.id,
|
||||
internal_note: item.internal_note,
|
||||
reference: "exchange",
|
||||
reference_id: exchangeReference.id,
|
||||
details: {
|
||||
reference_id: item.id,
|
||||
exchange_id: exchangeReference.id,
|
||||
quantity: item.quantity,
|
||||
metadata: item.metadata,
|
||||
},
|
||||
})
|
||||
|
||||
additionalItems.push(
|
||||
em.create(ExchangeItem, {
|
||||
item_id: item.id,
|
||||
quantity: item.quantity,
|
||||
note: item.note,
|
||||
metadata: item.metadata,
|
||||
is_additional_item: true,
|
||||
})
|
||||
)
|
||||
} else {
|
||||
itemsToAdd.push(item)
|
||||
|
||||
additionalNewItems.push(
|
||||
em.create(ExchangeItem, {
|
||||
quantity: item.quantity,
|
||||
note: item.note,
|
||||
metadata: item.metadata,
|
||||
is_additional_item: true,
|
||||
})
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
const createItems = await service.lineItemService_.create(
|
||||
itemsToAdd,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
createItems.forEach((item, index) => {
|
||||
const addedItem = itemsToAdd[index]
|
||||
additionalNewItems[index].item_id = item.id
|
||||
actions.push({
|
||||
action: ChangeActionType.ITEM_ADD,
|
||||
exchange_id: exchangeReference.id,
|
||||
internal_note: addedItem.internal_note,
|
||||
reference: "exchange",
|
||||
reference_id: exchangeReference.id,
|
||||
details: {
|
||||
reference_id: item.id,
|
||||
exchange_id: exchangeReference.id,
|
||||
quantity: addedItem.quantity,
|
||||
metadata: addedItem.metadata,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
return additionalNewItems.concat(additionalItems)
|
||||
}
|
||||
|
||||
async function processShippingMethods(
|
||||
service,
|
||||
data,
|
||||
exchangeReference,
|
||||
actions,
|
||||
sharedContext
|
||||
) {
|
||||
for (const shippingMethod of data.shipping_methods ?? []) {
|
||||
let shippingMethodId
|
||||
|
||||
if (!isString(shippingMethod)) {
|
||||
const methods = await service.createShippingMethods(
|
||||
[
|
||||
{
|
||||
order_id: data.order_id,
|
||||
exchange_id: exchangeReference.id,
|
||||
...shippingMethod,
|
||||
},
|
||||
],
|
||||
sharedContext
|
||||
)
|
||||
shippingMethodId = methods[0].id
|
||||
} else {
|
||||
shippingMethodId = shippingMethod
|
||||
}
|
||||
|
||||
const method = await service.retrieveShippingMethod(
|
||||
shippingMethodId,
|
||||
{ relations: ["tax_lines", "adjustments"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const calculatedAmount = getShippingMethodsTotals([method as any], {})[
|
||||
method.id
|
||||
]
|
||||
|
||||
actions.push({
|
||||
action: ChangeActionType.SHIPPING_ADD,
|
||||
reference: "order_shipping_method",
|
||||
reference_id: shippingMethodId,
|
||||
exchange_id: exchangeReference.id,
|
||||
amount: calculatedAmount.total,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async function processReturnShipping(
|
||||
service,
|
||||
data,
|
||||
exchangeReference,
|
||||
returnReference,
|
||||
actions,
|
||||
sharedContext
|
||||
) {
|
||||
let returnShippingMethodId
|
||||
|
||||
if (!isString(data.return_shipping)) {
|
||||
const methods = await service.createShippingMethods(
|
||||
[
|
||||
{
|
||||
order_id: data.order_id,
|
||||
exchange_id: exchangeReference.id,
|
||||
return_id: returnReference.id,
|
||||
...data.return_shipping,
|
||||
},
|
||||
],
|
||||
sharedContext
|
||||
)
|
||||
returnShippingMethodId = methods[0].id
|
||||
} else {
|
||||
returnShippingMethodId = data.return_shipping
|
||||
}
|
||||
|
||||
const method = await service.retrieveShippingMethod(
|
||||
returnShippingMethodId,
|
||||
{ relations: ["tax_lines", "adjustments"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const calculatedAmount = getShippingMethodsTotals([method as any], {})[
|
||||
method.id
|
||||
]
|
||||
|
||||
actions.push({
|
||||
action: ChangeActionType.SHIPPING_ADD,
|
||||
reference: "order_shipping_method",
|
||||
reference_id: returnShippingMethodId,
|
||||
return_id: returnReference.id,
|
||||
exchange_id: exchangeReference.id,
|
||||
amount: calculatedAmount.total,
|
||||
})
|
||||
}
|
||||
|
||||
export async function createExchange(
|
||||
this: any,
|
||||
data: OrderTypes.CreateOrderExchangeDTO,
|
||||
sharedContext?: Context
|
||||
) {
|
||||
const order = await this.orderService_.retrieve(
|
||||
data.order_id,
|
||||
{ relations: ["items"] },
|
||||
sharedContext
|
||||
)
|
||||
const actions: CreateOrderChangeActionDTO[] = []
|
||||
const em = sharedContext!.transactionManager as any
|
||||
const { exchangeReference, returnReference } =
|
||||
createExchangeAndReturnEntities(em, data, order)
|
||||
|
||||
returnReference.items = createReturnItems(
|
||||
em,
|
||||
data,
|
||||
exchangeReference,
|
||||
returnReference,
|
||||
actions
|
||||
)
|
||||
|
||||
exchangeReference.additional_items = await processAdditionalItems(
|
||||
em,
|
||||
this,
|
||||
data,
|
||||
order,
|
||||
exchangeReference,
|
||||
actions,
|
||||
sharedContext
|
||||
)
|
||||
await processShippingMethods(
|
||||
this,
|
||||
data,
|
||||
exchangeReference,
|
||||
actions,
|
||||
sharedContext
|
||||
)
|
||||
await processReturnShipping(
|
||||
this,
|
||||
data,
|
||||
exchangeReference,
|
||||
returnReference,
|
||||
actions,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const change = await this.createOrderChange_(
|
||||
{
|
||||
order_id: data.order_id,
|
||||
exchange_id: exchangeReference.id,
|
||||
return_id: returnReference.id,
|
||||
change_type: OrderChangeType.CLAIM,
|
||||
reference: "exchange",
|
||||
reference_id: exchangeReference.id,
|
||||
description: data.description,
|
||||
internal_note: data.internal_note,
|
||||
created_by: data.created_by,
|
||||
metadata: data.metadata,
|
||||
actions,
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
await promiseAll([
|
||||
this.createReturns([returnReference], sharedContext),
|
||||
this.createOrderExchanges([exchangeReference], sharedContext),
|
||||
this.confirmOrderChange(change[0].id, sharedContext),
|
||||
])
|
||||
|
||||
return exchangeReference
|
||||
}
|
||||
@@ -7,67 +7,25 @@ import {
|
||||
ReturnStatus,
|
||||
getShippingMethodsTotals,
|
||||
isString,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { Return, ReturnItem } from "@models"
|
||||
import { OrderChangeType } from "@types"
|
||||
import { ChangeActionType } from "../../utils"
|
||||
|
||||
export async function createReturn(
|
||||
this: any,
|
||||
data: OrderTypes.CreateOrderReturnDTO,
|
||||
sharedContext?: Context
|
||||
) {
|
||||
const order = await this.orderService_.retrieve(
|
||||
data.order_id,
|
||||
{
|
||||
relations: ["items"],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
function createReturnReference(em, data, order) {
|
||||
return em.create(Return, {
|
||||
order_id: data.order_id,
|
||||
order_version: order.version,
|
||||
status: ReturnStatus.REQUESTED,
|
||||
no_notification: data.no_notification,
|
||||
refund_amount: (data.refund_amount as unknown) ?? null,
|
||||
})
|
||||
}
|
||||
|
||||
const [returnRef] = await this.createReturns(
|
||||
[
|
||||
{
|
||||
order_id: data.order_id,
|
||||
order_version: order.version,
|
||||
status: ReturnStatus.REQUESTED,
|
||||
// TODO: add refund amount / calculate?
|
||||
// refund_amount: data.refund_amount ?? null,
|
||||
},
|
||||
],
|
||||
sharedContext
|
||||
)
|
||||
|
||||
let shippingMethodId
|
||||
|
||||
if (!isString(data.shipping_method)) {
|
||||
const methods = await this.createShippingMethods(
|
||||
[
|
||||
{
|
||||
order_id: data.order_id,
|
||||
...data.shipping_method,
|
||||
},
|
||||
],
|
||||
sharedContext
|
||||
)
|
||||
shippingMethodId = methods[0].id
|
||||
} else {
|
||||
shippingMethodId = data.shipping_method
|
||||
}
|
||||
|
||||
const method = await this.retrieveShippingMethod(
|
||||
shippingMethodId,
|
||||
{
|
||||
relations: ["tax_lines", "adjustments"],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const calculatedAmount = getShippingMethodsTotals([method as any], {})[
|
||||
method.id
|
||||
]
|
||||
|
||||
const actions: CreateOrderChangeActionDTO[] = data.items.map((item) => {
|
||||
return {
|
||||
function createReturnItems(em, data, returnRef, actions) {
|
||||
return data.items.map((item) => {
|
||||
actions.push({
|
||||
action: ChangeActionType.RETURN_ITEM,
|
||||
return_id: returnRef.id,
|
||||
internal_note: item.internal_note,
|
||||
@@ -79,8 +37,53 @@ export async function createReturn(
|
||||
quantity: item.quantity,
|
||||
metadata: item.metadata,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
return em.create(ReturnItem, {
|
||||
reason_id: item.reason_id,
|
||||
return_id: returnRef.id,
|
||||
item_id: item.id,
|
||||
quantity: item.quantity,
|
||||
note: item.note,
|
||||
metadata: item.metadata,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function processShippingMethod(
|
||||
service,
|
||||
data,
|
||||
returnRef,
|
||||
actions,
|
||||
sharedContext
|
||||
) {
|
||||
let shippingMethodId
|
||||
|
||||
if (!isString(data.shipping_method)) {
|
||||
const methods = await service.createShippingMethods(
|
||||
[
|
||||
{
|
||||
order_id: data.order_id,
|
||||
return_id: returnRef.id,
|
||||
...data.shipping_method,
|
||||
},
|
||||
],
|
||||
sharedContext
|
||||
)
|
||||
shippingMethodId = methods[0].id
|
||||
} else {
|
||||
shippingMethodId = data.shipping_method
|
||||
}
|
||||
|
||||
const method = await service.retrieveShippingMethod(
|
||||
shippingMethodId,
|
||||
{ relations: ["tax_lines", "adjustments"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const calculatedAmount = getShippingMethodsTotals([method as any], {})[
|
||||
method.id
|
||||
]
|
||||
|
||||
if (shippingMethodId) {
|
||||
actions.push({
|
||||
@@ -91,8 +94,16 @@ export async function createReturn(
|
||||
amount: calculatedAmount.total,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const change = await this.createOrderChange_(
|
||||
async function createOrderChange(
|
||||
service,
|
||||
data,
|
||||
returnRef,
|
||||
actions,
|
||||
sharedContext
|
||||
) {
|
||||
return await service.createOrderChange_(
|
||||
{
|
||||
order_id: data.order_id,
|
||||
return_id: returnRef.id,
|
||||
@@ -107,8 +118,36 @@ export async function createReturn(
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
await this.confirmOrderChange(change[0].id, sharedContext)
|
||||
export async function createReturn(
|
||||
this: any,
|
||||
data: OrderTypes.CreateOrderReturnDTO,
|
||||
sharedContext?: Context
|
||||
) {
|
||||
const order = await this.orderService_.retrieve(
|
||||
data.order_id,
|
||||
{ relations: ["items"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const em = sharedContext!.transactionManager as any
|
||||
const returnRef = createReturnReference(em, data, order)
|
||||
const actions: CreateOrderChangeActionDTO[] = []
|
||||
returnRef.items = createReturnItems(em, data, returnRef, actions)
|
||||
await processShippingMethod(this, data, returnRef, actions, sharedContext)
|
||||
const change = await createOrderChange(
|
||||
this,
|
||||
data,
|
||||
returnRef,
|
||||
actions,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
await promiseAll([
|
||||
this.createReturns([returnRef], sharedContext),
|
||||
this.confirmOrderChange(change[0].id, sharedContext),
|
||||
])
|
||||
|
||||
return returnRef
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
export * from "./cancel-fulfillment"
|
||||
export * from "./create-claim"
|
||||
export * from "./create-exchange"
|
||||
export * from "./create-return"
|
||||
export * from "./receive-return"
|
||||
export * from "./register-fulfillment"
|
||||
|
||||
@@ -1,8 +1,78 @@
|
||||
import { Context, OrderTypes } from "@medusajs/types"
|
||||
import { MathBN, ReturnStatus } from "@medusajs/utils"
|
||||
import { MathBN, ReturnStatus, promiseAll } from "@medusajs/utils"
|
||||
import { OrderChangeType } from "@types"
|
||||
import { ChangeActionType } from "../../utils"
|
||||
|
||||
function createReturnItems(data) {
|
||||
return data.items.map((item) => ({
|
||||
action: ChangeActionType.RECEIVE_RETURN_ITEM,
|
||||
internal_note: item.internal_note,
|
||||
reference: data.reference,
|
||||
reference_id: data.reference_id,
|
||||
details: {
|
||||
reference_id: item.id,
|
||||
quantity: item.quantity,
|
||||
metadata: item.metadata,
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
async function createOrderChange(
|
||||
service,
|
||||
data,
|
||||
returnEntry,
|
||||
items,
|
||||
sharedContext
|
||||
) {
|
||||
return await service.createOrderChange_(
|
||||
{
|
||||
order_id: returnEntry.order_id,
|
||||
return_id: returnEntry.id,
|
||||
reference: "return",
|
||||
reference_id: returnEntry.id,
|
||||
change_type: OrderChangeType.RETURN,
|
||||
description: data.description,
|
||||
internal_note: data.internal_note,
|
||||
created_by: data.created_by,
|
||||
metadata: data.metadata,
|
||||
actions: items,
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
function updateReturnItems(returnEntry, items) {
|
||||
return returnEntry.items
|
||||
.map((item) => {
|
||||
const data = items.find((i) => i.details.reference_id === item.item_id)
|
||||
if (!data) return
|
||||
|
||||
const receivedQuantity = MathBN.add(
|
||||
item.received_quantity || 0,
|
||||
data.details.quantity
|
||||
)
|
||||
item.received_quantity = receivedQuantity
|
||||
|
||||
return {
|
||||
id: item.id,
|
||||
received_quantity: receivedQuantity,
|
||||
}
|
||||
})
|
||||
.filter(Boolean)
|
||||
}
|
||||
|
||||
function checkAllItemsReceived(returnEntry) {
|
||||
return returnEntry.items.every((item) =>
|
||||
MathBN.eq(item.received_quantity, item.quantity)
|
||||
)
|
||||
}
|
||||
|
||||
function getReturnUpdateData(hasReceivedAllItems) {
|
||||
return hasReceivedAllItems
|
||||
? { status: ReturnStatus.RECEIVED, received_at: new Date() }
|
||||
: { status: ReturnStatus.PARTIALLY_RECEIVED }
|
||||
}
|
||||
|
||||
export async function receiveReturn(
|
||||
this: any,
|
||||
data: OrderTypes.ReceiveOrderReturnDTO,
|
||||
@@ -17,59 +87,28 @@ export async function receiveReturn(
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const items = data.items.map((item) => {
|
||||
return {
|
||||
action: ChangeActionType.RECEIVE_RETURN_ITEM,
|
||||
internal_note: item.internal_note,
|
||||
reference: data.reference,
|
||||
reference_id: data.reference_id,
|
||||
details: {
|
||||
reference_id: item.id,
|
||||
quantity: item.quantity,
|
||||
metadata: item.metadata,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const change = await this.createOrderChange_(
|
||||
{
|
||||
order_id: returnEntry.order_id,
|
||||
reference: "return",
|
||||
reference_id: returnEntry.id,
|
||||
change_type: OrderChangeType.RETURN,
|
||||
description: data.description,
|
||||
internal_note: data.internal_note,
|
||||
created_by: data.created_by,
|
||||
metadata: data.metadata,
|
||||
actions: items,
|
||||
},
|
||||
const items = createReturnItems(data)
|
||||
const change = await createOrderChange(
|
||||
this,
|
||||
data,
|
||||
returnEntry,
|
||||
items,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
await this.confirmOrderChange(change[0].id, sharedContext)
|
||||
|
||||
const hasReceivedAllItems = returnEntry.items.every((item) => {
|
||||
const retItem = items.find((dtItem) => {
|
||||
return item.detail.item_id === dtItem.details.reference_id
|
||||
})
|
||||
const quantity = retItem ? retItem.details.quantity : 0
|
||||
return MathBN.eq(
|
||||
MathBN.sub(item.detail.return_requested_quantity, quantity),
|
||||
0
|
||||
)
|
||||
})
|
||||
const retItemsToUpdate = updateReturnItems(returnEntry, items)
|
||||
const hasReceivedAllItems = checkAllItemsReceived(returnEntry)
|
||||
const retData = getReturnUpdateData(hasReceivedAllItems)
|
||||
|
||||
const retData = hasReceivedAllItems
|
||||
? { status: ReturnStatus.RECEIVED, received_at: new Date() }
|
||||
: { status: ReturnStatus.PARTIALLY_RECEIVED }
|
||||
|
||||
const returnRef = await this.updateReturns(
|
||||
{
|
||||
selector: { id: returnEntry.id },
|
||||
data: retData,
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
const [returnRef] = await promiseAll([
|
||||
this.updateReturns(
|
||||
{ selector: { id: returnEntry.id }, data: retData },
|
||||
sharedContext
|
||||
),
|
||||
this.updateReturnItems(retItemsToUpdate, sharedContext),
|
||||
])
|
||||
|
||||
return returnRef
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ import {
|
||||
Context,
|
||||
DAL,
|
||||
FindConfig,
|
||||
InternalModuleDeclaration,
|
||||
IOrderModuleService,
|
||||
InternalModuleDeclaration,
|
||||
ModuleJoinerConfig,
|
||||
ModulesSdkTypes,
|
||||
OrderDTO,
|
||||
@@ -16,18 +16,18 @@ import {
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
BigNumber,
|
||||
createRawPropertiesFromBigNumber,
|
||||
decorateCartTotals,
|
||||
deduplicate,
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
isObject,
|
||||
isString,
|
||||
MathBN,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
ModulesSdkUtils,
|
||||
OrderStatus,
|
||||
createRawPropertiesFromBigNumber,
|
||||
decorateCartTotals,
|
||||
deduplicate,
|
||||
isObject,
|
||||
isString,
|
||||
promiseAll,
|
||||
transformPropertiesToBigNumber,
|
||||
} from "@medusajs/utils"
|
||||
@@ -39,10 +39,13 @@ import {
|
||||
Order,
|
||||
OrderChange,
|
||||
OrderChangeAction,
|
||||
OrderClaim,
|
||||
OrderExchange,
|
||||
OrderItem,
|
||||
OrderShippingMethod,
|
||||
OrderSummary,
|
||||
Return,
|
||||
ReturnItem,
|
||||
ReturnReason,
|
||||
ShippingMethod,
|
||||
ShippingMethodAdjustment,
|
||||
@@ -64,8 +67,8 @@ import {
|
||||
} from "@types"
|
||||
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||
import {
|
||||
applyChangesToOrder,
|
||||
ApplyOrderChangeDTO,
|
||||
applyChangesToOrder,
|
||||
calculateOrderChange,
|
||||
formatOrder,
|
||||
} from "../utils"
|
||||
@@ -91,6 +94,9 @@ type InjectedDependencies = {
|
||||
orderShippingMethodService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
returnReasonService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
returnService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
returnItemService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
orderClaimService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
orderExchangeService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
|
||||
const generateMethodForModels = {
|
||||
@@ -109,6 +115,9 @@ const generateMethodForModels = {
|
||||
OrderShippingMethod,
|
||||
ReturnReason,
|
||||
Return,
|
||||
ReturnItem,
|
||||
OrderClaim,
|
||||
OrderExchange,
|
||||
}
|
||||
|
||||
export default class OrderModuleService<
|
||||
@@ -127,7 +136,10 @@ export default class OrderModuleService<
|
||||
TOrderSummary extends OrderSummary = OrderSummary,
|
||||
TOrderShippingMethod extends OrderShippingMethod = OrderShippingMethod,
|
||||
TReturnReason extends ReturnReason = ReturnReason,
|
||||
TReturn extends Return = Return
|
||||
TReturn extends Return = Return,
|
||||
TReturnItem extends ReturnItem = ReturnItem,
|
||||
TClaim extends OrderClaim = OrderClaim,
|
||||
TExchange extends OrderExchange = OrderExchange
|
||||
>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
OrderTypes.OrderDTO,
|
||||
@@ -149,6 +161,9 @@ export default class OrderModuleService<
|
||||
OrderSummary: { dto: OrderTypes.OrderSummaryDTO }
|
||||
Transaction: { dto: OrderTypes.OrderTransactionDTO }
|
||||
Return: { dto: any } // TODO: Add return dto
|
||||
ReturnItem: { dto: any } // TODO: Add return item dto
|
||||
OrderClaim: { dto: any } // TODO: Add claim dto
|
||||
OrderExchange: { dto: any } // TODO: Add exchange dto
|
||||
}
|
||||
>(Order, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
implements IOrderModuleService
|
||||
@@ -170,6 +185,9 @@ export default class OrderModuleService<
|
||||
protected orderShippingMethodService_: ModulesSdkTypes.IMedusaInternalService<TOrderShippingMethod>
|
||||
protected returnReasonService_: ModulesSdkTypes.IMedusaInternalService<TReturnReason>
|
||||
protected returnService_: ModulesSdkTypes.IMedusaInternalService<TReturn>
|
||||
protected returnItemService_: ModulesSdkTypes.IMedusaInternalService<TReturnItem>
|
||||
protected orderClaimService_: ModulesSdkTypes.IMedusaInternalService<TClaim>
|
||||
protected orderExchangeService_: ModulesSdkTypes.IMedusaInternalService<TExchange>
|
||||
|
||||
constructor(
|
||||
{
|
||||
@@ -190,6 +208,9 @@ export default class OrderModuleService<
|
||||
orderShippingMethodService,
|
||||
returnReasonService,
|
||||
returnService,
|
||||
returnItemService,
|
||||
orderClaimService,
|
||||
orderExchangeService,
|
||||
}: InjectedDependencies,
|
||||
protected readonly moduleDeclaration: InternalModuleDeclaration
|
||||
) {
|
||||
@@ -213,6 +234,9 @@ export default class OrderModuleService<
|
||||
this.orderShippingMethodService_ = orderShippingMethodService
|
||||
this.returnReasonService_ = returnReasonService
|
||||
this.returnService_ = returnService
|
||||
this.returnItemService_ = returnItemService
|
||||
this.orderClaimService_ = orderClaimService
|
||||
this.orderExchangeService_ = orderExchangeService
|
||||
}
|
||||
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
@@ -289,7 +313,10 @@ export default class OrderModuleService<
|
||||
|
||||
const order = await super.retrieve(id, config, sharedContext)
|
||||
|
||||
return formatOrder(order, { includeTotals }) as OrderTypes.OrderDTO
|
||||
return formatOrder(order, {
|
||||
entity: Order,
|
||||
includeTotals,
|
||||
}) as OrderTypes.OrderDTO
|
||||
}
|
||||
|
||||
async list(
|
||||
@@ -303,6 +330,7 @@ export default class OrderModuleService<
|
||||
const orders = await super.list(filters, config, sharedContext)
|
||||
|
||||
return formatOrder(orders, {
|
||||
entity: Order,
|
||||
includeTotals,
|
||||
}) as OrderTypes.OrderDTO[]
|
||||
}
|
||||
@@ -322,7 +350,10 @@ export default class OrderModuleService<
|
||||
)
|
||||
|
||||
return [
|
||||
formatOrder(orders, { includeTotals }) as OrderTypes.OrderDTO[],
|
||||
formatOrder(orders, {
|
||||
entity: Order,
|
||||
includeTotals,
|
||||
}) as OrderTypes.OrderDTO[],
|
||||
count,
|
||||
]
|
||||
}
|
||||
@@ -338,7 +369,10 @@ export default class OrderModuleService<
|
||||
|
||||
const returnOrder = await super.retrieveReturn(id, config, sharedContext)
|
||||
|
||||
return formatOrder(returnOrder, { includeTotals }) as OrderTypes.ReturnDTO
|
||||
return formatOrder(returnOrder, {
|
||||
entity: Return,
|
||||
includeTotals,
|
||||
}) as OrderTypes.ReturnDTO
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
@@ -353,6 +387,7 @@ export default class OrderModuleService<
|
||||
const returnOrders = await super.listReturns(filters, config, sharedContext)
|
||||
|
||||
return formatOrder(returnOrders, {
|
||||
entity: Return,
|
||||
includeTotals,
|
||||
}) as OrderTypes.ReturnDTO[]
|
||||
}
|
||||
@@ -373,7 +408,142 @@ export default class OrderModuleService<
|
||||
)
|
||||
|
||||
return [
|
||||
formatOrder(returnOrders, { includeTotals }) as OrderTypes.ReturnDTO[],
|
||||
formatOrder(returnOrders, {
|
||||
entity: Return,
|
||||
includeTotals,
|
||||
}) as OrderTypes.ReturnDTO[],
|
||||
count,
|
||||
]
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
async retrieveOrderClaim(
|
||||
id: string,
|
||||
config?: FindConfig<any> | undefined,
|
||||
@MedusaContext() sharedContext?: Context | undefined
|
||||
): Promise<OrderTypes.OrderClaimDTO> {
|
||||
config ??= {}
|
||||
const includeTotals = this.shouldIncludeTotals(config)
|
||||
|
||||
const returnOrder = await super.retrieveOrderClaim(
|
||||
id,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return formatOrder(returnOrder, {
|
||||
entity: OrderClaim,
|
||||
includeTotals,
|
||||
}) as OrderTypes.OrderClaimDTO
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
async listOrderClaims(
|
||||
filters?: any,
|
||||
config?: FindConfig<any> | undefined,
|
||||
@MedusaContext() sharedContext?: Context | undefined
|
||||
): Promise<OrderTypes.OrderClaimDTO[]> {
|
||||
config ??= {}
|
||||
const includeTotals = this.shouldIncludeTotals(config)
|
||||
|
||||
const returnOrders = await super.listOrderClaims(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return formatOrder(returnOrders, {
|
||||
entity: OrderClaim,
|
||||
includeTotals,
|
||||
}) as OrderTypes.OrderClaimDTO[]
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
async listAndCountOrderClaims(
|
||||
filters?: any,
|
||||
config?: FindConfig<any> | undefined,
|
||||
@MedusaContext() sharedContext?: Context | undefined
|
||||
): Promise<[OrderTypes.OrderClaimDTO[], number]> {
|
||||
config ??= {}
|
||||
const includeTotals = this.shouldIncludeTotals(config)
|
||||
|
||||
const [returnOrders, count] = await super.listAndCountOrderClaims(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return [
|
||||
formatOrder(returnOrders, {
|
||||
entity: OrderClaim,
|
||||
includeTotals,
|
||||
}) as OrderTypes.OrderClaimDTO[],
|
||||
count,
|
||||
]
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
async retrieveOrderExchange(
|
||||
id: string,
|
||||
config?: FindConfig<any> | undefined,
|
||||
@MedusaContext() sharedContext?: Context | undefined
|
||||
): Promise<OrderTypes.OrderExchangeDTO> {
|
||||
config ??= {}
|
||||
const includeTotals = this.shouldIncludeTotals(config)
|
||||
|
||||
const returnOrder = await super.retrieveOrderExchange(
|
||||
id,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return formatOrder(returnOrder, {
|
||||
entity: OrderExchange,
|
||||
includeTotals,
|
||||
}) as OrderTypes.OrderExchangeDTO
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
async listOrderExchanges(
|
||||
filters?: any,
|
||||
config?: FindConfig<any> | undefined,
|
||||
@MedusaContext() sharedContext?: Context | undefined
|
||||
): Promise<OrderTypes.OrderExchangeDTO[]> {
|
||||
config ??= {}
|
||||
const includeTotals = this.shouldIncludeTotals(config)
|
||||
|
||||
const returnOrders = await super.listOrderExchanges(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return formatOrder(returnOrders, {
|
||||
entity: OrderExchange,
|
||||
includeTotals,
|
||||
}) as OrderTypes.OrderExchangeDTO[]
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
async listAndCountOrderExchanges(
|
||||
filters?: any,
|
||||
config?: FindConfig<any> | undefined,
|
||||
@MedusaContext() sharedContext?: Context | undefined
|
||||
): Promise<[OrderTypes.OrderExchangeDTO[], number]> {
|
||||
config ??= {}
|
||||
const includeTotals = this.shouldIncludeTotals(config)
|
||||
|
||||
const [returnOrders, count] = await super.listAndCountOrderExchanges(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return [
|
||||
formatOrder(returnOrders, {
|
||||
entity: OrderExchange,
|
||||
includeTotals,
|
||||
}) as OrderTypes.OrderExchangeDTO[],
|
||||
count,
|
||||
]
|
||||
}
|
||||
@@ -1648,26 +1818,16 @@ export default class OrderModuleService<
|
||||
await this.orderChangeService_.update(updates as any, sharedContext)
|
||||
}
|
||||
|
||||
async confirmOrderChange(
|
||||
orderChangeId: string,
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
|
||||
async confirmOrderChange(
|
||||
orderChangeId: string[],
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
|
||||
async confirmOrderChange(orderChangeId: string, sharedContext?: Context)
|
||||
async confirmOrderChange(orderChangeId: string[], sharedContext?: Context)
|
||||
async confirmOrderChange(
|
||||
data: OrderTypes.ConfirmOrderChangeDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
|
||||
)
|
||||
async confirmOrderChange(
|
||||
data: OrderTypes.ConfirmOrderChangeDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
|
||||
)
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async confirmOrderChange(
|
||||
orderChangeIdOrData:
|
||||
@@ -1676,7 +1836,7 @@ export default class OrderModuleService<
|
||||
| OrderTypes.ConfirmOrderChangeDTO
|
||||
| OrderTypes.ConfirmOrderChangeDTO[],
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<void> {
|
||||
): Promise<OrderTypes.OrderChangeReturn> {
|
||||
const data = Array.isArray(orderChangeIdOrData)
|
||||
? orderChangeIdOrData
|
||||
: [orderChangeIdOrData]
|
||||
@@ -1715,29 +1875,19 @@ export default class OrderModuleService<
|
||||
return change.actions
|
||||
})
|
||||
|
||||
await this.applyOrderChanges_(orderChanges.flat(), sharedContext)
|
||||
return await this.applyOrderChanges_(orderChanges.flat(), sharedContext)
|
||||
}
|
||||
|
||||
async declineOrderChange(
|
||||
orderChangeId: string,
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
|
||||
async declineOrderChange(
|
||||
orderChangeId: string[],
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
|
||||
async declineOrderChange(orderChangeId: string, sharedContext?: Context)
|
||||
async declineOrderChange(orderChangeId: string[], sharedContext?: Context)
|
||||
async declineOrderChange(
|
||||
data: OrderTypes.DeclineOrderChangeDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
|
||||
)
|
||||
async declineOrderChange(
|
||||
data: OrderTypes.DeclineOrderChangeDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
|
||||
)
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async declineOrderChange(
|
||||
orderChangeIdOrData:
|
||||
@@ -1772,7 +1922,7 @@ export default class OrderModuleService<
|
||||
async applyPendingOrderActions(
|
||||
orderId: string | string[],
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<void> {
|
||||
): Promise<OrderTypes.OrderChangeReturn> {
|
||||
const orderIds = Array.isArray(orderId) ? orderId : [orderId]
|
||||
|
||||
const orders = await this.list(
|
||||
@@ -1811,7 +1961,7 @@ export default class OrderModuleService<
|
||||
sharedContext
|
||||
)
|
||||
|
||||
await this.applyOrderChanges_(
|
||||
return await this.applyOrderChanges_(
|
||||
changes as ApplyOrderChangeDTO[],
|
||||
sharedContext
|
||||
)
|
||||
@@ -2059,7 +2209,7 @@ export default class OrderModuleService<
|
||||
private async applyOrderChanges_(
|
||||
changeActions: ApplyOrderChangeDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<void> {
|
||||
): Promise<OrderTypes.OrderChangeReturn> {
|
||||
const actionsMap: Record<string, any[]> = {}
|
||||
const ordersIds: string[] = []
|
||||
const usedActions: any[] = []
|
||||
@@ -2085,10 +2235,13 @@ export default class OrderModuleService<
|
||||
}
|
||||
|
||||
if (!ordersIds.length) {
|
||||
return
|
||||
return {
|
||||
items: [],
|
||||
shippingMethods: [],
|
||||
}
|
||||
}
|
||||
|
||||
const orders = await this.list(
|
||||
let orders = await super.list(
|
||||
{ id: deduplicate(ordersIds) },
|
||||
{
|
||||
select: [
|
||||
@@ -2096,13 +2249,22 @@ export default class OrderModuleService<
|
||||
"version",
|
||||
"items.detail",
|
||||
"transactions",
|
||||
"shipping_methods",
|
||||
"summary",
|
||||
"total",
|
||||
],
|
||||
relations: ["transactions", "items", "items.detail"],
|
||||
relations: [
|
||||
"transactions",
|
||||
"items",
|
||||
"items.detail",
|
||||
"shipping_methods",
|
||||
],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
orders = formatOrder(orders, {
|
||||
entity: Order,
|
||||
}) as OrderDTO[]
|
||||
|
||||
const {
|
||||
itemsToUpsert,
|
||||
@@ -2131,6 +2293,11 @@ export default class OrderModuleService<
|
||||
)
|
||||
: null,
|
||||
])
|
||||
|
||||
return {
|
||||
items: itemsToUpsert as any,
|
||||
shippingMethods: shippingMethodsToInsert as any,
|
||||
}
|
||||
}
|
||||
|
||||
async addTransactions(
|
||||
@@ -2663,6 +2830,88 @@ export default class OrderModuleService<
|
||||
return await BundledActions.receiveReturn.bind(this)(data, sharedContext)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async createClaim(
|
||||
data: OrderTypes.CreateOrderClaimDTO,
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<any> {
|
||||
const ret = await this.createClaim_(data, sharedContext)
|
||||
|
||||
const claim = await this.retrieveOrderClaim(
|
||||
ret.id,
|
||||
{
|
||||
relations: [
|
||||
"additional_items",
|
||||
"additional_items.item",
|
||||
"claim_items",
|
||||
"claim_items.item",
|
||||
"return",
|
||||
"return.items",
|
||||
"shipping_methods",
|
||||
"shipping_methods.tax_lines",
|
||||
"shipping_methods.adjustments",
|
||||
"transactions",
|
||||
],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<OrderTypes.OrderClaimDTO[]>(
|
||||
claim,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createExchange_(
|
||||
data: OrderTypes.CreateOrderExchangeDTO,
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<any> {
|
||||
return await BundledActions.createExchange.bind(this)(data, sharedContext)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async createExchange(
|
||||
data: OrderTypes.CreateOrderExchangeDTO,
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<any> {
|
||||
const ret = await this.createExchange_(data, sharedContext)
|
||||
|
||||
const claim = await this.retrieveOrderExchange(
|
||||
ret.id,
|
||||
{
|
||||
relations: [
|
||||
"additional_items",
|
||||
"additional_items.item",
|
||||
"return",
|
||||
"return.items",
|
||||
"shipping_methods",
|
||||
"shipping_methods.tax_lines",
|
||||
"shipping_methods.adjustments",
|
||||
"transactions",
|
||||
],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<OrderTypes.OrderExchangeDTO[]>(
|
||||
claim,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createClaim_(
|
||||
data: OrderTypes.CreateOrderClaimDTO,
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<any> {
|
||||
return await BundledActions.createClaim.bind(this)(data, sharedContext)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async registerFulfillment(
|
||||
data: OrderTypes.RegisterOrderFulfillmentDTO,
|
||||
|
||||
@@ -9,3 +9,4 @@ export * from "./receive-return-item"
|
||||
export * from "./return-item"
|
||||
export * from "./ship-item"
|
||||
export * from "./shipping-add"
|
||||
export * from "./write-off-item"
|
||||
|
||||
@@ -7,7 +7,7 @@ import { setActionReference } from "../set-action-reference"
|
||||
OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
||||
operation({ action, currentOrder }) {
|
||||
const existing = currentOrder.items.find(
|
||||
(item) => item.id === action.reference_id
|
||||
(item) => item.id === action.details.reference_id
|
||||
)
|
||||
|
||||
if (existing) {
|
||||
@@ -23,7 +23,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
||||
setActionReference(existing, action)
|
||||
} else {
|
||||
currentOrder.items.push({
|
||||
id: action.reference_id!,
|
||||
id: action.details.reference_id!,
|
||||
order_id: currentOrder.id,
|
||||
return_id: action.details.return_id,
|
||||
claim_id: action.details.claim_id,
|
||||
@@ -38,7 +38,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
||||
},
|
||||
revert({ action, currentOrder }) {
|
||||
const existingIndex = currentOrder.items.findIndex(
|
||||
(item) => item.id === action.reference_id
|
||||
(item) => item.id === action.details.reference_id
|
||||
)
|
||||
|
||||
if (existingIndex > -1) {
|
||||
@@ -55,13 +55,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_ADD, {
|
||||
}
|
||||
},
|
||||
validate({ action }) {
|
||||
const refId = action.reference_id
|
||||
if (!isDefined(action.reference_id)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"Reference ID is required."
|
||||
)
|
||||
}
|
||||
const refId = action.details?.reference_id
|
||||
|
||||
if (!isDefined(action.amount) && !isDefined(action.details?.unit_price)) {
|
||||
throw new MedusaError(
|
||||
|
||||
@@ -8,7 +8,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_REMOVE, {
|
||||
isDeduction: true,
|
||||
operation({ action, currentOrder }) {
|
||||
const existingIndex = currentOrder.items.findIndex(
|
||||
(item) => item.id === action.reference_id
|
||||
(item) => item.id === action.details.reference_id
|
||||
)
|
||||
|
||||
const existing = currentOrder.items[existingIndex]
|
||||
@@ -31,7 +31,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_REMOVE, {
|
||||
},
|
||||
revert({ action, currentOrder }) {
|
||||
const existing = currentOrder.items.find(
|
||||
(item) => item.id === action.reference_id
|
||||
(item) => item.id === action.details.reference_id
|
||||
)
|
||||
|
||||
if (existing) {
|
||||
@@ -42,14 +42,14 @@ OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_REMOVE, {
|
||||
)
|
||||
} else {
|
||||
currentOrder.items.push({
|
||||
id: action.reference_id!,
|
||||
id: action.details.reference_id!,
|
||||
unit_price: action.details.unit_price,
|
||||
quantity: action.details.quantity,
|
||||
} as VirtualOrder["items"][0])
|
||||
}
|
||||
},
|
||||
validate({ action, currentOrder }) {
|
||||
const refId = action.reference_id
|
||||
const refId = action.details?.reference_id
|
||||
if (!isDefined(refId)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
|
||||
@@ -31,24 +31,23 @@ export function applyChangesToOrder(
|
||||
const version = actionsMap[order.id][0].version ?? 1
|
||||
|
||||
for (const item of calculated.order.items) {
|
||||
const orderItem = item.detail as any
|
||||
const orderItem = (item.detail as any) ?? item
|
||||
const itemId = item.detail ? orderItem.item_id : item.id
|
||||
|
||||
itemsToUpsert.push({
|
||||
id: orderItem.version === version ? orderItem.id : undefined,
|
||||
item_id: item.id,
|
||||
item_id: itemId,
|
||||
order_id: order.id,
|
||||
version,
|
||||
return_id: item.detail.return_id,
|
||||
claim_id: item.detail.claim_id,
|
||||
exchange_id: item.detail.exchange_id,
|
||||
quantity: item.detail.quantity,
|
||||
fulfilled_quantity: item.detail.fulfilled_quantity,
|
||||
shipped_quantity: item.detail.shipped_quantity,
|
||||
return_requested_quantity: item.detail.return_requested_quantity,
|
||||
return_received_quantity: item.detail.return_received_quantity,
|
||||
return_dismissed_quantity: item.detail.return_dismissed_quantity,
|
||||
written_off_quantity: item.detail.written_off_quantity,
|
||||
metadata: item.detail.metadata,
|
||||
} as any)
|
||||
quantity: orderItem.quantity,
|
||||
fulfilled_quantity: orderItem.fulfilled_quantity,
|
||||
shipped_quantity: orderItem.shipped_quantity,
|
||||
return_requested_quantity: orderItem.return_requested_quantity,
|
||||
return_received_quantity: orderItem.return_received_quantity,
|
||||
return_dismissed_quantity: orderItem.return_dismissed_quantity,
|
||||
written_off_quantity: orderItem.written_off_quantity,
|
||||
metadata: orderItem.metadata,
|
||||
} as OrderItem)
|
||||
}
|
||||
|
||||
const orderSummary = order.summary as any
|
||||
@@ -61,6 +60,10 @@ export function applyChangesToOrder(
|
||||
|
||||
if (version > order.version) {
|
||||
for (const shippingMethod of calculated.order.shipping_methods ?? []) {
|
||||
if (!shippingMethod) {
|
||||
continue
|
||||
}
|
||||
|
||||
const sm = {
|
||||
...((shippingMethod as any).detail ?? shippingMethod),
|
||||
version,
|
||||
|
||||
@@ -29,13 +29,16 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
}
|
||||
}
|
||||
|
||||
const config = mapRepositoryToOrderModel(findOptions_)
|
||||
const isRelatedEntity = entity !== Order
|
||||
const config = mapRepositoryToOrderModel(findOptions_, isRelatedEntity)
|
||||
|
||||
let orderAlias = "o0"
|
||||
if (entity !== Order) {
|
||||
// first relation is always order if entity is not Order
|
||||
if (isRelatedEntity) {
|
||||
// first relation is always order if the entity is not Order
|
||||
config.options.populate.unshift("order")
|
||||
orderAlias = "o1"
|
||||
|
||||
config.options.populate.unshift("order.items")
|
||||
}
|
||||
|
||||
let defaultVersion = knex.raw(`"${orderAlias}"."version"`)
|
||||
@@ -44,7 +47,7 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
const sql = manager
|
||||
.qb(Order, "_sub0")
|
||||
.select("version")
|
||||
.where({ id: knex.raw(`"o0"."order_id"`) })
|
||||
.where({ id: knex.raw(`"${orderAlias}"."order_id"`) })
|
||||
.getKnexQuery()
|
||||
.toString()
|
||||
|
||||
@@ -55,23 +58,22 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
delete config.where?.version
|
||||
|
||||
config.options.populateWhere ??= {}
|
||||
const popWhere = config.options.populateWhere
|
||||
|
||||
if (entity !== Order) {
|
||||
config.options.populateWhere.order ??= {}
|
||||
config.options.populateWhere.order.version = version
|
||||
|
||||
config.options.populateWhere.order.summary ??= {}
|
||||
config.options.populateWhere.order.summary.version = version
|
||||
} else {
|
||||
config.options.populateWhere.summary ??= {}
|
||||
config.options.populateWhere.summary.version = version
|
||||
if (isRelatedEntity) {
|
||||
popWhere.order ??= {}
|
||||
}
|
||||
|
||||
config.options.populateWhere.items ??= {}
|
||||
config.options.populateWhere.items.version = version
|
||||
const orderWhere = isRelatedEntity ? popWhere.order : popWhere
|
||||
|
||||
config.options.populateWhere.shipping_methods ??= {}
|
||||
config.options.populateWhere.shipping_methods.version = version
|
||||
orderWhere.summary ??= {}
|
||||
orderWhere.summary.version = version
|
||||
|
||||
orderWhere.items ??= {}
|
||||
orderWhere.items.version = version
|
||||
|
||||
popWhere.shipping_methods ??= {}
|
||||
popWhere.shipping_methods.version = version
|
||||
|
||||
if (!config.options.orderBy) {
|
||||
config.options.orderBy = { id: "ASC" }
|
||||
@@ -98,10 +100,11 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
})
|
||||
}
|
||||
|
||||
const config = mapRepositoryToOrderModel(findOptions_)
|
||||
const isRelatedEntity = entity !== Order
|
||||
const config = mapRepositoryToOrderModel(findOptions_, isRelatedEntity)
|
||||
|
||||
let orderAlias = "o0"
|
||||
if (entity !== Order) {
|
||||
if (isRelatedEntity) {
|
||||
// first relation is always order if entity is not Order
|
||||
config.options.populate.unshift("order")
|
||||
orderAlias = "o1"
|
||||
@@ -113,7 +116,7 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
const sql = manager
|
||||
.qb(Order, "_sub0")
|
||||
.select("version")
|
||||
.where({ id: knex.raw(`"o0"."order_id"`) })
|
||||
.where({ id: knex.raw(`"${orderAlias}"."order_id"`) })
|
||||
.getKnexQuery()
|
||||
.toString()
|
||||
|
||||
@@ -124,23 +127,22 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
delete config.where.version
|
||||
|
||||
config.options.populateWhere ??= {}
|
||||
const popWhere = config.options.populateWhere
|
||||
|
||||
if (entity !== Order) {
|
||||
config.options.populateWhere.order ??= {}
|
||||
config.options.populateWhere.order.version = version
|
||||
|
||||
config.options.populateWhere.order.summary ??= {}
|
||||
config.options.populateWhere.order.summary.version = version
|
||||
} else {
|
||||
config.options.populateWhere.summary ??= {}
|
||||
config.options.populateWhere.summary.version = version
|
||||
if (isRelatedEntity) {
|
||||
popWhere.order ??= {}
|
||||
}
|
||||
|
||||
config.options.populateWhere.items ??= {}
|
||||
config.options.populateWhere.items.version = version
|
||||
const orderWhere = isRelatedEntity ? popWhere.order : popWhere
|
||||
|
||||
config.options.populateWhere.shipping_methods ??= {}
|
||||
config.options.populateWhere.shipping_methods.version = version
|
||||
orderWhere.summary ??= {}
|
||||
orderWhere.summary.version = version
|
||||
|
||||
orderWhere.items ??= {}
|
||||
orderWhere.items.version = version
|
||||
|
||||
popWhere.shipping_methods ??= {}
|
||||
popWhere.shipping_methods.version = version
|
||||
|
||||
if (!config.options.orderBy) {
|
||||
config.options.orderBy = { id: "ASC" }
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
BigNumber,
|
||||
MathBN,
|
||||
isDefined,
|
||||
isPresent,
|
||||
transformPropertiesToBigNumber,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
@@ -183,9 +184,14 @@ export class OrderChangeProcessing {
|
||||
action: InternalOrderChangeEvent,
|
||||
isReplay = false
|
||||
): BigNumberInput | void {
|
||||
const definedType = OrderChangeProcessing.typeDefinition[action.action]
|
||||
if (!isPresent(definedType)) {
|
||||
throw new Error(`Action type ${action.action} is not defined`)
|
||||
}
|
||||
|
||||
const type = {
|
||||
...OrderChangeProcessing.defaultConfig,
|
||||
...OrderChangeProcessing.typeDefinition[action.action],
|
||||
...definedType,
|
||||
}
|
||||
|
||||
this.actionsProcessed[action.action] ??= []
|
||||
|
||||
@@ -5,18 +5,32 @@ import {
|
||||
deduplicate,
|
||||
isDefined,
|
||||
} from "@medusajs/utils"
|
||||
import { Order, OrderClaim, OrderExchange, Return } from "@models"
|
||||
|
||||
export function formatOrder(
|
||||
order,
|
||||
options: {
|
||||
entity: any
|
||||
includeTotals?: boolean
|
||||
}
|
||||
): OrderTypes.OrderDTO | OrderTypes.OrderDTO[] {
|
||||
): Partial<OrderTypes.OrderDTO> | Partial<OrderTypes.OrderDTO>[] {
|
||||
const isArray = Array.isArray(order)
|
||||
const orders = [...(isArray ? order : [order])]
|
||||
|
||||
orders.map((order) => {
|
||||
order.items = order.items?.map((orderItem) => {
|
||||
let mainOrder = order
|
||||
|
||||
const isRelatedEntity = options?.entity !== Order
|
||||
|
||||
if (isRelatedEntity) {
|
||||
if (!order.order) {
|
||||
return order
|
||||
}
|
||||
|
||||
mainOrder = order.order
|
||||
}
|
||||
|
||||
mainOrder.items = mainOrder.items?.map((orderItem) => {
|
||||
const detail = { ...orderItem }
|
||||
delete detail.order
|
||||
delete detail.item
|
||||
@@ -29,30 +43,118 @@ export function formatOrder(
|
||||
}
|
||||
})
|
||||
|
||||
order.shipping_methods = order.shipping_methods?.map((shippingMethod) => {
|
||||
const sm = { ...shippingMethod.shipping_method }
|
||||
|
||||
delete shippingMethod.shipping_method
|
||||
return {
|
||||
...sm,
|
||||
order_id: shippingMethod.order_id,
|
||||
detail: {
|
||||
...shippingMethod,
|
||||
},
|
||||
if (isRelatedEntity) {
|
||||
if (order.return) {
|
||||
formatOrderReturn(order.return, mainOrder)
|
||||
}
|
||||
})
|
||||
|
||||
order.summary = order.summary?.[0]?.totals
|
||||
if (options.entity === OrderClaim) {
|
||||
formatClaim(order)
|
||||
} else if (options.entity === OrderExchange) {
|
||||
formatExchange(order)
|
||||
} else if (options.entity === Return) {
|
||||
formatReturn(order)
|
||||
}
|
||||
}
|
||||
|
||||
return options?.includeTotals
|
||||
? createRawPropertiesFromBigNumber(decorateCartTotals(order))
|
||||
: order
|
||||
if (order.shipping_methods) {
|
||||
order.shipping_methods = order.shipping_methods?.map((shippingMethod) => {
|
||||
const sm = { ...shippingMethod.shipping_method }
|
||||
|
||||
delete shippingMethod.shipping_method
|
||||
return {
|
||||
...sm,
|
||||
order_id: shippingMethod.order_id,
|
||||
detail: {
|
||||
...shippingMethod,
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (mainOrder.summary) {
|
||||
mainOrder.summary = mainOrder.summary?.[0]?.totals
|
||||
}
|
||||
|
||||
return createRawPropertiesFromBigNumber(
|
||||
options?.includeTotals ? decorateCartTotals(order) : order
|
||||
)
|
||||
})
|
||||
|
||||
return isArray ? orders : orders[0]
|
||||
}
|
||||
|
||||
export function mapRepositoryToOrderModel(config) {
|
||||
function formatOrderReturn(orderReturn, mainOrder) {
|
||||
orderReturn.items = orderReturn.items.filter(
|
||||
(item) => !item.is_additional_item
|
||||
)
|
||||
orderReturn.items.forEach((orderItem) => {
|
||||
const item = mainOrder.items?.find((item) => item.id === orderItem.item_id)
|
||||
|
||||
orderItem.detail = item?.detail
|
||||
})
|
||||
}
|
||||
|
||||
function formatClaim(claim) {
|
||||
if (claim.additional_items) {
|
||||
claim.additional_items = claim.additional_items.filter(
|
||||
(item) => item.is_additional_item
|
||||
)
|
||||
|
||||
claim.additional_items.forEach((orderItem) => {
|
||||
const item = claim.order.items?.find(
|
||||
(item) => item.id === orderItem.item_id
|
||||
)
|
||||
|
||||
orderItem.detail = item?.detail
|
||||
})
|
||||
}
|
||||
|
||||
if (!claim.claim_items) {
|
||||
return
|
||||
}
|
||||
|
||||
claim.claim_items = claim.claim_items.filter(
|
||||
(item) => !item.is_additional_item
|
||||
)
|
||||
claim.claim_items.forEach((orderItem) => {
|
||||
const item = claim.order.items?.find(
|
||||
(item) => item.id === orderItem.item_id
|
||||
)
|
||||
|
||||
orderItem.detail = item?.detail
|
||||
})
|
||||
}
|
||||
|
||||
function formatExchange(exchange) {
|
||||
if (!exchange.additional_items) {
|
||||
return
|
||||
}
|
||||
|
||||
exchange.additional_items.forEach((orderItem) => {
|
||||
const item = exchange.order.items?.find(
|
||||
(item) => item.id === orderItem.item_id
|
||||
)
|
||||
|
||||
orderItem.detail = item?.detail
|
||||
})
|
||||
}
|
||||
|
||||
function formatReturn(returnOrder) {
|
||||
if (!returnOrder.items) {
|
||||
return
|
||||
}
|
||||
|
||||
returnOrder.items.forEach((orderItem) => {
|
||||
const item = returnOrder.order.items?.find(
|
||||
(item) => item.id === orderItem.item_id
|
||||
)
|
||||
|
||||
orderItem.detail = item?.detail
|
||||
})
|
||||
}
|
||||
|
||||
export function mapRepositoryToOrderModel(config, isRelatedEntity = false) {
|
||||
const conf = { ...config }
|
||||
|
||||
function replace(obj, type): string[] | undefined {
|
||||
|
||||
Reference in New Issue
Block a user