chore(medusa): return endpoints (#7823)

This commit is contained in:
Carlos R. L. Rodrigues authored and GitHub committed 2024-06-25 13:53:18 +00:00
1 parent 5600e58b7f
commit 61758f374b
54 files changed
+305 -135

No files matched your search

@@ -7,7 +7,7 @@ import {
updateLinksWorkflowId, updateLinksWorkflowId,
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { Modules } from "@medusajs/utils" import { Modules } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { import {
adminHeaders, adminHeaders,
createAdminUser, createAdminUser,
@@ -1,6 +1,6 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IFulfillmentModuleService } from "@medusajs/types" import { IFulfillmentModuleService } from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createAdminUser } from "../../../helpers/create-admin-user" import { createAdminUser } from "../../../helpers/create-admin-user"
jest.setTimeout(100000) jest.setTimeout(100000)
@@ -8,7 +8,7 @@ import {
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IFulfillmentModuleService } from "@medusajs/types" import { IFulfillmentModuleService } from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { import {
generateCreateFulfillmentData, generateCreateFulfillmentData,
generateCreateShippingOptionsData, generateCreateShippingOptionsData,
@@ -1,6 +1,6 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IFulfillmentModuleService } from "@medusajs/types" import { IFulfillmentModuleService } from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createAdminUser } from "../../../helpers/create-admin-user" import { createAdminUser } from "../../../helpers/create-admin-user"
import { import {
generateCreateFulfillmentData, generateCreateFulfillmentData,
@@ -1,6 +1,6 @@
import { initialize, runMigrations } from "@medusajs/link-modules" import { initialize, runMigrations } from "@medusajs/link-modules"
import { MedusaModule, ModuleJoinerConfig } from "@medusajs/modules-sdk" import { MedusaModule, ModuleJoinerConfig } from "@medusajs/modules-sdk"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(5000000) jest.setTimeout(5000000)
@@ -21,7 +21,7 @@ import {
Modules, Modules,
remoteQueryObjectFromString, remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(500000) jest.setTimeout(500000)
@@ -20,7 +20,7 @@ import {
Modules, Modules,
remoteQueryObjectFromString, remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(500000) jest.setTimeout(500000)
@@ -18,10 +18,10 @@ import {
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
Modules, Modules,
remoteQueryObjectFromString,
RuleOperator, RuleOperator,
remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(500000) jest.setTimeout(500000)
@@ -18,10 +18,10 @@ import {
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
Modules, Modules,
remoteQueryObjectFromString,
RuleOperator, RuleOperator,
remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(500000) jest.setTimeout(500000)
@@ -1,4 +1,4 @@
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { import {
adminHeaders, adminHeaders,
createAdminUser, createAdminUser,
@@ -4,7 +4,7 @@ import {
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPaymentModuleService, IRegionModuleService } from "@medusajs/types" import { IPaymentModuleService, IRegionModuleService } from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -4,8 +4,8 @@ import {
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { LinkModuleUtils, ModuleRegistrationName } from "@medusajs/modules-sdk" import { LinkModuleUtils, ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPaymentModuleService, IRegionModuleService } from "@medusajs/types" import { IPaymentModuleService, IRegionModuleService } from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist"
import { Modules } from "@medusajs/utils" import { Modules } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -6,7 +6,7 @@ import {
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IProductModuleService } from "@medusajs/types" import { IProductModuleService } from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -1,3 +1,7 @@
import {
batchShippingOptionRulesWorkflow,
createShippingOptionsWorkflow,
} from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { import {
BatchWorkflowInput, BatchWorkflowInput,
@@ -10,16 +14,12 @@ import {
ShippingProfileDTO, ShippingProfileDTO,
UpdateShippingOptionRuleDTO, UpdateShippingOptionRuleDTO,
} from "@medusajs/types" } from "@medusajs/types"
import {medusaIntegrationTestRunner} from "medusa-test-utils/dist"
import {
batchShippingOptionRulesWorkflow,
createShippingOptionsWorkflow,
} from "@medusajs/core-flows"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
remoteQueryObjectFromString,
RuleOperator, RuleOperator,
remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(100000) jest.setTimeout(100000)
@@ -1,3 +1,4 @@
import { createShippingOptionsWorkflow } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { import {
FulfillmentSetDTO, FulfillmentSetDTO,
@@ -7,13 +8,12 @@ import {
ServiceZoneDTO, ServiceZoneDTO,
ShippingProfileDTO, ShippingProfileDTO,
} from "@medusajs/types" } from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist"
import { createShippingOptionsWorkflow } from "@medusajs/core-flows"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
remoteQueryObjectFromString,
RuleOperator, RuleOperator,
remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(100000) jest.setTimeout(100000)
@@ -1,3 +1,7 @@
import {
createShippingOptionsWorkflow,
deleteShippingOptionsWorkflow,
} from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { import {
FulfillmentSetDTO, FulfillmentSetDTO,
@@ -7,16 +11,12 @@ import {
ServiceZoneDTO, ServiceZoneDTO,
ShippingProfileDTO, ShippingProfileDTO,
} from "@medusajs/types" } from "@medusajs/types"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist"
import {
createShippingOptionsWorkflow,
deleteShippingOptionsWorkflow,
} from "@medusajs/core-flows"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
remoteQueryObjectFromString,
RuleOperator, RuleOperator,
remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(100000) jest.setTimeout(100000)
@@ -17,7 +17,7 @@ import {
RuleOperator, RuleOperator,
remoteQueryObjectFromString, remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(100000) jest.setTimeout(100000)
@@ -1,12 +1,12 @@
import { ITaxModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ITaxModuleService } from "@medusajs/types"
import { createAdminUser } from "../../../../helpers/create-admin-user"
import { import {
createTaxRateRulesStepId, createTaxRateRulesStepId,
updateTaxRatesWorkflow, updateTaxRatesWorkflow,
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist" import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createAdminUser } from "../../../../helpers/create-admin-user"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -1,5 +1,5 @@
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createAdminUser } from "../../../helpers/create-admin-user" import { createAdminUser } from "../../../helpers/create-admin-user"
import { medusaIntegrationTestRunner } from "medusa-test-utils/dist"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -1,6 +1,6 @@
import { LinkWorkflowInput } from "@medusajs/types/src" import { LinkWorkflowInput } from "@medusajs/types"
import { ContainerRegistrationKeys, Modules, promiseAll } from "@medusajs/utils" import { ContainerRegistrationKeys, Modules, promiseAll } from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk" import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const linkSalesChannelsToApiKeyStepId = "link-sales-channels-to-api-key" export const linkSalesChannelsToApiKeyStepId = "link-sales-channels-to-api-key"
export const linkSalesChannelsToApiKeyStep = createStep( export const linkSalesChannelsToApiKeyStep = createStep(
@@ -1,9 +1,9 @@
import { LinkWorkflowInput } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { import {
linkSalesChannelsToApiKeyStep, linkSalesChannelsToApiKeyStep,
validateSalesChannelsExistStep, validateSalesChannelsExistStep,
} from "../steps" } from "../steps"
import { LinkWorkflowInput } from "@medusajs/types/src"
export const linkSalesChannelsToApiKeyWorkflowId = export const linkSalesChannelsToApiKeyWorkflowId =
"link-sales-channels-to-api-key" "link-sales-channels-to-api-key"
@@ -1,8 +1,7 @@
import { ICustomerModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { LinkWorkflowInput } from "@medusajs/types/src" import { ICustomerModuleService, LinkWorkflowInput } from "@medusajs/types"
import { promiseAll } from "@medusajs/utils" import { promiseAll } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const linkCustomersToCustomerGroupStepId = export const linkCustomersToCustomerGroupStepId =
"link-customers-to-customer-group" "link-customers-to-customer-group"
@@ -1,6 +1,6 @@
import { LinkWorkflowInput } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { linkCustomersToCustomerGroupStep } from "../steps" import { linkCustomersToCustomerGroupStep } from "../steps"
import { LinkWorkflowInput } from "@medusajs/types/src"
export const linkCustomersToCustomerGroupWorkflowId = export const linkCustomersToCustomerGroupWorkflowId =
"link-customers-to-customer-group" "link-customers-to-customer-group"
@@ -1,15 +1,15 @@
import { createStep, StepResponse } from "@medusajs/workflows-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { import {
FulfillmentWorkflow, FulfillmentWorkflow,
IFulfillmentModuleService, IFulfillmentModuleService,
ShippingOptionDTO, ShippingOptionDTO,
UpsertShippingOptionDTO,
} from "@medusajs/types" } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { import {
arrayDifference, arrayDifference,
getSelectsAndRelationsFromObjectArray, getSelectsAndRelationsFromObjectArray,
} from "@medusajs/utils" } from "@medusajs/utils"
import { UpsertShippingOptionDTO } from "@medusajs/types/src" import { createStep, StepResponse } from "@medusajs/workflows-sdk"
type StepInput = Omit< type StepInput = Omit<
| FulfillmentWorkflow.CreateShippingOptionsWorkflowInput | FulfillmentWorkflow.CreateShippingOptionsWorkflowInput
@@ -12,8 +12,8 @@ export const createReturnStep = createStep(
ModuleRegistrationName.ORDER ModuleRegistrationName.ORDER
) )
await service.createReturn(data) const orderReturn = await service.createReturn(data)
return new StepResponse(void 0, data.order_id) return new StepResponse(orderReturn, data.order_id)
}, },
async (orderId, { container }) => { async (orderId, { container }) => {
if (!orderId) { if (!orderId) {
@@ -0,0 +1,33 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IOrderModuleService, ReceiveOrderReturnDTO } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
type ReceiveReturnStepInput = ReceiveOrderReturnDTO
export const receiveReturnStepId = "receive-return"
export const receiveReturnStep = createStep(
receiveReturnStepId,
async (data: ReceiveReturnStepInput, { container }) => {
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
if (!data.items?.length) {
return new StepResponse(void 0)
}
const received = await service.receiveReturn(data)
return new StepResponse(received, data.return_id)
},
async (orderId, { container }) => {
if (!orderId) {
return
}
const service = container.resolve<IOrderModuleService>(
ModuleRegistrationName.ORDER
)
await service.revertLastVersion(orderId)
}
)
@@ -1,4 +1,4 @@
import { OrderDTO, OrderWorkflow } from "@medusajs/types" import { OrderDTO, OrderWorkflow, ReturnDTO } from "@medusajs/types"
import { MedusaError, OrderStatus, arrayDifference } from "@medusajs/utils" import { MedusaError, OrderStatus, arrayDifference } from "@medusajs/utils"
export function throwIfOrderIsCancelled({ order }: { order: OrderDTO }) { export function throwIfOrderIsCancelled({ order }: { order: OrderDTO }) {
@@ -30,3 +30,37 @@ export function throwIfItemsDoesNotExistsInOrder({
) )
} }
} }
export function throwIfReturnIsCancelled({
orderReturn,
}: {
orderReturn: ReturnDTO
}) {
if (orderReturn.canceled_at) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`return with id ${orderReturn.id} has been canceled.`
)
}
}
export function throwIfItemsDoesNotExistsInReturn({
orderReturn,
inputItems,
}: {
orderReturn: Pick<ReturnDTO, "id" | "items">
inputItems: OrderWorkflow.CreateOrderFulfillmentWorkflowInput["items"]
}) {
const orderReturnItemIds = orderReturn.items?.map((i) => i.id) ?? []
const inputItemIds = inputItems.map((i) => i.id)
const diff = arrayDifference(inputItemIds, orderReturnItemIds)
if (diff.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Items with ids ${diff.join(", ")} does not exist in Return with id ${
orderReturn.id
}.`
)
}
}
@@ -1,4 +1,5 @@
import { import {
BigNumberInput,
CreateOrderShippingMethodDTO, CreateOrderShippingMethodDTO,
FulfillmentWorkflow, FulfillmentWorkflow,
OrderDTO, OrderDTO,
@@ -7,25 +8,26 @@ import {
WithCalculatedPrice, WithCalculatedPrice,
} from "@medusajs/types" } from "@medusajs/types"
import { import {
arrayDifference,
ContainerRegistrationKeys, ContainerRegistrationKeys,
isDefined,
MathBN, MathBN,
MedusaError, MedusaError,
Modules, Modules,
arrayDifference,
isDefined,
remoteQueryObjectFromString, remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { import {
WorkflowData,
createStep, createStep,
createWorkflow, createWorkflow,
parallelize, parallelize,
transform, transform,
WorkflowData,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { createRemoteLinkStep, useRemoteQueryStep } from "../../common" import { createRemoteLinkStep, useRemoteQueryStep } from "../../common"
import { createReturnFulfillmentWorkflow } from "../../fulfillment" import { createReturnFulfillmentWorkflow } from "../../fulfillment"
import { updateOrderTaxLinesStep } from "../steps" import { updateOrderTaxLinesStep } from "../steps"
import { createReturnStep } from "../steps/create-return" import { createReturnStep } from "../steps/create-return"
import { receiveReturnStep } from "../steps/receive-return"
import { import {
throwIfItemsDoesNotExistsInOrder, throwIfItemsDoesNotExistsInOrder,
throwIfOrderIsCancelled, throwIfOrderIsCancelled,
@@ -139,6 +141,37 @@ function validateCustomRefundAmount({
} }
} }
function prepareReceiveItems({
receiveNow,
returnId,
items,
createdBy,
}: {
receiveNow: boolean
returnId: string
items: {
id: string
quantity: BigNumberInput
}[]
createdBy?: string
}) {
if (!receiveNow) {
return {
return_id: returnId,
items: [],
}
}
return {
return_id: returnId,
items: (items ?? []).map((i) => ({
id: i.id,
quantity: i.quantity,
})),
created_by: createdBy,
}
}
function prepareFulfillmentData({ function prepareFulfillmentData({
order, order,
input, input,
@@ -316,7 +349,7 @@ export const createReturnOrderWorkflow = createWorkflow(
} }
) )
parallelize( const [returnCreated] = parallelize(
createReturnStep({ createReturnStep({
order_id: input.order_id, order_id: input.order_id,
items: input.items, items: input.items,
@@ -329,5 +362,16 @@ export const createReturnOrderWorkflow = createWorkflow(
}), }),
createRemoteLinkStep(link) createRemoteLinkStep(link)
) )
const receiveItems = transform(
{
receiveNow: input.receive_now ?? false,
returnId: returnCreated.id,
items: order.items!,
createdBy: input.created_by!,
},
prepareReceiveItems
)
receiveReturnStep(receiveItems)
} }
) )
@@ -7,4 +7,5 @@ export * from "./create-orders"
export * from "./create-return" export * from "./create-return"
export * from "./create-shipment" export * from "./create-shipment"
export * from "./get-order-detail" export * from "./get-order-detail"
export * from "./receive-return"
export * from "./update-tax-lines" export * from "./update-tax-lines"
@@ -0,0 +1,53 @@
import { OrderWorkflow } from "@medusajs/types"
import {
WorkflowData,
createStep,
createWorkflow,
} from "@medusajs/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { ReturnDTO } from "@medusajs/types"
import { receiveReturnStep } from "../steps/receive-return"
import {
throwIfItemsDoesNotExistsInReturn,
throwIfReturnIsCancelled,
} from "../utils/order-validation"
const validationStep = createStep(
"receive-return-order-validation",
async function (
{
orderReturn,
input,
}: {
orderReturn
input: OrderWorkflow.ReceiveOrderReturnWorkflowInput
},
context
) {
throwIfReturnIsCancelled({ orderReturn })
throwIfItemsDoesNotExistsInReturn({ orderReturn, inputItems: input.items })
}
)
export const receiveReturnOrderWorkflowId = "receive-return-order"
export const receiveReturnOrderWorkflow = createWorkflow(
receiveReturnOrderWorkflowId,
function (
input: WorkflowData<OrderWorkflow.ReceiveOrderReturnWorkflowInput>
): WorkflowData<ReturnDTO> {
const orderReturn: ReturnDTO = useRemoteQueryStep({
entry_point: "returns",
fields: ["id", "canceled_at", "items.*"],
variables: { id: input.return_id },
list: false,
throw_if_key_not_found: true,
})
validationStep({ orderReturn, input })
const received = receiveReturnStep(input)
return received
}
)
@@ -1,5 +1,4 @@
import { CreatePriceListPricesWorkflowDTO } from "@medusajs/types" import { CreatePriceListPricesWorkflowDTO, PricingTypes } from "@medusajs/types"
import { PricingTypes } from "@medusajs/types/src"
import { import {
WorkflowData, WorkflowData,
createWorkflow, createWorkflow,
@@ -1,5 +1,4 @@
import { UpdatePriceListPricesWorkflowDTO } from "@medusajs/types" import { PricingTypes, UpdatePriceListPricesWorkflowDTO } from "@medusajs/types"
import { PricingTypes } from "@medusajs/types/src"
import { import {
WorkflowData, WorkflowData,
createWorkflow, createWorkflow,
@@ -1,6 +1,5 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IProductModuleService } from "@medusajs/types" import { IProductModuleService, LinkWorkflowInput } from "@medusajs/types"
import { LinkWorkflowInput } from "@medusajs/types/src"
import { StepResponse, createStep } from "@medusajs/workflows-sdk" import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const batchLinkProductsToCollectionStepId = export const batchLinkProductsToCollectionStepId =
@@ -1,5 +1,5 @@
import { LinkWorkflowInput } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk" import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { LinkWorkflowInput } from "@medusajs/types/src"
import { batchLinkProductsToCollectionStep } from "../steps/batch-link-products-collection" import { batchLinkProductsToCollectionStep } from "../steps/batch-link-products-collection"
export const batchLinkProductsToCollectionWorkflowId = export const batchLinkProductsToCollectionWorkflowId =
@@ -1,19 +1,19 @@
import {
BatchWorkflowInput,
BatchWorkflowOutput,
CreateProductWorkflowInputDTO,
ProductTypes,
UpdateProductWorkflowInputDTO,
} from "@medusajs/types"
import { import {
WorkflowData, WorkflowData,
createWorkflow, createWorkflow,
parallelize, parallelize,
transform, transform,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import {
ProductTypes,
BatchWorkflowInput,
CreateProductWorkflowInputDTO,
UpdateProductWorkflowInputDTO,
} from "@medusajs/types"
import { BatchWorkflowOutput } from "@medusajs/types/src"
import { createProductsWorkflow } from "./create-products" import { createProductsWorkflow } from "./create-products"
import { updateProductsWorkflow } from "./update-products"
import { deleteProductsWorkflow } from "./delete-products" import { deleteProductsWorkflow } from "./delete-products"
import { updateProductsWorkflow } from "./update-products"
export const batchProductsWorkflowId = "batch-products" export const batchProductsWorkflowId = "batch-products"
export const batchProductsWorkflow = createWorkflow( export const batchProductsWorkflow = createWorkflow(
@@ -1,10 +1,12 @@
import {
ContainerRegistrationKeys,
ModulesSdkUtils,
toMikroOrmEntities,
} from "@medusajs/utils"
import { MockEventBusService } from "."
import { getDatabaseURL, getMikroOrmWrapper, TestDatabase } from "./database" import { getDatabaseURL, getMikroOrmWrapper, TestDatabase } from "./database"
import { initModules, InitModulesOptions } from "./init-modules" import { initModules, InitModulesOptions } from "./init-modules"
import { ContainerRegistrationKeys, ModulesSdkUtils } from "@medusajs/utils"
import { MockEventBusService } from "."
import { toMikroOrmEntities } from "@medusajs/utils/src"
export interface SuiteOptions<TService = unknown> { export interface SuiteOptions<TService = unknown> {
MikroOrmWrapper: TestDatabase MikroOrmWrapper: TestDatabase
medusaApp: any medusaApp: any
@@ -3,3 +3,4 @@ export * from "./cancel-order"
export * from "./create-fulfillment" export * from "./create-fulfillment"
export * from "./create-return-order" export * from "./create-return-order"
export * from "./create-shipment" export * from "./create-shipment"
export * from "./receive-return"
@@ -0,0 +1,17 @@
import { BigNumberInput } from "../../totals"
interface ReceiveReturnItem {
id: string
quantity: BigNumberInput
internal_note?: string | null
reason_id?: string | null
note?: string | null
metadata?: Record<string, any>
}
export interface ReceiveOrderReturnWorkflowInput {
return_id: string
created_by?: string | null // The id of the authenticated user
items: ReceiveReturnItem[]
internal_note?: string | null
}
@@ -8,9 +8,10 @@ import {
} from "@medusajs/orchestration" } from "@medusajs/orchestration"
import { Context, LoadedModule, MedusaContainer } from "@medusajs/types" import { Context, LoadedModule, MedusaContainer } from "@medusajs/types"
import { import {
isPresent, ContainerRegistrationKeys,
MedusaContextType, MedusaContextType,
ModuleRegistrationName, ModuleRegistrationName,
isPresent,
} from "@medusajs/utils" } from "@medusajs/utils"
import { EOL } from "os" import { EOL } from "os"
import { ulid } from "ulid" import { ulid } from "ulid"
@@ -25,7 +26,6 @@ import {
MainExportedWorkflow, MainExportedWorkflow,
WorkflowResult, WorkflowResult,
} from "./type" } from "./type"
import { ContainerRegistrationKeys } from "@medusajs/utils/dist"
function createContextualWorkflowRunner< function createContextualWorkflowRunner<
TData = unknown, TData = unknown,
@@ -1,11 +1,11 @@
import { linkSalesChannelsToApiKeyWorkflow } from "@medusajs/core-flows"
import { LinkMethodRequest } from "@medusajs/types"
import { ApiKeyType, MedusaError } from "@medusajs/utils" import { ApiKeyType, MedusaError } from "@medusajs/utils"
import { import {
AuthenticatedMedusaRequest, AuthenticatedMedusaRequest,
MedusaResponse, MedusaResponse,
} from "../../../../../types/routing" } from "../../../../../types/routing"
import { refetchApiKey } from "../../helpers" import { refetchApiKey } from "../../helpers"
import { LinkMethodRequest } from "@medusajs/types/src"
import { linkSalesChannelsToApiKeyWorkflow } from "@medusajs/core-flows"
export const POST = async ( export const POST = async (
req: AuthenticatedMedusaRequest<LinkMethodRequest>, req: AuthenticatedMedusaRequest<LinkMethodRequest>,
@@ -1,9 +1,9 @@
import { batchLinkProductsToCollectionWorkflow } from "@medusajs/core-flows" import { batchLinkProductsToCollectionWorkflow } from "@medusajs/core-flows"
import { LinkMethodRequest } from "@medusajs/types"
import { import {
AuthenticatedMedusaRequest, AuthenticatedMedusaRequest,
MedusaResponse, MedusaResponse,
} from "../../../../../types/routing" } from "../../../../../types/routing"
import { LinkMethodRequest } from "@medusajs/types/src"
import { refetchCollection } from "../../helpers" import { refetchCollection } from "../../helpers"
export const POST = async ( export const POST = async (
@@ -4,7 +4,7 @@ import {
MedusaResponse, MedusaResponse,
} from "../../../../../types/routing" } from "../../../../../types/routing"
import { LinkMethodRequest } from "@medusajs/types/src" import { LinkMethodRequest } from "@medusajs/types"
import { refetchCustomerGroup } from "../../helpers" import { refetchCustomerGroup } from "../../helpers"
export const POST = async ( export const POST = async (
@@ -1,10 +1,10 @@
import { batchPriceListPricesWorkflow } from "@medusajs/core-flows"
import { LinkMethodRequest } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils" import { MedusaError } from "@medusajs/utils"
import { import {
AuthenticatedMedusaRequest, AuthenticatedMedusaRequest,
MedusaResponse, MedusaResponse,
} from "../../../../../types/routing" } from "../../../../../types/routing"
import { batchPriceListPricesWorkflow } from "@medusajs/core-flows"
import { LinkMethodRequest } from "@medusajs/types/src"
import { fetchPriceList, fetchPriceListPriceIdsForProduct } from "../../helpers" import { fetchPriceList, fetchPriceListPriceIdsForProduct } from "../../helpers"
export const POST = async ( export const POST = async (
@@ -1,35 +1,20 @@
import { import { receiveReturnOrderWorkflow } from "@medusajs/core-flows"
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { import {
AuthenticatedMedusaRequest, AuthenticatedMedusaRequest,
MedusaResponse, MedusaResponse,
} from "../../../../../types/routing" } from "../../../../../types/routing"
import { AdminPostReceiveReturnsReqSchemaType } from "../../validators"
export const POST = async ( export const POST = async (
req: AuthenticatedMedusaRequest, req: AuthenticatedMedusaRequest<AdminPostReceiveReturnsReqSchemaType>,
res: MedusaResponse res: MedusaResponse
) => { ) => {
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) const input = req.validatedBody as AdminPostReceiveReturnsReqSchemaType
const queryObject = remoteQueryObjectFromString({ const workflow = receiveReturnOrderWorkflow(req.scope)
entryPoint: "return", const { result } = await workflow.run({
variables: { input,
filters: {
...req.filterableFields,
},
...req.remoteQueryConfig.pagination,
},
fields: req.remoteQueryConfig.fields,
}) })
const { rows: orders, metadata } = await remoteQuery(queryObject) res.status(200).json({ return: result })
res.json({
orders,
count: metadata.count,
offset: metadata.skip,
limit: metadata.take,
})
} }
@@ -19,17 +19,13 @@ export const GET = async (
filters: { filters: {
...req.filterableFields, ...req.filterableFields,
}, },
...req.remoteQueryConfig.pagination,
}, },
fields: req.remoteQueryConfig.fields, fields: req.remoteQueryConfig.fields,
}) })
const { rows: orders, metadata } = await remoteQuery(queryObject) const [orderReturn] = await remoteQuery(queryObject)
res.json({ res.json({
orders, return: orderReturn,
count: metadata.count,
offset: metadata.skip,
limit: metadata.take,
}) })
} }
@@ -16,7 +16,7 @@ export const GET = async (
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const queryObject = remoteQueryObjectFromString({ const queryObject = remoteQueryObjectFromString({
entryPoint: "order", entryPoint: "returns",
variables: { variables: {
filters: { filters: {
...req.filterableFields, ...req.filterableFields,
@@ -26,10 +26,10 @@ export const GET = async (
fields: req.remoteQueryConfig.fields, fields: req.remoteQueryConfig.fields,
}) })
const { rows: orders, metadata } = await remoteQuery(queryObject) const { rows: returns, metadata } = await remoteQuery(queryObject)
res.json({ res.json({
orders, returns,
count: metadata.count, count: metadata.count,
offset: metadata.skip, offset: metadata.skip,
limit: metadata.take, limit: metadata.take,
@@ -53,7 +53,7 @@ export const AdminPostReturnsReqSchema = z.object({
order_id: z.string(), order_id: z.string(),
items: z.array(ItemSchema), items: z.array(ItemSchema),
return_shipping: ReturnShippingSchema, return_shipping: ReturnShippingSchema,
note: z.string().nullish(), internal_note: z.string().nullish(),
receive_now: z.boolean().optional(), receive_now: z.boolean().optional(),
refund_amount: z.number().optional(), refund_amount: z.number().optional(),
location_id: z.string().nullish(), location_id: z.string().nullish(),
@@ -61,3 +61,12 @@ export const AdminPostReturnsReqSchema = z.object({
export type AdminPostReturnsReqSchemaType = z.infer< export type AdminPostReturnsReqSchemaType = z.infer<
typeof AdminPostReturnsReqSchema typeof AdminPostReturnsReqSchema
> >
export const AdminPostReceiveReturnsReqSchema = z.object({
return_id: z.string(),
items: z.array(ItemSchema),
internal_note: z.string().nullish(),
})
export type AdminPostReceiveReturnsReqSchemaType = z.infer<
typeof AdminPostReceiveReturnsReqSchema
>
@@ -3,9 +3,9 @@ import {
MedusaResponse, MedusaResponse,
} from "../../../../../types/routing" } from "../../../../../types/routing"
import { LinkMethodRequest } from "@medusajs/types/src"
import { refetchSalesChannel } from "../../helpers"
import { linkProductsToSalesChannelWorkflow } from "@medusajs/core-flows" import { linkProductsToSalesChannelWorkflow } from "@medusajs/core-flows"
import { LinkMethodRequest } from "@medusajs/types"
import { refetchSalesChannel } from "../../helpers"
export const POST = async ( export const POST = async (
req: AuthenticatedMedusaRequest<LinkMethodRequest>, req: AuthenticatedMedusaRequest<LinkMethodRequest>,
@@ -3,9 +3,9 @@ import {
MedusaResponse, MedusaResponse,
} from "../../../../../types/routing" } from "../../../../../types/routing"
import { LinkMethodRequest } from "@medusajs/types/src"
import { refetchStockLocation } from "../../helpers"
import { linkSalesChannelsToStockLocationWorkflow } from "@medusajs/core-flows" import { linkSalesChannelsToStockLocationWorkflow } from "@medusajs/core-flows"
import { LinkMethodRequest } from "@medusajs/types"
import { refetchStockLocation } from "../../helpers"
export const POST = async ( export const POST = async (
req: AuthenticatedMedusaRequest<LinkMethodRequest>, req: AuthenticatedMedusaRequest<LinkMethodRequest>,
@@ -1,7 +1,3 @@
import { FulfillmentEvents, GeoZoneType, Modules } from "@medusajs/utils"
import { moduleIntegrationTestRunner } from "medusa-test-utils"
import { MockEventBusService } from "medusa-test-utils/dist"
import { buildExpectedEventMessageShape } from "../../__fixtures__"
import { import {
CreateFulfillmentSetDTO, CreateFulfillmentSetDTO,
CreateServiceZoneDTO, CreateServiceZoneDTO,
@@ -9,6 +5,12 @@ import {
ServiceZoneDTO, ServiceZoneDTO,
UpdateFulfillmentSetDTO, UpdateFulfillmentSetDTO,
} from "@medusajs/types" } from "@medusajs/types"
import { FulfillmentEvents, GeoZoneType, Modules } from "@medusajs/utils"
import {
MockEventBusService,
moduleIntegrationTestRunner,
} from "medusa-test-utils"
import { buildExpectedEventMessageShape } from "../../__fixtures__"
jest.setTimeout(100000) jest.setTimeout(100000)
@@ -1,15 +1,15 @@
import { ModulesDefinition } from "@medusajs/modules-sdk" import { ModulesDefinition } from "@medusajs/modules-sdk"
import { FulfillmentSetDTO, IFulfillmentModuleService } from "@medusajs/types" import { FulfillmentSetDTO, IFulfillmentModuleService } from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import { FulfillmentProviderService } from "@services"
import { import {
SuiteOptions,
initModules, initModules,
moduleIntegrationTestRunner, moduleIntegrationTestRunner,
SuiteOptions, } from "medusa-test-utils"
} from "medusa-test-utils/dist"
import { resolve } from "path" import { resolve } from "path"
import { createFullDataStructure } from "../../__fixtures__" import { createFullDataStructure } from "../../__fixtures__"
import { FulfillmentProviderService } from "@services"
import { FulfillmentProviderServiceFixtures } from "../../__fixtures__/providers" import { FulfillmentProviderServiceFixtures } from "../../__fixtures__/providers"
import { Modules } from "@medusajs/utils"
let moduleOptions = { let moduleOptions = {
providers: [ providers: [
@@ -1,20 +1,20 @@
import { import {
CreateShippingOptionDTO, CreateShippingOptionDTO,
IFulfillmentModuleService, IFulfillmentModuleService,
UpdateShippingOptionDTO,
} from "@medusajs/types" } from "@medusajs/types"
import { FulfillmentEvents, GeoZoneType, Modules } from "@medusajs/utils"
import { FulfillmentProviderService } from "@services"
import { import {
MockEventBusService, MockEventBusService,
moduleIntegrationTestRunner, moduleIntegrationTestRunner,
} from "medusa-test-utils" } from "medusa-test-utils"
import { resolve } from "path"
import { import {
buildExpectedEventMessageShape, buildExpectedEventMessageShape,
generateCreateShippingOptionsData, generateCreateShippingOptionsData,
} from "../../__fixtures__" } from "../../__fixtures__"
import { resolve } from "path"
import { FulfillmentProviderService } from "@services"
import { FulfillmentProviderServiceFixtures } from "../../__fixtures__/providers" import { FulfillmentProviderServiceFixtures } from "../../__fixtures__/providers"
import { FulfillmentEvents, GeoZoneType, Modules } from "@medusajs/utils"
import { UpdateShippingOptionDTO } from "@medusajs/types/src"
jest.setTimeout(100000) jest.setTimeout(100000)
@@ -1,10 +1,7 @@
import { INotificationModuleService } from "@medusajs/types" import { INotificationModuleService } from "@medusajs/types"
import {
moduleIntegrationTestRunner,
SuiteOptions,
} from "medusa-test-utils/dist"
import { resolve } from "path"
import { Modules } from "@medusajs/utils" import { Modules } from "@medusajs/utils"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { resolve } from "path"
let moduleOptions = { let moduleOptions = {
providers: [ providers: [
@@ -1,10 +1,10 @@
import { IPaymentModuleService } from "@medusajs/types" import { IPaymentModuleService } from "@medusajs/types"
import { Modules, promiseAll } from "@medusajs/utils" import { Modules, promiseAll } from "@medusajs/utils"
import { moduleIntegrationTestRunner } from "medusa-test-utils/dist" import { moduleIntegrationTestRunner } from "medusa-test-utils"
import { import {
createPaymentCollections, createPaymentCollections,
createPayments,
createPaymentSessions, createPaymentSessions,
createPayments,
} from "../../../__fixtures__" } from "../../../__fixtures__"
jest.setTimeout(30000) jest.setTimeout(30000)