chore(core-flows): return fulfillment link (#8304)
This commit is contained in:
committed by
GitHub
parent
feabe0e6c0
commit
d63ca00214
@@ -1,12 +1,24 @@
|
||||
import { OrderChangeDTO, OrderDTO, ReturnDTO } from "@medusajs/types"
|
||||
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils"
|
||||
import {
|
||||
FulfillmentWorkflow,
|
||||
OrderChangeDTO,
|
||||
OrderDTO,
|
||||
ReturnDTO,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ChangeActionType,
|
||||
MedusaError,
|
||||
Modules,
|
||||
OrderChangeStatus,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
transform,
|
||||
when,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../../common"
|
||||
import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common"
|
||||
import { createReturnFulfillmentWorkflow } from "../../../fulfillment/workflows/create-return-fulfillment"
|
||||
import { previewOrderChangeStep } from "../../steps"
|
||||
import { confirmOrderChanges } from "../../steps/confirm-order-changes"
|
||||
import { createReturnItemsStep } from "../../steps/create-return-items"
|
||||
@@ -36,13 +48,76 @@ const validationStep = createStep(
|
||||
}
|
||||
)
|
||||
|
||||
function prepareFulfillmentData({
|
||||
order,
|
||||
items,
|
||||
returnShippingOption,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
items: any[]
|
||||
returnShippingOption: {
|
||||
id: string
|
||||
provider_id: string
|
||||
service_zone: {
|
||||
fulfillment_set: {
|
||||
location?: {
|
||||
id: string
|
||||
address: Record<string, any>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}) {
|
||||
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)!
|
||||
return {
|
||||
line_item_id: i.item_id,
|
||||
quantity: i.quantity,
|
||||
return_quantity: i.quantity,
|
||||
title: orderItem.variant_title ?? orderItem.title,
|
||||
sku: orderItem.variant_sku || "",
|
||||
barcode: orderItem.variant_barcode || "",
|
||||
} as FulfillmentWorkflow.CreateFulfillmentItemWorkflowDTO
|
||||
})
|
||||
|
||||
const locationId =
|
||||
returnShippingOption.service_zone.fulfillment_set.location?.id
|
||||
|
||||
// delivery address is the stock location address
|
||||
const address =
|
||||
returnShippingOption.service_zone.fulfillment_set.location?.address ?? {}
|
||||
|
||||
delete address.id
|
||||
|
||||
if (!locationId) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`Cannot create return without stock location, either provide a location or you should link the shipping option ${returnShippingOption.id} to a stock location.`
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
input: {
|
||||
location_id: locationId,
|
||||
provider_id: returnShippingOption.provider_id,
|
||||
shipping_option_id: returnShippingOption.id,
|
||||
items: fulfillmentItems,
|
||||
delivery_address: address,
|
||||
order: order,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export const confirmReturnRequestWorkflowId = "confirm-return-request"
|
||||
export const confirmReturnRequestWorkflow = createWorkflow(
|
||||
confirmReturnRequestWorkflowId,
|
||||
function (input: WorkflowInput): WorkflowData<OrderDTO> {
|
||||
const orderReturn: ReturnDTO = useRemoteQueryStep({
|
||||
entry_point: "return",
|
||||
fields: ["id", "status", "order_id", "canceled_at"],
|
||||
fields: ["id", "status", "order_id", "location_id", "canceled_at"],
|
||||
variables: { id: input.return_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
@@ -50,7 +125,16 @@ export const confirmReturnRequestWorkflow = createWorkflow(
|
||||
|
||||
const order: OrderDTO = useRemoteQueryStep({
|
||||
entry_point: "orders",
|
||||
fields: ["id", "version", "canceled_at"],
|
||||
fields: [
|
||||
"id",
|
||||
"version",
|
||||
"canceled_at",
|
||||
"items.id",
|
||||
"items.title",
|
||||
"items.variant_title",
|
||||
"items.variant_sku",
|
||||
"items.variant_barcode",
|
||||
],
|
||||
variables: { id: orderReturn.order_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
@@ -85,13 +169,79 @@ export const confirmReturnRequestWorkflow = createWorkflow(
|
||||
|
||||
validationStep({ order, orderReturn, orderChange })
|
||||
|
||||
createReturnItemsStep({
|
||||
const createdReturnItems = createReturnItemsStep({
|
||||
returnId: orderReturn.id,
|
||||
changes: returnItemActions,
|
||||
})
|
||||
|
||||
confirmOrderChanges({ changes: [orderChange], orderId: order.id })
|
||||
|
||||
const returnModified = useRemoteQueryStep({
|
||||
entry_point: "return",
|
||||
fields: [
|
||||
"id",
|
||||
"status",
|
||||
"order_id",
|
||||
"canceled_at",
|
||||
"shipping_methods.shipping_option_id",
|
||||
],
|
||||
variables: { id: input.return_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "return-query" })
|
||||
|
||||
const returnShippingOptionId = transform(
|
||||
{ returnModified },
|
||||
({ returnModified }) => {
|
||||
if (!returnModified.shipping_methods?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
return returnModified.shipping_methods[0].shipping_option_id
|
||||
}
|
||||
)
|
||||
|
||||
when({ returnShippingOptionId }, ({ returnShippingOptionId }) => {
|
||||
return !!returnShippingOptionId
|
||||
}).then(() => {
|
||||
const returnShippingOption = useRemoteQueryStep({
|
||||
entry_point: "shipping_options",
|
||||
fields: [
|
||||
"id",
|
||||
"provider_id",
|
||||
"service_zone.fulfillment_set.location.id",
|
||||
"service_zone.fulfillment_set.location.address.*",
|
||||
],
|
||||
variables: {
|
||||
id: returnShippingOptionId,
|
||||
},
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "return-shipping-option" })
|
||||
|
||||
const fulfillmentData = transform(
|
||||
{ order, items: createdReturnItems, returnShippingOption },
|
||||
prepareFulfillmentData
|
||||
)
|
||||
|
||||
const returnFulfillment =
|
||||
createReturnFulfillmentWorkflow.runAsStep(fulfillmentData)
|
||||
|
||||
const link = transform(
|
||||
{ orderReturn, fulfillment: returnFulfillment },
|
||||
(data) => {
|
||||
return [
|
||||
{
|
||||
[Modules.ORDER]: { return_id: data.orderReturn.id },
|
||||
[Modules.FULFILLMENT]: { fulfillment_id: data.fulfillment.id },
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
createRemoteLinkStep(link)
|
||||
})
|
||||
|
||||
return previewOrderChangeStep(order.id)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -12,7 +12,6 @@ import {
|
||||
WorkflowData,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common"
|
||||
@@ -50,7 +49,10 @@ function prepareShippingMethodData({
|
||||
adjustments: [],
|
||||
}
|
||||
|
||||
if (isDefined(inputShippingOption.price) && inputShippingOption.price >= 0) {
|
||||
if (
|
||||
isDefined(inputShippingOption.price) &&
|
||||
MathBN.gte(inputShippingOption.price, 0)
|
||||
) {
|
||||
obj.amount = inputShippingOption.price
|
||||
} else {
|
||||
if (returnShippingOption.price_type === "calculated") {
|
||||
@@ -288,27 +290,26 @@ export const createAndCompleteReturnOrderWorkflow = createWorkflow(
|
||||
const returnFulfillment =
|
||||
createReturnFulfillmentWorkflow.runAsStep(fulfillmentData)
|
||||
|
||||
const returnCreated = createCompleteReturnStep({
|
||||
order_id: input.order_id,
|
||||
items: input.items,
|
||||
shipping_method: shippingMethodData,
|
||||
created_by: input.created_by,
|
||||
})
|
||||
|
||||
const link = transform(
|
||||
{ order_id: input.order_id, fulfillment: returnFulfillment },
|
||||
{ returnCreated, fulfillment: returnFulfillment },
|
||||
(data) => {
|
||||
return [
|
||||
{
|
||||
[Modules.ORDER]: { order_id: data.order_id },
|
||||
[Modules.ORDER]: { return_id: data.returnCreated.id },
|
||||
[Modules.FULFILLMENT]: { fulfillment_id: data.fulfillment.id },
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
const [returnCreated] = parallelize(
|
||||
createCompleteReturnStep({
|
||||
order_id: input.order_id,
|
||||
items: input.items,
|
||||
shipping_method: shippingMethodData,
|
||||
created_by: input.created_by,
|
||||
}),
|
||||
createRemoteLinkStep(link)
|
||||
)
|
||||
createRemoteLinkStep(link)
|
||||
|
||||
const receiveItems = transform(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user