refactor: Implement test run helper to work with services

This commit is contained in:
Faruk AYDIN
2022-10-14 20:18:58 +02:00
parent 56a9aeece7
commit 628f872180
11 changed files with 262 additions and 119 deletions

View File

@@ -0,0 +1,65 @@
import Step from '../models/step';
import { processFlow } from '../services/flow';
import { processTrigger } from '../services/trigger';
import { processAction } from '../services/action';
type TestRunOptions = {
stepId: string;
};
const testRun = async (options: TestRunOptions) => {
const untilStep = await Step.query()
.findById(options.stepId)
.throwIfNotFound();
const flow = await untilStep.$relatedQuery('flow');
const [triggerStep, ...actionSteps] = await flow
.$relatedQuery('steps')
.withGraphFetched('connection')
.orderBy('position', 'asc');
const { data, error: triggerError } = await processFlow({
flowId: flow.id,
testRun: true,
});
const firstTriggerDataItem = data[0];
const { executionId, executionStep: triggerExecutionStep } =
await processTrigger({
flowId: flow.id,
stepId: triggerStep.id,
triggerDataItem: firstTriggerDataItem,
testRun: true,
});
if (triggerError) {
const { executionStep: triggerExecutionStepWithError } =
await processTrigger({
flowId: flow.id,
stepId: triggerStep.id,
error: triggerError,
testRun: true,
});
return { executionStep: triggerExecutionStepWithError };
}
if (triggerStep.id === untilStep.id) {
return { executionStep: triggerExecutionStep };
}
for (const actionStep of actionSteps) {
const { executionStep: actionExecutionStep } = await processAction({
flowId: flow.id,
stepId: actionStep.id,
executionId,
});
if (actionStep.id === untilStep.id || actionExecutionStep.errorDetails) {
return { executionStep: actionExecutionStep };
}
}
};
export default testRun;