feat: move create inventory to @medusajs/workflows (#5301)
**Why** - We have some workflow-like flows in @medusajs/medusa. These should be moved over to the workflows package. - Inventory Items <> Variant currently assume a 1-1 mapping. There should be support for a many-to-many mapping. **What** - PR introduces a feature flag for supporting many-to-many mappings for inventory and variants. - Deletes legacy transaction handler in @medusajs/medusa. - Adjusts existing createInventoryItems handler to remove dependency on variant data. **Unkowns** ~~1. Couldn't find an existing test for the CreateProduct workflow. It should be tested that this still works as expected.~~ 2. Have removed transaction managers as we should move to handling consistency through orchestration tooling. Are we ready for that?
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
export * from "./cart"
|
||||
export * from "./product"
|
||||
export * from "./inventory"
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import { Workflows } from "../../definitions"
|
||||
import {
|
||||
TransactionStepsDefinition,
|
||||
WorkflowManager,
|
||||
} from "@medusajs/orchestration"
|
||||
import { exportWorkflow, pipe } from "../../helper"
|
||||
|
||||
import { InventoryTypes, WorkflowTypes } from "@medusajs/types"
|
||||
import { InventoryHandlers } from "../../handlers"
|
||||
|
||||
export enum CreateInventoryItemActions {
|
||||
prepare = "prepare",
|
||||
createInventoryItems = "createInventoryItems",
|
||||
}
|
||||
|
||||
const workflowSteps: TransactionStepsDefinition = {
|
||||
next: {
|
||||
action: CreateInventoryItemActions.createInventoryItems,
|
||||
},
|
||||
}
|
||||
|
||||
const handlers = new Map([
|
||||
[
|
||||
CreateInventoryItemActions.createInventoryItems,
|
||||
{
|
||||
invoke: pipe(
|
||||
{
|
||||
inputAlias: CreateInventoryItemActions.prepare,
|
||||
merge: true,
|
||||
invoke: {
|
||||
from: CreateInventoryItemActions.prepare,
|
||||
},
|
||||
},
|
||||
InventoryHandlers.createInventoryItems
|
||||
),
|
||||
compensate: pipe(
|
||||
{
|
||||
merge: true,
|
||||
invoke: {
|
||||
from: CreateInventoryItemActions.createInventoryItems,
|
||||
alias:
|
||||
InventoryHandlers.removeInventoryItems.aliases.inventoryItems,
|
||||
},
|
||||
},
|
||||
InventoryHandlers.removeInventoryItems
|
||||
),
|
||||
},
|
||||
],
|
||||
])
|
||||
|
||||
WorkflowManager.register(
|
||||
Workflows.CreateInventoryItems,
|
||||
workflowSteps,
|
||||
handlers
|
||||
)
|
||||
|
||||
export const createInventoryItems = exportWorkflow<
|
||||
WorkflowTypes.InventoryWorkflow.CreateInventoryItemsWorkflowInputDTO,
|
||||
{ tag: string; inventoryItem: InventoryTypes.InventoryItemDTO }[]
|
||||
>(
|
||||
Workflows.CreateInventoryItems,
|
||||
CreateInventoryItemActions.createInventoryItems,
|
||||
async (data) => data
|
||||
)
|
||||
1
packages/workflows/src/definition/inventory/index.ts
Normal file
1
packages/workflows/src/definition/inventory/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./create-inventory-item"
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
MiddlewaresHandlers,
|
||||
ProductHandlers,
|
||||
} from "../../handlers"
|
||||
import { prepareCreateInventoryItems } from "./prepare-create-inventory-items"
|
||||
|
||||
export enum CreateProductsActions {
|
||||
prepare = "prepare",
|
||||
@@ -175,9 +176,10 @@ const handlers = new Map([
|
||||
merge: true,
|
||||
invoke: {
|
||||
from: CreateProductsActions.createProducts,
|
||||
alias: InventoryHandlers.createInventoryItems.aliases.products,
|
||||
alias: prepareCreateInventoryItems.aliases.products,
|
||||
},
|
||||
},
|
||||
prepareCreateInventoryItems,
|
||||
InventoryHandlers.createInventoryItems
|
||||
),
|
||||
compensate: pipe(
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import { WorkflowArguments } from "../../helper"
|
||||
|
||||
type AssociationTaggedVariant = ProductTypes.ProductVariantDTO & {
|
||||
_associationTag?: string
|
||||
}
|
||||
|
||||
type ObjectWithVariant = { variants: ProductTypes.ProductVariantDTO[] }
|
||||
|
||||
export async function prepareCreateInventoryItems({
|
||||
data,
|
||||
}: WorkflowArguments<{
|
||||
products: ObjectWithVariant[]
|
||||
}>) {
|
||||
const taggedVariants = data.products.reduce<AssociationTaggedVariant[]>(
|
||||
(acc, product: ObjectWithVariant) => {
|
||||
const cleanVariants = product.variants.reduce<AssociationTaggedVariant[]>(
|
||||
(acc, variant: AssociationTaggedVariant) => {
|
||||
if (!variant.manage_inventory) {
|
||||
return acc
|
||||
}
|
||||
|
||||
variant._associationTag = variant.id
|
||||
|
||||
acc.push(variant)
|
||||
return acc
|
||||
},
|
||||
[]
|
||||
)
|
||||
return acc.concat(cleanVariants)
|
||||
},
|
||||
[]
|
||||
)
|
||||
|
||||
return {
|
||||
alias: prepareCreateInventoryItems.aliases.output,
|
||||
value: {
|
||||
inventoryItems: taggedVariants,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
prepareCreateInventoryItems.aliases = {
|
||||
products: "products",
|
||||
output: "prepareCreateInventoryItemsOutput",
|
||||
}
|
||||
Reference in New Issue
Block a user