chore(order): cancel return (#7881)

This commit is contained in:
Carlos R. L. Rodrigues
2024-07-02 06:52:58 -03:00
committed by GitHub
parent b3236ff31c
commit 07715e6b50
46 changed files with 1339 additions and 121 deletions

View File

@@ -25,7 +25,6 @@ import { ReturnItem, Transaction } from "@models"
import Claim from "./claim"
import Exchange from "./exchange"
import Order from "./order"
import OrderItem from "./order-item"
import OrderShippingMethod from "./order-shipping-method"
type OptionalReturnProps = DAL.EntityDateColumns
@@ -131,7 +130,7 @@ export default class Return {
@OneToMany(() => ReturnItem, (itemDetail) => itemDetail.return, {
cascade: [Cascade.PERSIST],
})
items = new Collection<Rel<OrderItem>>(this)
items = new Collection<Rel<ReturnItem>>(this)
@OneToMany(
() => OrderShippingMethod,

View File

@@ -78,7 +78,7 @@ describe("Order Return - Actions", function () {
actions,
})
}).toThrow(
"Cannot request to return more items than what was shipped for item 1."
"Cannot request to return more items than what was fulfilled for item 1."
)
expect(() => {
@@ -190,7 +190,7 @@ describe("Order Return - Actions", function () {
],
})
}).toThrow(
"Cannot request to return more items than what was shipped for item 3."
"Cannot request to return more items than what was fulfilled for item 3."
)
expect(() => {

View File

@@ -0,0 +1,144 @@
import {
Context,
CreateOrderChangeActionDTO,
OrderTypes,
} from "@medusajs/types"
import { promiseAll } from "@medusajs/utils"
import { ChangeActionType } from "../../utils"
async function createOrderChange(
service,
data,
returnRef,
actions,
sharedContext
) {
return await service.createOrderChange_(
{
order_id: returnRef.order_id,
claim_id: returnRef.id,
reference: "return",
reference_id: returnRef.id,
description: data.description,
internal_note: data.internal_note,
created_by: data.created_by,
metadata: data.metadata,
actions,
},
sharedContext
)
}
export async function cancelClaim(
this: any,
data: OrderTypes.CancelOrderClaimDTO,
sharedContext?: Context
) {
const claimOrder = await this.retrieveClaim(
data.claim_id,
{
select: [
"id",
"order_id",
"return.id",
"return.items.id",
"return.items.quantity",
"claim_items.item_id",
"claim_items.quantity",
"additional_items.id",
"additional_items.quantity",
],
relations: ["return.items", "additional_items", "shipping_methods"],
},
sharedContext
)
const actions: CreateOrderChangeActionDTO[] = []
claimOrder.return.items.forEach((item) => {
actions.push({
action: ChangeActionType.CANCEL_RETURN_ITEM,
order_id: claimOrder.order_id,
claim_id: claimOrder.id,
return_id: claimOrder.return.id,
reference: "return",
reference_id: claimOrder.return.id,
details: {
reference_id: item.id,
order_id: claimOrder.order_id,
claim_id: claimOrder.id,
return_id: claimOrder.return.id,
quantity: item.quantity,
},
})
})
claimOrder.claim_items.forEach((item) => {
actions.push({
action: ChangeActionType.REINSTATE_ITEM,
claim_id: claimOrder.id,
reference: "claim",
reference_id: claimOrder.id,
details: {
reference_id: item.id,
claim_id: claimOrder.id,
quantity: item.quantity,
},
})
})
claimOrder.additional_items.forEach((item) => {
actions.push({
action: ChangeActionType.ITEM_REMOVE,
order_id: claimOrder.order_id,
claim_id: claimOrder.id,
reference: "claim",
reference_id: claimOrder.id,
details: {
reference_id: item.id,
order_id: claimOrder.order_id,
claim_id: claimOrder.id,
quantity: item.quantity,
},
})
})
claimOrder.shipping_methods?.forEach((shipping) => {
actions.push({
action: ChangeActionType.SHIPPING_REMOVE,
order_id: claimOrder.order_id,
claim_id: claimOrder.id,
return_id: claimOrder.return.id,
reference: "claim",
reference_id: shipping.id,
amount: shipping.price,
})
})
const [change] = await createOrderChange(
this,
data,
claimOrder,
actions,
sharedContext
)
await promiseAll([
this.updateClaims(
[
{
data: {
canceled_at: new Date(),
},
selector: {
id: claimOrder.id,
},
},
],
sharedContext
),
this.confirmOrderChange(change.id, sharedContext),
])
return claimOrder
}

View File

@@ -0,0 +1,128 @@
import {
Context,
CreateOrderChangeActionDTO,
OrderTypes,
} from "@medusajs/types"
import { promiseAll } from "@medusajs/utils"
import { ChangeActionType } from "../../utils"
async function createOrderChange(
service,
data,
returnRef,
actions,
sharedContext
) {
return await service.createOrderChange_(
{
order_id: returnRef.order_id,
exchange_id: returnRef.id,
reference: "return",
reference_id: returnRef.id,
description: data.description,
internal_note: data.internal_note,
created_by: data.created_by,
metadata: data.metadata,
actions,
},
sharedContext
)
}
export async function cancelExchange(
this: any,
data: OrderTypes.CancelOrderExchangeDTO,
sharedContext?: Context
) {
const exchangeOrder = await this.retrieveExchange(
data.exchange_id,
{
select: [
"id",
"order_id",
"return.id",
"return.items.item_id",
"return.items.quantity",
"additional_items.id",
"additional_items.quantity",
],
relations: ["return.items", "additional_items", "shipping_methods"],
},
sharedContext
)
const actions: CreateOrderChangeActionDTO[] = []
exchangeOrder.return.items.forEach((item) => {
actions.push({
action: ChangeActionType.CANCEL_RETURN_ITEM,
order_id: exchangeOrder.order_id,
exchange_id: exchangeOrder.id,
return_id: exchangeOrder.return.id,
reference: "return",
reference_id: exchangeOrder.return.id,
details: {
reference_id: item.item_id,
order_id: exchangeOrder.order_id,
exchange_id: exchangeOrder.id,
return_id: exchangeOrder.return.id,
quantity: item.quantity,
},
})
})
exchangeOrder.additional_items.forEach((item) => {
actions.push({
action: ChangeActionType.ITEM_REMOVE,
order_id: exchangeOrder.order_id,
exchange_id: exchangeOrder.id,
reference: "exchange",
reference_id: exchangeOrder.id,
details: {
order_id: exchangeOrder.order_id,
reference_id: item.item_id,
exchange_id: exchangeOrder.id,
quantity: item.quantity,
},
})
})
exchangeOrder.shipping_methods?.forEach((shipping) => {
actions.push({
action: ChangeActionType.SHIPPING_REMOVE,
order_id: exchangeOrder.order_id,
exchange_id: exchangeOrder.id,
return_id: exchangeOrder.return.id,
reference: "exchange",
reference_id: shipping.id,
amount: shipping.price,
})
})
const [change] = await createOrderChange(
this,
data,
exchangeOrder,
actions,
sharedContext
)
await promiseAll([
this.updateExchanges(
[
{
data: {
canceled_at: new Date(),
},
selector: {
id: exchangeOrder.id,
},
},
],
sharedContext
),
this.confirmOrderChange(change.id, sharedContext),
])
return exchangeOrder
}

View File

@@ -0,0 +1,107 @@
import {
Context,
CreateOrderChangeActionDTO,
OrderTypes,
} from "@medusajs/types"
import { promiseAll } from "@medusajs/utils"
import { ChangeActionType } from "../../utils"
async function createOrderChange(
service,
data,
returnRef,
actions,
sharedContext
) {
return await service.createOrderChange_(
{
order_id: returnRef.order_id,
return_id: returnRef.id,
reference: "return",
reference_id: returnRef.id,
description: data.description,
internal_note: data.internal_note,
created_by: data.created_by,
metadata: data.metadata,
actions,
},
sharedContext
)
}
export async function cancelReturn(
this: any,
data: OrderTypes.CancelOrderReturnDTO,
sharedContext?: Context
) {
const returnOrder = await this.retrieveReturn(
data.return_id,
{
select: [
"id",
"order_id",
"items.item_id",
"items.quantity",
"items.received_quantity",
],
relations: ["items", "shipping_methods"],
},
sharedContext
)
const actions: CreateOrderChangeActionDTO[] = []
returnOrder.items.forEach((item) => {
actions.push({
action: ChangeActionType.CANCEL_RETURN_ITEM,
order_id: returnOrder.order_id,
return_id: returnOrder.id,
reference: "return",
reference_id: returnOrder.id,
details: {
reference_id: item.item_id,
order_id: returnOrder.order_id,
return_id: returnOrder.id,
quantity: item.quantity,
},
})
})
returnOrder.shipping_methods?.forEach((shipping) => {
actions.push({
action: ChangeActionType.SHIPPING_REMOVE,
order_id: returnOrder.order_id,
return_id: returnOrder.id,
reference: "return",
reference_id: shipping.id,
amount: shipping.price,
})
})
const [change] = await createOrderChange(
this,
data,
returnOrder,
actions,
sharedContext
)
await promiseAll([
this.updateReturns(
[
{
data: {
canceled_at: new Date(),
},
selector: {
id: returnOrder.id,
},
},
],
sharedContext
),
this.confirmOrderChange(change.id, sharedContext),
])
return returnOrder
}

View File

@@ -196,9 +196,9 @@ async function processShippingMethods(
const methods = await service.createShippingMethods(
[
{
...shippingMethod,
order_id: data.order_id,
claim_id: claimReference.id,
...shippingMethod,
},
],
sharedContext
@@ -247,10 +247,10 @@ async function processReturnShipping(
const methods = await service.createShippingMethods(
[
{
...data.return_shipping,
order_id: data.order_id,
claim_id: claimReference.id,
return_id: returnReference.id,
...data.return_shipping,
},
],
sharedContext

View File

@@ -162,9 +162,9 @@ async function processShippingMethods(
const methods = await service.createShippingMethods(
[
{
...shippingMethod,
order_id: data.order_id,
exchange_id: exchangeReference.id,
...shippingMethod,
},
],
sharedContext
@@ -208,10 +208,10 @@ async function processReturnShipping(
const methods = await service.createShippingMethods(
[
{
...data.return_shipping,
order_id: data.order_id,
exchange_id: exchangeReference.id,
return_id: returnReference.id,
...data.return_shipping,
},
],
sharedContext

View File

@@ -6,6 +6,7 @@ import {
import {
ReturnStatus,
getShippingMethodsTotals,
isDefined,
isString,
promiseAll,
} from "@medusajs/utils"
@@ -59,13 +60,17 @@ async function processShippingMethod(
) {
let shippingMethodId
if (!isDefined(data.shipping_method)) {
return
}
if (!isString(data.shipping_method)) {
const methods = await service.createShippingMethods(
[
{
...data.shipping_method,
order_id: data.order_id,
return_id: returnRef.id,
...data.shipping_method,
},
],
sharedContext
@@ -90,8 +95,11 @@ async function processShippingMethod(
action: ChangeActionType.SHIPPING_ADD,
reference: "order_shipping_method",
reference_id: shippingMethodId,
return_id: returnRef.id,
amount: calculatedAmount.total,
details: {
order_id: returnRef.order_id,
return_id: returnRef.id,
},
})
}
}
@@ -134,8 +142,11 @@ export async function createReturn(
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,

View File

@@ -1,4 +1,7 @@
export * from "./cancel-claim"
export * from "./cancel-exchange"
export * from "./cancel-fulfillment"
export * from "./cancel-return"
export * from "./create-claim"
export * from "./create-exchange"
export * from "./create-return"

View File

@@ -1188,9 +1188,13 @@ export default class OrderModuleService<
return {
shipping_method: dt,
order_id: dt.order_id,
return_id: dt.return_id,
claim_id: dt.claim_id,
exchange_id: dt.exchange_id,
version: mapOrderVersion[dt.order_id],
}
})
methods = await this.createShippingMethodsBulk_(
orderShippingMethodData as any,
sharedContext
@@ -1214,11 +1218,14 @@ export default class OrderModuleService<
sharedContext
)
const methods = data.map((method) => {
const methods = data.map((methodData) => {
return {
shipping_method: method,
shipping_method: methodData,
order_id: order.id,
version: method.version ?? order.version ?? 1,
return_id: methodData.return_id,
claim_id: methodData.claim_id,
exchange_id: methodData.exchange_id,
version: methodData.version ?? order.version ?? 1,
}
})
@@ -1952,6 +1959,9 @@ export default class OrderModuleService<
select: [
"id",
"order_id",
"return_id",
"exchange_id",
"claim_id",
"ordering",
"version",
"applied",
@@ -2108,7 +2118,15 @@ export default class OrderModuleService<
sharedContext?: Context
): Promise<any> {
const options = {
select: ["id", "order_id", "version", "status"],
select: [
"id",
"order_id",
"return_id",
"claim_id",
"exchange_id",
"version",
"status",
],
relations: [] as string[],
order: {},
}
@@ -2253,15 +2271,7 @@ export default class OrderModuleService<
let orders = await super.listOrders(
{ id: deduplicate(ordersIds) },
{
select: [
"id",
"version",
"items.detail",
"transactions",
"shipping_methods",
"summary",
"total",
],
select: ["id", "version", "items.detail", "summary", "total"],
relations: [
"transactions",
"items",
@@ -2277,7 +2287,7 @@ export default class OrderModuleService<
const {
itemsToUpsert,
shippingMethodsToInsert,
shippingMethodsToUpsert,
summariesToUpsert,
orderToUpdate,
} = applyChangesToOrder(orders, actionsMap)
@@ -2295,9 +2305,9 @@ export default class OrderModuleService<
summariesToUpsert.length
? this.orderSummaryService_.upsert(summariesToUpsert, sharedContext)
: null,
shippingMethodsToInsert.length
? this.orderShippingMethodService_.create(
shippingMethodsToInsert,
shippingMethodsToUpsert.length
? this.orderShippingMethodService_.upsert(
shippingMethodsToUpsert,
sharedContext
)
: null,
@@ -2305,7 +2315,7 @@ export default class OrderModuleService<
return {
items: itemsToUpsert as any,
shippingMethods: shippingMethodsToInsert as any,
shippingMethods: shippingMethodsToUpsert as any,
}
}
@@ -2803,8 +2813,7 @@ export default class OrderModuleService<
async createReturn(
data: OrderTypes.CreateOrderReturnDTO,
@MedusaContext() sharedContext?: Context
): Promise<any> {
// TODO: type ReturnDTO
): Promise<OrderTypes.ReturnDTO> {
const ret = await BundledActions.createReturn.bind(this)(
data,
sharedContext
@@ -2813,7 +2822,12 @@ export default class OrderModuleService<
return await this.retrieveReturn(
ret.id,
{
relations: ["items"],
relations: [
"items",
"shipping_methods",
"shipping_methods.tax_lines",
"shipping_methods.adjustments",
],
},
sharedContext
)
@@ -2823,11 +2837,16 @@ export default class OrderModuleService<
async receiveReturn(
data: OrderTypes.ReceiveOrderReturnDTO,
@MedusaContext() sharedContext?: Context
): Promise<any> {
): Promise<OrderTypes.ReturnDTO> {
const ret = await this.receiveReturn_(data, sharedContext)
return await this.retrieveReturn(ret.id, {
relations: ["items"],
relations: [
"items",
"shipping_methods",
"shipping_methods.tax_lines",
"shipping_methods.adjustments",
],
})
}
@@ -2839,11 +2858,36 @@ export default class OrderModuleService<
return await BundledActions.receiveReturn.bind(this)(data, sharedContext)
}
@InjectManager("baseRepository_")
async cancelReturn(
data: OrderTypes.CancelOrderReturnDTO,
@MedusaContext() sharedContext?: Context
): Promise<OrderTypes.ReturnDTO> {
const ret = await this.cancelReturn_(data, sharedContext)
return await this.retrieveReturn(ret.id, {
relations: [
"items",
"shipping_methods",
"shipping_methods.tax_lines",
"shipping_methods.adjustments",
],
})
}
@InjectTransactionManager("baseRepository_")
private async cancelReturn_(
data: OrderTypes.CancelOrderReturnDTO,
@MedusaContext() sharedContext?: Context
): Promise<any> {
return await BundledActions.cancelReturn.bind(this)(data, sharedContext)
}
@InjectManager("baseRepository_")
async createClaim(
data: OrderTypes.CreateOrderClaimDTO,
@MedusaContext() sharedContext?: Context
): Promise<any> {
): Promise<OrderTypes.OrderClaimDTO> {
const ret = await this.createClaim_(data, sharedContext)
const claim = await this.retrieveOrderClaim(
@@ -2865,7 +2909,7 @@ export default class OrderModuleService<
sharedContext
)
return await this.baseRepository_.serialize<OrderTypes.OrderClaimDTO[]>(
return await this.baseRepository_.serialize<OrderTypes.OrderClaimDTO>(
claim,
{
populate: true,
@@ -2874,18 +2918,38 @@ export default class OrderModuleService<
}
@InjectTransactionManager("baseRepository_")
async createExchange_(
data: OrderTypes.CreateOrderExchangeDTO,
async createClaim_(
data: OrderTypes.CreateOrderClaimDTO,
@MedusaContext() sharedContext?: Context
): Promise<any> {
return await BundledActions.createExchange.bind(this)(data, sharedContext)
return await BundledActions.createClaim.bind(this)(data, sharedContext)
}
@InjectManager("baseRepository_")
async cancelClaim(
data: OrderTypes.CancelOrderClaimDTO,
@MedusaContext() sharedContext?: Context
): Promise<OrderTypes.OrderClaimDTO> {
const ret = await this.cancelClaim_(data, sharedContext)
return await this.retrieveOrderClaim(ret.id, {
relations: ["items"],
})
}
@InjectTransactionManager("baseRepository_")
private async cancelClaim_(
data: OrderTypes.CancelOrderClaimDTO,
@MedusaContext() sharedContext?: Context
): Promise<any> {
return await BundledActions.cancelClaim.bind(this)(data, sharedContext)
}
@InjectManager("baseRepository_")
async createExchange(
data: OrderTypes.CreateOrderExchangeDTO,
@MedusaContext() sharedContext?: Context
): Promise<any> {
): Promise<OrderTypes.OrderExchangeDTO> {
const ret = await this.createExchange_(data, sharedContext)
const claim = await this.retrieveOrderExchange(
@@ -2905,7 +2969,7 @@ export default class OrderModuleService<
sharedContext
)
return await this.baseRepository_.serialize<OrderTypes.OrderExchangeDTO[]>(
return await this.baseRepository_.serialize<OrderTypes.OrderExchangeDTO>(
claim,
{
populate: true,
@@ -2914,11 +2978,31 @@ export default class OrderModuleService<
}
@InjectTransactionManager("baseRepository_")
async createClaim_(
data: OrderTypes.CreateOrderClaimDTO,
async createExchange_(
data: OrderTypes.CreateOrderExchangeDTO,
@MedusaContext() sharedContext?: Context
): Promise<any> {
return await BundledActions.createClaim.bind(this)(data, sharedContext)
return await BundledActions.createExchange.bind(this)(data, sharedContext)
}
@InjectManager("baseRepository_")
async cancelExchange(
data: OrderTypes.CancelOrderExchangeDTO,
@MedusaContext() sharedContext?: Context
): Promise<OrderTypes.OrderExchangeDTO> {
const ret = await this.cancelExchange_(data, sharedContext)
return await this.retrieveOrderExchange(ret.id, {
relations: ["items"],
})
}
@InjectTransactionManager("baseRepository_")
private async cancelExchange_(
data: OrderTypes.CancelOrderExchangeDTO,
@MedusaContext() sharedContext?: Context
): Promise<any> {
return await BundledActions.cancelExchange.bind(this)(data, sharedContext)
}
@InjectTransactionManager("baseRepository_")

View File

@@ -4,6 +4,9 @@ export interface CreateOrderShippingMethodDTO {
name: string
shipping_option_id?: string
order_id: string
return_id?: string
claim_id?: string
exchange_id?: string
version?: number
amount: BigNumberInput
data?: Record<string, unknown>

View File

@@ -32,7 +32,7 @@ export type VirtualOrder = {
}[]
shipping_methods: {
shipping_method_id: string
id: string
order_id: string
return_id?: string
claim_id?: string
@@ -89,14 +89,14 @@ export interface OrderChangeEvent {
claim_id?: string
exchange_id?: string
group_id?: string
change_id?: string
evaluationOnly?: boolean
details?: any
resolve?: {
group_id?: string
change_id?: string
reference_id?: string
amount?: BigNumberInput
}

View File

@@ -1,6 +1,5 @@
export enum ChangeActionType {
CANCEL = "CANCEL",
CANCEL_RETURN = "CANCEL_RETURN",
FULFILL_ITEM = "FULFILL_ITEM",
CANCEL_ITEM_FULFILLMENT = "CANCEL_ITEM_FULFILLMENT",
ITEM_ADD = "ITEM_ADD",
@@ -8,7 +7,10 @@ export enum ChangeActionType {
RECEIVE_DAMAGED_RETURN_ITEM = "RECEIVE_DAMAGED_RETURN_ITEM",
RECEIVE_RETURN_ITEM = "RECEIVE_RETURN_ITEM",
RETURN_ITEM = "RETURN_ITEM",
CANCEL_RETURN_ITEM = "CANCEL_RETURN_ITEM",
SHIPPING_ADD = "SHIPPING_ADD",
SHIPPING_REMOVE = "SHIPPING_REMOVE",
SHIP_ITEM = "SHIP_ITEM",
WRITE_OFF_ITEM = "WRITE_OFF_ITEM",
REINSTATE_ITEM = "REINSTATE_ITEM",
}

View File

@@ -3,7 +3,7 @@ import { ChangeActionType } from "../action-key"
import { OrderChangeProcessing } from "../calculate-order-change"
import { setActionReference } from "../set-action-reference"
OrderChangeProcessing.registerActionType(ChangeActionType.CANCEL_RETURN, {
OrderChangeProcessing.registerActionType(ChangeActionType.CANCEL_RETURN_ITEM, {
operation({ action, currentOrder }) {
const existing = currentOrder.items.find(
(item) => item.id === action.details.reference_id

View File

@@ -9,4 +9,5 @@ export * from "./receive-return-item"
export * from "./return-item"
export * from "./ship-item"
export * from "./shipping-add"
export * from "./shipping-remove"
export * from "./write-off-item"

View File

@@ -0,0 +1,64 @@
import { MathBN, MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import { OrderChangeProcessing } from "../calculate-order-change"
import { setActionReference } from "../set-action-reference"
OrderChangeProcessing.registerActionType(ChangeActionType.REINSTATE_ITEM, {
operation({ action, currentOrder }) {
const existing = currentOrder.items.find(
(item) => item.id === action.details.reference_id
)!
existing.detail.written_off_quantity ??= 0
existing.detail.written_off_quantity = MathBN.sub(
existing.detail.written_off_quantity,
action.details.quantity
)
setActionReference(existing, action)
},
revert({ action, currentOrder }) {
const existing = currentOrder.items.find(
(item) => item.id === action.details.reference_id
)!
existing.detail.written_off_quantity = MathBN.add(
existing.detail.written_off_quantity,
action.details.quantity
)
},
validate({ action, currentOrder }) {
const refId = action.details?.reference_id
if (!isDefined(refId)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Details reference ID is required."
)
}
const existing = currentOrder.items.find((item) => item.id === refId)
if (!existing) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Item ID "${refId}" not found.`
)
}
if (!action.details?.quantity) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Quantity to reinstate item ${refId} is required.`
)
}
const quantityAvailable = existing!.quantity ?? 0
const greater = MathBN.gt(action.details?.quantity, quantityAvailable)
if (greater) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Cannot unclaim more items than what was ordered."
)
}
},
})

View File

@@ -57,7 +57,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.RETURN_ITEM, {
}
const quantityAvailable = MathBN.sub(
existing!.detail?.shipped_quantity ?? 0,
existing!.detail?.fulfilled_quantity ?? 0,
existing!.detail?.return_requested_quantity ?? 0
)
@@ -65,7 +65,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.RETURN_ITEM, {
if (greater) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Cannot request to return more items than what was shipped for item ${refId}.`
`Cannot request to return more items than what was fulfilled for item ${refId}.`
)
}
},

View File

@@ -1,6 +1,7 @@
import { MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import { OrderChangeProcessing } from "../calculate-order-change"
import { setActionReference } from "../set-action-reference"
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_ADD, {
operation({ action, currentOrder }) {
@@ -8,15 +9,20 @@ OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_ADD, {
? currentOrder.shipping_methods
: [currentOrder.shipping_methods]
shipping.push({
shipping_method_id: action.reference_id!,
order_id: currentOrder.id,
return_id: action.return_id,
claim_id: action.claim_id,
exchange_id: action.exchange_id,
const existing = shipping.find((sh) => sh.id === action.reference_id)
price: action.amount as number,
})
if (existing) {
setActionReference(existing, action)
} else {
shipping.push({
id: action.reference_id!,
order_id: currentOrder.id,
return_id: action.return_id,
claim_id: action.claim_id,
exchange_id: action.exchange_id,
price: action.amount as number,
})
}
currentOrder.shipping_methods = shipping
},
@@ -26,7 +32,7 @@ OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_ADD, {
: [currentOrder.shipping_methods]
const existingIndex = shipping.findIndex(
(item) => item.shipping_method_id === action.reference_id
(item) => item.id === action.reference_id
)
if (existingIndex > -1) {

View File

@@ -0,0 +1,56 @@
import { MedusaError, isDefined } from "@medusajs/utils"
import { ChangeActionType } from "../action-key"
import { OrderChangeProcessing } from "../calculate-order-change"
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_REMOVE, {
operation({ action, currentOrder }) {
const shipping = Array.isArray(currentOrder.shipping_methods)
? currentOrder.shipping_methods
: [currentOrder.shipping_methods]
const existingIndex = shipping.findIndex(
(item) => item.id === action.reference_id
)
if (existingIndex > -1) {
shipping.splice(existingIndex, 1)
}
currentOrder.shipping_methods = shipping
},
revert({ action, currentOrder }) {
const shipping = Array.isArray(currentOrder.shipping_methods)
? currentOrder.shipping_methods
: [currentOrder.shipping_methods]
const existingIndex = shipping.findIndex(
(item) => item.id === action.reference_id
)
if (existingIndex > -1) {
shipping.push({
id: action.reference_id!,
order_id: currentOrder.id,
return_id: action.return_id,
claim_id: action.claim_id,
exchange_id: action.exchange_id,
price: action.amount as number,
})
}
},
validate({ action }) {
if (!action.reference_id) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Reference ID is required."
)
}
if (!isDefined(action.amount)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Amount is required."
)
}
},
})

View File

@@ -15,7 +15,7 @@ export function applyChangesToOrder(
actionsMap: Record<string, any[]>
) {
const itemsToUpsert: OrderItem[] = []
const shippingMethodsToInsert: OrderShippingMethod[] = []
const shippingMethodsToUpsert: OrderShippingMethod[] = []
const summariesToUpsert: any[] = []
const orderToUpdate: any[] = []
@@ -68,8 +68,9 @@ export function applyChangesToOrder(
...((shippingMethod as any).detail ?? shippingMethod),
version,
}
delete sm.id
shippingMethodsToInsert.push(sm)
shippingMethodsToUpsert.push(sm)
}
orderToUpdate.push({
@@ -85,7 +86,7 @@ export function applyChangesToOrder(
return {
itemsToUpsert,
shippingMethodsToInsert,
shippingMethodsToUpsert,
summariesToUpsert,
orderToUpdate,
}

View File

@@ -34,11 +34,18 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
let orderAlias = "o0"
if (isRelatedEntity) {
if (!config.options.populate.includes("order.items")) {
config.options.populate.unshift("order.items")
}
// first relation is always order if the entity is not Order
const index = config.options.populate.findIndex((p) => p === "order")
if (index > -1) {
config.options.populate.splice(index, 1)
}
config.options.populate.unshift("order")
orderAlias = "o1"
config.options.populate.unshift("order.items")
}
let defaultVersion = knex.raw(`"${orderAlias}"."version"`)
@@ -105,7 +112,12 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
let orderAlias = "o0"
if (isRelatedEntity) {
// first relation is always order if entity is not Order
// first relation is always order if the entity is not Order
const index = config.options.populate.findIndex((p) => p === "order")
if (index > -1) {
config.options.populate.splice(index, 1)
}
config.options.populate.unshift("order")
orderAlias = "o1"
}

View File

@@ -118,10 +118,10 @@ export class OrderChangeProcessing {
const amount = MathBN.mult(action.amount!, type.isDeduction ? -1 : 1)
if (action.group_id && !action.evaluationOnly) {
this.groupTotal[action.group_id] ??= 0
this.groupTotal[action.group_id] = MathBN.add(
this.groupTotal[action.group_id],
if (action.change_id && !action.evaluationOnly) {
this.groupTotal[action.change_id] ??= 0
this.groupTotal[action.change_id] = MathBN.add(
this.groupTotal[action.change_id],
amount
)
}
@@ -146,7 +146,7 @@ export class OrderChangeProcessing {
amount
)
} else {
if (!this.isEventDone(action) && !action.group_id) {
if (!this.isEventDone(action) && !action.change_id) {
summary.difference_sum = MathBN.add(summary.difference_sum, amount)
}
summary.current_order_total = MathBN.add(
@@ -243,8 +243,8 @@ export class OrderChangeProcessing {
if (action.resolve.reference_id) {
this.resolveReferences(action)
}
const groupId = action.resolve.group_id ?? "__default"
if (action.resolve.group_id) {
const groupId = action.resolve.change_id ?? "__default"
if (action.resolve.change_id) {
// resolve all actions in the same group
this.resolveGroup(action)
}
@@ -320,7 +320,7 @@ export class OrderChangeProcessing {
const type = OrderChangeProcessing.typeDefinition[actionKey]
const actions = this.actionsProcessed[actionKey]
for (const action of actions) {
if (!resolve?.group_id || action?.group_id !== resolve.group_id) {
if (!resolve?.change_id || action?.change_id !== resolve.change_id) {
continue
}

View File

@@ -5,8 +5,9 @@ import {
deduplicate,
isDefined,
} from "@medusajs/utils"
import { Order, OrderClaim, OrderExchange, Return } from "@models"
// Reshape the order object to match the OrderDTO
// This function is used to format the order object before returning to the main module methods
export function formatOrder(
order,
options: {
@@ -20,8 +21,9 @@ export function formatOrder(
orders.map((order) => {
let mainOrder = order
const isRelatedEntity = options?.entity !== Order
const isRelatedEntity = options?.entity?.name !== "Order"
// If the entity is a related entity, the original order is located in the order property
if (isRelatedEntity) {
if (!order.order) {
return order
@@ -48,11 +50,12 @@ export function formatOrder(
formatOrderReturn(order.return, mainOrder)
}
if (options.entity === OrderClaim) {
const entityName = options.entity.name
if (entityName === "OrderClaim") {
formatClaim(order)
} else if (options.entity === OrderExchange) {
} else if (entityName === "OrderExchange") {
formatExchange(order)
} else if (options.entity === Return) {
} else if (entityName === "Return") {
formatReturn(order)
}
}
@@ -85,9 +88,6 @@ export function formatOrder(
}
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)
@@ -154,7 +154,15 @@ function formatReturn(returnOrder) {
})
}
// Map the public order model to the repository model format
// As the public responses have a different shape than the repository responses, this function is used to map the public properties to the internal db entities
// e.g "items" is the relation between "line-item" and "order" + "version", The line item itself is in "items.item"
// This helper maps to the correct repository to query the DB, and the function "formatOrder" remap the response to the public shape
export function mapRepositoryToOrderModel(config, isRelatedEntity = false) {
if (isRelatedEntity) {
return mapRepositoryToRelatedEntity(config)
}
const conf = { ...config }
function replace(obj, type): string[] | undefined {
@@ -223,3 +231,36 @@ export function mapRepositoryToOrderModel(config, isRelatedEntity = false) {
return conf
}
// This function has the same purpose as "mapRepositoryToOrderModel" but for returns, claims and exchanges
function mapRepositoryToRelatedEntity(config) {
const conf = { ...config }
function replace(obj, type): string[] | undefined {
if (!isDefined(obj[type])) {
return
}
return deduplicate(
obj[type].sort().map((rel) => {
if (
rel.includes("shipping_methods") &&
!rel.includes("shipping_methods.shipping_method")
) {
obj.populate.push("shipping_methods.shipping_method")
return rel.replace(
"shipping_methods",
"shipping_methods.shipping_method"
)
}
return rel
})
)
}
conf.options.fields = replace(config.options, "fields")
conf.options.populate = replace(config.options, "populate")
return conf
}