chore(medusa,core-flows): update request return items and shipping (#8151)

This commit is contained in:
Carlos R. L. Rodrigues authored and GitHub committed 2024-07-16 15:23:28 -03:00
1 parent f73ca97ecc
commit 23053afe95
30 files changed
+619 -110

No files matched your search

@@ -26,8 +26,8 @@ medusaIntegrationTestRunner({
items: [ items: [
{ {
title: "Custom Item 2", title: "Custom Item 2",
quantity: 1, quantity: 2,
unit_price: 50, unit_price: 25,
}, },
], ],
sales_channel_id: "test", sales_channel_id: "test",
@@ -152,7 +152,7 @@ medusaIntegrationTestRunner({
items: [ items: [
{ {
id: item.id, id: item.id,
quantity: 1, quantity: 2,
}, },
], ],
}, },
@@ -208,7 +208,7 @@ medusaIntegrationTestRunner({
items: [ items: [
{ {
id: item.id, id: item.id,
quantity: 1, quantity: 2,
}, },
], ],
}, },
@@ -222,14 +222,15 @@ medusaIntegrationTestRunner({
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
title: "Custom Item 2", title: "Custom Item 2",
unit_price: 50, unit_price: 25,
quantity: 1, quantity: 2,
subtotal: 50, subtotal: 50,
total: 50, total: 50,
fulfilled_total: 50, fulfilled_total: 50,
return_requested_total: 50, return_requested_total: 50,
detail: expect.objectContaining({ 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( result = await api.post(
`/admin/returns/${returnId}/shipping-method`, `/admin/returns/${returnId}/shipping-method`,
{ {
@@ -299,12 +326,20 @@ medusaIntegrationTestRunner({
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
title: "Custom Item 2", title: "Custom Item 2",
unit_price: 50, unit_price: 25,
quantity: 1, quantity: 2,
subtotal: 50, subtotal: 50,
total: 50, total: 50,
fulfilled_total: 50, fulfilled_total: 50,
return_requested_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([ shipping_methods: expect.arrayContaining([
@@ -331,8 +366,8 @@ medusaIntegrationTestRunner({
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
title: "Custom Item 2", title: "Custom Item 2",
unit_price: 50, unit_price: 25,
quantity: 1, quantity: 2,
subtotal: 50, subtotal: 50,
total: 50, total: 50,
fulfilled_total: 50, fulfilled_total: 50,
@@ -372,6 +407,34 @@ medusaIntegrationTestRunner({
adminHeaders 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( result = await api.post(
`/admin/returns/${returnId}/request`, `/admin/returns/${returnId}/request`,
{}, {},
@@ -385,8 +448,8 @@ medusaIntegrationTestRunner({
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
title: "Custom Item 2", title: "Custom Item 2",
unit_price: 50, unit_price: 25,
quantity: 1, quantity: 2,
subtotal: 50, subtotal: 50,
total: 50, total: 50,
fulfilled_total: 50, fulfilled_total: 50,
@@ -397,9 +460,9 @@ medusaIntegrationTestRunner({
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
name: "Return shipping", name: "Return shipping",
amount: 1000, amount: 1002,
subtotal: 1000, subtotal: 1002,
total: 1000, total: 1002,
}), }),
]), ]),
}) })
@@ -418,8 +481,8 @@ medusaIntegrationTestRunner({
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
title: "Custom Item 2", title: "Custom Item 2",
unit_price: 50, unit_price: 25,
quantity: 1, quantity: 2,
subtotal: 50, subtotal: 50,
total: 50, total: 50,
fulfilled_total: 50, fulfilled_total: 50,
@@ -430,9 +493,9 @@ medusaIntegrationTestRunner({
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
name: "Return shipping", name: "Return shipping",
amount: 1000, amount: 1002,
subtotal: 1000, subtotal: 1002,
total: 1000, total: 1002,
}), }),
]), ]),
}) })
@@ -25,4 +25,5 @@ export * from "./register-shipment"
export * from "./set-tax-lines-for-items" export * from "./set-tax-lines-for-items"
export * from "./update-order-change-actions" export * from "./update-order-change-actions"
export * from "./update-order-exchanges" export * from "./update-order-exchanges"
export * from "./update-shipping-methods"
export * from "./update-tax-lines" export * from "./update-tax-lines"
@@ -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<IOrderModuleService>(
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<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
await service.updateShippingMethods(dataBeforeUpdate)
}
)
@@ -5,10 +5,10 @@ import {
createWorkflow, createWorkflow,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { createOrderClaimsStep } from "../steps/create-claims" import { createOrderClaimsStep } from "../../steps/create-claims"
import { createOrderChangeStep } from "../steps/create-order-change" import { createOrderChangeStep } from "../../steps/create-order-change"
import { throwIfOrderIsCancelled } from "../utils/order-validation" import { throwIfOrderIsCancelled } from "../../utils/order-validation"
const validationStep = createStep( const validationStep = createStep(
"begin-claim-order-validation", "begin-claim-order-validation",
@@ -11,15 +11,15 @@ import {
createWorkflow, createWorkflow,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions"
import { previewOrderChangeStep } from "../steps/preview-order-change" import { previewOrderChangeStep } from "../../steps/preview-order-change"
import { import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
throwIfOrderIsCancelled, throwIfOrderIsCancelled,
} from "../utils/order-validation" } from "../../utils/order-validation"
import { addOrderLineItemsWorkflow } from "./add-line-items" import { addOrderLineItemsWorkflow } from "../add-line-items"
const validationStep = createStep( const validationStep = createStep(
"claim-add-new-item-validation", "claim-add-new-item-validation",
@@ -13,17 +13,17 @@ import {
transform, transform,
when, when,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions"
import { createReturnsStep } from "../steps/create-returns" import { createReturnsStep } from "../../steps/create-returns"
import { previewOrderChangeStep } from "../steps/preview-order-change" import { previewOrderChangeStep } from "../../steps/preview-order-change"
import { updateOrderClaimsStep } from "../steps/update-order-claims" import { updateOrderClaimsStep } from "../../steps/update-order-claims"
import { import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfItemsDoesNotExistsInOrder, throwIfItemsDoesNotExistsInOrder,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
throwIfOrderIsCancelled, throwIfOrderIsCancelled,
} from "../utils/order-validation" } from "../../utils/order-validation"
const validationStep = createStep( const validationStep = createStep(
"claim-request-item-return-validation", "claim-request-item-return-validation",
@@ -5,10 +5,10 @@ import {
createWorkflow, createWorkflow,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { createOrderExchangesStep } from "../steps/create-exchanges" import { createOrderExchangesStep } from "../../steps/create-exchanges"
import { createOrderChangeStep } from "../steps/create-order-change" import { createOrderChangeStep } from "../../steps/create-order-change"
import { throwIfOrderIsCancelled } from "../utils/order-validation" import { throwIfOrderIsCancelled } from "../../utils/order-validation"
const validationStep = createStep( const validationStep = createStep(
"begin-exchange-order-validation", "begin-exchange-order-validation",
@@ -6,18 +6,18 @@ import {
} from "@medusajs/types" } from "@medusajs/types"
import { ChangeActionType } from "@medusajs/utils" import { ChangeActionType } from "@medusajs/utils"
import { import {
WorkflowData,
createStep, createStep,
createWorkflow, createWorkflow,
transform, transform,
WorkflowData,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions"
import { createOrderShippingMethods } from "../steps/create-order-shipping-methods" import { createOrderShippingMethods } from "../../steps/create-order-shipping-methods"
import { import {
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
throwIfOrderIsCancelled, throwIfOrderIsCancelled,
} from "../utils/order-validation" } from "../../utils/order-validation"
const validationStep = createStep( const validationStep = createStep(
"validate-create-exchange-return-shipping-method", "validate-create-exchange-return-shipping-method",
@@ -11,15 +11,15 @@ import {
createWorkflow, createWorkflow,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions"
import { previewOrderChangeStep } from "../steps/preview-order-change" import { previewOrderChangeStep } from "../../steps/preview-order-change"
import { import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
throwIfOrderIsCancelled, throwIfOrderIsCancelled,
} from "../utils/order-validation" } from "../../utils/order-validation"
import { addOrderLineItemsWorkflow } from "./add-line-items" import { addOrderLineItemsWorkflow } from "../add-line-items"
const validationStep = createStep( const validationStep = createStep(
"exchange-add-new-item-validation", "exchange-add-new-item-validation",
@@ -13,17 +13,17 @@ import {
transform, transform,
when, when,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions"
import { createReturnsStep } from "../steps/create-returns" import { createReturnsStep } from "../../steps/create-returns"
import { previewOrderChangeStep } from "../steps/preview-order-change" import { previewOrderChangeStep } from "../../steps/preview-order-change"
import { updateOrderExchangesStep } from "../steps/update-order-exchanges" import { updateOrderExchangesStep } from "../../steps/update-order-exchanges"
import { import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfItemsDoesNotExistsInOrder, throwIfItemsDoesNotExistsInOrder,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
throwIfOrderIsCancelled, throwIfOrderIsCancelled,
} from "../utils/order-validation" } from "../../utils/order-validation"
const validationStep = createStep( const validationStep = createStep(
"exchange-request-item-return-validation", "exchange-request-item-return-validation",
@@ -1,33 +1,35 @@
export * from "./add-line-items" export * from "./add-line-items"
export * from "./archive-orders" 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"
export * from "./cancel-order-change" export * from "./cancel-order-change"
export * from "./cancel-order-fulfillment" export * from "./cancel-order-fulfillment"
export * from "./cancel-return" export * from "./claim/begin-order-claim"
export * from "./claim-request-item-return" export * from "./claim/claim-request-item-return"
export * from "./complete-orders" export * from "./complete-orders"
export * from "./confirm-return-request"
export * from "./create-complete-return" export * from "./create-complete-return"
export * from "./create-exchange-return-shipping-method"
export * from "./create-fulfillment" export * from "./create-fulfillment"
export * from "./create-order-change" export * from "./create-order-change"
export * from "./create-order-change-actions" export * from "./create-order-change-actions"
export * from "./create-orders" export * from "./create-orders"
export * from "./create-return-shipping-method"
export * from "./create-shipment" export * from "./create-shipment"
export * from "./decline-order-change" export * from "./decline-order-change"
export * from "./delete-order-change" export * from "./delete-order-change"
export * from "./delete-order-change-actions" export * from "./delete-order-change-actions"
export * from "./exchange-add-new-item" export * from "./exchange/begin-order-exchange"
export * from "./exchange-request-item-return" 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-order-detail"
export * from "./get-orders-list" export * from "./get-orders-list"
export * from "./receive-return" export * from "./return/begin-return"
export * from "./remove-request-item-return" export * from "./return/cancel-return"
export * from "./remove-return-shipping-method" export * from "./return/confirm-return-request"
export * from "./request-item-return" 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-order-change-actions"
export * from "./update-tax-lines" export * from "./update-tax-lines"
@@ -5,10 +5,10 @@ import {
createWorkflow, createWorkflow,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { createOrderChangeStep } from "../steps/create-order-change" import { createOrderChangeStep } from "../../steps/create-order-change"
import { createReturnsStep } from "../steps/create-returns" import { createReturnsStep } from "../../steps/create-returns"
import { throwIfOrderIsCancelled } from "../utils/order-validation" import { throwIfOrderIsCancelled } from "../../utils/order-validation"
const validationStep = createStep( const validationStep = createStep(
"begin-return-order-validation", "begin-return-order-validation",
@@ -10,9 +10,9 @@ import {
createStep, createStep,
createWorkflow, createWorkflow,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { cancelOrderReturnStep } from "../steps" import { cancelOrderReturnStep } from "../../steps"
import { throwIfIsCancelled } from "../utils/order-validation" import { throwIfIsCancelled } from "../../utils/order-validation"
const validateOrder = createStep( const validateOrder = createStep(
"validate-return", "validate-return",
@@ -6,14 +6,14 @@ import {
createWorkflow, createWorkflow,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { previewOrderChangeStep } from "../steps" import { previewOrderChangeStep } from "../../steps"
import { confirmOrderChanges } from "../steps/confirm-order-changes" import { confirmOrderChanges } from "../../steps/confirm-order-changes"
import { createReturnItems } from "../steps/create-return-items" import { createReturnItems } from "../../steps/create-return-items"
import { import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
} from "../utils/order-validation" } from "../../utils/order-validation"
type WorkflowInput = { type WorkflowInput = {
return_id: string return_id: string
@@ -11,14 +11,14 @@ import {
createWorkflow, createWorkflow,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { previewOrderChangeStep } from "../steps" import { previewOrderChangeStep } from "../../steps"
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions"
import { createOrderShippingMethods } from "../steps/create-order-shipping-methods" import { createOrderShippingMethods } from "../../steps/create-order-shipping-methods"
import { import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
} from "../utils/order-validation" } from "../../utils/order-validation"
const validationStep = createStep( const validationStep = createStep(
"validate-create-return-shipping-method", "validate-create-return-shipping-method",
@@ -4,14 +4,14 @@ import {
createStep, createStep,
createWorkflow, createWorkflow,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { ReturnDTO } from "@medusajs/types" import { ReturnDTO } from "@medusajs/types"
import { receiveReturnStep } from "../steps/receive-return" import { receiveReturnStep } from "../../steps/receive-return"
import { import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfItemsDoesNotExistsInReturn, throwIfItemsDoesNotExistsInReturn,
} from "../utils/order-validation" } from "../../utils/order-validation"
const validationStep = createStep( const validationStep = createStep(
"receive-return-order-validation", "receive-return-order-validation",
@@ -11,12 +11,15 @@ import {
createStep, createStep,
createWorkflow, createWorkflow,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { deleteOrderChangeActionsStep, previewOrderChangeStep } from "../steps" import {
deleteOrderChangeActionsStep,
previewOrderChangeStep,
} from "../../steps"
import { import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
} from "../utils/order-validation" } from "../../utils/order-validation"
const validationStep = createStep( const validationStep = createStep(
"remove-request-item-return-validation", "remove-request-item-return-validation",
@@ -35,7 +38,7 @@ const validationStep = createStep(
throwIfIsCancelled(orderReturn, "Return") throwIfIsCancelled(orderReturn, "Return")
throwIfOrderChangeIsNotActive({ orderChange }) throwIfOrderChangeIsNotActive({ orderChange })
const associatedAction = orderChange.actions?.find( const associatedAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id (a) => a.id === input.action_id
) as OrderChangeActionDTO ) as OrderChangeActionDTO
@@ -1,6 +1,7 @@
import { import {
OrderChangeActionDTO, OrderChangeActionDTO,
OrderChangeDTO, OrderChangeDTO,
OrderWorkflow,
ReturnDTO, ReturnDTO,
} from "@medusajs/types" } from "@medusajs/types"
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils" import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils"
@@ -11,14 +12,14 @@ import {
parallelize, parallelize,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { deleteOrderShippingMethods } from "../steps" import { deleteOrderShippingMethods } from "../../steps"
import { deleteOrderChangeActionsStep } from "../steps/delete-order-change-actions" import { deleteOrderChangeActionsStep } from "../../steps/delete-order-change-actions"
import { previewOrderChangeStep } from "../steps/preview-order-change" import { previewOrderChangeStep } from "../../steps/preview-order-change"
import { import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
} from "../utils/order-validation" } from "../../utils/order-validation"
const validationStep = createStep( const validationStep = createStep(
"validate-remove-return-shipping-method", "validate-remove-return-shipping-method",
@@ -34,7 +35,7 @@ const validationStep = createStep(
throwIfIsCancelled(orderReturn, "Return") throwIfIsCancelled(orderReturn, "Return")
throwIfOrderChangeIsNotActive({ orderChange }) throwIfOrderChangeIsNotActive({ orderChange })
const associatedAction = orderChange.actions?.find( const associatedAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id (a) => a.id === input.action_id
) as OrderChangeActionDTO ) as OrderChangeActionDTO
@@ -54,7 +55,9 @@ export const removeReturnShippingMethodWorkflowId =
"remove-return-shipping-method" "remove-return-shipping-method"
export const removeReturnShippingMethodWorkflow = createWorkflow( export const removeReturnShippingMethodWorkflow = createWorkflow(
removeReturnShippingMethodWorkflowId, removeReturnShippingMethodWorkflowId,
function (input: { return_id: string; action_id: string }): WorkflowData { function (
input: WorkflowData<OrderWorkflow.DeleteReturnShippingMethodWorkflowInput>
): WorkflowData {
const orderReturn: ReturnDTO = useRemoteQueryStep({ const orderReturn: ReturnDTO = useRemoteQueryStep({
entry_point: "return", entry_point: "return",
fields: ["id", "status", "order_id"], fields: ["id", "status", "order_id"],
@@ -81,7 +84,7 @@ export const removeReturnShippingMethodWorkflow = createWorkflow(
const dataToRemove = transform( const dataToRemove = transform(
{ orderChange, input }, { orderChange, input },
({ orderChange, input }) => { ({ orderChange, input }) => {
const associatedAction = orderChange.actions?.find( const associatedAction = (orderChange.actions ?? []).find(
(a) => a.id === input.action_id (a) => a.id === input.action_id
) as OrderChangeActionDTO ) as OrderChangeActionDTO
@@ -11,14 +11,14 @@ import {
createWorkflow, createWorkflow,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../../common"
import { previewOrderChangeStep } from "../steps" import { previewOrderChangeStep } from "../../steps"
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions" import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions"
import { import {
throwIfIsCancelled, throwIfIsCancelled,
throwIfItemsDoesNotExistsInOrder, throwIfItemsDoesNotExistsInOrder,
throwIfOrderChangeIsNotActive, throwIfOrderChangeIsNotActive,
} from "../utils/order-validation" } from "../../utils/order-validation"
const validationStep = createStep( const validationStep = createStep(
"request-item-return-validation", "request-item-return-validation",
@@ -28,7 +28,7 @@ const validationStep = createStep(
orderReturn, orderReturn,
items, items,
}: { }: {
order: OrderDTO order: Pick<OrderDTO, "id" | "items">
orderReturn: ReturnDTO orderReturn: ReturnDTO
orderChange: OrderChangeDTO orderChange: OrderChangeDTO
items: OrderWorkflow.RequestItemReturnWorkflowInput["items"] items: OrderWorkflow.RequestItemReturnWorkflowInput["items"]
@@ -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<OrderWorkflow.UpdateRequestItemReturnWorkflowInput>
): WorkflowData<OrderDTO> {
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)
}
)
@@ -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<OrderWorkflow.UpdateReturnShippingMethodWorkflowInput>
): 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)
}
)
@@ -326,7 +326,6 @@ export interface CreateOrderChangeActionDTO {
export interface UpdateOrderChangeActionDTO { export interface UpdateOrderChangeActionDTO {
id: string id: string
internal_note?: string | null internal_note?: string | null
metadata?: Record<string, unknown> | null
} }
/** ORDER TRANSACTION START */ /** ORDER TRANSACTION START */
+11
View File
@@ -78,6 +78,7 @@ import {
UpdateOrderReturnReasonWithSelectorDTO, UpdateOrderReturnReasonWithSelectorDTO,
UpdateOrderReturnWithSelectorDTO, UpdateOrderReturnWithSelectorDTO,
UpdateOrderShippingMethodAdjustmentDTO, UpdateOrderShippingMethodAdjustmentDTO,
UpdateOrderShippingMethodDTO,
UpdateOrderShippingMethodTaxLineDTO, UpdateOrderShippingMethodTaxLineDTO,
UpdateReturnDTO, UpdateReturnDTO,
UpsertOrderLineItemAdjustmentDTO, UpsertOrderLineItemAdjustmentDTO,
@@ -764,6 +765,16 @@ export interface IOrderModuleService extends IModuleService {
sharedContext?: Context sharedContext?: Context
): Promise<OrderShippingMethodDTO[]> ): Promise<OrderShippingMethodDTO[]>
updateShippingMethods(
data: UpdateOrderShippingMethodDTO[],
sharedContext?: Context
): Promise<OrderShippingMethodDTO[]>
updateShippingMethods(
data: UpdateOrderShippingMethodDTO,
sharedContext?: Context
): Promise<OrderShippingMethodDTO>
deleteShippingMethods( deleteShippingMethods(
methodIds: string[], methodIds: string[],
sharedContext?: Context sharedContext?: Context
@@ -12,3 +12,4 @@ export * from "./create-return-order"
export * from "./create-shipment" export * from "./create-shipment"
export * from "./receive-return" export * from "./receive-return"
export * from "./request-item-return" export * from "./request-item-return"
export * from "./shipping-return"
@@ -1,3 +1,4 @@
import { BigNumberInput } from "../../totals"
import { CreateReturnItem } from "./create-return-order" import { CreateReturnItem } from "./create-return-order"
export interface RequestItemReturnWorkflowInput { export interface RequestItemReturnWorkflowInput {
@@ -9,6 +10,15 @@ export interface DeleteRequestItemReturnWorkflowInput {
action_id: string action_id: string
} }
export interface UpdateRequestItemReturnWorkflowInput {
return_id: string
action_id: string
data: {
quantity?: BigNumberInput
internal_note?: string | null
}
}
export interface OrderExchangeRequestItemReturnWorkflowInput { export interface OrderExchangeRequestItemReturnWorkflowInput {
exchange_id: string exchange_id: string
items: CreateReturnItem[] items: CreateReturnItem[]
@@ -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<string, any> | null
}
}
export interface DeleteReturnShippingMethodWorkflowInput {
return_id: string
action_id: string
}
@@ -1,4 +1,7 @@
import { removeRequestItemReturnWorkflow } from "@medusajs/core-flows" import {
removeRequestItemReturnWorkflow,
updateRequestItemReturnWorkflow,
} from "@medusajs/core-flows"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
remoteQueryObjectFromString, remoteQueryObjectFromString,
@@ -7,6 +10,42 @@ import {
AuthenticatedMedusaRequest, AuthenticatedMedusaRequest,
MedusaResponse, MedusaResponse,
} from "../../../../../../types/routing" } from "../../../../../../types/routing"
import { AdminPostReturnsRequestItemsActionReqSchemaType } from "../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostReturnsRequestItemsActionReqSchemaType>,
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 ( export const DELETE = async (
req: AuthenticatedMedusaRequest, req: AuthenticatedMedusaRequest,
@@ -1,4 +1,7 @@
import { removeReturnShippingMethodWorkflow } from "@medusajs/core-flows" import {
removeReturnShippingMethodWorkflow,
updateReturnShippingMethodWorkflow,
} from "@medusajs/core-flows"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
remoteQueryObjectFromString, remoteQueryObjectFromString,
@@ -7,6 +10,42 @@ import {
AuthenticatedMedusaRequest, AuthenticatedMedusaRequest,
MedusaResponse, MedusaResponse,
} from "../../../../../../types/routing" } from "../../../../../../types/routing"
import { AdminPostReturnsShippingActionReqSchemaType } from "../../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostReturnsShippingActionReqSchemaType>,
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 ( export const DELETE = async (
req: AuthenticatedMedusaRequest, req: AuthenticatedMedusaRequest,
@@ -7,7 +7,9 @@ import {
AdminGetOrdersParams, AdminGetOrdersParams,
AdminPostReturnsConfirmRequestReqSchema, AdminPostReturnsConfirmRequestReqSchema,
AdminPostReturnsReqSchema, AdminPostReturnsReqSchema,
AdminPostReturnsRequestItemsActionReqSchema,
AdminPostReturnsRequestItemsReqSchema, AdminPostReturnsRequestItemsReqSchema,
AdminPostReturnsShippingActionReqSchema,
AdminPostReturnsShippingReqSchema, AdminPostReturnsShippingReqSchema,
} from "./validators" } 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"], method: ["DELETE"],
matcher: "/admin/returns/:id/request-items/:action_id", 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"], method: ["DELETE"],
matcher: "/admin/returns/:id/shipping-method/:action_id", matcher: "/admin/returns/:id/shipping-method/:action_id",
@@ -108,6 +108,16 @@ export type AdminPostReturnsShippingReqSchemaType = z.infer<
typeof AdminPostReturnsShippingReqSchema 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({ export const AdminPostReturnsRequestItemsReqSchema = z.object({
items: z.array( items: z.array(
z.object({ z.object({
@@ -124,6 +134,15 @@ export type AdminPostReturnsRequestItemsReqSchemaType = z.infer<
typeof AdminPostReturnsRequestItemsReqSchema 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({ export const AdminPostReturnsConfirmRequestReqSchema = z.object({
no_notification: z.boolean().optional(), no_notification: z.boolean().optional(),
}) })