chore(workflows-sdk): check exported workflow (#7592)
This commit is contained in:
committed by
GitHub
parent
68fb04b849
commit
41df24e2dc
@@ -80,9 +80,8 @@ describe("WorkflowManager", () => {
|
||||
expect(wf).toEqual(["create-product", "broken-delivery", "deliver-product"])
|
||||
})
|
||||
|
||||
it("should NOT throw when registering a workflow with an existing id in Medusa V1", () => {
|
||||
let err
|
||||
try {
|
||||
it("should throw when registering a workflow with an existing id and different definition", () => {
|
||||
const exec = () =>
|
||||
WorkflowManager.register(
|
||||
"create-product",
|
||||
{
|
||||
@@ -96,38 +95,8 @@ describe("WorkflowManager", () => {
|
||||
},
|
||||
handlers
|
||||
)
|
||||
} catch (e) {
|
||||
err = e
|
||||
}
|
||||
|
||||
expect(err).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should throw when registering a workflow with an existing id in Medusa V2", () => {
|
||||
let err
|
||||
const env = process.env.MEDUSA_FF_MEDUSA_V2
|
||||
process.env.MEDUSA_FF_MEDUSA_V2 = "true"
|
||||
try {
|
||||
WorkflowManager.register(
|
||||
"create-product",
|
||||
{
|
||||
action: "foo",
|
||||
next: {
|
||||
action: "bar",
|
||||
next: {
|
||||
action: "xor",
|
||||
},
|
||||
},
|
||||
},
|
||||
handlers
|
||||
)
|
||||
} catch (e) {
|
||||
err = e
|
||||
}
|
||||
process.env.MEDUSA_FF_MEDUSA_V2 = env
|
||||
|
||||
expect(err).toBeDefined()
|
||||
expect(err.message).toBe(
|
||||
expect(exec).toThrowError(
|
||||
`Workflow with id "create-product" and step definition already exists.`
|
||||
)
|
||||
})
|
||||
@@ -135,8 +104,6 @@ describe("WorkflowManager", () => {
|
||||
it("should not throw when registering a workflow with an existing id but identical definition", () => {
|
||||
let err
|
||||
|
||||
const env = process.env.MEDUSA_FF_MEDUSA_V2
|
||||
process.env.MEDUSA_FF_MEDUSA_V2 = "true"
|
||||
try {
|
||||
WorkflowManager.register(
|
||||
"create-product",
|
||||
@@ -151,7 +118,6 @@ describe("WorkflowManager", () => {
|
||||
} catch (e) {
|
||||
err = e
|
||||
}
|
||||
process.env.MEDUSA_FF_MEDUSA_V2 = env
|
||||
|
||||
expect(err).not.toBeDefined()
|
||||
})
|
||||
|
||||
@@ -77,6 +77,10 @@ export class WorkflowManager {
|
||||
return new OrchestratorBuilder(workflow.flow_)
|
||||
}
|
||||
|
||||
static getEmptyTransactionDefinition(): OrchestratorBuilder {
|
||||
return new OrchestratorBuilder()
|
||||
}
|
||||
|
||||
static register(
|
||||
workflowId: string,
|
||||
flow: TransactionStepsDefinition | OrchestratorBuilder | undefined,
|
||||
@@ -101,11 +105,9 @@ export class WorkflowManager {
|
||||
: true
|
||||
|
||||
if (!areStepsEqual) {
|
||||
if (process.env.MEDUSA_FF_MEDUSA_V2 == "true") {
|
||||
throw new Error(
|
||||
`Workflow with id "${workflowId}" and step definition already exists.`
|
||||
)
|
||||
}
|
||||
throw new Error(
|
||||
`Workflow with id "${workflowId}" and step definition already exists.`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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]
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user