Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(orchestration): prevent throwing on already defined workflow #5337

Merged
merged 18 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/strong-dragons-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/orchestration": patch
"@medusajs/utilsgst": patch
---

chore(orchestration): prevent throwing on already defined workflow
46 changes: 46 additions & 0 deletions packages/orchestration/src/__tests__/workflow/local-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
12 changes: 9 additions & 3 deletions packages/orchestration/src/workflow/workflow-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,17 @@ export class WorkflowManager {
requiredModules?: Set<string>,
optionalModules?: Set<string>
) {
const finalFlow = flow instanceof OrchestratorBuilder ? flow.build() : flow

if (WorkflowManager.workflows.has(workflowId)) {
throw new Error(`Workflow with id "${workflowId}" is already defined.`)
}
const areStepsEqual =
JSON.stringify(finalFlow) ===
JSON.stringify(WorkflowManager.workflows.get(workflowId)!.flow_)

const finalFlow = flow instanceof OrchestratorBuilder ? flow.build() : flow
if (!areStepsEqual) {
throw new Error(`Workflow with id "${workflowId}" and step definition already exists.`)
}
}

WorkflowManager.workflows.set(workflowId, {
id: workflowId,
Expand Down
33 changes: 33 additions & 0 deletions packages/utils/src/common/__tests__/deep-equal-obj.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { deepEqualObj } from "../deep-equal-obj"

describe("deepEqualObj", function () {
it("should return true if objects are equal", function () {
const object1 = {
foo: "bar",
bar: "foo",
xar: { foo: "bar", wor: { bar: "foo", ror: ["test", "test1"] } },
}
const object2 = {
foo: "bar",
bar: "foo",
xar: { foo: "bar", wor: { bar: "foo", ror: ["test", "test1"] } },
}

expect(deepEqualObj(object1, object2)).toBe(true)
})

it("should return false if objects are not equal", function () {
const object1 = {
foo: "bar",
bar: "foo",
xar: { foo: "bar", wor: { bar: "foo", ror: ["test", "test1"] } },
}
const object2 = {
foo: "bar",
bar: "foo",
xar: { foo: "bar", wor: { bar: "foo", ror: ["test", "test1_"] } },
}

expect(deepEqualObj(object1, object2)).toBe(false)
})
})
24 changes: 24 additions & 0 deletions packages/utils/src/common/deep-equal-obj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export function deepEqualObj(obj1: object, obj2: object): boolean {
if (typeof obj1 !== typeof obj2) {
return false
}

if (typeof obj1 !== "object" || obj1 === null) {
return obj1 === obj2
}

const obj1Keys = Object.keys(obj1)
const obj2Keys = Object.keys(obj2)

if (obj1Keys.length !== obj2Keys.length) {
return false
}

for (const key of obj1Keys) {
if (!obj2Keys.includes(key) || !deepEqualObj(obj1[key], obj2[key])) {
return false
}
}

return true
}
1 change: 1 addition & 0 deletions packages/utils/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ export * from "./to-pascal-case"
export * from "./transaction"
export * from "./upper-case-first"
export * from "./wrap-handler"
export * from "./deep-equal-obj"