chore(workflows-sdk): check exported workflow (#7592)
This commit is contained in:
@@ -80,9 +80,8 @@ describe("WorkflowManager", () => {
|
|||||||
expect(wf).toEqual(["create-product", "broken-delivery", "deliver-product"])
|
expect(wf).toEqual(["create-product", "broken-delivery", "deliver-product"])
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should NOT throw when registering a workflow with an existing id in Medusa V1", () => {
|
it("should throw when registering a workflow with an existing id and different definition", () => {
|
||||||
let err
|
const exec = () =>
|
||||||
try {
|
|
||||||
WorkflowManager.register(
|
WorkflowManager.register(
|
||||||
"create-product",
|
"create-product",
|
||||||
{
|
{
|
||||||
@@ -96,38 +95,8 @@ describe("WorkflowManager", () => {
|
|||||||
},
|
},
|
||||||
handlers
|
handlers
|
||||||
)
|
)
|
||||||
} catch (e) {
|
|
||||||
err = e
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(err).toBeUndefined()
|
expect(exec).toThrowError(
|
||||||
})
|
|
||||||
|
|
||||||
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(
|
|
||||||
`Workflow with id "create-product" and step definition already exists.`
|
`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", () => {
|
it("should not throw when registering a workflow with an existing id but identical definition", () => {
|
||||||
let err
|
let err
|
||||||
|
|
||||||
const env = process.env.MEDUSA_FF_MEDUSA_V2
|
|
||||||
process.env.MEDUSA_FF_MEDUSA_V2 = "true"
|
|
||||||
try {
|
try {
|
||||||
WorkflowManager.register(
|
WorkflowManager.register(
|
||||||
"create-product",
|
"create-product",
|
||||||
@@ -151,7 +118,6 @@ describe("WorkflowManager", () => {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
err = e
|
err = e
|
||||||
}
|
}
|
||||||
process.env.MEDUSA_FF_MEDUSA_V2 = env
|
|
||||||
|
|
||||||
expect(err).not.toBeDefined()
|
expect(err).not.toBeDefined()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -77,6 +77,10 @@ export class WorkflowManager {
|
|||||||
return new OrchestratorBuilder(workflow.flow_)
|
return new OrchestratorBuilder(workflow.flow_)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static getEmptyTransactionDefinition(): OrchestratorBuilder {
|
||||||
|
return new OrchestratorBuilder()
|
||||||
|
}
|
||||||
|
|
||||||
static register(
|
static register(
|
||||||
workflowId: string,
|
workflowId: string,
|
||||||
flow: TransactionStepsDefinition | OrchestratorBuilder | undefined,
|
flow: TransactionStepsDefinition | OrchestratorBuilder | undefined,
|
||||||
@@ -101,11 +105,9 @@ export class WorkflowManager {
|
|||||||
: true
|
: true
|
||||||
|
|
||||||
if (!areStepsEqual) {
|
if (!areStepsEqual) {
|
||||||
if (process.env.MEDUSA_FF_MEDUSA_V2 == "true") {
|
throw new Error(
|
||||||
throw new Error(
|
`Workflow with id "${workflowId}" and step definition already exists.`
|
||||||
`Workflow with id "${workflowId}" and step definition already exists.`
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { WorkflowManager } from "@medusajs/orchestration"
|
||||||
import { promiseAll } from "@medusajs/utils"
|
import { promiseAll } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
createStep,
|
createStep,
|
||||||
@@ -14,6 +15,7 @@ jest.setTimeout(30000)
|
|||||||
const afterEach_ = () => {
|
const afterEach_ = () => {
|
||||||
jest.clearAllMocks()
|
jest.clearAllMocks()
|
||||||
MedusaWorkflow.workflows = {}
|
MedusaWorkflow.workflows = {}
|
||||||
|
WorkflowManager.unregisterAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("Workflow composer", function () {
|
describe("Workflow composer", function () {
|
||||||
@@ -1972,4 +1974,52 @@ describe("Workflow composer", function () {
|
|||||||
expect(mockStep1Fn.mock.calls[0][0]).toEqual(workflowInput)
|
expect(mockStep1Fn.mock.calls[0][0]).toEqual(workflowInput)
|
||||||
expect(mockCompensateSte1.mock.calls[0][0]).toEqual({ data: "data" })
|
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) {
|
static registerWorkflow(workflowId, exportedWorkflow) {
|
||||||
if (workflowId in MedusaWorkflow.workflows) {
|
if (workflowId in MedusaWorkflow.workflows) {
|
||||||
throw new Error(`Workflow with id ${workflowId} already registered.`)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
MedusaWorkflow.workflows[workflowId] = exportedWorkflow
|
MedusaWorkflow.workflows[workflowId] = exportedWorkflow
|
||||||
|
|||||||
@@ -102,15 +102,15 @@ export function createWorkflow<
|
|||||||
|
|
||||||
const handlers: WorkflowHandler = new Map()
|
const handlers: WorkflowHandler = new Map()
|
||||||
|
|
||||||
if (WorkflowManager.getWorkflow(name)) {
|
let newWorkflow = false
|
||||||
WorkflowManager.unregister(name)
|
if (!WorkflowManager.getWorkflow(name)) {
|
||||||
|
newWorkflow = true
|
||||||
|
WorkflowManager.register(name, undefined, handlers, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
WorkflowManager.register(name, undefined, handlers, options)
|
|
||||||
|
|
||||||
const context: CreateWorkflowComposerContext = {
|
const context: CreateWorkflowComposerContext = {
|
||||||
workflowId: name,
|
workflowId: name,
|
||||||
flow: WorkflowManager.getTransactionDefinition(name),
|
flow: WorkflowManager.getEmptyTransactionDefinition(),
|
||||||
handlers,
|
handlers,
|
||||||
hooks_: [],
|
hooks_: [],
|
||||||
hooksCallback_: {},
|
hooksCallback_: {},
|
||||||
@@ -141,7 +141,11 @@ export function createWorkflow<
|
|||||||
|
|
||||||
delete global[OrchestrationUtils.SymbolMedusaWorkflowComposerContext]
|
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>(
|
const workflow = exportWorkflow<TData, TResult>(
|
||||||
name,
|
name,
|
||||||
|
|||||||
Reference in New Issue
Block a user