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

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

View File

@@ -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,
}),
]),
})

View File

@@ -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"

View File

@@ -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)
}
)

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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"

View File

@@ -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",

View File

@@ -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",

View File

@@ -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

View File

@@ -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",

View File

@@ -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",

View File

@@ -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

View File

@@ -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<OrderWorkflow.DeleteReturnShippingMethodWorkflowInput>
): 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

View File

@@ -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<OrderDTO, "id" | "items">
orderReturn: ReturnDTO
orderChange: OrderChangeDTO
items: OrderWorkflow.RequestItemReturnWorkflowInput["items"]

View File

@@ -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)
}
)

View File

@@ -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)
}
)

View File

@@ -326,7 +326,6 @@ export interface CreateOrderChangeActionDTO {
export interface UpdateOrderChangeActionDTO {
id: string
internal_note?: string | null
metadata?: Record<string, unknown> | null
}
/** ORDER TRANSACTION START */

View File

@@ -78,6 +78,7 @@ import {
UpdateOrderReturnReasonWithSelectorDTO,
UpdateOrderReturnWithSelectorDTO,
UpdateOrderShippingMethodAdjustmentDTO,
UpdateOrderShippingMethodDTO,
UpdateOrderShippingMethodTaxLineDTO,
UpdateReturnDTO,
UpsertOrderLineItemAdjustmentDTO,
@@ -764,6 +765,16 @@ export interface IOrderModuleService extends IModuleService {
sharedContext?: Context
): Promise<OrderShippingMethodDTO[]>
updateShippingMethods(
data: UpdateOrderShippingMethodDTO[],
sharedContext?: Context
): Promise<OrderShippingMethodDTO[]>
updateShippingMethods(
data: UpdateOrderShippingMethodDTO,
sharedContext?: Context
): Promise<OrderShippingMethodDTO>
deleteShippingMethods(
methodIds: string[],
sharedContext?: Context

View File

@@ -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"

View File

@@ -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[]

View File

@@ -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
}

View File

@@ -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<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 (
req: AuthenticatedMedusaRequest,

View File

@@ -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<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 (
req: AuthenticatedMedusaRequest,

View File

@@ -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",

View File

@@ -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(),
})