From b7e6b1461ba99ceaab1d3c773121d84501b3cd15 Mon Sep 17 00:00:00 2001 From: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Tue, 16 Jul 2024 11:07:04 +0100 Subject: [PATCH] feat: Returns order previews (#8135) * work on order previews * fix create return shipping flow * fix http tests * fix tests --- .../http/__tests__/returns/returns.spec.ts | 94 +++++++++++++------ integration-tests/http/package.json | 2 +- .../return/create-return-shipping.spec.ts | 12 ++- .../order/workflows/return/items.spec.ts | 40 +++++--- integration-tests/modules/package.json | 2 +- .../order/workflows/confirm-return-request.ts | 5 +- .../create-return-shipping-method.ts | 15 ++- .../order/workflows/request-item-return.ts | 8 +- .../admin/returns/[id]/request-items/route.ts | 3 +- .../api/admin/returns/[id]/request/route.ts | 5 +- .../returns/[id]/shipping-method/route.ts | 26 ++++- .../src/api/admin/returns/query-config.ts | 2 - .../medusa/src/api/admin/returns/route.ts | 5 +- 13 files changed, 154 insertions(+), 65 deletions(-) diff --git a/integration-tests/http/__tests__/returns/returns.spec.ts b/integration-tests/http/__tests__/returns/returns.spec.ts index d60d6e5d64..a61ef397fd 100644 --- a/integration-tests/http/__tests__/returns/returns.spec.ts +++ b/integration-tests/http/__tests__/returns/returns.spec.ts @@ -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, }), - ], + ]), }) ) }) diff --git a/integration-tests/http/package.json b/integration-tests/http/package.json index 32807ea86a..48eba21a8b 100644 --- a/integration-tests/http/package.json +++ b/integration-tests/http/package.json @@ -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" }, diff --git a/integration-tests/modules/__tests__/order/workflows/return/create-return-shipping.spec.ts b/integration-tests/modules/__tests__/order/workflows/return/create-return-shipping.spec.ts index a1b4f1dc50..a1ef37accf 100644 --- a/integration-tests/modules/__tests__/order/workflows/return/create-return-shipping.spec.ts +++ b/integration-tests/modules/__tests__/order/workflows/return/create-return-shipping.spec.ts @@ -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", diff --git a/integration-tests/modules/__tests__/order/workflows/return/items.spec.ts b/integration-tests/modules/__tests__/order/workflows/return/items.spec.ts index e008187efe..fb833a7ae7 100644 --- a/integration-tests/modules/__tests__/order/workflows/return/items.spec.ts +++ b/integration-tests/modules/__tests__/order/workflows/return/items.spec.ts @@ -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 }, {} ) diff --git a/integration-tests/modules/package.json b/integration-tests/modules/package.json index bf4f947ff3..b77283cb3c 100644 --- a/integration-tests/modules/package.json +++ b/integration-tests/modules/package.json @@ -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" }, diff --git a/packages/core/core-flows/src/order/workflows/confirm-return-request.ts b/packages/core/core-flows/src/order/workflows/confirm-return-request.ts index 7ed1d0bc60..cb5d72c589 100644 --- a/packages/core/core-flows/src/order/workflows/confirm-return-request.ts +++ b/packages/core/core-flows/src/order/workflows/confirm-return-request.ts @@ -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 { + function (input: WorkflowInput): WorkflowData { 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) } ) diff --git a/packages/core/core-flows/src/order/workflows/create-return-shipping-method.ts b/packages/core/core-flows/src/order/workflows/create-return-shipping-method.ts index 7a8ea59146..b50379c8d2 100644 --- a/packages/core/core-flows/src/order/workflows/create-return-shipping-method.ts +++ b/packages/core/core-flows/src/order/workflows/create-return-shipping-method.ts @@ -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 { 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, diff --git a/packages/core/core-flows/src/order/workflows/request-item-return.ts b/packages/core/core-flows/src/order/workflows/request-item-return.ts index 16b4b990ac..27c6077c5b 100644 --- a/packages/core/core-flows/src/order/workflows/request-item-return.ts +++ b/packages/core/core-flows/src/order/workflows/request-item-return.ts @@ -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 - ): WorkflowData { + ): WorkflowData { 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) } ) diff --git a/packages/medusa/src/api/admin/returns/[id]/request-items/route.ts b/packages/medusa/src/api/admin/returns/[id]/request-items/route.ts index 9f590f3267..13119989a0 100644 --- a/packages/medusa/src/api/admin/returns/[id]/request-items/route.ts +++ b/packages/medusa/src/api/admin/returns/[id]/request-items/route.ts @@ -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, }) } diff --git a/packages/medusa/src/api/admin/returns/[id]/request/route.ts b/packages/medusa/src/api/admin/returns/[id]/request/route.ts index 6b14fae184..37f5f3eaba 100644 --- a/packages/medusa/src/api/admin/returns/[id]/request/route.ts +++ b/packages/medusa/src/api/admin/returns/[id]/request/route.ts @@ -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, }) } diff --git a/packages/medusa/src/api/admin/returns/[id]/shipping-method/route.ts b/packages/medusa/src/api/admin/returns/[id]/shipping-method/route.ts index 6acf15e648..783a7fd2b1 100644 --- a/packages/medusa/src/api/admin/returns/[id]/shipping-method/route.ts +++ b/packages/medusa/src/api/admin/returns/[id]/shipping-method/route.ts @@ -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, }) } diff --git a/packages/medusa/src/api/admin/returns/query-config.ts b/packages/medusa/src/api/admin/returns/query-config.ts index 22b10e367d..e3b8e510af 100644 --- a/packages/medusa/src/api/admin/returns/query-config.ts +++ b/packages/medusa/src/api/admin/returns/query-config.ts @@ -9,8 +9,6 @@ export const defaultAdminReturnFields = [ "refund_amount", "created_at", "updated_at", - "*items", - "*shipping_methods", ] export const retrieveTransformQueryConfig = { diff --git a/packages/medusa/src/api/admin/returns/route.ts b/packages/medusa/src/api/admin/returns/route.ts index 880732135b..ad75cf8a3b 100644 --- a/packages/medusa/src/api/admin/returns/route.ts +++ b/packages/medusa/src/api/admin/returns/route.ts @@ -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, + }) }