chore(orchestration): prevent throwing on already defined workflow (#5337)
**What** At the moment, when importing something from medusa entry point, if two medusa packages are installed because of a plugin, the evaluation of the import can end up throwing that a workflow is already defined. To prevent that from happening, we can just not throw if it is already defined and just return prematurely and make it idempotent.
This commit is contained in:
committed by
GitHub
parent
a1807aea83
commit
e47461d95c
@@ -80,6 +80,52 @@ describe("WorkflowManager", () => {
|
||||
expect(wf).toEqual(["create-product", "broken-delivery", "deliver-product"])
|
||||
})
|
||||
|
||||
it("should throw when registering a workflow with an existing id", () => {
|
||||
let err
|
||||
try {
|
||||
WorkflowManager.register(
|
||||
"create-product",
|
||||
{
|
||||
action: "foo",
|
||||
next: {
|
||||
action: "bar",
|
||||
next: {
|
||||
action: "xor",
|
||||
},
|
||||
},
|
||||
},
|
||||
handlers
|
||||
)
|
||||
} catch (e) {
|
||||
err = e
|
||||
}
|
||||
|
||||
expect(err).toBeDefined()
|
||||
expect(err.message).toBe(
|
||||
`Workflow with id "create-product" and step definition already exists.`
|
||||
)
|
||||
})
|
||||
|
||||
it("should not throw when registering a workflow with an existing id but identical definition", () => {
|
||||
let err
|
||||
try {
|
||||
WorkflowManager.register(
|
||||
"create-product",
|
||||
{
|
||||
action: "foo",
|
||||
next: {
|
||||
action: "bar",
|
||||
},
|
||||
},
|
||||
handlers
|
||||
)
|
||||
} catch (e) {
|
||||
err = e
|
||||
}
|
||||
|
||||
expect(err).not.toBeDefined()
|
||||
})
|
||||
|
||||
it("should begin a transaction and returns its final state", async () => {
|
||||
const flow = new LocalWorkflow("create-product", container)
|
||||
const transaction = await flow.run("t-id", {
|
||||
|
||||
@@ -75,12 +75,18 @@ export class WorkflowManager {
|
||||
requiredModules?: Set<string>,
|
||||
optionalModules?: Set<string>
|
||||
) {
|
||||
if (WorkflowManager.workflows.has(workflowId)) {
|
||||
throw new Error(`Workflow with id "${workflowId}" is already defined.`)
|
||||
}
|
||||
|
||||
const finalFlow = flow instanceof OrchestratorBuilder ? flow.build() : flow
|
||||
|
||||
if (WorkflowManager.workflows.has(workflowId)) {
|
||||
const areStepsEqual =
|
||||
JSON.stringify(finalFlow) ===
|
||||
JSON.stringify(WorkflowManager.workflows.get(workflowId)!.flow_)
|
||||
|
||||
if (!areStepsEqual) {
|
||||
throw new Error(`Workflow with id "${workflowId}" and step definition already exists.`)
|
||||
}
|
||||
}
|
||||
|
||||
WorkflowManager.workflows.set(workflowId, {
|
||||
id: workflowId,
|
||||
flow_: finalFlow,
|
||||
|
||||
Reference in New Issue
Block a user