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:
Sebastian Rindom
2023-10-11 11:01:56 -07:00
committed by GitHub
parent bbd9dd408f
commit 66413d094e
25 changed files with 480 additions and 281 deletions

View File

@@ -1,2 +1,3 @@
export * from "./cart"
export * from "./product"
export * from "./inventory"

View File

@@ -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
)

View File

@@ -0,0 +1 @@
export * from "./create-inventory-item"

View File

@@ -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(

View File

@@ -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",
}