chore(order): cancel return (#7881)
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user