fix(workflow-engine-*): subscribe response and error (#6869)

This commit is contained in:
Carlos R. L. Rodrigues
2024-03-29 05:23:41 -03:00
committed by GitHub
parent d97af91a8d
commit e603726985
6 changed files with 67 additions and 43 deletions
@@ -5,11 +5,12 @@ import {
TransactionStep,
} from "@medusajs/orchestration"
import { ContainerLike, Context, MedusaContainer } from "@medusajs/types"
import { InjectSharedContext, isString, MedusaContext } from "@medusajs/utils"
import { InjectSharedContext, MedusaContext, isString } from "@medusajs/utils"
import {
type FlowRunOptions,
MedusaWorkflow,
ReturnWorkflow,
resolveValue,
type FlowRunOptions,
} from "@medusajs/workflows-sdk"
import { ulid } from "ulid"
import { InMemoryDistributedTransactionStorage } from "../utils"
@@ -462,31 +463,40 @@ export class WorkflowOrchestratorService {
notify({ eventType: "onStepBegin", step })
},
onStepSuccess: ({ step, transaction }) => {
const response = transaction.getContext().invoke[step.id]
onStepSuccess: async ({ step, transaction }) => {
const stepName = step.definition.action!
const response = await resolveValue(
transaction.getContext().invoke[stepName],
transaction
)
customEventHandlers?.onStepSuccess?.({ step, transaction, response })
notify({ eventType: "onStepSuccess", step, response })
},
onStepFailure: ({ step, transaction }) => {
const errors = transaction.getErrors(TransactionHandlerType.INVOKE)[
step.id
]
const stepName = step.definition.action!
const errors = transaction
.getErrors(TransactionHandlerType.INVOKE)
.filter((err) => err.action === stepName)
customEventHandlers?.onStepFailure?.({ step, transaction, errors })
notify({ eventType: "onStepFailure", step, errors })
},
onCompensateStepSuccess: ({ step, transaction }) => {
const response = transaction.getContext().compensate[step.id]
const stepName = step.definition.action!
const response = transaction.getContext().compensate[stepName]
customEventHandlers?.onStepSuccess?.({ step, transaction, response })
notify({ eventType: "onCompensateStepSuccess", step, response })
},
onCompensateStepFailure: ({ step, transaction }) => {
const errors = transaction.getErrors(TransactionHandlerType.COMPENSATE)[
step.id
]
const stepName = step.definition.action!
const errors = transaction
.getErrors(TransactionHandlerType.COMPENSATE)
.filter((err) => err.action === stepName)
customEventHandlers?.onStepFailure?.({ step, transaction, errors })
notify({ eventType: "onCompensateStepFailure", step, errors })