chore(order): cancel return (#7881)
This commit is contained in:
committed by
GitHub
parent
b3236ff31c
commit
07715e6b50
@@ -0,0 +1,29 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { CancelOrderClaimDTO, IOrderModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type CancelOrderClaimStepInput = CancelOrderClaimDTO
|
||||
|
||||
export const cancelOrderClaimStepId = "cancel-order-claim"
|
||||
export const cancelOrderClaimStep = createStep(
|
||||
cancelOrderClaimStepId,
|
||||
async (data: CancelOrderClaimStepInput, { container }) => {
|
||||
const service = container.resolve<IOrderModuleService>(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
await service.cancelClaim(data)
|
||||
return new StepResponse(void 0, data.return_id)
|
||||
},
|
||||
async (orderId, { container }) => {
|
||||
if (!orderId) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IOrderModuleService>(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
await service.revertLastVersion(orderId)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { CancelOrderExchangeDTO, IOrderModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type CancelOrderExchangeStepInput = CancelOrderExchangeDTO
|
||||
|
||||
export const cancelOrderExchangeStepId = "cancel-order-swap"
|
||||
export const cancelOrderExchangeStep = createStep(
|
||||
cancelOrderExchangeStepId,
|
||||
async (data: CancelOrderExchangeStepInput, { container }) => {
|
||||
const service = container.resolve<IOrderModuleService>(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
await service.cancelExchange(data)
|
||||
return new StepResponse(void 0, data.return_id)
|
||||
},
|
||||
async (orderId, { container }) => {
|
||||
if (!orderId) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IOrderModuleService>(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
await service.revertLastVersion(orderId)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { CancelOrderReturnDTO, IOrderModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type CancelOrderReturnStepInput = CancelOrderReturnDTO
|
||||
|
||||
export const cancelOrderReturnStepId = "cancel-order-return"
|
||||
export const cancelOrderReturnStep = createStep(
|
||||
cancelOrderReturnStepId,
|
||||
async (data: CancelOrderReturnStepInput, { container }) => {
|
||||
const service = container.resolve<IOrderModuleService>(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
await service.cancelReturn(data)
|
||||
return new StepResponse(void 0, data.return_id)
|
||||
},
|
||||
async (orderId, { container }) => {
|
||||
if (!orderId) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve<IOrderModuleService>(
|
||||
ModuleRegistrationName.ORDER
|
||||
)
|
||||
|
||||
await service.revertLastVersion(orderId)
|
||||
}
|
||||
)
|
||||
@@ -1,5 +1,8 @@
|
||||
export * from "./archive-orders"
|
||||
export * from "./cancel-claim"
|
||||
export * from "./cancel-exchange"
|
||||
export * from "./cancel-orders"
|
||||
export * from "./cancel-return"
|
||||
export * from "./complete-orders"
|
||||
export * from "./create-orders"
|
||||
export * from "./get-item-tax-lines"
|
||||
|
||||
@@ -60,7 +60,7 @@ const validateOrder = createStep(
|
||||
pred: (obj: any) => boolean,
|
||||
type: string
|
||||
) => {
|
||||
if (arr?.filter(pred).length) {
|
||||
if (arr?.some(pred)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
`All ${type} must be canceled before canceling an order`
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import {
|
||||
FulfillmentDTO,
|
||||
OrderWorkflow,
|
||||
PaymentCollectionDTO,
|
||||
ReturnDTO,
|
||||
} from "@medusajs/types"
|
||||
import { MathBN, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../common"
|
||||
import { cancelOrderReturnStep } from "../steps"
|
||||
import { throwIfReturnIsCancelled } from "../utils/order-validation"
|
||||
|
||||
const validateOrder = createStep(
|
||||
"validate-return",
|
||||
({
|
||||
orderReturn,
|
||||
}: {
|
||||
orderReturn: ReturnDTO
|
||||
input: OrderWorkflow.CancelReturnWorkflowInput
|
||||
}) => {
|
||||
const orderReturn_ = orderReturn as ReturnDTO & {
|
||||
payment_collections: PaymentCollectionDTO[]
|
||||
fulfillments: FulfillmentDTO[]
|
||||
}
|
||||
|
||||
throwIfReturnIsCancelled({ orderReturn })
|
||||
|
||||
const throwErrorIf = (
|
||||
arr: unknown[],
|
||||
pred: (obj: any) => boolean,
|
||||
message: string
|
||||
) => {
|
||||
if (arr?.some(pred)) {
|
||||
throw new MedusaError(MedusaError.Types.NOT_ALLOWED, message)
|
||||
}
|
||||
}
|
||||
|
||||
const notCanceled = (o) => !o.canceled_at
|
||||
const hasReceived = (o) => MathBN.gt(o.received_quantity, 0)
|
||||
|
||||
throwErrorIf(
|
||||
orderReturn_.fulfillments,
|
||||
notCanceled,
|
||||
"All fulfillments must be canceled before canceling a return"
|
||||
)
|
||||
|
||||
throwErrorIf(
|
||||
orderReturn_.items!,
|
||||
hasReceived,
|
||||
"Can't cancel a return which has returned items"
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
export const cancelReturnWorkflowId = "cancel-return"
|
||||
export const cancelReturnWorkflow = createWorkflow(
|
||||
cancelReturnWorkflowId,
|
||||
(
|
||||
input: WorkflowData<OrderWorkflow.CancelReturnWorkflowInput>
|
||||
): WorkflowData<void> => {
|
||||
const orderReturn: ReturnDTO & { fulfillments: FulfillmentDTO[] } =
|
||||
useRemoteQueryStep({
|
||||
entry_point: "return",
|
||||
fields: [
|
||||
"id",
|
||||
"items.id",
|
||||
"items.received_quantity",
|
||||
"fulfillments.canceled_at",
|
||||
],
|
||||
variables: { id: input.return_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
})
|
||||
|
||||
validateOrder({ orderReturn, input })
|
||||
|
||||
cancelOrderReturnStep({ return_id: orderReturn.id })
|
||||
}
|
||||
)
|
||||
@@ -97,9 +97,13 @@ function prepareShippingMethodData({
|
||||
returnShippingOption,
|
||||
}: {
|
||||
orderId: string
|
||||
inputShippingOption: OrderWorkflow.CreateOrderReturnWorkflowInput["return_shipping"]
|
||||
inputShippingOption?: OrderWorkflow.CreateOrderReturnWorkflowInput["return_shipping"]
|
||||
returnShippingOption: ShippingOptionDTO & WithCalculatedPrice
|
||||
}) {
|
||||
if (!inputShippingOption) {
|
||||
return
|
||||
}
|
||||
|
||||
const obj: CreateOrderShippingMethodDTO = {
|
||||
name: returnShippingOption.name,
|
||||
order_id: orderId,
|
||||
@@ -217,7 +221,7 @@ function prepareFulfillmentData({
|
||||
input: {
|
||||
location_id: locationId,
|
||||
provider_id: returnShippingOption.provider_id,
|
||||
shipping_option_id: input.return_shipping.option_id,
|
||||
shipping_option_id: input.return_shipping?.option_id,
|
||||
items: fulfillmentItems,
|
||||
labels: [] as FulfillmentWorkflow.CreateFulfillmentLabelWorkflowDTO[],
|
||||
delivery_address: order.shipping_address ?? ({} as any), // TODO: should it be the stock location address?
|
||||
@@ -235,11 +239,11 @@ function prepareReturnShippingOptionQueryVariables({
|
||||
region_id?: string
|
||||
}
|
||||
input: {
|
||||
return_shipping: OrderWorkflow.CreateOrderReturnWorkflowInput["return_shipping"]
|
||||
return_shipping?: OrderWorkflow.CreateOrderReturnWorkflowInput["return_shipping"]
|
||||
}
|
||||
}) {
|
||||
const variables = {
|
||||
id: input.return_shipping.option_id,
|
||||
id: input.return_shipping?.option_id,
|
||||
calculated_price: {
|
||||
context: {
|
||||
currency_code: order.currency_code,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export * from "./archive-orders"
|
||||
export * from "./cancel-order"
|
||||
export * from "./cancel-order-fulfillment"
|
||||
export * from "./cancel-return"
|
||||
export * from "./complete-orders"
|
||||
export * from "./create-fulfillment"
|
||||
export * from "./create-orders"
|
||||
|
||||
Reference in New Issue
Block a user