feat(core-flows, fulfillment): Add create return specific method and add more tests (#7357)

* feat(core-flows, fulfillment): Add create return specific method and add more tests

* fix defautl providers in tests fixtures

* more tests

* wip fixes

* fix flow and tests

* cleanup
This commit is contained in:
Adrien de Peretti
2024-05-21 13:48:59 +02:00
committed by GitHub
parent 35dc3c5cf7
commit c4fde7ea5c
9 changed files with 318 additions and 91 deletions

View File

@@ -7,6 +7,7 @@ import {
WithCalculatedPrice,
} from "@medusajs/types"
import {
createStep,
createWorkflow,
transform,
WorkflowData,
@@ -19,6 +20,7 @@ import {
MathBN,
MedusaError,
Modules,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { updateOrderTaxLinesStep } from "../steps"
import { createReturnStep } from "../steps/create-return"
@@ -55,7 +57,7 @@ function throwIfItemsDoesNotExistsInOrder({
}
}
function validateReturnReasons(
async function validateReturnReasons(
{
orderId,
inputItems,
@@ -66,24 +68,32 @@ function validateReturnReasons(
{ container }
) {
const reasonIds = inputItems.map((i) => i.reason_id).filter(Boolean)
if (!reasonIds.length) {
return
}
const remoteQuery = container.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const returnReasons = remoteQuery({
entry_point: "return_reasons",
fields: ["return_reason_children.*"],
variables: { id: [inputItems.map((item) => item.reason_id)] },
const remoteQueryObject = remoteQueryObjectFromString({
entryPoint: "return_reasons",
fields: [
"id",
"parent_return_reason_id",
"parent_return_reason",
"return_reason_children.id",
],
variables: { id: [inputItems.map((item) => item.reason_id)], limit: null },
})
const returnReasons = await remoteQuery(remoteQueryObject)
const reasons = returnReasons.map((r) => r.id)
const hasInvalidReasons = reasons.filter(
// We do not allow for root reason to be applied
(reason) => reason.return_reason_children.length > 0
)
const hasInvalidReasons = returnReasons
.filter(
// We do not allow for root reason to be applied
(reason) => reason.return_reason_children.length > 0
)
.map((r) => r.id)
const hasNonExistingReasons = arrayDifference(reasonIds, reasons)
if (hasNonExistingReasons.length) {
@@ -95,7 +105,7 @@ function validateReturnReasons(
)
}
if (hasInvalidReasons.length()) {
if (hasInvalidReasons.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Cannot apply return reason with id ${hasInvalidReasons.join(
@@ -238,12 +248,34 @@ function prepareReturnShippingOptionQueryVariables({
return variables
}
const validationStep = createStep(
"create-return-order-validation",
async function (
{
order,
input,
}: {
order
input: OrderWorkflow.CreateOrderReturnWorkflowInput
},
context
) {
throwIfOrderIsCancelled({ order })
throwIfItemsDoesNotExistsInOrder({ order, inputItems: input.items })
await validateReturnReasons(
{ orderId: input.order_id, inputItems: input.items },
context
)
validateCustomRefundAmount({ order, refundAmount: input.refund_amount })
}
)
export const createReturnOrderWorkflowId = "create-return-order"
export const createReturnOrderWorkflow = createWorkflow(
createReturnOrderWorkflowId,
(
function (
input: WorkflowData<OrderWorkflow.CreateOrderReturnWorkflowInput>
): WorkflowData<void> => {
): WorkflowData<void> {
const order: OrderDTO = useRemoteQueryStep({
entry_point: "orders",
fields: [
@@ -259,19 +291,7 @@ export const createReturnOrderWorkflow = createWorkflow(
throw_if_key_not_found: true,
})
transform({ order }, throwIfOrderIsCancelled)
transform(
{ order, inputItems: input.items },
throwIfItemsDoesNotExistsInOrder
)
transform(
{ orderId: input.order_id, inputItems: input.items },
validateReturnReasons
)
transform(
{ order, refundAmount: input.refund_amount },
validateCustomRefundAmount
)
validationStep({ order, input })
const returnShippingOptionsVariables = transform(
{ input, order },