chore(workflows-sdk): add idempotencyKey to shared context (#6860)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
TransactionStepsDefinition,
|
||||
WorkflowManager,
|
||||
WorkflowStepHandlerArguments,
|
||||
} from "@medusajs/orchestration"
|
||||
import { OrchestrationUtils, deepCopy, isString } from "@medusajs/utils"
|
||||
import { ulid } from "ulid"
|
||||
@@ -120,16 +121,20 @@ function applyStep<
|
||||
}
|
||||
|
||||
const handler = {
|
||||
invoke: async (transactionContext) => {
|
||||
invoke: async (transactionContext: WorkflowStepHandlerArguments) => {
|
||||
const metadata = transactionContext.metadata
|
||||
const idempotencyKey = metadata.idempotency_key
|
||||
|
||||
transactionContext.context!.idempotencyKey = idempotencyKey
|
||||
const executionContext: StepExecutionContext = {
|
||||
workflowId: transactionContext.model_id,
|
||||
stepName: transactionContext.action,
|
||||
workflowId: metadata.model_id,
|
||||
stepName: metadata.action,
|
||||
action: "invoke",
|
||||
idempotencyKey: transactionContext.idempotency_key,
|
||||
attempt: transactionContext.attempt,
|
||||
idempotencyKey,
|
||||
attempt: metadata.attempt,
|
||||
container: transactionContext.container,
|
||||
metadata: transactionContext.metadata,
|
||||
context: transactionContext.context,
|
||||
metadata,
|
||||
context: transactionContext.context!,
|
||||
}
|
||||
|
||||
const argInput = input
|
||||
@@ -151,19 +156,25 @@ function applyStep<
|
||||
}
|
||||
},
|
||||
compensate: compensateFn
|
||||
? async (transactionContext) => {
|
||||
? async (transactionContext: WorkflowStepHandlerArguments) => {
|
||||
const metadata = transactionContext.metadata
|
||||
const idempotencyKey = metadata.idempotency_key
|
||||
|
||||
transactionContext.context!.idempotencyKey = idempotencyKey
|
||||
|
||||
const executionContext: StepExecutionContext = {
|
||||
workflowId: transactionContext.model_id,
|
||||
stepName: transactionContext.action,
|
||||
workflowId: metadata.model_id,
|
||||
stepName: metadata.action,
|
||||
action: "compensate",
|
||||
idempotencyKey: transactionContext.idempotency_key,
|
||||
attempt: transactionContext.attempt,
|
||||
idempotencyKey,
|
||||
attempt: metadata.attempt,
|
||||
container: transactionContext.container,
|
||||
metadata: transactionContext.metadata,
|
||||
context: transactionContext.context,
|
||||
metadata,
|
||||
context: transactionContext.context!,
|
||||
}
|
||||
|
||||
const stepOutput = transactionContext.invoke[stepName]?.output
|
||||
const stepOutput = (transactionContext.invoke[stepName] as any)
|
||||
?.output
|
||||
const invokeResult =
|
||||
stepOutput?.__type ===
|
||||
OrchestrationUtils.SymbolWorkflowStepResponse
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { OrchestrationUtils } from "@medusajs/utils"
|
||||
import { WorkflowStepHandlerArguments } from "@medusajs/orchestration"
|
||||
import { OrchestrationUtils, deepCopy } from "@medusajs/utils"
|
||||
import { resolveValue } from "./helpers"
|
||||
import {
|
||||
CreateWorkflowComposerContext,
|
||||
@@ -108,22 +109,27 @@ export function hook<TOutput>(
|
||||
|
||||
return hookBinder(name, function (context) {
|
||||
return {
|
||||
__value: async function (transactionContext) {
|
||||
__value: async function (
|
||||
transactionContext: WorkflowStepHandlerArguments
|
||||
) {
|
||||
const metadata = transactionContext.metadata
|
||||
const idempotencyKey = metadata.idempotency_key
|
||||
|
||||
transactionContext.context!.idempotencyKey = idempotencyKey
|
||||
|
||||
const executionContext: StepExecutionContext = {
|
||||
workflowId: transactionContext.model_id,
|
||||
stepName: transactionContext.action,
|
||||
action: transactionContext.action_type,
|
||||
idempotencyKey: transactionContext.idempotency_key,
|
||||
attempt: transactionContext.attempt,
|
||||
workflowId: metadata.model_id,
|
||||
stepName: metadata.action,
|
||||
action: metadata.action_type,
|
||||
idempotencyKey,
|
||||
attempt: metadata.attempt,
|
||||
container: transactionContext.container,
|
||||
metadata: transactionContext.metadata,
|
||||
context: transactionContext.context,
|
||||
metadata,
|
||||
context: transactionContext.context!,
|
||||
}
|
||||
|
||||
const allValues = await resolveValue(value, transactionContext)
|
||||
const stepValue = allValues
|
||||
? JSON.parse(JSON.stringify(allValues))
|
||||
: allValues
|
||||
const stepValue = allValues ? deepCopy(allValues) : allValues
|
||||
|
||||
let finalResult
|
||||
const functions = context.hooksCallback_[name]
|
||||
|
||||
Reference in New Issue
Block a user