feat(core-flows,medusa,order): remove return shipping (#8137)

What:
* `DELETE /admin/returs/:id/shipping_methods/:action_id`

FIXES: CC-187
This commit is contained in:
Carlos R. L. Rodrigues
2024-07-16 10:27:16 +00:00
committed by GitHub
parent b7e6b1461b
commit 7123f9ff63
22 changed files with 383 additions and 23 deletions
@@ -6,7 +6,7 @@ interface StepInput {
items: CreateOrderLineItemDTO[]
}
export const createOrderLineItemsStepId = "create-line-items-step"
export const createOrderLineItemsStepId = "create-order-line-items-step"
export const createOrderLineItemsStep = createStep(
createOrderLineItemsStepId,
async (input: StepInput, { container }) => {
@@ -0,0 +1,31 @@
import { IOrderModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
interface StepInput {
ids: string[]
}
export const deleteOrderLineItems = createStep(
"delete-order-line-items",
async (input: StepInput, { container }) => {
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
const deleted = await service.softDeleteLineItems(input.ids)
return new StepResponse(deleted, input.ids)
},
async (ids, { container }) => {
if (!ids) {
return
}
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
await service.restoreLineItems(ids)
}
)
@@ -2,20 +2,20 @@ import { IOrderModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
export const deleteOrderChangeStepId = "delete-order-change"
export const deleteOrderChangeStep = createStep(
deleteOrderChangeStepId,
async (data: { id: string }, { container }) => {
export const deleteOrderChangesStepId = "delete-order-change"
export const deleteOrderChangesStep = createStep(
deleteOrderChangesStepId,
async (data: { ids: string[] }, { container }) => {
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
const deleted = await service.softDeleteOrderChanges(data.id)
const deleted = await service.softDeleteOrderChanges(data.ids)
return new StepResponse(deleted, data.id)
return new StepResponse(deleted, data.ids)
},
async (id, { container }) => {
if (!id) {
async (ids, { container }) => {
if (!ids) {
return
}
@@ -23,6 +23,6 @@ export const deleteOrderChangeStep = createStep(
ModuleRegistrationName.ORDER
)
await service.restoreOrderChanges(id)
await service.restoreOrderChanges(ids)
}
)
@@ -0,0 +1,31 @@
import { IOrderModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
interface StepInput {
ids: string[]
}
export const deleteOrderShippingMethods = createStep(
"delete-order-shipping-methods",
async (input: StepInput, { container }) => {
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
const deleted = await service.softDeleteShippingMethods(input.ids)
return new StepResponse(deleted, input.ids)
},
async (ids, { container }) => {
if (!ids) {
return
}
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
await service.restoreShippingMethods(ids)
}
)
@@ -14,8 +14,10 @@ export * from "./create-order-change-actions"
export * from "./create-orders"
export * from "./create-returns"
export * from "./decline-order-change"
export * from "./delete-order-change"
export * from "./delete-line-items"
export * from "./delete-order-change-actions"
export * from "./delete-order-changes"
export * from "./delete-order-shipping-methods"
export * from "./get-item-tax-lines"
export * from "./preview-order-change"
export * from "./register-fulfillment"
@@ -1,10 +1,10 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { deleteOrderChangeStep } from "../steps"
import { deleteOrderChangesStep } from "../steps"
export const deleteOrderChangeWorkflowId = "delete-order-change"
export const deleteOrderChangeWorkflow = createWorkflow(
deleteOrderChangeWorkflowId,
(input: WorkflowData<{ id: string }>): WorkflowData<void> => {
deleteOrderChangeStep(input)
(input: WorkflowData<{ ids: string[] }>): WorkflowData<void> => {
deleteOrderChangesStep(input)
}
)
@@ -26,7 +26,7 @@ export * from "./exchange-request-item-return"
export * from "./get-order-detail"
export * from "./get-orders-list"
export * from "./receive-return"
export * from "./remove-return-shipping-method"
export * from "./request-item-return"
export * from "./update-order-change-actions"
export * from "./update-tax-lines"
@@ -0,0 +1,102 @@
import {
OrderChangeActionDTO,
OrderChangeDTO,
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 { 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"
const validationStep = createStep(
"validate-remove-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 removeReturnShippingMethodWorkflowId =
"remove-return-shipping-method"
export const removeReturnShippingMethodWorkflow = createWorkflow(
removeReturnShippingMethodWorkflowId,
function (input: { return_id: string; action_id: string }): 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 dataToRemove = transform(
{ orderChange, input },
({ orderChange, input }) => {
const associatedAction = orderChange.actions.find(
(a) => a.id === input.action_id
) as OrderChangeActionDTO
return {
actionId: associatedAction.id,
shippingMethodId: associatedAction.reference_id,
}
}
)
parallelize(
deleteOrderChangeActionsStep({ ids: [dataToRemove.actionId] }),
deleteOrderShippingMethods({ ids: [dataToRemove.shippingMethodId] })
)
return previewOrderChangeStep(orderReturn.order_id)
}
)