fix(workflows): Workflow types (#4673)
* fix(workflow): types and handlers * add middleware pipe example * Create nine-gorillas-collect.md
This commit is contained in:
1 parent
dae34297eb
commit
e78c47b66f
8 files changed
+56
-59
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/workflows": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Feat/workflow confusion elimination
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
import { InventoryItemDTO, ProductTypes } from "@medusajs/types"
|
import { InventoryItemDTO, ProductTypes } from "@medusajs/types"
|
||||||
|
|
||||||
import { InputAlias } from "../definitions"
|
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,
|
container,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments & {
|
}: WorkflowArguments & {
|
||||||
@@ -11,13 +12,13 @@ export async function attachInventoryItems({
|
|||||||
variant: ProductTypes.ProductVariantDTO
|
variant: ProductTypes.ProductVariantDTO
|
||||||
[InputAlias.InventoryItems]: InventoryItemDTO
|
[InputAlias.InventoryItems]: InventoryItemDTO
|
||||||
}[]
|
}[]
|
||||||
}) {
|
}): Promise<PipelineHandlerResult<T>> {
|
||||||
const manager = container.resolve("manager")
|
const manager = container.resolve("manager")
|
||||||
const productVariantInventoryService = container
|
const productVariantInventoryService = container
|
||||||
.resolve("productVariantInventoryService")
|
.resolve("productVariantInventoryService")
|
||||||
.withTransaction(manager)
|
.withTransaction(manager)
|
||||||
|
|
||||||
const value = await Promise.all(
|
return (await Promise.all(
|
||||||
data
|
data
|
||||||
.filter((d) => d)
|
.filter((d) => d)
|
||||||
.map(async ({ variant, [InputAlias.InventoryItems]: inventoryItem }) => {
|
.map(async ({ variant, [InputAlias.InventoryItems]: inventoryItem }) => {
|
||||||
@@ -26,10 +27,5 @@ export async function attachInventoryItems({
|
|||||||
inventoryItem.id
|
inventoryItem.id
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
)
|
)) as PipelineHandlerResult<T>
|
||||||
|
|
||||||
return {
|
|
||||||
alias: InputAlias.AttachedInventoryItems,
|
|
||||||
value,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,22 @@
|
|||||||
import { IInventoryService, ProductTypes } from "@medusajs/types"
|
import {
|
||||||
|
IInventoryService,
|
||||||
|
InventoryItemDTO,
|
||||||
|
ProductTypes,
|
||||||
|
} from "@medusajs/types"
|
||||||
|
|
||||||
import { InputAlias } from "../definitions"
|
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,
|
container,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments & {
|
}: WorkflowArguments & {
|
||||||
data: {
|
data: {
|
||||||
[InputAlias.Products]: ProductTypes.ProductDTO[]
|
[InputAlias.Products]: ProductTypes.ProductDTO[]
|
||||||
}
|
}
|
||||||
}) {
|
}): Promise<PipelineHandlerResult<T>> {
|
||||||
const manager = container.resolve("manager")
|
const manager = container.resolve("manager")
|
||||||
const inventoryService: IInventoryService =
|
const inventoryService: IInventoryService =
|
||||||
container.resolve("inventoryService")
|
container.resolve("inventoryService")
|
||||||
@@ -27,7 +33,7 @@ export async function createInventoryItems({
|
|||||||
[]
|
[]
|
||||||
)
|
)
|
||||||
|
|
||||||
const value = await Promise.all(
|
return (await Promise.all(
|
||||||
variants.map(async (variant) => {
|
variants.map(async (variant) => {
|
||||||
if (!variant.manage_inventory) {
|
if (!variant.manage_inventory) {
|
||||||
return
|
return
|
||||||
@@ -50,10 +56,5 @@ export async function createInventoryItems({
|
|||||||
|
|
||||||
return { variant, inventoryItem }
|
return { variant, inventoryItem }
|
||||||
})
|
})
|
||||||
)
|
)) as PipelineHandlerResult<T>
|
||||||
|
|
||||||
return {
|
|
||||||
alias: InputAlias.InventoryItems,
|
|
||||||
value,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,15 @@
|
|||||||
import { InputAlias } from "../definitions"
|
import { InputAlias } from "../definitions"
|
||||||
import { ProductTypes } from "@medusajs/types"
|
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,
|
container,
|
||||||
context,
|
context,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments & {
|
}: WorkflowArguments & {
|
||||||
data: { [InputAlias.Products]: ProductTypes.CreateProductDTO[] }
|
data: { [InputAlias.Products]: ProductTypes.CreateProductDTO[] }
|
||||||
}) {
|
}): Promise<PipelineHandlerResult<T>> {
|
||||||
const productModuleService = container.resolve("productModuleService")
|
const productModuleService = container.resolve("productModuleService")
|
||||||
|
|
||||||
const value = await productModuleService.create(
|
return await productModuleService.create(data[InputAlias.Products], context)
|
||||||
data[InputAlias.Products],
|
|
||||||
context
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
|
||||||
alias: InputAlias.Products,
|
|
||||||
value,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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 { InputAlias } from "../definitions"
|
||||||
import { WorkflowArguments } from "../helper"
|
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
|
||||||
|
|
||||||
export async function removeInventoryItems({
|
export async function removeInventoryItems<T = void[]>({
|
||||||
container,
|
container,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments & {
|
}: WorkflowArguments & {
|
||||||
data: {
|
data: {
|
||||||
[InputAlias.InventoryItems]: InventoryItemDTO
|
[InputAlias.InventoryItems]: InventoryItemDTO
|
||||||
}[]
|
}[]
|
||||||
}) {
|
}): Promise<PipelineHandlerResult<T>> {
|
||||||
const manager = container.resolve("manager")
|
const manager = container.resolve("manager")
|
||||||
const inventoryService = container.resolve("inventoryService")
|
const inventoryService = container.resolve("inventoryService")
|
||||||
const context = { transactionManager: manager }
|
const context = { transactionManager: manager }
|
||||||
|
|
||||||
const value = await Promise.all(
|
return (await Promise.all(
|
||||||
data.map(async ({ [InputAlias.InventoryItems]: inventoryItem }) => {
|
data.map(async ({ [InputAlias.InventoryItems]: inventoryItem }) => {
|
||||||
return await inventoryService!.deleteInventoryItem(
|
return await inventoryService!.deleteInventoryItem(
|
||||||
inventoryItem.id,
|
inventoryItem.id,
|
||||||
context
|
context
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
)
|
)) as PipelineHandlerResult<T>
|
||||||
|
|
||||||
return {
|
|
||||||
alias: InputAlias.RemovedInventoryItems,
|
|
||||||
value,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,17 @@
|
|||||||
import { InputAlias } from "../definitions"
|
import { InputAlias } from "../definitions"
|
||||||
import { ProductTypes } from "@medusajs/types"
|
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,
|
container,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments & {
|
}: WorkflowArguments & {
|
||||||
data: {
|
data: {
|
||||||
[InputAlias.Products]: ProductTypes.ProductDTO[]
|
[InputAlias.Products]: ProductTypes.ProductDTO[]
|
||||||
}
|
}
|
||||||
}) {
|
}): Promise<PipelineHandlerResult<T>> {
|
||||||
const productModuleService = container.resolve("productModuleService")
|
const productModuleService = container.resolve("productModuleService")
|
||||||
const value = await productModuleService.softDelete(
|
return await productModuleService.softDelete(
|
||||||
data[InputAlias.Products].map((p) => p.id)
|
data[InputAlias.Products].map((p) => p.id)
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
|
||||||
alias: InputAlias.RemovedProducts,
|
|
||||||
value,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -6,20 +6,20 @@ import {
|
|||||||
|
|
||||||
import { InputAlias } from "../definitions"
|
import { InputAlias } from "../definitions"
|
||||||
|
|
||||||
type WorkflowStepReturn = {
|
type WorkflowStepMiddlewareReturn = {
|
||||||
alias: string
|
alias: string
|
||||||
value: any
|
value: any
|
||||||
}
|
}
|
||||||
|
|
||||||
type WorkflowStepInput = {
|
type WorkflowStepMiddlewareInput = {
|
||||||
from: string
|
from: string
|
||||||
alias: string
|
alias: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PipelineInput {
|
interface PipelineInput {
|
||||||
inputAlias?: InputAlias | string
|
inputAlias?: InputAlias | string
|
||||||
invoke?: WorkflowStepInput | WorkflowStepInput[]
|
invoke?: WorkflowStepMiddlewareInput | WorkflowStepMiddlewareInput[]
|
||||||
compensate?: WorkflowStepInput | WorkflowStepInput[]
|
compensate?: WorkflowStepMiddlewareInput | WorkflowStepMiddlewareInput[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export type WorkflowArguments = {
|
export type WorkflowArguments = {
|
||||||
@@ -30,9 +30,13 @@ export type WorkflowArguments = {
|
|||||||
context: Context | SharedContext
|
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
|
args: WorkflowArguments
|
||||||
) => Promise<WorkflowStepReturn | WorkflowStepReturn[]>
|
) => PipelineHandlerResult<T> | Promise<PipelineHandlerResult<T>>
|
||||||
|
|
||||||
export function pipe(
|
export function pipe(
|
||||||
input: PipelineInput,
|
input: PipelineInput,
|
||||||
|
|||||||
Reference in new issue
Block a user