feat: Returns order previews (#8135)
* work on order previews * fix create return shipping flow * fix http tests * fix tests
This commit is contained in:
@@ -185,8 +185,18 @@ medusaIntegrationTestRunner({
|
||||
display_id: 1,
|
||||
order_version: 2,
|
||||
status: "requested",
|
||||
items: [],
|
||||
shipping_methods: [],
|
||||
})
|
||||
)
|
||||
|
||||
expect(result.data.order_preview).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
return_id: returnId,
|
||||
change_type: "return",
|
||||
actions: [],
|
||||
description: "Test",
|
||||
status: "pending",
|
||||
order_id: order.id,
|
||||
})
|
||||
)
|
||||
|
||||
@@ -205,15 +215,21 @@ medusaIntegrationTestRunner({
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(result.data.return).toEqual(
|
||||
expect(result.data.order_preview).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
order_id: order.id,
|
||||
display_id: 1,
|
||||
order_version: 2,
|
||||
status: "requested",
|
||||
items: [],
|
||||
shipping_methods: [],
|
||||
id: order.id,
|
||||
items: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
title: "Custom Item 2",
|
||||
unit_price: 50,
|
||||
quantity: 1,
|
||||
subtotal: 50,
|
||||
total: 50,
|
||||
fulfilled_total: 50,
|
||||
return_requested_total: 50,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
|
||||
@@ -225,11 +241,30 @@ medusaIntegrationTestRunner({
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(result.data.order.shipping_methods[1]).toEqual(
|
||||
expect(result.data.order_preview).toEqual(
|
||||
expect.objectContaining({
|
||||
amount: 1000,
|
||||
name: "Return shipping",
|
||||
shipping_option_id: returnShippingOption.id,
|
||||
id: order.id,
|
||||
items: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
title: "Custom Item 2",
|
||||
unit_price: 50,
|
||||
quantity: 1,
|
||||
subtotal: 50,
|
||||
total: 50,
|
||||
fulfilled_total: 50,
|
||||
return_requested_total: 50,
|
||||
}),
|
||||
]),
|
||||
shipping_methods: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
name: "Return shipping",
|
||||
amount: 1000,
|
||||
subtotal: 1000,
|
||||
total: 1000,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
|
||||
@@ -239,27 +274,30 @@ medusaIntegrationTestRunner({
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(result.data.return).toEqual(
|
||||
expect(result.data.order_preview).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
order_id: order.id,
|
||||
display_id: 1,
|
||||
order_version: 2,
|
||||
status: "requested",
|
||||
items: [
|
||||
id: order.id,
|
||||
items: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
title: "Custom Item 2",
|
||||
unit_price: 50,
|
||||
quantity: 1,
|
||||
item_id: item.id,
|
||||
received_quantity: 0,
|
||||
subtotal: 50,
|
||||
total: 50,
|
||||
fulfilled_total: 50,
|
||||
return_requested_total: 50,
|
||||
}),
|
||||
],
|
||||
shipping_methods: [
|
||||
]),
|
||||
shipping_methods: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
amount: 1000,
|
||||
id: expect.any(String),
|
||||
name: "Return shipping",
|
||||
shipping_option_id: returnShippingOption.id,
|
||||
amount: 1000,
|
||||
subtotal: 1000,
|
||||
total: 1000,
|
||||
}),
|
||||
],
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"license": "MIT",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"test:integration": "jest --no-cache --maxWorkers=50% --bail --detectOpenHandles --forceExit --logHeapUsage",
|
||||
"test:integration": "jest --no-cache --maxWorkers=50% --bail --detectOpenHandles --forceExit --logHeapUsage",
|
||||
"test:integration:chunk": "jest --silent --no-cache --bail --maxWorkers=50% --forceExit --testPathPattern=$(echo $CHUNKS | jq -r \".[${CHUNK}] | .[]\")",
|
||||
"build": "tsc ./src/* --allowJs --outDir ./dist"
|
||||
},
|
||||
|
||||
@@ -70,7 +70,11 @@ medusaIntegrationTestRunner({
|
||||
},
|
||||
})
|
||||
|
||||
expect(orderChangePreview.shipping_methods[1].actions).toEqual([
|
||||
const shippingMethod = orderChangePreview.shipping_methods?.find(
|
||||
(sm) => sm.shipping_option_id === shippingOptionId
|
||||
)
|
||||
|
||||
expect((shippingMethod as any).actions).toEqual([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
reference: "order_shipping_method",
|
||||
@@ -95,7 +99,11 @@ medusaIntegrationTestRunner({
|
||||
},
|
||||
})
|
||||
|
||||
expect(orderChangePreview.shipping_methods[1].actions).toEqual([
|
||||
const shippingMethod = orderChangePreview.shipping_methods?.find(
|
||||
(sm) => sm.shipping_option_id === shippingOptionId
|
||||
)
|
||||
|
||||
expect((shippingMethod as any).actions).toEqual([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
reference: "order_shipping_method",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
beginReturnOrderWorkflow,
|
||||
createOrderFulfillmentWorkflow,
|
||||
requestItemReturnWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import { IOrderModuleService, OrderDTO, ReturnDTO } from "@medusajs/types"
|
||||
@@ -37,6 +38,18 @@ medusaIntegrationTestRunner({
|
||||
inventoryItem: fixtures.inventoryItem,
|
||||
})
|
||||
|
||||
await createOrderFulfillmentWorkflow(container).run({
|
||||
input: {
|
||||
order_id: order.id,
|
||||
items: [
|
||||
{
|
||||
quantity: 1,
|
||||
id: order.items![0].id,
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
await beginReturnOrderWorkflow(container).run({
|
||||
input: { order_id: order.id },
|
||||
throwOnError: true,
|
||||
@@ -59,9 +72,7 @@ medusaIntegrationTestRunner({
|
||||
describe("requestItemReturnWorkflow", () => {
|
||||
it("should successfully add a return item to order change", async () => {
|
||||
const item = order.items![0]
|
||||
const {
|
||||
result: [returnItem],
|
||||
} = await requestItemReturnWorkflow(container).run({
|
||||
const { result } = await requestItemReturnWorkflow(container).run({
|
||||
input: {
|
||||
return_id: returnOrder.id,
|
||||
items: [
|
||||
@@ -74,19 +85,18 @@ medusaIntegrationTestRunner({
|
||||
},
|
||||
})
|
||||
|
||||
const returnItem = result.items?.[0]
|
||||
|
||||
expect(returnItem).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
order_id: order.id,
|
||||
return_id: returnOrder.id,
|
||||
reference: "return",
|
||||
reference_id: returnOrder.id,
|
||||
details: {
|
||||
reference_id: item.id,
|
||||
quantity: 1,
|
||||
},
|
||||
internal_note: "test",
|
||||
action: "RETURN_ITEM",
|
||||
title: "Custom Item 2",
|
||||
unit_price: 50,
|
||||
quantity: 1,
|
||||
subtotal: 50,
|
||||
total: 50,
|
||||
fulfilled_total: 50,
|
||||
return_requested_total: 50,
|
||||
})
|
||||
)
|
||||
})
|
||||
@@ -142,7 +152,7 @@ medusaIntegrationTestRunner({
|
||||
const item = order.items![0]
|
||||
|
||||
const [orderChange] = await service.listOrderChanges(
|
||||
{ order_id: order.id },
|
||||
{ order_id: order.id, return_id: returnOrder.id },
|
||||
{}
|
||||
)
|
||||
|
||||
@@ -173,7 +183,7 @@ medusaIntegrationTestRunner({
|
||||
const item = order.items![0]
|
||||
|
||||
const [orderChange] = await service.listOrderChanges(
|
||||
{ order_id: order.id },
|
||||
{ order_id: order.id, return_id: returnOrder.id },
|
||||
{}
|
||||
)
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"license": "MIT",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"test:integration": "jest --silent --no-cache --maxWorkers=50% --bail --detectOpenHandles --forceExit --logHeapUsage",
|
||||
"test:integration": "jest --silent=false --no-cache --maxWorkers=50% --bail --detectOpenHandles --forceExit --logHeapUsage",
|
||||
"test:integration:chunk": "jest --silent --no-cache --bail --maxWorkers=50% --forceExit --testPathPattern=$(echo $CHUNKS | jq -r \".[${CHUNK}] | .[]\")",
|
||||
"build": "tsc ./src/* --allowJs --outDir ./dist"
|
||||
},
|
||||
|
||||
@@ -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