feat: Admin Returns API (#8117)

* feat: Add request item + add shipping APIs

* wip

* finalize workflow

* move steps

* add returns to js-sdk

* few chores

* fix test

* fix another test :)
This commit is contained in:
Oli Juhl
2024-07-15 15:57:06 +02:00
committed by GitHub
parent 53ddea717c
commit 00c7900337
23 changed files with 845 additions and 77 deletions
@@ -0,0 +1,28 @@
import { OrderChangeDTO } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
type ConfirmOrderChangesInput = {
orderId: string
changes: OrderChangeDTO[]
}
export const confirmOrderChanges = createStep(
"confirm-order-changes",
async (input: ConfirmOrderChangesInput, { container }) => {
const orderModuleService = container.resolve(ModuleRegistrationName.ORDER)
await orderModuleService.confirmOrderChange(
input.changes.map((action) => action.id)
)
return new StepResponse(null, input.orderId)
},
async (orderId, { container }) => {
if (!orderId) {
return
}
const orderModuleService = container.resolve(ModuleRegistrationName.ORDER)
await orderModuleService.revertLastVersion(orderId)
}
)
@@ -0,0 +1,55 @@
import { IOrderModuleService, OrderChangeActionDTO } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
type CreateReturnItemsInput = {
changes: OrderChangeActionDTO[]
returnId: string
}
export const createReturnItems = createStep(
"create-return-items",
async (input: CreateReturnItemsInput, { container }) => {
const orderModuleService = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
const returnItems = input.changes.map((item) => {
return {
return_id: item.reference_id,
item_id: item.details?.reference_id,
quantity: item.details?.quantity as number,
note: item.internal_note,
metadata: (item.details?.metadata as Record<string, unknown>) ?? {},
}
})
const [prevReturn] = await orderModuleService.listReturns(
{ id: input.returnId },
{
select: ["id"],
relations: ["items"],
}
)
const createdReturnItems = await orderModuleService.updateReturns([
{ selector: { id: input.returnId }, data: { items: returnItems } },
])
return new StepResponse(createdReturnItems, prevReturn)
},
async (prevData, { container }) => {
if (!prevData) {
return
}
const orderModuleService = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
await orderModuleService.updateReturns(
{ id: prevData.id },
{ items: prevData.items }
)
}
)
@@ -0,0 +1,93 @@
import { OrderChangeDTO, OrderDTO, ReturnDTO } from "@medusajs/types"
import { ChangeActionType } from "@medusajs/utils"
import {
createStep,
createWorkflow,
transform,
WorkflowData,
} 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 {
throwIfIsCancelled,
throwIfOrderChangeIsNotActive,
} from "../utils/order-validation"
type WorkflowInput = {
return_id: string
}
const validationStep = createStep(
"validate-create-return-shipping-method",
async function ({
order,
orderChange,
orderReturn,
}: {
order: OrderDTO
orderReturn: ReturnDTO
orderChange: OrderChangeDTO
}) {
throwIfIsCancelled(order, "Order")
throwIfIsCancelled(orderReturn, "Return")
throwIfOrderChangeIsNotActive({ orderChange })
}
)
export const confirmReturnRequestWorkflowId = "confirm-return-request"
export const confirmReturnRequestWorkflow = createWorkflow(
confirmReturnRequestWorkflowId,
function (input: WorkflowInput): 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", "version", "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",
"actions.id",
"actions.action",
"actions.details",
"actions.reference",
"actions.reference_id",
"actions.internal_note",
],
variables: {
filters: {
order_id: orderReturn.order_id,
return_id: orderReturn.id,
},
},
list: false,
}).config({ name: "order-change-query" })
const returnItemActions = transform({ orderChange }, (data) => {
return data.orderChange.actions.filter(
(act) => act.action === ChangeActionType.RETURN_ITEM
)
})
validationStep({ order, orderReturn, orderChange })
createReturnItems({ returnId: orderReturn.id, changes: returnItemActions })
confirmOrderChanges({ changes: [orderChange], orderId: order.id })
return previewOrderChangeStep(order.id)
}
)
@@ -16,7 +16,7 @@ import { createOrderChangeActionsStep } from "../steps/create-order-change-actio
import { createOrderShippingMethods } from "../steps/create-order-shipping-methods"
import {
throwIfIsCancelled,
throwIfOrderChangeIsNotActive
throwIfOrderChangeIsNotActive,
} from "../utils/order-validation"
const validationStep = createStep(
@@ -41,14 +41,14 @@ export const createReturnShippingMethodWorkflowId =
export const createReturnShippingMethodWorkflow = createWorkflow(
createReturnShippingMethodWorkflowId,
function (input: {
returnId: string
shippingOptionId: string
customShippingPrice?: BigNumberInput
return_id: string
shipping_option_id: string
custom_price?: BigNumberInput
}): WorkflowData {
const orderReturn: ReturnDTO = useRemoteQueryStep({
entry_point: "return",
fields: ["id", "status", "order_id"],
variables: { id: input.returnId },
variables: { id: input.return_id },
list: false,
throw_if_key_not_found: true,
})
@@ -70,7 +70,7 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
"calculated_price.is_calculated_price_tax_inclusive",
],
variables: {
id: input.shippingOptionId,
id: input.shipping_option_id,
calculated_price: {
context: { currency_code: order.currency_code },
},
@@ -102,7 +102,9 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
const orderChange: OrderChangeDTO = useRemoteQueryStep({
entry_point: "order_change",
fields: ["id", "status"],
variables: { order_id: orderReturn.order_id },
variables: {
filters: { order_id: orderReturn.order_id, return_id: orderReturn.id },
},
list: false,
}).config({ name: "order-change-query" })
@@ -114,7 +116,7 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
returnId: orderReturn.id,
shippingOption: shippingOptions[0],
methodId: createdMethods[0].id,
customPrice: input.customShippingPrice,
customPrice: input.custom_price,
},
({ shippingOption, returnId, orderId, methodId, customPrice }) => {
const methodPrice =
@@ -125,10 +127,8 @@ export const createReturnShippingMethodWorkflow = createWorkflow(
reference: "order_shipping_method",
reference_id: methodId,
amount: methodPrice,
details: {
order_id: orderId,
return_id: returnId,
},
order_id: orderId,
return_id: returnId,
}
}
)
@@ -9,6 +9,7 @@ export * from "./cancel-order-fulfillment"
export * from "./cancel-return"
export * from "./claim-request-item-return"
export * from "./complete-orders"
export * from "./confirm-return-request"
export * from "./create-complete-return"
export * from "./create-fulfillment"
export * from "./create-order-change"
@@ -18,7 +18,6 @@ import {
throwIfIsCancelled,
throwIfItemsDoesNotExistsInOrder,
throwIfOrderChangeIsNotActive,
throwIfOrderIsCancelled,
} from "../utils/order-validation"
const validationStep = createStep(
@@ -34,7 +33,7 @@ const validationStep = createStep(
orderChange: OrderChangeDTO
items: OrderWorkflow.RequestItemReturnWorkflowInput["items"]
}) {
throwIfOrderIsCancelled({ order })
throwIfIsCancelled(order, "Order")
throwIfIsCancelled(orderReturn, "Return")
throwIfOrderChangeIsNotActive({ orderChange })
throwIfItemsDoesNotExistsInOrder({ order, inputItems: items })
@@ -65,8 +64,10 @@ export const requestItemReturnWorkflow = createWorkflow(
const orderChange: OrderChangeDTO = useRemoteQueryStep({
entry_point: "order_change",
fields: ["id", "status"],
variables: { order_id: orderReturn.order_id },
fields: ["id", "status", "order_id", "return_id"],
variables: {
filters: { order_id: orderReturn.order_id, return_id: orderReturn.id },
},
list: false,
}).config({ name: "order-change-query" })
@@ -86,7 +87,6 @@ export const requestItemReturnWorkflow = createWorkflow(
reference_id: orderReturn.id,
details: {
reference_id: item.id,
return_id: orderReturn.id,
quantity: item.quantity,
metadata: item.metadata,
},