chore(workflows-sdk): check exported workflow (#7592)

This commit is contained in:
Carlos R. L. Rodrigues
2024-06-04 07:18:40 -03:00
committed by GitHub
parent 68fb04b849
commit 41df24e2dc
5 changed files with 71 additions and 49 deletions

View File

@@ -1,3 +1,4 @@
import { WorkflowManager } from "@medusajs/orchestration"
import { promiseAll } from "@medusajs/utils"
import {
createStep,
@@ -14,6 +15,7 @@ jest.setTimeout(30000)
const afterEach_ = () => {
jest.clearAllMocks()
MedusaWorkflow.workflows = {}
WorkflowManager.unregisterAll()
}
describe("Workflow composer", function () {
@@ -1972,4 +1974,52 @@ describe("Workflow composer", function () {
expect(mockStep1Fn.mock.calls[0][0]).toEqual(workflowInput)
expect(mockCompensateSte1.mock.calls[0][0]).toEqual({ data: "data" })
})
it("should throw if the same workflow is defined using different steps", async () => {
const step1 = createStep("step1", () => {
return new StepResponse({
obj: {
nested: "nested",
},
})
})
const step2 = createStep("step2", () => {
return new StepResponse({
obj: "returned from 2**",
})
})
const step3 = createStep("step3", () => {
return new StepResponse({
obj: "returned from 3**",
})
})
createWorkflow("workflow1", function () {
const { obj } = step1()
const s2 = step2()
return [{ step1_nested_obj: obj.nested }, s2]
})
expect(() =>
createWorkflow("workflow1", function () {
const { obj } = step1()
const s2 = step2()
step3()
return [{ step1_nested_obj: obj.nested }, s2]
})
).toThrowError(
`Workflow with id "workflow1" and step definition already exists.`
)
createWorkflow("workflow1", function () {
const { obj } = step1()
const s2 = step2()
return [{ step1_nested_obj: obj.nested }, s2]
})
})
})

View File

@@ -16,7 +16,7 @@ export class MedusaWorkflow {
static registerWorkflow(workflowId, exportedWorkflow) {
if (workflowId in MedusaWorkflow.workflows) {
throw new Error(`Workflow with id ${workflowId} already registered.`)
return
}
MedusaWorkflow.workflows[workflowId] = exportedWorkflow

View File

@@ -102,15 +102,15 @@ export function createWorkflow<
const handlers: WorkflowHandler = new Map()
if (WorkflowManager.getWorkflow(name)) {
WorkflowManager.unregister(name)
let newWorkflow = false
if (!WorkflowManager.getWorkflow(name)) {
newWorkflow = true
WorkflowManager.register(name, undefined, handlers, options)
}
WorkflowManager.register(name, undefined, handlers, options)
const context: CreateWorkflowComposerContext = {
workflowId: name,
flow: WorkflowManager.getTransactionDefinition(name),
flow: WorkflowManager.getEmptyTransactionDefinition(),
handlers,
hooks_: [],
hooksCallback_: {},
@@ -141,7 +141,11 @@ export function createWorkflow<
delete global[OrchestrationUtils.SymbolMedusaWorkflowComposerContext]
WorkflowManager.update(name, context.flow, handlers, options)
if (newWorkflow) {
WorkflowManager.update(name, context.flow, handlers, options)
} else {
WorkflowManager.register(name, context.flow, handlers, options)
}
const workflow = exportWorkflow<TData, TResult>(
name,