From 23053afe95a382be5ed8db026baf56220b8eedaa Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Tue, 16 Jul 2024 15:23:28 -0300 Subject: [PATCH] chore(medusa,core-flows): update request return items and shipping (#8151) --- .../http/__tests__/returns/returns.spec.ts | 105 ++++++++++++--- .../core/core-flows/src/order/steps/index.ts | 1 + .../order/steps/update-shipping-methods.ts | 42 ++++++ .../{ => claim}/begin-order-claim.ts | 8 +- .../{ => claim}/claim-add-new-item.ts | 10 +- .../{ => claim}/claim-request-item-return.ts | 12 +- .../{ => exchange}/begin-order-exchange.ts | 8 +- .../create-exchange-return-shipping-method.ts | 10 +- .../{ => exchange}/exchange-add-new-item.ts | 10 +- .../exchange-request-item-return.ts | 12 +- .../core-flows/src/order/workflows/index.ts | 30 +++-- .../workflows/{ => return}/begin-return.ts | 8 +- .../workflows/{ => return}/cancel-return.ts | 6 +- .../{ => return}/confirm-return-request.ts | 10 +- .../create-return-shipping-method.ts | 10 +- .../workflows/{ => return}/receive-return.ts | 6 +- .../remove-request-item-return.ts | 11 +- .../remove-return-shipping-method.ts | 19 +-- .../{ => return}/request-item-return.ts | 10 +- .../return/update-request-item-return.ts | 117 +++++++++++++++++ .../return/update-return-shipping-method.ts | 120 ++++++++++++++++++ packages/core/types/src/order/mutations.ts | 1 - packages/core/types/src/order/service.ts | 11 ++ .../core/types/src/workflow/order/index.ts | 1 + .../src/workflow/order/request-item-return.ts | 10 ++ .../src/workflow/order/shipping-return.ts | 16 +++ .../[id]/request-items/[action_id]/route.ts | 41 +++++- .../[id]/shipping-method/[action_id]/route.ts | 41 +++++- .../src/api/admin/returns/middlewares.ts | 24 ++++ .../src/api/admin/returns/validators.ts | 19 +++ 30 files changed, 619 insertions(+), 110 deletions(-) create mode 100644 packages/core/core-flows/src/order/steps/update-shipping-methods.ts rename packages/core/core-flows/src/order/workflows/{ => claim}/begin-order-claim.ts (84%) rename packages/core/core-flows/src/order/workflows/{ => claim}/claim-add-new-item.ts (90%) rename packages/core/core-flows/src/order/workflows/{ => claim}/claim-request-item-return.ts (91%) rename packages/core/core-flows/src/order/workflows/{ => exchange}/begin-order-exchange.ts (84%) rename packages/core/core-flows/src/order/workflows/{ => exchange}/create-exchange-return-shipping-method.ts (93%) rename packages/core/core-flows/src/order/workflows/{ => exchange}/exchange-add-new-item.ts (90%) rename packages/core/core-flows/src/order/workflows/{ => exchange}/exchange-request-item-return.ts (92%) rename packages/core/core-flows/src/order/workflows/{ => return}/begin-return.ts (84%) rename packages/core/core-flows/src/order/workflows/{ => return}/cancel-return.ts (91%) rename packages/core/core-flows/src/order/workflows/{ => return}/confirm-return-request.ts (89%) rename packages/core/core-flows/src/order/workflows/{ => return}/create-return-shipping-method.ts (93%) rename packages/core/core-flows/src/order/workflows/{ => return}/receive-return.ts (89%) rename packages/core/core-flows/src/order/workflows/{ => return}/remove-request-item-return.ts (91%) rename packages/core/core-flows/src/order/workflows/{ => return}/remove-return-shipping-method.ts (82%) rename packages/core/core-flows/src/order/workflows/{ => return}/request-item-return.ts (91%) create mode 100644 packages/core/core-flows/src/order/workflows/return/update-request-item-return.ts create mode 100644 packages/core/core-flows/src/order/workflows/return/update-return-shipping-method.ts create mode 100644 packages/core/types/src/workflow/order/shipping-return.ts diff --git a/integration-tests/http/__tests__/returns/returns.spec.ts b/integration-tests/http/__tests__/returns/returns.spec.ts index 6106d1439e..7ca16097bb 100644 --- a/integration-tests/http/__tests__/returns/returns.spec.ts +++ b/integration-tests/http/__tests__/returns/returns.spec.ts @@ -26,8 +26,8 @@ medusaIntegrationTestRunner({ items: [ { title: "Custom Item 2", - quantity: 1, - unit_price: 50, + quantity: 2, + unit_price: 25, }, ], sales_channel_id: "test", @@ -152,7 +152,7 @@ medusaIntegrationTestRunner({ items: [ { id: item.id, - quantity: 1, + quantity: 2, }, ], }, @@ -208,7 +208,7 @@ medusaIntegrationTestRunner({ items: [ { id: item.id, - quantity: 1, + quantity: 2, }, ], }, @@ -222,14 +222,15 @@ medusaIntegrationTestRunner({ expect.objectContaining({ id: expect.any(String), title: "Custom Item 2", - unit_price: 50, - quantity: 1, + unit_price: 25, + quantity: 2, subtotal: 50, total: 50, fulfilled_total: 50, return_requested_total: 50, detail: expect.objectContaining({ - return_requested_quantity: 1, + quantity: 2, + return_requested_quantity: 2, }), }), ]), @@ -284,6 +285,32 @@ medusaIntegrationTestRunner({ }) ) + // updated the requested quantitty + const updateReturnItemActionId = + result.data.order_preview.items[0].actions[0].id + result = await api.post( + `/admin/returns/${returnId}/request-items/${updateReturnItemActionId}`, + { + quantity: 2, + internal_note: "Test internal note", + }, + adminHeaders + ) + + expect(result.data.order_preview).toEqual( + expect.objectContaining({ + id: order.id, + items: expect.arrayContaining([ + expect.objectContaining({ + detail: expect.objectContaining({ + quantity: 2, + return_requested_quantity: 2, + }), + }), + ]), + }) + ) + result = await api.post( `/admin/returns/${returnId}/shipping-method`, { @@ -299,12 +326,20 @@ medusaIntegrationTestRunner({ expect.objectContaining({ id: expect.any(String), title: "Custom Item 2", - unit_price: 50, - quantity: 1, + unit_price: 25, + quantity: 2, subtotal: 50, total: 50, fulfilled_total: 50, return_requested_total: 50, + actions: expect.arrayContaining([ + expect.objectContaining({ + details: expect.objectContaining({ + quantity: 2, + }), + internal_note: "Test internal note", + }), + ]), }), ]), shipping_methods: expect.arrayContaining([ @@ -331,8 +366,8 @@ medusaIntegrationTestRunner({ expect.objectContaining({ id: expect.any(String), title: "Custom Item 2", - unit_price: 50, - quantity: 1, + unit_price: 25, + quantity: 2, subtotal: 50, total: 50, fulfilled_total: 50, @@ -372,6 +407,34 @@ medusaIntegrationTestRunner({ adminHeaders ) + // updates the shipping method price + const updateShippingActionId = + result.data.order_preview.shipping_methods[1].actions[0].id + result = await api.post( + `/admin/returns/${returnId}/shipping-method/${updateShippingActionId}`, + { + custom_price: 1002, + internal_note: "cx agent note", + }, + adminHeaders + ) + + expect(result.data.order_preview.shipping_methods).toHaveLength(2) + expect(result.data.order_preview.shipping_methods[1]).toEqual( + expect.objectContaining({ + id: expect.any(String), + name: "Return shipping", + amount: 1002, + subtotal: 1002, + total: 1002, + actions: [ + expect.objectContaining({ + internal_note: "cx agent note", + }), + ], + }) + ) + result = await api.post( `/admin/returns/${returnId}/request`, {}, @@ -385,8 +448,8 @@ medusaIntegrationTestRunner({ expect.objectContaining({ id: expect.any(String), title: "Custom Item 2", - unit_price: 50, - quantity: 1, + unit_price: 25, + quantity: 2, subtotal: 50, total: 50, fulfilled_total: 50, @@ -397,9 +460,9 @@ medusaIntegrationTestRunner({ expect.objectContaining({ id: expect.any(String), name: "Return shipping", - amount: 1000, - subtotal: 1000, - total: 1000, + amount: 1002, + subtotal: 1002, + total: 1002, }), ]), }) @@ -418,8 +481,8 @@ medusaIntegrationTestRunner({ expect.objectContaining({ id: expect.any(String), title: "Custom Item 2", - unit_price: 50, - quantity: 1, + unit_price: 25, + quantity: 2, subtotal: 50, total: 50, fulfilled_total: 50, @@ -430,9 +493,9 @@ medusaIntegrationTestRunner({ expect.objectContaining({ id: expect.any(String), name: "Return shipping", - amount: 1000, - subtotal: 1000, - total: 1000, + amount: 1002, + subtotal: 1002, + total: 1002, }), ]), }) diff --git a/packages/core/core-flows/src/order/steps/index.ts b/packages/core/core-flows/src/order/steps/index.ts index 51c42a21b5..9c6aff59ee 100644 --- a/packages/core/core-flows/src/order/steps/index.ts +++ b/packages/core/core-flows/src/order/steps/index.ts @@ -25,4 +25,5 @@ export * from "./register-shipment" export * from "./set-tax-lines-for-items" export * from "./update-order-change-actions" export * from "./update-order-exchanges" +export * from "./update-shipping-methods" export * from "./update-tax-lines" diff --git a/packages/core/core-flows/src/order/steps/update-shipping-methods.ts b/packages/core/core-flows/src/order/steps/update-shipping-methods.ts new file mode 100644 index 0000000000..6f628c8f90 --- /dev/null +++ b/packages/core/core-flows/src/order/steps/update-shipping-methods.ts @@ -0,0 +1,42 @@ +import { + IOrderModuleService, + UpdateOrderShippingMethodDTO, +} from "@medusajs/types" +import { + ModuleRegistrationName, + getSelectsAndRelationsFromObjectArray, +} from "@medusajs/utils" +import { StepResponse, createStep } from "@medusajs/workflows-sdk" + +export const updateOrderShippingMethodsStepId = "update-order-shopping-methods" +export const updateOrderShippingMethodsStep = createStep( + updateOrderShippingMethodsStepId, + async (data: UpdateOrderShippingMethodDTO[], { container }) => { + const service = container.resolve( + ModuleRegistrationName.ORDER + ) + + const { selects, relations } = getSelectsAndRelationsFromObjectArray(data, { + objectFields: ["metadata"], + }) + const dataBeforeUpdate = await service.listOrderClaims( + { id: data.map((d) => d.id) }, + { relations, select: selects } + ) + + const updated = await service.updateShippingMethods(data) + + return new StepResponse(updated, dataBeforeUpdate) + }, + async (dataBeforeUpdate, { container }) => { + if (!dataBeforeUpdate?.length) { + return + } + + const service = container.resolve( + ModuleRegistrationName.ORDER + ) + + await service.updateShippingMethods(dataBeforeUpdate) + } +) diff --git a/packages/core/core-flows/src/order/workflows/begin-order-claim.ts b/packages/core/core-flows/src/order/workflows/claim/begin-order-claim.ts similarity index 84% rename from packages/core/core-flows/src/order/workflows/begin-order-claim.ts rename to packages/core/core-flows/src/order/workflows/claim/begin-order-claim.ts index 4991f166e3..1866d76a1a 100644 --- a/packages/core/core-flows/src/order/workflows/begin-order-claim.ts +++ b/packages/core/core-flows/src/order/workflows/claim/begin-order-claim.ts @@ -5,10 +5,10 @@ import { createWorkflow, transform, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" -import { createOrderClaimsStep } from "../steps/create-claims" -import { createOrderChangeStep } from "../steps/create-order-change" -import { throwIfOrderIsCancelled } from "../utils/order-validation" +import { useRemoteQueryStep } from "../../../common" +import { createOrderClaimsStep } from "../../steps/create-claims" +import { createOrderChangeStep } from "../../steps/create-order-change" +import { throwIfOrderIsCancelled } from "../../utils/order-validation" const validationStep = createStep( "begin-claim-order-validation", diff --git a/packages/core/core-flows/src/order/workflows/claim-add-new-item.ts b/packages/core/core-flows/src/order/workflows/claim/claim-add-new-item.ts similarity index 90% rename from packages/core/core-flows/src/order/workflows/claim-add-new-item.ts rename to packages/core/core-flows/src/order/workflows/claim/claim-add-new-item.ts index 302ef11c29..53b59bd670 100644 --- a/packages/core/core-flows/src/order/workflows/claim-add-new-item.ts +++ b/packages/core/core-flows/src/order/workflows/claim/claim-add-new-item.ts @@ -11,15 +11,15 @@ import { createWorkflow, transform, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" -import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" -import { previewOrderChangeStep } from "../steps/preview-order-change" +import { useRemoteQueryStep } from "../../../common" +import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions" +import { previewOrderChangeStep } from "../../steps/preview-order-change" import { throwIfIsCancelled, throwIfOrderChangeIsNotActive, throwIfOrderIsCancelled, -} from "../utils/order-validation" -import { addOrderLineItemsWorkflow } from "./add-line-items" +} from "../../utils/order-validation" +import { addOrderLineItemsWorkflow } from "../add-line-items" const validationStep = createStep( "claim-add-new-item-validation", diff --git a/packages/core/core-flows/src/order/workflows/claim-request-item-return.ts b/packages/core/core-flows/src/order/workflows/claim/claim-request-item-return.ts similarity index 91% rename from packages/core/core-flows/src/order/workflows/claim-request-item-return.ts rename to packages/core/core-flows/src/order/workflows/claim/claim-request-item-return.ts index 98e5b424e9..7b9c72b1a2 100644 --- a/packages/core/core-flows/src/order/workflows/claim-request-item-return.ts +++ b/packages/core/core-flows/src/order/workflows/claim/claim-request-item-return.ts @@ -13,17 +13,17 @@ import { transform, when, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" -import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" -import { createReturnsStep } from "../steps/create-returns" -import { previewOrderChangeStep } from "../steps/preview-order-change" -import { updateOrderClaimsStep } from "../steps/update-order-claims" +import { useRemoteQueryStep } from "../../../common" +import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions" +import { createReturnsStep } from "../../steps/create-returns" +import { previewOrderChangeStep } from "../../steps/preview-order-change" +import { updateOrderClaimsStep } from "../../steps/update-order-claims" import { throwIfIsCancelled, throwIfItemsDoesNotExistsInOrder, throwIfOrderChangeIsNotActive, throwIfOrderIsCancelled, -} from "../utils/order-validation" +} from "../../utils/order-validation" const validationStep = createStep( "claim-request-item-return-validation", diff --git a/packages/core/core-flows/src/order/workflows/begin-order-exchange.ts b/packages/core/core-flows/src/order/workflows/exchange/begin-order-exchange.ts similarity index 84% rename from packages/core/core-flows/src/order/workflows/begin-order-exchange.ts rename to packages/core/core-flows/src/order/workflows/exchange/begin-order-exchange.ts index 3258c369b5..59e9962c0a 100644 --- a/packages/core/core-flows/src/order/workflows/begin-order-exchange.ts +++ b/packages/core/core-flows/src/order/workflows/exchange/begin-order-exchange.ts @@ -5,10 +5,10 @@ import { createWorkflow, transform, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" -import { createOrderExchangesStep } from "../steps/create-exchanges" -import { createOrderChangeStep } from "../steps/create-order-change" -import { throwIfOrderIsCancelled } from "../utils/order-validation" +import { useRemoteQueryStep } from "../../../common" +import { createOrderExchangesStep } from "../../steps/create-exchanges" +import { createOrderChangeStep } from "../../steps/create-order-change" +import { throwIfOrderIsCancelled } from "../../utils/order-validation" const validationStep = createStep( "begin-exchange-order-validation", diff --git a/packages/core/core-flows/src/order/workflows/create-exchange-return-shipping-method.ts b/packages/core/core-flows/src/order/workflows/exchange/create-exchange-return-shipping-method.ts similarity index 93% rename from packages/core/core-flows/src/order/workflows/create-exchange-return-shipping-method.ts rename to packages/core/core-flows/src/order/workflows/exchange/create-exchange-return-shipping-method.ts index 7843db7a6d..d31bd91ffa 100644 --- a/packages/core/core-flows/src/order/workflows/create-exchange-return-shipping-method.ts +++ b/packages/core/core-flows/src/order/workflows/exchange/create-exchange-return-shipping-method.ts @@ -6,18 +6,18 @@ import { } from "@medusajs/types" import { ChangeActionType } from "@medusajs/utils" import { + WorkflowData, createStep, createWorkflow, transform, - WorkflowData, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" -import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" -import { createOrderShippingMethods } from "../steps/create-order-shipping-methods" +import { useRemoteQueryStep } from "../../../common" +import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions" +import { createOrderShippingMethods } from "../../steps/create-order-shipping-methods" import { throwIfOrderChangeIsNotActive, throwIfOrderIsCancelled, -} from "../utils/order-validation" +} from "../../utils/order-validation" const validationStep = createStep( "validate-create-exchange-return-shipping-method", diff --git a/packages/core/core-flows/src/order/workflows/exchange-add-new-item.ts b/packages/core/core-flows/src/order/workflows/exchange/exchange-add-new-item.ts similarity index 90% rename from packages/core/core-flows/src/order/workflows/exchange-add-new-item.ts rename to packages/core/core-flows/src/order/workflows/exchange/exchange-add-new-item.ts index 288a3126ac..da97a3de31 100644 --- a/packages/core/core-flows/src/order/workflows/exchange-add-new-item.ts +++ b/packages/core/core-flows/src/order/workflows/exchange/exchange-add-new-item.ts @@ -11,15 +11,15 @@ import { createWorkflow, transform, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" -import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" -import { previewOrderChangeStep } from "../steps/preview-order-change" +import { useRemoteQueryStep } from "../../../common" +import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions" +import { previewOrderChangeStep } from "../../steps/preview-order-change" import { throwIfIsCancelled, throwIfOrderChangeIsNotActive, throwIfOrderIsCancelled, -} from "../utils/order-validation" -import { addOrderLineItemsWorkflow } from "./add-line-items" +} from "../../utils/order-validation" +import { addOrderLineItemsWorkflow } from "../add-line-items" const validationStep = createStep( "exchange-add-new-item-validation", diff --git a/packages/core/core-flows/src/order/workflows/exchange-request-item-return.ts b/packages/core/core-flows/src/order/workflows/exchange/exchange-request-item-return.ts similarity index 92% rename from packages/core/core-flows/src/order/workflows/exchange-request-item-return.ts rename to packages/core/core-flows/src/order/workflows/exchange/exchange-request-item-return.ts index 583ac6b53d..f2932368f9 100644 --- a/packages/core/core-flows/src/order/workflows/exchange-request-item-return.ts +++ b/packages/core/core-flows/src/order/workflows/exchange/exchange-request-item-return.ts @@ -13,17 +13,17 @@ import { transform, when, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" -import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" -import { createReturnsStep } from "../steps/create-returns" -import { previewOrderChangeStep } from "../steps/preview-order-change" -import { updateOrderExchangesStep } from "../steps/update-order-exchanges" +import { useRemoteQueryStep } from "../../../common" +import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions" +import { createReturnsStep } from "../../steps/create-returns" +import { previewOrderChangeStep } from "../../steps/preview-order-change" +import { updateOrderExchangesStep } from "../../steps/update-order-exchanges" import { throwIfIsCancelled, throwIfItemsDoesNotExistsInOrder, throwIfOrderChangeIsNotActive, throwIfOrderIsCancelled, -} from "../utils/order-validation" +} from "../../utils/order-validation" const validationStep = createStep( "exchange-request-item-return-validation", diff --git a/packages/core/core-flows/src/order/workflows/index.ts b/packages/core/core-flows/src/order/workflows/index.ts index d0d6287be0..8ebcff46ab 100644 --- a/packages/core/core-flows/src/order/workflows/index.ts +++ b/packages/core/core-flows/src/order/workflows/index.ts @@ -1,33 +1,35 @@ export * from "./add-line-items" export * from "./archive-orders" -export * from "./begin-order-claim" -export * from "./begin-order-exchange" -export * from "./begin-return" export * from "./cancel-order" export * from "./cancel-order-change" export * from "./cancel-order-fulfillment" -export * from "./cancel-return" -export * from "./claim-request-item-return" +export * from "./claim/begin-order-claim" +export * from "./claim/claim-request-item-return" export * from "./complete-orders" -export * from "./confirm-return-request" export * from "./create-complete-return" -export * from "./create-exchange-return-shipping-method" export * from "./create-fulfillment" export * from "./create-order-change" export * from "./create-order-change-actions" export * from "./create-orders" -export * from "./create-return-shipping-method" export * from "./create-shipment" export * from "./decline-order-change" export * from "./delete-order-change" export * from "./delete-order-change-actions" -export * from "./exchange-add-new-item" -export * from "./exchange-request-item-return" +export * from "./exchange/begin-order-exchange" +export * from "./exchange/create-exchange-return-shipping-method" +export * from "./exchange/exchange-add-new-item" +export * from "./exchange/exchange-request-item-return" export * from "./get-order-detail" export * from "./get-orders-list" -export * from "./receive-return" -export * from "./remove-request-item-return" -export * from "./remove-return-shipping-method" -export * from "./request-item-return" +export * from "./return/begin-return" +export * from "./return/cancel-return" +export * from "./return/confirm-return-request" +export * from "./return/create-return-shipping-method" +export * from "./return/receive-return" +export * from "./return/remove-request-item-return" +export * from "./return/remove-return-shipping-method" +export * from "./return/request-item-return" +export * from "./return/update-request-item-return" +export * from "./return/update-return-shipping-method" export * from "./update-order-change-actions" export * from "./update-tax-lines" diff --git a/packages/core/core-flows/src/order/workflows/begin-return.ts b/packages/core/core-flows/src/order/workflows/return/begin-return.ts similarity index 84% rename from packages/core/core-flows/src/order/workflows/begin-return.ts rename to packages/core/core-flows/src/order/workflows/return/begin-return.ts index 9dfefaef3b..f05fdf64e5 100644 --- a/packages/core/core-flows/src/order/workflows/begin-return.ts +++ b/packages/core/core-flows/src/order/workflows/return/begin-return.ts @@ -5,10 +5,10 @@ import { createWorkflow, transform, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" -import { createOrderChangeStep } from "../steps/create-order-change" -import { createReturnsStep } from "../steps/create-returns" -import { throwIfOrderIsCancelled } from "../utils/order-validation" +import { useRemoteQueryStep } from "../../../common" +import { createOrderChangeStep } from "../../steps/create-order-change" +import { createReturnsStep } from "../../steps/create-returns" +import { throwIfOrderIsCancelled } from "../../utils/order-validation" const validationStep = createStep( "begin-return-order-validation", diff --git a/packages/core/core-flows/src/order/workflows/cancel-return.ts b/packages/core/core-flows/src/order/workflows/return/cancel-return.ts similarity index 91% rename from packages/core/core-flows/src/order/workflows/cancel-return.ts rename to packages/core/core-flows/src/order/workflows/return/cancel-return.ts index f0f82f39e9..dab41ad843 100644 --- a/packages/core/core-flows/src/order/workflows/cancel-return.ts +++ b/packages/core/core-flows/src/order/workflows/return/cancel-return.ts @@ -10,9 +10,9 @@ import { createStep, createWorkflow, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" -import { cancelOrderReturnStep } from "../steps" -import { throwIfIsCancelled } from "../utils/order-validation" +import { useRemoteQueryStep } from "../../../common" +import { cancelOrderReturnStep } from "../../steps" +import { throwIfIsCancelled } from "../../utils/order-validation" const validateOrder = createStep( "validate-return", diff --git a/packages/core/core-flows/src/order/workflows/confirm-return-request.ts b/packages/core/core-flows/src/order/workflows/return/confirm-return-request.ts similarity index 89% rename from packages/core/core-flows/src/order/workflows/confirm-return-request.ts rename to packages/core/core-flows/src/order/workflows/return/confirm-return-request.ts index cb5d72c589..39a5359bb1 100644 --- a/packages/core/core-flows/src/order/workflows/confirm-return-request.ts +++ b/packages/core/core-flows/src/order/workflows/return/confirm-return-request.ts @@ -6,14 +6,14 @@ import { createWorkflow, 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 { useRemoteQueryStep } from "../../../common" +import { previewOrderChangeStep } from "../../steps" +import { confirmOrderChanges } from "../../steps/confirm-order-changes" +import { createReturnItems } from "../../steps/create-return-items" import { throwIfIsCancelled, throwIfOrderChangeIsNotActive, -} from "../utils/order-validation" +} from "../../utils/order-validation" type WorkflowInput = { return_id: string diff --git a/packages/core/core-flows/src/order/workflows/create-return-shipping-method.ts b/packages/core/core-flows/src/order/workflows/return/create-return-shipping-method.ts similarity index 93% rename from packages/core/core-flows/src/order/workflows/create-return-shipping-method.ts rename to packages/core/core-flows/src/order/workflows/return/create-return-shipping-method.ts index b50379c8d2..b1500ed48a 100644 --- a/packages/core/core-flows/src/order/workflows/create-return-shipping-method.ts +++ b/packages/core/core-flows/src/order/workflows/return/create-return-shipping-method.ts @@ -11,14 +11,14 @@ import { createWorkflow, 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 { useRemoteQueryStep } from "../../../common" +import { previewOrderChangeStep } from "../../steps" +import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions" +import { createOrderShippingMethods } from "../../steps/create-order-shipping-methods" import { throwIfIsCancelled, throwIfOrderChangeIsNotActive, -} from "../utils/order-validation" +} from "../../utils/order-validation" const validationStep = createStep( "validate-create-return-shipping-method", diff --git a/packages/core/core-flows/src/order/workflows/receive-return.ts b/packages/core/core-flows/src/order/workflows/return/receive-return.ts similarity index 89% rename from packages/core/core-flows/src/order/workflows/receive-return.ts rename to packages/core/core-flows/src/order/workflows/return/receive-return.ts index ee4dcdb4c5..b3011032da 100644 --- a/packages/core/core-flows/src/order/workflows/receive-return.ts +++ b/packages/core/core-flows/src/order/workflows/return/receive-return.ts @@ -4,14 +4,14 @@ import { createStep, createWorkflow, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" +import { useRemoteQueryStep } from "../../../common" import { ReturnDTO } from "@medusajs/types" -import { receiveReturnStep } from "../steps/receive-return" +import { receiveReturnStep } from "../../steps/receive-return" import { throwIfIsCancelled, throwIfItemsDoesNotExistsInReturn, -} from "../utils/order-validation" +} from "../../utils/order-validation" const validationStep = createStep( "receive-return-order-validation", diff --git a/packages/core/core-flows/src/order/workflows/remove-request-item-return.ts b/packages/core/core-flows/src/order/workflows/return/remove-request-item-return.ts similarity index 91% rename from packages/core/core-flows/src/order/workflows/remove-request-item-return.ts rename to packages/core/core-flows/src/order/workflows/return/remove-request-item-return.ts index 37866fd5ed..ec07903b47 100644 --- a/packages/core/core-flows/src/order/workflows/remove-request-item-return.ts +++ b/packages/core/core-flows/src/order/workflows/return/remove-request-item-return.ts @@ -11,12 +11,15 @@ import { createStep, createWorkflow, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" -import { deleteOrderChangeActionsStep, previewOrderChangeStep } from "../steps" +import { useRemoteQueryStep } from "../../../common" +import { + deleteOrderChangeActionsStep, + previewOrderChangeStep, +} from "../../steps" import { throwIfIsCancelled, throwIfOrderChangeIsNotActive, -} from "../utils/order-validation" +} from "../../utils/order-validation" const validationStep = createStep( "remove-request-item-return-validation", @@ -35,7 +38,7 @@ const validationStep = createStep( throwIfIsCancelled(orderReturn, "Return") throwIfOrderChangeIsNotActive({ orderChange }) - const associatedAction = orderChange.actions?.find( + const associatedAction = (orderChange.actions ?? []).find( (a) => a.id === input.action_id ) as OrderChangeActionDTO diff --git a/packages/core/core-flows/src/order/workflows/remove-return-shipping-method.ts b/packages/core/core-flows/src/order/workflows/return/remove-return-shipping-method.ts similarity index 82% rename from packages/core/core-flows/src/order/workflows/remove-return-shipping-method.ts rename to packages/core/core-flows/src/order/workflows/return/remove-return-shipping-method.ts index 57b0091bee..4e64c7cd1a 100644 --- a/packages/core/core-flows/src/order/workflows/remove-return-shipping-method.ts +++ b/packages/core/core-flows/src/order/workflows/return/remove-return-shipping-method.ts @@ -1,6 +1,7 @@ import { OrderChangeActionDTO, OrderChangeDTO, + OrderWorkflow, ReturnDTO, } from "@medusajs/types" import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils" @@ -11,14 +12,14 @@ import { parallelize, transform, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" -import { deleteOrderShippingMethods } from "../steps" -import { deleteOrderChangeActionsStep } from "../steps/delete-order-change-actions" -import { previewOrderChangeStep } from "../steps/preview-order-change" +import { useRemoteQueryStep } from "../../../common" +import { deleteOrderShippingMethods } from "../../steps" +import { deleteOrderChangeActionsStep } from "../../steps/delete-order-change-actions" +import { previewOrderChangeStep } from "../../steps/preview-order-change" import { throwIfIsCancelled, throwIfOrderChangeIsNotActive, -} from "../utils/order-validation" +} from "../../utils/order-validation" const validationStep = createStep( "validate-remove-return-shipping-method", @@ -34,7 +35,7 @@ const validationStep = createStep( throwIfIsCancelled(orderReturn, "Return") throwIfOrderChangeIsNotActive({ orderChange }) - const associatedAction = orderChange.actions?.find( + const associatedAction = (orderChange.actions ?? []).find( (a) => a.id === input.action_id ) as OrderChangeActionDTO @@ -54,7 +55,9 @@ export const removeReturnShippingMethodWorkflowId = "remove-return-shipping-method" export const removeReturnShippingMethodWorkflow = createWorkflow( removeReturnShippingMethodWorkflowId, - function (input: { return_id: string; action_id: string }): WorkflowData { + function ( + input: WorkflowData + ): WorkflowData { const orderReturn: ReturnDTO = useRemoteQueryStep({ entry_point: "return", fields: ["id", "status", "order_id"], @@ -81,7 +84,7 @@ export const removeReturnShippingMethodWorkflow = createWorkflow( const dataToRemove = transform( { orderChange, input }, ({ orderChange, input }) => { - const associatedAction = orderChange.actions?.find( + const associatedAction = (orderChange.actions ?? []).find( (a) => a.id === input.action_id ) as OrderChangeActionDTO diff --git a/packages/core/core-flows/src/order/workflows/request-item-return.ts b/packages/core/core-flows/src/order/workflows/return/request-item-return.ts similarity index 91% rename from packages/core/core-flows/src/order/workflows/request-item-return.ts rename to packages/core/core-flows/src/order/workflows/return/request-item-return.ts index 27c6077c5b..072cd373fd 100644 --- a/packages/core/core-flows/src/order/workflows/request-item-return.ts +++ b/packages/core/core-flows/src/order/workflows/return/request-item-return.ts @@ -11,14 +11,14 @@ import { createWorkflow, transform, } from "@medusajs/workflows-sdk" -import { useRemoteQueryStep } from "../../common" -import { previewOrderChangeStep } from "../steps" -import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" +import { useRemoteQueryStep } from "../../../common" +import { previewOrderChangeStep } from "../../steps" +import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions" import { throwIfIsCancelled, throwIfItemsDoesNotExistsInOrder, throwIfOrderChangeIsNotActive, -} from "../utils/order-validation" +} from "../../utils/order-validation" const validationStep = createStep( "request-item-return-validation", @@ -28,7 +28,7 @@ const validationStep = createStep( orderReturn, items, }: { - order: OrderDTO + order: Pick orderReturn: ReturnDTO orderChange: OrderChangeDTO items: OrderWorkflow.RequestItemReturnWorkflowInput["items"] diff --git a/packages/core/core-flows/src/order/workflows/return/update-request-item-return.ts b/packages/core/core-flows/src/order/workflows/return/update-request-item-return.ts new file mode 100644 index 0000000000..2db84323bf --- /dev/null +++ b/packages/core/core-flows/src/order/workflows/return/update-request-item-return.ts @@ -0,0 +1,117 @@ +import { + OrderChangeActionDTO, + OrderChangeDTO, + OrderDTO, + OrderWorkflow, + ReturnDTO, +} from "@medusajs/types" +import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils" +import { + WorkflowData, + createStep, + createWorkflow, + transform, +} from "@medusajs/workflows-sdk" +import { useRemoteQueryStep } from "../../../common" +import { + previewOrderChangeStep, + updateOrderChangeActionsStep, +} from "../../steps" +import { + throwIfIsCancelled, + throwIfOrderChangeIsNotActive, +} from "../../utils/order-validation" + +const validationStep = createStep( + "update-request-item-return-validation", + async function ({ + order, + orderChange, + orderReturn, + input, + }: { + order: OrderDTO + orderReturn: ReturnDTO + orderChange: OrderChangeDTO + input: OrderWorkflow.UpdateRequestItemReturnWorkflowInput + }) { + throwIfIsCancelled(order, "Order") + throwIfIsCancelled(orderReturn, "Return") + throwIfOrderChangeIsNotActive({ orderChange }) + + const associatedAction = (orderChange.actions ?? []).find( + (a) => a.id === input.action_id + ) as OrderChangeActionDTO + + if (!associatedAction) { + throw new Error( + `No request return found for return ${input.return_id} in order change ${orderChange.id}` + ) + } else if (associatedAction.action !== ChangeActionType.RETURN_ITEM) { + throw new Error( + `Action ${associatedAction.id} is not requesting item return` + ) + } + } +) + +export const updateRequestItemReturnWorkflowId = "update-request-item-return" +export const updateRequestItemReturnWorkflow = createWorkflow( + updateRequestItemReturnWorkflowId, + function ( + input: WorkflowData + ): WorkflowData { + const orderReturn: ReturnDTO = useRemoteQueryStep({ + entry_point: "return", + fields: ["id", "status", "order_id"], + variables: { id: input.return_id }, + list: false, + throw_if_key_not_found: true, + }) + + const order: OrderDTO = useRemoteQueryStep({ + entry_point: "orders", + fields: ["id", "status", "items.*"], + variables: { id: orderReturn.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: orderReturn.order_id, + return_id: orderReturn.id, + status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED], + }, + }, + list: false, + }).config({ name: "order-change-query" }) + + validationStep({ order, input, orderReturn, orderChange }) + + const updateData = transform( + { orderChange, input }, + ({ input, orderChange }) => { + const originalAction = (orderChange.actions ?? []).find( + (a) => a.id === input.action_id + ) as OrderChangeActionDTO + + const data = input.data + return { + id: input.action_id, + details: { + quantity: data.quantity ?? originalAction.details?.quantity, + }, + internal_note: data.internal_note, + } + } + ) + + updateOrderChangeActionsStep([updateData]) + + return previewOrderChangeStep(order.id) + } +) diff --git a/packages/core/core-flows/src/order/workflows/return/update-return-shipping-method.ts b/packages/core/core-flows/src/order/workflows/return/update-return-shipping-method.ts new file mode 100644 index 0000000000..3b03f61990 --- /dev/null +++ b/packages/core/core-flows/src/order/workflows/return/update-return-shipping-method.ts @@ -0,0 +1,120 @@ +import { + OrderChangeActionDTO, + OrderChangeDTO, + OrderWorkflow, + ReturnDTO, +} from "@medusajs/types" +import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils" +import { + WorkflowData, + createStep, + createWorkflow, + parallelize, + transform, +} from "@medusajs/workflows-sdk" +import { useRemoteQueryStep } from "../../../common" +import { + updateOrderChangeActionsStep, + updateOrderShippingMethodsStep, +} from "../../steps" +import { previewOrderChangeStep } from "../../steps/preview-order-change" +import { + throwIfIsCancelled, + throwIfOrderChangeIsNotActive, +} from "../../utils/order-validation" + +const validationStep = createStep( + "validate-update-return-shipping-method", + async function ({ + orderChange, + orderReturn, + input, + }: { + input: { return_id: string; action_id: string } + orderReturn: ReturnDTO + orderChange: OrderChangeDTO + }) { + throwIfIsCancelled(orderReturn, "Return") + throwIfOrderChangeIsNotActive({ orderChange }) + + const associatedAction = (orderChange.actions ?? []).find( + (a) => a.id === input.action_id + ) as OrderChangeActionDTO + + if (!associatedAction) { + throw new Error( + `No shipping method found for return ${input.return_id} in order change ${orderChange.id}` + ) + } else if (associatedAction.action !== ChangeActionType.SHIPPING_ADD) { + throw new Error( + `Action ${associatedAction.id} is not adding a shipping method` + ) + } + } +) + +export const updateReturnShippingMethodWorkflowId = + "update-return-shipping-method" +export const updateReturnShippingMethodWorkflow = createWorkflow( + updateReturnShippingMethodWorkflowId, + function ( + input: WorkflowData + ): WorkflowData { + const orderReturn: ReturnDTO = useRemoteQueryStep({ + entry_point: "return", + fields: ["id", "status", "order_id"], + variables: { id: input.return_id }, + list: false, + throw_if_key_not_found: true, + }) + + const orderChange: OrderChangeDTO = useRemoteQueryStep({ + entry_point: "order_change", + fields: ["id", "status", "version", "actions.*"], + variables: { + filters: { + order_id: orderReturn.order_id, + return_id: orderReturn.id, + status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED], + }, + }, + list: false, + }).config({ name: "order-change-query" }) + + validationStep({ orderReturn, orderChange, input }) + + const updateData = transform( + { orderChange, input }, + ({ input, orderChange }) => { + const originalAction = (orderChange.actions ?? []).find( + (a) => a.id === input.action_id + ) as OrderChangeActionDTO + + const data = input.data + + const action = { + id: originalAction.id, + internal_note: data.internal_note, + } + + const shippingMethod = { + id: originalAction.reference_id, + amount: data.custom_price, + metadata: data.metadata, + } + + return { + action, + shippingMethod, + } + } + ) + + parallelize( + updateOrderChangeActionsStep([updateData.action]), + updateOrderShippingMethodsStep([updateData.shippingMethod!]) + ) + + return previewOrderChangeStep(orderReturn.order_id) + } +) diff --git a/packages/core/types/src/order/mutations.ts b/packages/core/types/src/order/mutations.ts index 4a5af2c171..02c8302104 100644 --- a/packages/core/types/src/order/mutations.ts +++ b/packages/core/types/src/order/mutations.ts @@ -326,7 +326,6 @@ export interface CreateOrderChangeActionDTO { export interface UpdateOrderChangeActionDTO { id: string internal_note?: string | null - metadata?: Record | null } /** ORDER TRANSACTION START */ diff --git a/packages/core/types/src/order/service.ts b/packages/core/types/src/order/service.ts index 3e57f59fba..0506826864 100644 --- a/packages/core/types/src/order/service.ts +++ b/packages/core/types/src/order/service.ts @@ -78,6 +78,7 @@ import { UpdateOrderReturnReasonWithSelectorDTO, UpdateOrderReturnWithSelectorDTO, UpdateOrderShippingMethodAdjustmentDTO, + UpdateOrderShippingMethodDTO, UpdateOrderShippingMethodTaxLineDTO, UpdateReturnDTO, UpsertOrderLineItemAdjustmentDTO, @@ -764,6 +765,16 @@ export interface IOrderModuleService extends IModuleService { sharedContext?: Context ): Promise + updateShippingMethods( + data: UpdateOrderShippingMethodDTO[], + sharedContext?: Context + ): Promise + + updateShippingMethods( + data: UpdateOrderShippingMethodDTO, + sharedContext?: Context + ): Promise + deleteShippingMethods( methodIds: string[], sharedContext?: Context diff --git a/packages/core/types/src/workflow/order/index.ts b/packages/core/types/src/workflow/order/index.ts index b03d27c074..7a0b5cdee8 100644 --- a/packages/core/types/src/workflow/order/index.ts +++ b/packages/core/types/src/workflow/order/index.ts @@ -12,3 +12,4 @@ export * from "./create-return-order" export * from "./create-shipment" export * from "./receive-return" export * from "./request-item-return" +export * from "./shipping-return" diff --git a/packages/core/types/src/workflow/order/request-item-return.ts b/packages/core/types/src/workflow/order/request-item-return.ts index 19d09d8e68..8aec8b4b75 100644 --- a/packages/core/types/src/workflow/order/request-item-return.ts +++ b/packages/core/types/src/workflow/order/request-item-return.ts @@ -1,3 +1,4 @@ +import { BigNumberInput } from "../../totals" import { CreateReturnItem } from "./create-return-order" export interface RequestItemReturnWorkflowInput { @@ -9,6 +10,15 @@ export interface DeleteRequestItemReturnWorkflowInput { action_id: string } +export interface UpdateRequestItemReturnWorkflowInput { + return_id: string + action_id: string + data: { + quantity?: BigNumberInput + internal_note?: string | null + } +} + export interface OrderExchangeRequestItemReturnWorkflowInput { exchange_id: string items: CreateReturnItem[] diff --git a/packages/core/types/src/workflow/order/shipping-return.ts b/packages/core/types/src/workflow/order/shipping-return.ts new file mode 100644 index 0000000000..25160b5169 --- /dev/null +++ b/packages/core/types/src/workflow/order/shipping-return.ts @@ -0,0 +1,16 @@ +import { BigNumberInput } from "../../totals" + +export interface UpdateReturnShippingMethodWorkflowInput { + return_id: string + action_id: string + data: { + custom_price?: BigNumberInput + internal_note?: string | null + metadata?: Record | null + } +} + +export interface DeleteReturnShippingMethodWorkflowInput { + return_id: string + action_id: string +} diff --git a/packages/medusa/src/api/admin/returns/[id]/request-items/[action_id]/route.ts b/packages/medusa/src/api/admin/returns/[id]/request-items/[action_id]/route.ts index ecde753266..9ae224fc53 100644 --- a/packages/medusa/src/api/admin/returns/[id]/request-items/[action_id]/route.ts +++ b/packages/medusa/src/api/admin/returns/[id]/request-items/[action_id]/route.ts @@ -1,4 +1,7 @@ -import { removeRequestItemReturnWorkflow } from "@medusajs/core-flows" +import { + removeRequestItemReturnWorkflow, + updateRequestItemReturnWorkflow, +} from "@medusajs/core-flows" import { ContainerRegistrationKeys, remoteQueryObjectFromString, @@ -7,6 +10,42 @@ import { AuthenticatedMedusaRequest, MedusaResponse, } from "../../../../../../types/routing" +import { AdminPostReturnsRequestItemsActionReqSchemaType } from "../../../validators" + +export const POST = async ( + req: AuthenticatedMedusaRequest, + res: MedusaResponse +) => { + const { id, action_id } = req.params + + const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) + + const { result } = await updateRequestItemReturnWorkflow(req.scope).run({ + input: { + data: { ...req.validatedBody }, + return_id: id, + action_id, + }, + }) + + const queryObject = remoteQueryObjectFromString({ + entryPoint: "return", + variables: { + id, + filters: { + ...req.filterableFields, + }, + }, + fields: req.remoteQueryConfig.fields, + }) + + const [orderReturn] = await remoteQuery(queryObject) + + res.json({ + order_preview: result, + return: orderReturn, + }) +} export const DELETE = async ( req: AuthenticatedMedusaRequest, diff --git a/packages/medusa/src/api/admin/returns/[id]/shipping-method/[action_id]/route.ts b/packages/medusa/src/api/admin/returns/[id]/shipping-method/[action_id]/route.ts index 3601408b83..1f3c2a452c 100644 --- a/packages/medusa/src/api/admin/returns/[id]/shipping-method/[action_id]/route.ts +++ b/packages/medusa/src/api/admin/returns/[id]/shipping-method/[action_id]/route.ts @@ -1,4 +1,7 @@ -import { removeReturnShippingMethodWorkflow } from "@medusajs/core-flows" +import { + removeReturnShippingMethodWorkflow, + updateReturnShippingMethodWorkflow, +} from "@medusajs/core-flows" import { ContainerRegistrationKeys, remoteQueryObjectFromString, @@ -7,6 +10,42 @@ import { AuthenticatedMedusaRequest, MedusaResponse, } from "../../../../../../types/routing" +import { AdminPostReturnsShippingActionReqSchemaType } from "../../../validators" + +export const POST = async ( + req: AuthenticatedMedusaRequest, + res: MedusaResponse +) => { + const { id, action_id } = req.params + + const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) + + const { result } = await updateReturnShippingMethodWorkflow(req.scope).run({ + input: { + data: { ...req.validatedBody }, + return_id: id, + action_id, + }, + }) + + const queryObject = remoteQueryObjectFromString({ + entryPoint: "return", + variables: { + id, + filters: { + ...req.filterableFields, + }, + }, + fields: req.remoteQueryConfig.fields, + }) + + const [orderReturn] = await remoteQuery(queryObject) + + res.json({ + order_preview: result, + return: orderReturn, + }) +} export const DELETE = async ( req: AuthenticatedMedusaRequest, diff --git a/packages/medusa/src/api/admin/returns/middlewares.ts b/packages/medusa/src/api/admin/returns/middlewares.ts index ff3b3b1591..d65aa2ac72 100644 --- a/packages/medusa/src/api/admin/returns/middlewares.ts +++ b/packages/medusa/src/api/admin/returns/middlewares.ts @@ -7,7 +7,9 @@ import { AdminGetOrdersParams, AdminPostReturnsConfirmRequestReqSchema, AdminPostReturnsReqSchema, + AdminPostReturnsRequestItemsActionReqSchema, AdminPostReturnsRequestItemsReqSchema, + AdminPostReturnsShippingActionReqSchema, AdminPostReturnsShippingReqSchema, } from "./validators" @@ -54,6 +56,17 @@ export const adminReturnRoutesMiddlewares: MiddlewareRoute[] = [ ), ], }, + { + method: ["POST"], + matcher: "/admin/returns/:id/request-items/:action_id", + middlewares: [ + validateAndTransformBody(AdminPostReturnsRequestItemsActionReqSchema), + validateAndTransformQuery( + AdminGetOrdersOrderParams, + QueryConfig.retrieveTransformQueryConfig + ), + ], + }, { method: ["DELETE"], matcher: "/admin/returns/:id/request-items/:action_id", @@ -75,6 +88,17 @@ export const adminReturnRoutesMiddlewares: MiddlewareRoute[] = [ ), ], }, + { + method: ["POST"], + matcher: "/admin/returns/:id/shipping-method/:action_id", + middlewares: [ + validateAndTransformBody(AdminPostReturnsShippingActionReqSchema), + validateAndTransformQuery( + AdminGetOrdersOrderParams, + QueryConfig.retrieveTransformQueryConfig + ), + ], + }, { method: ["DELETE"], matcher: "/admin/returns/:id/shipping-method/:action_id", diff --git a/packages/medusa/src/api/admin/returns/validators.ts b/packages/medusa/src/api/admin/returns/validators.ts index bf01628a80..69473b8d44 100644 --- a/packages/medusa/src/api/admin/returns/validators.ts +++ b/packages/medusa/src/api/admin/returns/validators.ts @@ -108,6 +108,16 @@ export type AdminPostReturnsShippingReqSchemaType = z.infer< typeof AdminPostReturnsShippingReqSchema > +export const AdminPostReturnsShippingActionReqSchema = z.object({ + custom_price: z.number().optional(), + internal_note: z.string().nullish().optional(), + metadata: z.record(z.unknown()).nullish().optional(), +}) + +export type AdminPostReturnsShippingActionReqSchemaType = z.infer< + typeof AdminPostReturnsShippingActionReqSchema +> + export const AdminPostReturnsRequestItemsReqSchema = z.object({ items: z.array( z.object({ @@ -124,6 +134,15 @@ export type AdminPostReturnsRequestItemsReqSchemaType = z.infer< typeof AdminPostReturnsRequestItemsReqSchema > +export const AdminPostReturnsRequestItemsActionReqSchema = z.object({ + quantity: z.number().optional(), + internal_note: z.string().nullish().optional(), +}) + +export type AdminPostReturnsRequestItemsActionReqSchemaType = z.infer< + typeof AdminPostReturnsRequestItemsActionReqSchema +> + export const AdminPostReturnsConfirmRequestReqSchema = z.object({ no_notification: z.boolean().optional(), })