Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Nov 12, 2024
1 parent 1b1c40f commit b6957cb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
10 changes: 6 additions & 4 deletions packages/core/workflows-sdk/src/composer/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import {
OrchestrationUtils,
} from "@medusajs/utils"
import {
createStep,
CreateWorkflowComposerContext,
ReturnWorkflow,
StepFunction,
StepResponse,
WorkflowData,
} from "../utils/composer/type"
WorkflowResponse,
} from "../utils/composer"
import { proxify } from "../utils/composer/helpers/proxy"
import { ExportedWorkflow } from "../helper"
import { createStep, StepResponse, WorkflowResponse } from "../utils/composer"
import { ulid } from "ulid"
import {
LocalWorkflowExecutionOptions,
Expand Down Expand Up @@ -127,7 +129,7 @@ export class WorkflowRunner<TData, TResult, THooks extends any[]> {

return
},
async (transaction, { container }) => {
async (transaction) => {
if (!transaction) {
return
}
Expand Down Expand Up @@ -219,7 +221,7 @@ export class Composer {
}

get workflowRunner(): BackawrdCompatibleWorkflowRunner<any, any, any> {
// TODO: Once we are read
// TODO: Once we are ready to get read of the backward compatibility layer we can remove this and return directly the runner such as `return this.#workflowRunner`
const runner = (container?: MedusaContainer) => {
if (container) {
this.#workflowRunner.container = container
Expand Down
61 changes: 31 additions & 30 deletions packages/core/workflows-sdk/src/composer/workflow-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ export class WorkflowExporter<TData = unknown, TResult = unknown> {
const { defaultResult, dataPreparation } =
this.#localWorkflowExecutionOptions

resultFrom ??= defaultResult
throwOnError ??= true
logOnError ??= false
const resultFrom_ = resultFrom ?? defaultResult
const throwOnError_ = throwOnError ?? true
const logOnError_ = logOnError ?? false

const context = {
...outerContext,
Expand All @@ -279,12 +279,13 @@ export class WorkflowExporter<TData = unknown, TResult = unknown> {
context.transactionId ??= ulid()
context.eventGroupId ??= ulid()

let input_ = input
if (typeof dataPreparation === "function") {
try {
const copyInput = input ? JSON.parse(JSON.stringify(input)) : input
input = (await dataPreparation(copyInput)) as any
input_ = (await dataPreparation(copyInput)) as any
} catch (err) {
if (throwOnError) {
if (throwOnError_) {
throw new Error(
`Data preparation failed: ${err.message}${EOL}${err.stack}`
)
Expand All @@ -298,13 +299,13 @@ export class WorkflowExporter<TData = unknown, TResult = unknown> {
return await this.#executeAction(
this.#executionWrapper.run,
{
throwOnError,
resultFrom,
logOnError,
throwOnError: throwOnError_,
resultFrom: resultFrom_,
logOnError: logOnError_,
container,
},
context.transactionId,
input,
input_,
context,
events
)
Expand All @@ -326,12 +327,12 @@ export class WorkflowExporter<TData = unknown, TResult = unknown> {
) {
const { defaultResult } = this.#localWorkflowExecutionOptions

idempotencyKey ??= ""
resultFrom ??= defaultResult
throwOnError ??= true
logOnError ??= false
const idempotencyKey_ = idempotencyKey ?? ""
const resultFrom_ = resultFrom ?? defaultResult
const throwOnError_ = throwOnError ?? true
const logOnError_ = logOnError ?? false

const [, transactionId] = idempotencyKey.split(":")
const [, transactionId] = idempotencyKey_.split(":")
const context = {
...outerContext,
transactionId,
Expand All @@ -343,12 +344,12 @@ export class WorkflowExporter<TData = unknown, TResult = unknown> {
return await this.#executeAction(
this.#executionWrapper.registerStepSuccess,
{
throwOnError,
resultFrom,
logOnError,
throwOnError: throwOnError_,
resultFrom: resultFrom_,
logOnError: logOnError_,
container,
},
idempotencyKey,
idempotencyKey_,
response,
context,
events
Expand All @@ -371,12 +372,12 @@ export class WorkflowExporter<TData = unknown, TResult = unknown> {
) {
const { defaultResult } = this.#localWorkflowExecutionOptions

idempotencyKey ??= ""
resultFrom ??= defaultResult
throwOnError ??= true
logOnError ??= false
const idempotencyKey_ = idempotencyKey ?? ""
const resultFrom_ = resultFrom ?? defaultResult
const throwOnError_ = throwOnError ?? true
const logOnError_ = logOnError ?? false

const [, transactionId] = idempotencyKey.split(":")
const [, transactionId] = idempotencyKey_.split(":")
const context = {
...outerContext,
transactionId,
Expand All @@ -388,9 +389,9 @@ export class WorkflowExporter<TData = unknown, TResult = unknown> {
return await this.#executeAction(
this.#executionWrapper.registerStepFailure,
{
throwOnError,
resultFrom,
logOnError,
throwOnError: throwOnError_,
resultFrom: resultFrom_,
logOnError: logOnError_,
container,
},
idempotencyKey,
Expand All @@ -409,8 +410,8 @@ export class WorkflowExporter<TData = unknown, TResult = unknown> {
events,
container,
}: FlowCancelOptions = {}) {
throwOnError ??= true
logOnError ??= false
const throwOnError_ = throwOnError ?? true
const logOnError_ = logOnError ?? false

const context = {
...outerContext,
Expand All @@ -423,10 +424,10 @@ export class WorkflowExporter<TData = unknown, TResult = unknown> {
return await this.#executeAction(
this.#executionWrapper.cancel,
{
throwOnError,
throwOnError: throwOnError_,
resultFrom: undefined,
isCancel: true,
logOnError,
logOnError: logOnError_,
container,
},
transaction ?? transactionId!,
Expand Down

0 comments on commit b6957cb

Please sign in to comment.