chore(order): cancel return (#7881)
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -6,7 +6,7 @@ import { BigNumberInput, BigNumberRawValue, BigNumberValue } from "../totals"
|
||||
|
||||
export type ChangeActionType =
|
||||
| "CANCEL"
|
||||
| "CANCEL_RETURN"
|
||||
| "CANCEL_RETURN_ITEM"
|
||||
| "FULFILL_ITEM"
|
||||
| "CANCEL_ITEM_FULFILLMENT"
|
||||
| "ITEM_ADD"
|
||||
@@ -15,8 +15,10 @@ export type ChangeActionType =
|
||||
| "RECEIVE_RETURN_ITEM"
|
||||
| "RETURN_ITEM"
|
||||
| "SHIPPING_ADD"
|
||||
| "SHIPPING_REMOVE"
|
||||
| "SHIP_ITEM"
|
||||
| "WRITE_OFF_ITEM"
|
||||
| "REINSTATE_ITEM"
|
||||
|
||||
export type OrderSummaryDTO = {
|
||||
total: BigNumberValue
|
||||
|
||||
@@ -207,6 +207,9 @@ export interface UpdateOrderLineItemDTO
|
||||
export interface CreateOrderShippingMethodDTO {
|
||||
name: string
|
||||
order_id: string
|
||||
return_id?: string
|
||||
claim_id?: string
|
||||
exchange_id?: string
|
||||
version?: number
|
||||
amount: BigNumberInput
|
||||
shipping_option_id?: string
|
||||
@@ -381,6 +384,7 @@ interface BaseOrderBundledItemActionsDTO {
|
||||
internal_note?: string | null
|
||||
note?: string | null
|
||||
metadata?: Record<string, unknown> | null
|
||||
[key: string]: any
|
||||
}
|
||||
interface BaseOrderBundledActionsDTO {
|
||||
order_id: string
|
||||
@@ -424,6 +428,11 @@ export interface CreateOrderReturnDTO extends BaseOrderBundledActionsDTO {
|
||||
no_notification?: boolean
|
||||
}
|
||||
|
||||
export interface CancelOrderReturnDTO
|
||||
extends Omit<BaseOrderBundledActionsDTO, "order_id"> {
|
||||
return_id: string
|
||||
}
|
||||
|
||||
export type OrderClaimType = "refund" | "replace"
|
||||
export type ClaimReason =
|
||||
| "missing_item"
|
||||
@@ -446,17 +455,23 @@ export interface CreateOrderClaimDTO extends BaseOrderBundledActionsDTO {
|
||||
no_notification?: boolean
|
||||
}
|
||||
|
||||
export interface CancelOrderClaimDTO
|
||||
extends Omit<BaseOrderBundledActionsDTO, "order_id"> {
|
||||
claim_id: string
|
||||
}
|
||||
|
||||
export interface CreateOrderExchangeDTO extends BaseOrderBundledActionsDTO {
|
||||
additional_items?: BaseOrderBundledItemActionsDTO[]
|
||||
shipping_methods?: Omit<CreateOrderShippingMethodDTO, "order_id">[] | string[]
|
||||
return_shipping: Omit<CreateOrderShippingMethodDTO, "order_id"> | string
|
||||
return_shipping?: Omit<CreateOrderShippingMethodDTO, "order_id"> | string
|
||||
difference_due?: BigNumberInput
|
||||
allow_backorder?: boolean
|
||||
no_notification?: boolean
|
||||
}
|
||||
|
||||
export interface CancelOrderReturnDTO {
|
||||
return_id: string
|
||||
export interface CancelOrderExchangeDTO
|
||||
extends Omit<BaseOrderBundledActionsDTO, "order_id"> {
|
||||
exchange_id: string
|
||||
}
|
||||
|
||||
export interface ReceiveOrderReturnDTO
|
||||
|
||||
@@ -33,7 +33,10 @@ import {
|
||||
} from "./common"
|
||||
import {
|
||||
CancelOrderChangeDTO,
|
||||
CancelOrderClaimDTO,
|
||||
CancelOrderExchangeDTO,
|
||||
CancelOrderFulfillmentDTO,
|
||||
CancelOrderReturnDTO,
|
||||
ConfirmOrderChangeDTO,
|
||||
CreateOrderAddressDTO,
|
||||
CreateOrderAdjustmentDTO,
|
||||
@@ -1582,12 +1585,10 @@ export interface IOrderModuleService extends IModuleService {
|
||||
sharedContext?: Context
|
||||
): Promise<ReturnDTO>
|
||||
|
||||
/*
|
||||
cancelReturn(
|
||||
returnData: CancelOrderReturnDTO,
|
||||
data: CancelOrderReturnDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<void>
|
||||
*/
|
||||
): Promise<ReturnDTO>
|
||||
|
||||
receiveReturn(
|
||||
returnData: ReceiveOrderReturnDTO,
|
||||
@@ -1599,8 +1600,18 @@ export interface IOrderModuleService extends IModuleService {
|
||||
sharedContext?: Context
|
||||
): Promise<OrderClaimDTO>
|
||||
|
||||
cancelClaim(
|
||||
data: CancelOrderClaimDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<OrderClaimDTO>
|
||||
|
||||
createExchange(
|
||||
exchangeData: CreateOrderExchangeDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<OrderExchangeDTO>
|
||||
|
||||
cancelExchange(
|
||||
data: CancelOrderExchangeDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<OrderExchangeDTO>
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface CancelOrderClaimWorkflowInput {
|
||||
claim_id: string
|
||||
no_notification?: boolean
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface CancelOrderExchangeWorkflowInput {
|
||||
exchange_id: string
|
||||
no_notification?: boolean
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface CancelReturnWorkflowInput {
|
||||
return_id: string
|
||||
no_notification?: boolean
|
||||
}
|
||||
@@ -13,7 +13,7 @@ export interface CreateOrderReturnWorkflowInput {
|
||||
order_id: string
|
||||
created_by?: string | null // The id of the authenticated user
|
||||
items: CreateReturnItem[]
|
||||
return_shipping: {
|
||||
return_shipping?: {
|
||||
option_id: string
|
||||
price?: number
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
export * from "./cancel-claim"
|
||||
export * from "./cancel-exchange"
|
||||
export * from "./cancel-fulfillment"
|
||||
export * from "./cancel-order"
|
||||
export * from "./cancel-return"
|
||||
export * from "./create-fulfillment"
|
||||
export * from "./create-return-order"
|
||||
export * from "./create-shipment"
|
||||
|
||||
@@ -86,6 +86,18 @@ export const LINKS = {
|
||||
Modules.PAYMENT,
|
||||
"payment_collection_id"
|
||||
),
|
||||
OrderClaimPaymentCollection: composeLinkName(
|
||||
Modules.ORDER,
|
||||
"claim_id",
|
||||
Modules.PAYMENT,
|
||||
"payment_collection_id"
|
||||
),
|
||||
OrderExchangePaymentCollection: composeLinkName(
|
||||
Modules.ORDER,
|
||||
"exchange_id",
|
||||
Modules.PAYMENT,
|
||||
"payment_collection_id"
|
||||
),
|
||||
OrderFulfillment: composeLinkName(
|
||||
Modules.ORDER,
|
||||
"order_id",
|
||||
|
||||
Reference in New Issue
Block a user