Feat(core-flows, inventory-next, medusa, types): Add create inventory item endpoint (#6693)
* init create inventory item * fix integration tests * pr feedback * rename to validate * pr feedback * bulk delete * undo change w/ naming * update dto body * move integration test * pr fixes * fix for pr review * revert medusa-config changes * pr feedback * fix build
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
import { InventoryItemDTO } from "@medusajs/types"
|
||||
|
||||
export const attachInventoryItemToVariantsStepId =
|
||||
"attach-inventory-items-to-variants-step"
|
||||
export const attachInventoryItemToVariants = createStep(
|
||||
attachInventoryItemToVariantsStepId,
|
||||
async (
|
||||
input: {
|
||||
inventoryItemId: string
|
||||
tag?: string
|
||||
}[],
|
||||
{ container }
|
||||
) => {
|
||||
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
const linkDefinitions = input
|
||||
.filter(({ tag }) => !!tag)
|
||||
.map(({ inventoryItemId, tag }) => ({
|
||||
productService: {
|
||||
variant_id: tag,
|
||||
},
|
||||
inventoryService: {
|
||||
inventory_item_id: inventoryItemId,
|
||||
},
|
||||
}))
|
||||
|
||||
const links = await remoteLink.create(linkDefinitions)
|
||||
|
||||
return new StepResponse(links, linkDefinitions)
|
||||
},
|
||||
async (linkDefinitions, { container }) => {
|
||||
if (!linkDefinitions?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
await remoteLink.dismiss(linkDefinitions)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,34 @@
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
import { CreateInventoryItemInput } from "@medusajs/types"
|
||||
import { IInventoryServiceNext } from "@medusajs/types"
|
||||
import { InventoryNext } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "../../../../modules-sdk/dist"
|
||||
import { promiseAll } from "@medusajs/utils"
|
||||
|
||||
export const createInventoryItemsStepId = "create-inventory-items"
|
||||
export const createInventoryItemsStep = createStep(
|
||||
createInventoryItemsStepId,
|
||||
async (data: InventoryNext.CreateInventoryItemInput[], { container }) => {
|
||||
const inventoryService: IInventoryServiceNext = container.resolve(
|
||||
ModuleRegistrationName.INVENTORY
|
||||
)
|
||||
|
||||
const createdItems: InventoryNext.InventoryItemDTO[] =
|
||||
await inventoryService.create(data)
|
||||
|
||||
return new StepResponse(
|
||||
createdItems,
|
||||
createdItems.map((i) => i.id)
|
||||
)
|
||||
},
|
||||
async (data: string[] | undefined, { container }) => {
|
||||
if (!data?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const inventoryService = container.resolve(ModuleRegistrationName.INVENTORY)
|
||||
|
||||
await inventoryService!.delete(data)
|
||||
}
|
||||
)
|
||||
@@ -1,2 +1,5 @@
|
||||
export * from "./attach-inventory-items"
|
||||
export * from "./create-inventory-items"
|
||||
export * from "./validate-singular-inventory-items-for-tags"
|
||||
export * from "./create-inventory-levels"
|
||||
export * from "./validate-inventory-locations"
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
import { InventoryNext } from "@medusajs/types"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
export const validateInventoryItemsForCreateStepId =
|
||||
"validate-inventory-items-for-create-step"
|
||||
export const validateInventoryItemsForCreate = createStep(
|
||||
validateInventoryItemsForCreateStepId,
|
||||
async (
|
||||
input: {
|
||||
tag?: string
|
||||
}[],
|
||||
{ container }
|
||||
) => {
|
||||
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
const linkService = remoteLink.getLinkModule(
|
||||
Modules.PRODUCT,
|
||||
"variant_id",
|
||||
Modules.INVENTORY,
|
||||
"inventory_item_id"
|
||||
)
|
||||
|
||||
const existingItems = await linkService.list(
|
||||
{ variant_id: input.map((i) => i.tag) },
|
||||
{ select: ["variant_id", "inventory_item_id"] }
|
||||
)
|
||||
|
||||
if (existingItems.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
"Inventory items already exist for variants with ids: " +
|
||||
existingItems.map((i) => i.variant_id).join(", ")
|
||||
)
|
||||
}
|
||||
|
||||
return new StepResponse(input)
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,26 @@
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
attachInventoryItemToVariants,
|
||||
createInventoryItemsStep,
|
||||
validateInventoryItemsForCreate,
|
||||
} from "../steps"
|
||||
|
||||
import { InventoryNext } from "@medusajs/types"
|
||||
|
||||
interface WorkflowInput {
|
||||
items: InventoryNext.CreateInventoryItemInput[]
|
||||
}
|
||||
|
||||
export const createInventoryItemsWorkflowId = "create-inventory-items-workflow"
|
||||
export const createInventoryItemsWorkflow = createWorkflow(
|
||||
createInventoryItemsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>) => {
|
||||
const items = createInventoryItemsStep(input.items)
|
||||
|
||||
return items
|
||||
}
|
||||
)
|
||||
@@ -1 +1,2 @@
|
||||
export * from "./create-inventory-items"
|
||||
export * from "./create-inventory-levels"
|
||||
|
||||
Reference in New Issue
Block a user