Feat(core-flows, inventory-next, medusa, types): Add create location level endpoint (#6695)

* initialize create location levels

* add enough from pr to make code build and test

* fix integration tests

* pr feedback

* fix errors

* rm dto

* Update packages/core-flows/src/inventory/steps/validate-inventory-locations.ts

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Philip KorsholmandOli Juhl authored and GitHub committed 2024-03-18 10:28:57 +01:00
1 parent e77a02aca5
commit 84b8836cbf
14 files changed
+266 -139

No files matched your search

+1
View File
@@ -7,6 +7,7 @@ export * from "./definitions"
export * from "./fulfillment"
export * as Handlers from "./handlers"
export * from "./invite"
export * from "./inventory"
export * from "./payment"
export * from "./price-list"
export * from "./pricing"
@@ -0,0 +1,2 @@
export * from "./workflows"
export * from "./steps"
@@ -0,0 +1,34 @@
import {
CreateInventoryLevelInput,
IInventoryServiceNext,
} from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const createInventoryLevelsStepId = "create-inventory-levels"
export const createInventoryLevelsStep = createStep(
createInventoryLevelsStepId,
async (data: CreateInventoryLevelInput[], { container }) => {
const service = container.resolve<IInventoryServiceNext>(
ModuleRegistrationName.INVENTORY
)
const inventoryLevels = await service.createInventoryLevels(data)
return new StepResponse(
inventoryLevels,
inventoryLevels.map((level) => level.id)
)
},
async (ids, { container }) => {
if (!ids?.length) {
return
}
const service = container.resolve<IInventoryServiceNext>(
ModuleRegistrationName.INVENTORY
)
await service.deleteInventoryLevels(ids)
}
)
@@ -0,0 +1,2 @@
export * from "./create-inventory-levels"
export * from "./validate-inventory-locations"
@@ -0,0 +1,40 @@
import {
ContainerRegistrationKeys,
MedusaError,
arrayDifference,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { CreateInventoryLevelInput } from "@medusajs/types"
import { createStep } from "@medusajs/workflows-sdk"
export const validateInventoryLocationsStepId = "validate-inventory-levels-step"
export const validateInventoryLocationsStep = createStep(
validateInventoryLocationsStepId,
async (data: CreateInventoryLevelInput[], { container }) => {
const remoteQuery = container.resolve(
ContainerRegistrationKeys.REMOTE_QUERY
)
const locationQuery = remoteQueryObjectFromString({
entryPoint: "stock_location",
variables: {
id: data.map((d) => d.location_id),
},
fields: ["id"],
})
const stockLocations = await remoteQuery(locationQuery)
const diff = arrayDifference(
data.map((d) => d.location_id),
stockLocations.map((l) => l.id)
)
if (diff.length > 0) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Stock locations with ids: ${diff.join(", ")} was not found`
)
}
}
)
@@ -0,0 +1,20 @@
import { CreateInventoryLevelInput, InventoryLevelDTO } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import {
createInventoryLevelsStep,
validateInventoryLocationsStep,
} from "../steps"
interface WorkflowInput {
inventory_levels: CreateInventoryLevelInput[]
}
export const createInventoryLevelsWorkflowId =
"create-inventory-levels-workflow"
export const createInventoryLevelsWorkflow = createWorkflow(
createInventoryLevelsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<InventoryLevelDTO[]> => {
validateInventoryLocationsStep(input.inventory_levels)
return createInventoryLevelsStep(input.inventory_levels)
}
)
@@ -0,0 +1 @@
export * from "./create-inventory-levels"