Feat: draft order api (#6797)

This commit is contained in:
Carlos R. L. Rodrigues
2024-04-06 16:35:10 +02:00
committed by GitHub
parent 883a75c4f3
commit df0751f122
63 changed files with 2179 additions and 1424 deletions

View File

@@ -3,6 +3,7 @@ import {
createStep,
createWorkflow,
hook,
MedusaWorkflow,
parallelize,
StepResponse,
transform,
@@ -10,11 +11,16 @@ import {
jest.setTimeout(30000)
const afterEach_ = () => {
jest.clearAllMocks()
MedusaWorkflow.workflows = {}
}
describe("Workflow composer", function () {
afterEach(afterEach_)
describe("Using steps returning plain values", function () {
afterEach(async () => {
jest.clearAllMocks()
})
afterEach(afterEach_)
it("should compose a new workflow composed retryable steps", async () => {
const maxRetries = 1
@@ -657,83 +663,6 @@ describe("Workflow composer", function () {
})
})
it("should overwrite existing workflows if the same name is used", async () => {
const mockStep1Fn = jest.fn().mockImplementation((input, context) => {
return { inputs: [input], obj: "return from 1" }
})
const mockStep2Fn = jest.fn().mockImplementation((...inputs) => {
const context = inputs.pop()
return {
inputs,
obj: "return from 2",
}
})
const mockStep3Fn = jest.fn().mockImplementation((...inputs) => {
const context = inputs.pop()
return {
inputs,
obj: "return from 3",
}
})
const step1 = createStep("step1", mockStep1Fn)
const step2 = createStep("step2", mockStep2Fn)
const step3 = createStep("step3", mockStep3Fn)
createWorkflow("workflow1", function (input) {
const returnStep1 = step1(input)
const ret2 = step2(returnStep1)
return step3({ one: returnStep1, two: ret2 })
})
const overriddenWorkflow = createWorkflow("workflow1", function (input) {
const ret2 = step2(input)
const returnStep1 = step1(ret2)
return step3({ one: returnStep1, two: ret2 })
})
const workflowInput = { test: "payload1" }
const { result: workflowResult } = await overriddenWorkflow().run({
input: workflowInput,
})
expect(mockStep1Fn).toHaveBeenCalledTimes(1)
expect(mockStep1Fn.mock.calls[0]).toHaveLength(2)
expect(mockStep1Fn.mock.calls[0][0]).toEqual({
inputs: [workflowInput],
obj: "return from 2",
})
expect(mockStep2Fn).toHaveBeenCalledTimes(1)
expect(mockStep2Fn.mock.calls[0]).toHaveLength(2)
expect(mockStep2Fn.mock.calls[0][0]).toEqual(workflowInput)
expect(mockStep3Fn).toHaveBeenCalledTimes(1)
expect(mockStep3Fn.mock.calls[0]).toHaveLength(2)
expect(mockStep3Fn.mock.calls[0][0]).toEqual({
one: {
inputs: [{ inputs: [{ test: "payload1" }], obj: "return from 2" }],
obj: "return from 1",
},
two: { inputs: [{ test: "payload1" }], obj: "return from 2" },
})
expect(workflowResult).toEqual({
inputs: [
{
one: {
inputs: [
{ inputs: [{ test: "payload1" }], obj: "return from 2" },
],
obj: "return from 1",
},
two: { inputs: [{ test: "payload1" }], obj: "return from 2" },
},
],
obj: "return from 3",
})
})
it("should transform the values before forward them to the next step", async () => {
const mockStep1Fn = jest.fn().mockImplementation((obj, context) => {
const ret = {
@@ -958,9 +887,7 @@ describe("Workflow composer", function () {
})
describe("Using steps returning StepResponse", function () {
afterEach(async () => {
jest.clearAllMocks()
})
afterEach(afterEach_)
it("should compose a new workflow composed of retryable steps", async () => {
const maxRetries = 1
@@ -1636,83 +1563,6 @@ describe("Workflow composer", function () {
})
})
it("should overwrite existing workflows if the same name is used", async () => {
const mockStep1Fn = jest.fn().mockImplementation((input, context) => {
return new StepResponse({ inputs: [input], obj: "return from 1" })
})
const mockStep2Fn = jest.fn().mockImplementation((...inputs) => {
const context = inputs.pop()
return new StepResponse({
inputs,
obj: "return from 2",
})
})
const mockStep3Fn = jest.fn().mockImplementation((...inputs) => {
const context = inputs.pop()
return new StepResponse({
inputs,
obj: "return from 3",
})
})
const step1 = createStep("step1", mockStep1Fn)
const step2 = createStep("step2", mockStep2Fn)
const step3 = createStep("step3", mockStep3Fn)
createWorkflow("workflow1", function (input) {
const returnStep1 = step1(input)
const ret2 = step2(returnStep1)
return step3({ one: returnStep1, two: ret2 })
})
const overriddenWorkflow = createWorkflow("workflow1", function (input) {
const ret2 = step2(input)
const returnStep1 = step1(ret2)
return step3({ one: returnStep1, two: ret2 })
})
const workflowInput = { test: "payload1" }
const { result: workflowResult } = await overriddenWorkflow().run({
input: workflowInput,
})
expect(mockStep1Fn).toHaveBeenCalledTimes(1)
expect(mockStep1Fn.mock.calls[0]).toHaveLength(2)
expect(mockStep1Fn.mock.calls[0][0]).toEqual({
inputs: [workflowInput],
obj: "return from 2",
})
expect(mockStep2Fn).toHaveBeenCalledTimes(1)
expect(mockStep2Fn.mock.calls[0]).toHaveLength(2)
expect(mockStep2Fn.mock.calls[0][0]).toEqual(workflowInput)
expect(mockStep3Fn).toHaveBeenCalledTimes(1)
expect(mockStep3Fn.mock.calls[0]).toHaveLength(2)
expect(mockStep3Fn.mock.calls[0][0]).toEqual({
one: {
inputs: [{ inputs: [{ test: "payload1" }], obj: "return from 2" }],
obj: "return from 1",
},
two: { inputs: [{ test: "payload1" }], obj: "return from 2" },
})
expect(workflowResult).toEqual({
inputs: [
{
one: {
inputs: [
{ inputs: [{ test: "payload1" }], obj: "return from 2" },
],
obj: "return from 1",
},
two: { inputs: [{ test: "payload1" }], obj: "return from 2" },
},
],
obj: "return from 3",
})
})
it("should transform the values before forward them to the next step", async () => {
const mockStep1Fn = jest.fn().mockImplementation((obj, context) => {
const ret = new StepResponse({

View File

@@ -1,4 +1,5 @@
import { createMedusaContainer } from "@medusajs/utils"
import { MedusaWorkflow } from "../../medusa-workflow"
import { exportWorkflow } from "../workflow-export"
jest.mock("@medusajs/orchestration", () => {
@@ -63,6 +64,10 @@ jest.mock("@medusajs/orchestration", () => {
})
describe("Export Workflow", function () {
afterEach(() => {
MedusaWorkflow.workflows = {}
})
it("should prepare the input data before initializing the transaction", async function () {
let transformedInput
const prepare = jest.fn().mockImplementation(async (data) => {
@@ -98,6 +103,10 @@ describe("Export Workflow", function () {
})
describe("Using the exported workflow run", function () {
afterEach(() => {
MedusaWorkflow.workflows = {}
})
it("should prepare the input data before initializing the transaction", async function () {
let transformedInput
const prepare = jest.fn().mockImplementation(async (data) => {

View File

@@ -9,12 +9,16 @@ export class MedusaWorkflow {
container?: LoadedModule[] | MedusaContainer
) => Omit<
LocalWorkflow,
"run" | "registerStepSuccess" | "registerStepFailure"
"run" | "registerStepSuccess" | "registerStepFailure" | "cancel"
> &
ExportedWorkflow
> = {}
static registerWorkflow(workflowId, exportedWorkflow) {
if (workflowId in MedusaWorkflow.workflows) {
throw new Error(`Workflow with id ${workflowId} already registered.`)
}
MedusaWorkflow.workflows[workflowId] = exportedWorkflow
}

View File

@@ -72,7 +72,7 @@ export type ReturnWorkflow<
container?: LoadedModule[] | MedusaContainer
): Omit<
LocalWorkflow,
"run" | "registerStepSuccess" | "registerStepFailure"
"run" | "registerStepSuccess" | "registerStepFailure" | "cancel"
> &
ExportedWorkflow<TData, TResult, TDataOverride, TResultOverride>
} & THooks & {