feat: Returns order previews (#8135)
* work on order previews * fix create return shipping flow * fix http tests * fix tests
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../common"
|
||||
import { previewOrderChangeStep } from "../steps"
|
||||
import { confirmOrderChanges } from "../steps/confirm-order-changes"
|
||||
import { createReturnItems } from "../steps/create-return-items"
|
||||
import {
|
||||
@@ -38,7 +39,7 @@ const validationStep = createStep(
|
||||
export const confirmReturnRequestWorkflowId = "confirm-return-request"
|
||||
export const confirmReturnRequestWorkflow = createWorkflow(
|
||||
confirmReturnRequestWorkflowId,
|
||||
function (input: WorkflowInput): WorkflowData<void> {
|
||||
function (input: WorkflowInput): WorkflowData<OrderDTO> {
|
||||
const orderReturn: ReturnDTO = useRemoteQueryStep({
|
||||
entry_point: "return",
|
||||
fields: ["id", "status", "order_id"],
|
||||
@@ -86,5 +87,7 @@ export const confirmReturnRequestWorkflow = createWorkflow(
|
||||
createReturnItems({ returnId: orderReturn.id, changes: returnItemActions })
|
||||
|
||||
confirmOrderChanges({ changes: [orderChange], orderId: order.id })
|
||||
|
||||
return previewOrderChangeStep(order.id)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -12,9 +12,9 @@ import {
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../common"
|
||||
import { previewOrderChangeStep } from "../steps"
|
||||
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions"
|
||||
import { createOrderShippingMethods } from "../steps/create-order-shipping-methods"
|
||||
import { previewOrderChangeStep } from "../steps/preview-order-change"
|
||||
import {
|
||||
throwIfIsCancelled,
|
||||
throwIfOrderChangeIsNotActive,
|
||||
@@ -45,7 +45,7 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
|
||||
return_id: string
|
||||
shipping_option_id: string
|
||||
custom_price?: BigNumberInput
|
||||
}): WorkflowData {
|
||||
}): WorkflowData<OrderDTO> {
|
||||
const orderReturn: ReturnDTO = useRemoteQueryStep({
|
||||
entry_point: "return",
|
||||
fields: ["id", "status", "order_id"],
|
||||
@@ -93,14 +93,19 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
|
||||
validationStep({ order, orderReturn, orderChange })
|
||||
|
||||
const shippingMethodInput = transform(
|
||||
{ orderReturn, shippingOptions, orderChange },
|
||||
{
|
||||
orderReturn,
|
||||
shippingOptions,
|
||||
customPrice: input.custom_price,
|
||||
orderChange,
|
||||
},
|
||||
(data) => {
|
||||
const option = data.shippingOptions[0]
|
||||
const orderChange = data.orderChange
|
||||
|
||||
return {
|
||||
shipping_option_id: option.id,
|
||||
amount: option.calculated_price.calculated_amount,
|
||||
amount: data.customPrice ?? option.calculated_price.calculated_amount,
|
||||
is_tax_inclusive:
|
||||
!!option.calculated_price.is_calculated_price_tax_inclusive,
|
||||
data: option.data ?? {},
|
||||
@@ -141,8 +146,8 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
|
||||
return {
|
||||
action: ChangeActionType.SHIPPING_ADD,
|
||||
reference: "order_shipping_method",
|
||||
reference_id: createdMethod.id,
|
||||
order_change_id: orderChange.id,
|
||||
reference_id: createdMethod.id,
|
||||
amount: methodPrice,
|
||||
order_id: order.id,
|
||||
return_id: orderReturn.id,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import {
|
||||
OrderChangeActionDTO,
|
||||
OrderChangeDTO,
|
||||
OrderDTO,
|
||||
OrderWorkflow,
|
||||
@@ -13,6 +12,7 @@ import {
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../common"
|
||||
import { previewOrderChangeStep } from "../steps"
|
||||
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions"
|
||||
import {
|
||||
throwIfIsCancelled,
|
||||
@@ -45,7 +45,7 @@ export const requestItemReturnWorkflow = createWorkflow(
|
||||
requestItemReturnWorkflowId,
|
||||
function (
|
||||
input: WorkflowData<OrderWorkflow.RequestItemReturnWorkflowInput>
|
||||
): WorkflowData<OrderChangeActionDTO[]> {
|
||||
): WorkflowData<OrderDTO> {
|
||||
const orderReturn: ReturnDTO = useRemoteQueryStep({
|
||||
entry_point: "return",
|
||||
fields: ["id", "status", "order_id"],
|
||||
@@ -94,6 +94,8 @@ export const requestItemReturnWorkflow = createWorkflow(
|
||||
}
|
||||
)
|
||||
|
||||
return createOrderChangeActionsStep(orderChangeActionInput)
|
||||
createOrderChangeActionsStep(orderChangeActionInput)
|
||||
|
||||
return previewOrderChangeStep(order.id)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -17,7 +17,7 @@ export const POST = async (
|
||||
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
await requestItemReturnWorkflow(req.scope).run({
|
||||
const { result } = await requestItemReturnWorkflow(req.scope).run({
|
||||
input: { ...req.validatedBody, return_id: id },
|
||||
})
|
||||
|
||||
@@ -35,6 +35,7 @@ export const POST = async (
|
||||
const [orderReturn] = await remoteQuery(queryObject)
|
||||
|
||||
res.json({
|
||||
order_preview: result,
|
||||
return: orderReturn,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -17,10 +17,12 @@ export const POST = async (
|
||||
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
await confirmReturnRequestWorkflow(req.scope).run({
|
||||
const { result } = await confirmReturnRequestWorkflow(req.scope).run({
|
||||
input: { return_id: id },
|
||||
})
|
||||
|
||||
console.log("RESULT: ", result)
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "return",
|
||||
variables: {
|
||||
@@ -35,6 +37,7 @@ export const POST = async (
|
||||
const [orderReturn] = await remoteQuery(queryObject)
|
||||
|
||||
res.json({
|
||||
order_preview: result,
|
||||
return: orderReturn,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { createReturnShippingMethodWorkflow } from "@medusajs/core-flows"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
@@ -11,13 +15,27 @@ export const POST = async (
|
||||
) => {
|
||||
const { id } = req.params
|
||||
|
||||
const { result: orderPreview } = await createReturnShippingMethodWorkflow(
|
||||
req.scope
|
||||
).run({
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const { result } = await createReturnShippingMethodWorkflow(req.scope).run({
|
||||
input: { ...req.validatedBody, return_id: id },
|
||||
})
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "return",
|
||||
variables: {
|
||||
id,
|
||||
filters: {
|
||||
...req.filterableFields,
|
||||
},
|
||||
},
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
})
|
||||
|
||||
const [orderReturn] = await remoteQuery(queryObject)
|
||||
|
||||
res.json({
|
||||
order: orderPreview,
|
||||
order_preview: result,
|
||||
return: orderReturn,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,8 +9,6 @@ export const defaultAdminReturnFields = [
|
||||
"refund_amount",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"*items",
|
||||
"*shipping_methods",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
|
||||
@@ -61,5 +61,8 @@ export const POST = async (
|
||||
|
||||
const [orderReturn] = await remoteQuery(queryObject)
|
||||
|
||||
res.status(200).json({ return: orderReturn })
|
||||
res.json({
|
||||
order_preview: result,
|
||||
return: orderReturn,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user