feat(core-flows,order,medusa): exchange endpoints (#8396)
This commit is contained in:
committed by
GitHub
parent
5125d1328d
commit
f415e6664c
@@ -57,6 +57,7 @@ function prepareFulfillmentData({
|
||||
items,
|
||||
shippingOption,
|
||||
deliveryAddress,
|
||||
isReturn,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
items: any[]
|
||||
@@ -73,16 +74,17 @@ function prepareFulfillmentData({
|
||||
}
|
||||
}
|
||||
deliveryAddress?: Record<string, any>
|
||||
isReturn?: boolean
|
||||
}) {
|
||||
const orderItemsMap = new Map<string, Required<OrderDTO>["items"][0]>(
|
||||
order.items!.map((i) => [i.id, i])
|
||||
)
|
||||
const fulfillmentItems = items.map((i) => {
|
||||
const orderItem = orderItemsMap.get(i.item_id)!
|
||||
const orderItem = orderItemsMap.get(i.item_id) ?? i.item
|
||||
return {
|
||||
line_item_id: i.item_id,
|
||||
quantity: i.quantity,
|
||||
return_quantity: i.quantity,
|
||||
quantity: !isReturn ? i.quantity : undefined,
|
||||
return_quantity: isReturn ? i.quantity : undefined,
|
||||
title: orderItem.variant_title ?? orderItem.title,
|
||||
sku: orderItem.variant_sku || "",
|
||||
barcode: orderItem.variant_barcode || "",
|
||||
@@ -270,11 +272,12 @@ export const confirmClaimRequestWorkflow = createWorkflow(
|
||||
"id",
|
||||
"version",
|
||||
"canceled_at",
|
||||
"additional_items.id",
|
||||
"additional_items.title",
|
||||
"additional_items.variant_title",
|
||||
"additional_items.variant_sku",
|
||||
"additional_items.variant_barcode",
|
||||
"additional_items.item_id",
|
||||
"additional_items.quantity",
|
||||
"additional_items.item.title",
|
||||
"additional_items.item.variant_title",
|
||||
"additional_items.item.variant_sku",
|
||||
"additional_items.item.variant_barcode",
|
||||
],
|
||||
variables: { id: claimId },
|
||||
list: false,
|
||||
@@ -345,6 +348,7 @@ export const confirmClaimRequestWorkflow = createWorkflow(
|
||||
order,
|
||||
items: order.items!,
|
||||
shippingOption: returnShippingOption,
|
||||
isReturn: true,
|
||||
},
|
||||
prepareFulfillmentData
|
||||
)
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import {
|
||||
OrderChangeActionDTO,
|
||||
OrderChangeDTO,
|
||||
OrderClaimDTO,
|
||||
OrderDTO,
|
||||
OrderWorkflow,
|
||||
} from "@medusajs/types"
|
||||
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../../common"
|
||||
import {
|
||||
deleteOrderChangeActionsStep,
|
||||
previewOrderChangeStep,
|
||||
} from "../../steps"
|
||||
import {
|
||||
throwIfIsCancelled,
|
||||
throwIfOrderChangeIsNotActive,
|
||||
} from "../../utils/order-validation"
|
||||
|
||||
const validationStep = createStep(
|
||||
"remove-item-claim-add-action-validation",
|
||||
async function ({
|
||||
order,
|
||||
orderChange,
|
||||
orderClaim,
|
||||
input,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
orderClaim: OrderClaimDTO
|
||||
orderChange: OrderChangeDTO
|
||||
input: OrderWorkflow.DeleteOrderClaimItemActionWorkflowInput
|
||||
}) {
|
||||
throwIfIsCancelled(order, "Order")
|
||||
throwIfIsCancelled(orderClaim, "Claim")
|
||||
throwIfOrderChangeIsNotActive({ orderChange })
|
||||
|
||||
const associatedAction = (orderChange.actions ?? []).find(
|
||||
(a) => a.id === input.action_id
|
||||
) as OrderChangeActionDTO
|
||||
|
||||
if (!associatedAction) {
|
||||
throw new Error(
|
||||
`No item claim found for claim ${input.claim_id} in order change ${orderChange.id}`
|
||||
)
|
||||
} else if (associatedAction.action !== ChangeActionType.ITEM_ADD) {
|
||||
throw new Error(`Action ${associatedAction.id} is not adding an item`)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export const removeAddItemClaimActionWorkflowId = "remove-item-claim-add-action"
|
||||
export const removeAddItemClaimActionWorkflow = createWorkflow(
|
||||
removeAddItemClaimActionWorkflowId,
|
||||
function (
|
||||
input: WorkflowData<OrderWorkflow.DeleteOrderClaimItemActionWorkflowInput>
|
||||
): WorkflowResponse<OrderDTO> {
|
||||
const orderClaim: OrderClaimDTO = useRemoteQueryStep({
|
||||
entry_point: "order_claim",
|
||||
fields: ["id", "status", "order_id", "canceled_at"],
|
||||
variables: { id: input.claim_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
})
|
||||
|
||||
const order: OrderDTO = useRemoteQueryStep({
|
||||
entry_point: "orders",
|
||||
fields: ["id", "status", "canceled_at", "items.*"],
|
||||
variables: { id: orderClaim.order_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "order-query" })
|
||||
|
||||
const orderChange: OrderChangeDTO = useRemoteQueryStep({
|
||||
entry_point: "order_change",
|
||||
fields: ["id", "status", "version", "actions.*"],
|
||||
variables: {
|
||||
filters: {
|
||||
order_id: orderClaim.order_id,
|
||||
claim_id: orderClaim.id,
|
||||
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
|
||||
},
|
||||
},
|
||||
list: false,
|
||||
}).config({ name: "order-change-query" })
|
||||
|
||||
validationStep({ order, input, orderClaim, orderChange })
|
||||
|
||||
deleteOrderChangeActionsStep({ ids: [input.action_id] })
|
||||
|
||||
return new WorkflowResponse(previewOrderChangeStep(order.id))
|
||||
}
|
||||
)
|
||||
@@ -52,6 +52,7 @@ function prepareFulfillmentData({
|
||||
items,
|
||||
shippingOption,
|
||||
deliveryAddress,
|
||||
isReturn,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
items: any[]
|
||||
@@ -68,16 +69,17 @@ function prepareFulfillmentData({
|
||||
}
|
||||
}
|
||||
deliveryAddress?: Record<string, any>
|
||||
isReturn?: boolean
|
||||
}) {
|
||||
const orderItemsMap = new Map<string, Required<OrderDTO>["items"][0]>(
|
||||
order.items!.map((i) => [i.id, i])
|
||||
)
|
||||
const fulfillmentItems = items.map((i) => {
|
||||
const orderItem = orderItemsMap.get(i.item_id)!
|
||||
const orderItem = orderItemsMap.get(i.item_id) ?? i.item
|
||||
return {
|
||||
line_item_id: i.item_id,
|
||||
quantity: i.quantity,
|
||||
return_quantity: i.quantity,
|
||||
quantity: !isReturn ? i.quantity : undefined,
|
||||
return_quantity: isReturn ? i.quantity : undefined,
|
||||
title: orderItem.variant_title ?? orderItem.title,
|
||||
sku: orderItem.variant_sku || "",
|
||||
barcode: orderItem.variant_barcode || "",
|
||||
@@ -257,11 +259,12 @@ export const confirmExchangeRequestWorkflow = createWorkflow(
|
||||
"id",
|
||||
"version",
|
||||
"canceled_at",
|
||||
"additional_items.id",
|
||||
"additional_items.title",
|
||||
"additional_items.variant_title",
|
||||
"additional_items.variant_sku",
|
||||
"additional_items.variant_barcode",
|
||||
"additional_items.item_id",
|
||||
"additional_items.quantity",
|
||||
"additional_items.item.title",
|
||||
"additional_items.item.variant_title",
|
||||
"additional_items.item.variant_sku",
|
||||
"additional_items.item.variant_barcode",
|
||||
],
|
||||
variables: { id: exchangeId },
|
||||
list: false,
|
||||
@@ -332,6 +335,7 @@ export const confirmExchangeRequestWorkflow = createWorkflow(
|
||||
order,
|
||||
items: order.items!,
|
||||
shippingOption: returnShippingOption,
|
||||
isReturn: true,
|
||||
},
|
||||
prepareFulfillmentData
|
||||
)
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import {
|
||||
OrderChangeActionDTO,
|
||||
OrderChangeDTO,
|
||||
OrderDTO,
|
||||
OrderExchangeDTO,
|
||||
OrderWorkflow,
|
||||
} from "@medusajs/types"
|
||||
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../../common"
|
||||
import {
|
||||
deleteOrderChangeActionsStep,
|
||||
previewOrderChangeStep,
|
||||
} from "../../steps"
|
||||
import {
|
||||
throwIfIsCancelled,
|
||||
throwIfOrderChangeIsNotActive,
|
||||
} from "../../utils/order-validation"
|
||||
|
||||
const validationStep = createStep(
|
||||
"remove-item-exchange-action-validation",
|
||||
async function ({
|
||||
order,
|
||||
orderChange,
|
||||
orderExchange,
|
||||
input,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
orderExchange: OrderExchangeDTO
|
||||
orderChange: OrderChangeDTO
|
||||
input: OrderWorkflow.DeleteOrderExchangeItemActionWorkflowInput
|
||||
}) {
|
||||
throwIfIsCancelled(order, "Order")
|
||||
throwIfIsCancelled(orderExchange, "Exchange")
|
||||
throwIfOrderChangeIsNotActive({ orderChange })
|
||||
|
||||
const associatedAction = (orderChange.actions ?? []).find(
|
||||
(a) => a.id === input.action_id
|
||||
) as OrderChangeActionDTO
|
||||
|
||||
if (!associatedAction) {
|
||||
throw new Error(
|
||||
`No item exchange found for exchange ${input.exchange_id} in order change ${orderChange.id}`
|
||||
)
|
||||
} else if (associatedAction.action !== ChangeActionType.ITEM_ADD) {
|
||||
throw new Error(`Action ${associatedAction.id} is not adding an item`)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export const removeItemExchangeActionWorkflowId = "remove-item-exchange-action"
|
||||
export const removeItemExchangeActionWorkflow = createWorkflow(
|
||||
removeItemExchangeActionWorkflowId,
|
||||
function (
|
||||
input: WorkflowData<OrderWorkflow.DeleteOrderExchangeItemActionWorkflowInput>
|
||||
): WorkflowResponse<OrderDTO> {
|
||||
const orderExchange: OrderExchangeDTO = useRemoteQueryStep({
|
||||
entry_point: "order_exchange",
|
||||
fields: ["id", "status", "order_id", "canceled_at"],
|
||||
variables: { id: input.exchange_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
})
|
||||
|
||||
const order: OrderDTO = useRemoteQueryStep({
|
||||
entry_point: "orders",
|
||||
fields: ["id", "status", "canceled_at", "items.*"],
|
||||
variables: { id: orderExchange.order_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "order-query" })
|
||||
|
||||
const orderChange: OrderChangeDTO = useRemoteQueryStep({
|
||||
entry_point: "order_change",
|
||||
fields: ["id", "status", "version", "actions.*"],
|
||||
variables: {
|
||||
filters: {
|
||||
order_id: orderExchange.order_id,
|
||||
exchange_id: orderExchange.id,
|
||||
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
|
||||
},
|
||||
},
|
||||
list: false,
|
||||
}).config({ name: "order-change-query" })
|
||||
|
||||
validationStep({ order, input, orderExchange, orderChange })
|
||||
|
||||
deleteOrderChangeActionsStep({ ids: [input.action_id] })
|
||||
|
||||
return new WorkflowResponse(previewOrderChangeStep(order.id))
|
||||
}
|
||||
)
|
||||
@@ -11,6 +11,7 @@ export * from "./claim/claim-item"
|
||||
export * from "./claim/claim-request-item-return"
|
||||
export * from "./claim/confirm-claim-request"
|
||||
export * from "./claim/create-claim-shipping-method"
|
||||
export * from "./claim/remove-claim-add-item-action"
|
||||
export * from "./claim/remove-claim-item-action"
|
||||
export * from "./claim/remove-claim-shipping-method"
|
||||
export * from "./claim/update-claim-add-item"
|
||||
@@ -32,6 +33,7 @@ export * from "./exchange/confirm-exchange-request"
|
||||
export * from "./exchange/create-exchange-shipping-method"
|
||||
export * from "./exchange/exchange-add-new-item"
|
||||
export * from "./exchange/exchange-request-item-return"
|
||||
export * from "./exchange/remove-exchange-item-action"
|
||||
export * from "./exchange/remove-exchange-shipping-method"
|
||||
export * from "./exchange/update-exchange-add-item"
|
||||
export * from "./exchange/update-exchange-shipping-method"
|
||||
|
||||
Reference in New Issue
Block a user