fix(workflows): Workflow types (#4673)

* fix(workflow): types and handlers

* add middleware pipe example

* Create nine-gorillas-collect.md
This commit is contained in:
Adrien de Peretti
2023-08-02 13:50:06 +02:00
committed by GitHub
parent dae34297eb
commit e78c47b66f
8 changed files with 56 additions and 59 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/workflows": patch
---
Feat/workflow confusion elimination
@@ -1,9 +1,10 @@
import { InventoryItemDTO, ProductTypes } from "@medusajs/types"
import { InputAlias } from "../definitions"
import { WorkflowArguments } from "../helper"
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
import { ProductVariantInventoryItem } from "@medusajs/medusa/src"
export async function attachInventoryItems({
export async function attachInventoryItems<T = ProductVariantInventoryItem[]>({
container,
data,
}: WorkflowArguments & {
@@ -11,13 +12,13 @@ export async function attachInventoryItems({
variant: ProductTypes.ProductVariantDTO
[InputAlias.InventoryItems]: InventoryItemDTO
}[]
}) {
}): Promise<PipelineHandlerResult<T>> {
const manager = container.resolve("manager")
const productVariantInventoryService = container
.resolve("productVariantInventoryService")
.withTransaction(manager)
const value = await Promise.all(
return (await Promise.all(
data
.filter((d) => d)
.map(async ({ variant, [InputAlias.InventoryItems]: inventoryItem }) => {
@@ -26,10 +27,5 @@ export async function attachInventoryItems({
inventoryItem.id
)
})
)
return {
alias: InputAlias.AttachedInventoryItems,
value,
}
)) as PipelineHandlerResult<T>
}
@@ -1,16 +1,22 @@
import { IInventoryService, ProductTypes } from "@medusajs/types"
import {
IInventoryService,
InventoryItemDTO,
ProductTypes,
} from "@medusajs/types"
import { InputAlias } from "../definitions"
import { WorkflowArguments } from "../helper"
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
export async function createInventoryItems({
export async function createInventoryItems<
T = { variant: any; inventoryItem: InventoryItemDTO }[]
>({
container,
data,
}: WorkflowArguments & {
data: {
[InputAlias.Products]: ProductTypes.ProductDTO[]
}
}) {
}): Promise<PipelineHandlerResult<T>> {
const manager = container.resolve("manager")
const inventoryService: IInventoryService =
container.resolve("inventoryService")
@@ -27,7 +33,7 @@ export async function createInventoryItems({
[]
)
const value = await Promise.all(
return (await Promise.all(
variants.map(async (variant) => {
if (!variant.manage_inventory) {
return
@@ -50,10 +56,5 @@ export async function createInventoryItems({
return { variant, inventoryItem }
})
)
return {
alias: InputAlias.InventoryItems,
value,
}
)) as PipelineHandlerResult<T>
}
@@ -1,23 +1,15 @@
import { InputAlias } from "../definitions"
import { ProductTypes } from "@medusajs/types"
import { WorkflowArguments } from "../helper"
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
export async function createProducts({
export async function createProducts<T = ProductTypes.ProductDTO[]>({
container,
context,
data,
}: WorkflowArguments & {
data: { [InputAlias.Products]: ProductTypes.CreateProductDTO[] }
}) {
}): Promise<PipelineHandlerResult<T>> {
const productModuleService = container.resolve("productModuleService")
const value = await productModuleService.create(
data[InputAlias.Products],
context
)
return {
alias: InputAlias.Products,
value,
}
return await productModuleService.create(data[InputAlias.Products], context)
}
@@ -0,0 +1,9 @@
import { InputAlias } from "../../definitions"
import { PipelineHandlerResult } from "../../helper"
export function createProductsPrepareData({ data }): PipelineHandlerResult {
return {
alias: InputAlias.Products as string,
value: [{}],
}
}
@@ -1,31 +1,26 @@
import { InventoryItemDTO, MedusaContainer } from "@medusajs/types"
import { InventoryItemDTO } from "@medusajs/types"
import { InputAlias } from "../definitions"
import { WorkflowArguments } from "../helper"
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
export async function removeInventoryItems({
export async function removeInventoryItems<T = void[]>({
container,
data,
}: WorkflowArguments & {
data: {
[InputAlias.InventoryItems]: InventoryItemDTO
}[]
}) {
}): Promise<PipelineHandlerResult<T>> {
const manager = container.resolve("manager")
const inventoryService = container.resolve("inventoryService")
const context = { transactionManager: manager }
const value = await Promise.all(
return (await Promise.all(
data.map(async ({ [InputAlias.InventoryItems]: inventoryItem }) => {
return await inventoryService!.deleteInventoryItem(
inventoryItem.id,
context
)
})
)
return {
alias: InputAlias.RemovedInventoryItems,
value,
}
)) as PipelineHandlerResult<T>
}
@@ -1,22 +1,17 @@
import { InputAlias } from "../definitions"
import { ProductTypes } from "@medusajs/types"
import { WorkflowArguments } from "../helper"
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
export async function removeProducts({
export async function removeProducts<T = any>({
container,
data,
}: WorkflowArguments & {
data: {
[InputAlias.Products]: ProductTypes.ProductDTO[]
}
}) {
}): Promise<PipelineHandlerResult<T>> {
const productModuleService = container.resolve("productModuleService")
const value = await productModuleService.softDelete(
return await productModuleService.softDelete(
data[InputAlias.Products].map((p) => p.id)
)
return {
alias: InputAlias.RemovedProducts,
value,
}
}
+10 -6
View File
@@ -6,20 +6,20 @@ import {
import { InputAlias } from "../definitions"
type WorkflowStepReturn = {
type WorkflowStepMiddlewareReturn = {
alias: string
value: any
}
type WorkflowStepInput = {
type WorkflowStepMiddlewareInput = {
from: string
alias: string
}
interface PipelineInput {
inputAlias?: InputAlias | string
invoke?: WorkflowStepInput | WorkflowStepInput[]
compensate?: WorkflowStepInput | WorkflowStepInput[]
invoke?: WorkflowStepMiddlewareInput | WorkflowStepMiddlewareInput[]
compensate?: WorkflowStepMiddlewareInput | WorkflowStepMiddlewareInput[]
}
export type WorkflowArguments = {
@@ -30,9 +30,13 @@ export type WorkflowArguments = {
context: Context | SharedContext
}
export type PipelineHandler = (
export type PipelineHandlerResult<T = undefined> = T extends undefined
? WorkflowStepMiddlewareReturn | WorkflowStepMiddlewareReturn[]
: T
export type PipelineHandler<T extends any = undefined> = (
args: WorkflowArguments
) => Promise<WorkflowStepReturn | WorkflowStepReturn[]>
) => PipelineHandlerResult<T> | Promise<PipelineHandlerResult<T>>
export function pipe(
input: PipelineInput,