fix(workflows-sdk): when then return value (#9427)
This commit is contained in:
1 parent
cb5c228a36
commit
8155c4e9ee
3 files changed
+44
-3
No files matched your search
@@ -22,9 +22,11 @@ export class OrchestratorBuilder {
|
|||||||
this.steps = {
|
this.steps = {
|
||||||
depth: -1,
|
depth: -1,
|
||||||
parent: null,
|
parent: null,
|
||||||
next: steps
|
next: Object.keys(steps ?? {}).length
|
||||||
? JSON.parse(
|
? JSON.parse(
|
||||||
JSON.stringify((steps.action ? steps : steps.next) as InternalStep)
|
JSON.stringify(
|
||||||
|
(steps!.action ? steps : steps!.next) as InternalStep
|
||||||
|
)
|
||||||
)
|
)
|
||||||
: undefined,
|
: undefined,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,6 +134,33 @@ describe("Workflow composer", () => {
|
|||||||
expect(res2).toEqual({ result: "default response" })
|
expect(res2).toEqual({ result: "default response" })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should not return value if when condition is false", async function () {
|
||||||
|
const workflow = createWorkflow(
|
||||||
|
getNewWorkflowId(),
|
||||||
|
function (input: { ret: boolean }) {
|
||||||
|
const value = when({ input }, ({ input }) => {
|
||||||
|
return input.ret
|
||||||
|
}).then(() => {
|
||||||
|
return { hasValue: true }
|
||||||
|
})
|
||||||
|
|
||||||
|
return new WorkflowResponse(value)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const { result } = await workflow.run({
|
||||||
|
input: { ret: false },
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(result).toEqual(undefined)
|
||||||
|
|
||||||
|
const { result: res2 } = await workflow.run({
|
||||||
|
input: { ret: true },
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res2).toEqual({ hasValue: true })
|
||||||
|
})
|
||||||
|
|
||||||
it("should revert the workflow and sub workflow on failure", async function () {
|
it("should revert the workflow and sub workflow on failure", async function () {
|
||||||
const step1Mock = jest.fn()
|
const step1Mock = jest.fn()
|
||||||
const step1 = createStep(
|
const step1 = createStep(
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
import { OrchestrationUtils } from "@medusajs/utils"
|
import { OrchestrationUtils } from "@medusajs/utils"
|
||||||
|
import { ulid } from "ulid"
|
||||||
|
import { createStep } from "./create-step"
|
||||||
|
import { StepResponse } from "./helpers/step-response"
|
||||||
import { StepExecutionContext, WorkflowData } from "./type"
|
import { StepExecutionContext, WorkflowData } from "./type"
|
||||||
|
|
||||||
type ConditionFunction<T extends object | WorkflowData> = (
|
type ConditionFunction<T extends object | WorkflowData> = (
|
||||||
@@ -41,17 +44,26 @@ export function when(input, condition) {
|
|||||||
then: (fn) => {
|
then: (fn) => {
|
||||||
thenCalled = true
|
thenCalled = true
|
||||||
const ret = fn()
|
const ret = fn()
|
||||||
|
let returnStep = ret
|
||||||
|
|
||||||
const applyCondition =
|
const applyCondition =
|
||||||
global[OrchestrationUtils.SymbolMedusaWorkflowComposerCondition].steps
|
global[OrchestrationUtils.SymbolMedusaWorkflowComposerCondition].steps
|
||||||
|
|
||||||
|
if (ret?.__type !== OrchestrationUtils.SymbolWorkflowStep) {
|
||||||
|
const retStep = createStep(
|
||||||
|
"when-then-" + ulid(),
|
||||||
|
() => new StepResponse(ret)
|
||||||
|
)
|
||||||
|
returnStep = retStep()
|
||||||
|
}
|
||||||
|
|
||||||
for (const step of applyCondition) {
|
for (const step of applyCondition) {
|
||||||
step.if(input, condition)
|
step.if(input, condition)
|
||||||
}
|
}
|
||||||
|
|
||||||
delete global[OrchestrationUtils.SymbolMedusaWorkflowComposerCondition]
|
delete global[OrchestrationUtils.SymbolMedusaWorkflowComposerCondition]
|
||||||
|
|
||||||
return ret
|
return returnStep
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user