chore(workflows, core-flows): Split workflows tooling and definitions (#5705)

This commit is contained in:
Adrien de Peretti
2023-11-24 14:55:48 +01:00
committed by GitHub
parent fc1ef29ed9
commit ddbeed4ea6
138 changed files with 274 additions and 241 deletions

View File

@@ -1,5 +1,5 @@
import { LocalWorkflow } from "../../workflow/local-workflow"
import { TransactionState } from "../../transaction/types"
import { LocalWorkflow } from "../../workflow/local-workflow"
import { WorkflowManager } from "../../workflow/workflow-manager"
describe("WorkflowManager", () => {
@@ -217,4 +217,18 @@ describe("WorkflowManager", () => {
WorkflowManager.getWorkflow("create-product")?.handlers_.has("xor")
).toEqual(false)
})
it("should return the final flow definition when calling getFlow()", async () => {
const flow = new LocalWorkflow("deliver-product", container)
expect(flow.getFlow()).toEqual({
action: "foo",
next: {
action: "callExternal",
async: true,
noCompensation: true,
next: { action: "bar" },
},
})
})
})

View File

@@ -1,19 +1,18 @@
import { Context, LoadedModule, MedusaContainer } from "@medusajs/types"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import {
DistributedTransaction,
TransactionOrchestrator,
TransactionStepsDefinition,
} from "../transaction"
import { OrchestratorBuilder } from "../transaction/orchestrator-builder"
import {
WorkflowDefinition,
WorkflowManager,
WorkflowStepHandler,
} from "./workflow-manager"
import { OrchestratorBuilder } from "../transaction/orchestrator-builder"
import { asValue } from "awilix"
import { createMedusaContainer } from "@medusajs/utils"
type StepHandler = {
invoke: WorkflowStepHandler
compensate?: WorkflowStepHandler
@@ -45,7 +44,7 @@ export class LocalWorkflow {
// Medusa container
if (!Array.isArray(modulesLoaded) && modulesLoaded) {
const cradle = modulesLoaded.cradle
for (const key in cradle) {
for (const key of Object.keys(cradle ?? {})) {
container.register(key, asValue(cradle[key]))
}
}
@@ -72,6 +71,14 @@ export class LocalWorkflow {
}
}
public getFlow() {
if (this.flow.hasChanges) {
this.commit()
}
return this.workflow.flow_
}
async run(uniqueTransactionId: string, input?: unknown, context?: Context) {
if (this.flow.hasChanges) {
this.commit()