feat(workflows-sdk): conditional step (#7912)

* chore: move ModuleRegistrationName to utils

* chore(workflows-sdk): conditional step

* type

* when condition
This commit is contained in:
Carlos R. L. Rodrigues
2024-07-05 05:54:18 -03:00
committed by GitHub
parent 36c1db7479
commit 9badad24aa
13 changed files with 264 additions and 525 deletions
@@ -1,6 +1,7 @@
export * from "./workflow_1"
export * from "./workflow_2"
export * from "./workflow_async"
export * from "./workflow_conditional_step"
export * from "./workflow_idempotent"
export * from "./workflow_step_timeout"
export * from "./workflow_transaction_timeout"
@@ -0,0 +1,57 @@
import {
createStep,
createWorkflow,
StepResponse,
} from "@medusajs/workflows-sdk"
import { when } from "@medusajs/workflows-sdk/src/utils/composer"
const step_1 = createStep(
"step_1",
jest.fn((input) => {
input.test = "test"
return new StepResponse(input, { compensate: 123 })
})
)
export const conditionalStep2Invoke = jest.fn((input, context) => {
if (input) {
return new StepResponse({ notAsyncResponse: input.hey })
}
})
const step_2 = createStep("step_2", conditionalStep2Invoke)
export const conditionalStep3Invoke = jest.fn((res) => {
return new StepResponse({
done: {
inputFromSyncStep: res.notAsyncResponse,
},
})
})
const step_3 = createStep("step_3", conditionalStep3Invoke)
createWorkflow(
{
name: "workflow_conditional_step",
retentionTime: 1000,
},
function (input) {
step_1(input)
const ret = step_2({ hey: "oh" })
const ret2_async = when({ input, ret }, ({ input, ret }) => {
return input.runNewStepName && ret.notAsyncResponse === "oh"
}).then(() => {
return step_2({ hey: "hello" }).config({
name: "new_step_name",
async: true,
})
})
return when({ ret2_async }, ({ ret2_async }) => {
return ret2_async?.notAsyncResponse === "hello"
}).then(() => {
return step_3(ret2_async)
})
}
)
@@ -8,7 +8,12 @@ import { Modules, TransactionHandlerType } from "@medusajs/utils"
import { moduleIntegrationTestRunner } from "medusa-test-utils"
import { setTimeout as setTimeoutPromise } from "timers/promises"
import "../__fixtures__"
import { workflow2Step2Invoke, workflow2Step3Invoke } from "../__fixtures__"
import {
conditionalStep2Invoke,
conditionalStep3Invoke,
workflow2Step2Invoke,
workflow2Step3Invoke,
} from "../__fixtures__"
import {
eventGroupWorkflowId,
workflowEventGroupIdStep1Mock,
@@ -92,6 +97,10 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
})
describe("Testing basic workflow", function () {
beforeEach(() => {
jest.clearAllMocks()
})
it("should return a list of workflow executions and remove after completed when there is no retentionTime set", async () => {
await workflowOrcModule.run("workflow_1", {
input: {
@@ -232,6 +241,46 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
expect(onFinish).toHaveBeenCalledTimes(0)
})
it("should run conditional steps if condition is true", (done) => {
void workflowOrcModule.subscribe({
workflowId: "workflow_conditional_step",
subscriber: (event) => {
if (event.eventType === "onFinish") {
done()
expect(conditionalStep2Invoke).toHaveBeenCalledTimes(2)
expect(conditionalStep3Invoke).toHaveBeenCalledTimes(1)
}
},
})
workflowOrcModule.run("workflow_conditional_step", {
input: {
runNewStepName: true,
},
throwOnError: true,
})
})
it("should not run conditional steps if condition is false", (done) => {
void workflowOrcModule.subscribe({
workflowId: "workflow_conditional_step",
subscriber: (event) => {
if (event.eventType === "onFinish") {
done()
expect(conditionalStep2Invoke).toHaveBeenCalledTimes(1)
expect(conditionalStep3Invoke).toHaveBeenCalledTimes(0)
}
},
})
workflowOrcModule.run("workflow_conditional_step", {
input: {
runNewStepName: false,
},
throwOnError: true,
})
})
})
describe("Scheduled workflows", () => {