Skip to content

Commit

Permalink
fix(workflows-sdk): Value resolver should resolve non StepResponse (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p authored Mar 18, 2024
1 parent 0c705d7 commit 4b06c17
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-weeks-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/workflows-sdk": patch
---

fix(workflows-sdk): Value resolver should resolve non StepResponse
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
StepResponse,
createStep,
createWorkflow,
StepResponse,
} from "@medusajs/workflows-sdk"

const step_1 = createStep(
Expand All @@ -15,7 +15,6 @@ const step_1 = createStep(
return
}

console.log("reverted", compensateInput.compensate)
return new StepResponse({
reverted: true,
})
Expand All @@ -25,8 +24,6 @@ const step_1 = createStep(
const step_2 = createStep(
"step_2",
jest.fn((input, context) => {
console.log("triggered async request", context.metadata.idempotency_key)

if (input) {
return new StepResponse({ notAsyncResponse: input.hey })
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
StepResponse,
createStep,
createWorkflow,
StepResponse,
} from "@medusajs/workflows-sdk"

const step_1 = createStep(
Expand All @@ -15,22 +15,20 @@ const step_1 = createStep(
return
}

console.log("reverted", compensateInput.compensate)
return new StepResponse({
reverted: true,
})
})
)

export const workflow2Step2Invoke = jest.fn((input, context) => {
if (input) {
return new StepResponse({ notAsyncResponse: input.hey })
}
})
const step_2 = createStep(
"step_2",
jest.fn((input, context) => {
console.log("triggered async request", context.metadata.idempotency_key)

if (input) {
return new StepResponse({ notAsyncResponse: input.hey })
}
}),
workflow2Step2Invoke,
jest.fn((_, context) => {
return new StepResponse({
step: context.metadata.action,
Expand All @@ -40,16 +38,14 @@ const step_2 = createStep(
})
)

const step_3 = createStep(
"step_3",
jest.fn((res) => {
return new StepResponse({
done: {
inputFromSyncStep: res.notAsyncResponse,
},
})
export const workflow2Step3Invoke = jest.fn((res) => {
return new StepResponse({
done: {
inputFromSyncStep: res.notAsyncResponse,
},
})
)
})
const step_3 = createStep("step_3", workflow2Step3Invoke)

createWorkflow(
{
Expand All @@ -59,13 +55,13 @@ createWorkflow(
function (input) {
step_1(input)

const ret2 = step_2({ hey: "oh" })
step_2({ hey: "oh" })

step_2({ hey: "async hello" }).config({
const ret2_async = step_2({ hey: "async hello" }).config({
name: "new_step_name",
async: true,
})

return step_3(ret2)
return step_3(ret2_async)
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { knex } from "knex"
import { setTimeout } from "timers/promises"
import "../__fixtures__"
import { DB_URL, TestDatabase } from "../utils"
import { workflow2Step2Invoke, workflow2Step3Invoke } from "../__fixtures__"

const sharedPgConnection = knex<any, any>({
client: "pg",
Expand Down Expand Up @@ -124,6 +125,17 @@ describe("Workflow Orchestrator module", function () {
stepResponse: { uhuuuu: "yeaah!" },
})

expect(workflow2Step2Invoke).toBeCalledTimes(2)
expect(workflow2Step2Invoke.mock.calls[0][0]).toEqual({ hey: "oh" })
expect(workflow2Step2Invoke.mock.calls[1][0]).toEqual({
hey: "async hello",
})

expect(workflow2Step3Invoke).toBeCalledTimes(1)
expect(workflow2Step3Invoke.mock.calls[0][0]).toEqual({
uhuuuu: "yeaah!",
})

executionsList = await query({
workflow_executions: {
fields: ["id"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OrchestrationUtils, deepCopy, promiseAll } from "@medusajs/utils"
import { deepCopy, OrchestrationUtils, promiseAll } from "@medusajs/utils"

async function resolveProperty(property, transactionContext) {
const { invoke: invokeRes } = transactionContext
Expand All @@ -12,7 +12,8 @@ async function resolveProperty(property, transactionContext) {
} else if (property?.__type === OrchestrationUtils.SymbolWorkflowHook) {
return await property.__value(transactionContext)
} else if (property?.__type === OrchestrationUtils.SymbolWorkflowStep) {
const output = invokeRes[property.__step__]?.output
const output =
invokeRes[property.__step__]?.output ?? invokeRes[property.__step__]
if (output?.__type === OrchestrationUtils.SymbolWorkflowStepResponse) {
return output.output
}
Expand Down

0 comments on commit 4b06c17

Please sign in to comment.