fix: Make processor to work with multiple trigger objects

This commit is contained in:
Faruk AYDIN
2022-03-12 14:20:45 +03:00
committed by Ömer Faruk Aydın
parent fec55d698a
commit 3f7a888429
3 changed files with 89 additions and 45 deletions

View File

@@ -20,6 +20,6 @@ export default class MyTweet {
const userTimeline = await this.client.v1.userTimelineByUsername(username); const userTimeline = await this.client.v1.userTimelineByUsername(username);
const fetchedTweets = userTimeline.tweets; const fetchedTweets = userTimeline.tweets;
return fetchedTweets[0]; return fetchedTweets;
} }
} }

View File

@@ -23,7 +23,7 @@ const executeFlow = async (
.throwIfNotFound(); .throwIfNotFound();
const flow = await step.$relatedQuery('flow'); const flow = await step.$relatedQuery('flow');
const data = await new Processor(flow, step).run(); const data = await new Processor(flow, step, { testRun: true }).run();
// TODO: Use this snippet to execute flows with the background job. // TODO: Use this snippet to execute flows with the background job.
// const data = processorQueue.add('processorJob', { // const data = processorQueue.add('processorJob', {

View File

@@ -10,12 +10,14 @@ type ExecutionSteps = Record<string, ExecutionStep>;
class Processor { class Processor {
flow: Flow; flow: Flow;
untilStep: Step; untilStep: Step;
testRun: boolean;
static variableRegExp = /({{step\..+\..+}})/g; static variableRegExp = /({{step\..+\..+}})/g;
constructor(flow: Flow, untilStep: Step) { constructor(flow: Flow, untilStep: Step, { testRun = false }) {
this.flow = flow; this.flow = flow;
this.untilStep = untilStep; this.untilStep = untilStep;
this.testRun = testRun;
} }
async run() { async run() {
@@ -24,17 +26,30 @@ class Processor {
.withGraphFetched('connection') .withGraphFetched('connection')
.orderBy('position', 'asc'); .orderBy('position', 'asc');
const triggerStep = steps.find((step) => step.type === 'trigger');
let initialTriggerData = await this.getInitialTriggerData(triggerStep);
if (this.testRun) {
initialTriggerData = [initialTriggerData[0]];
}
const executions: Execution[] = [];
for await (const data of initialTriggerData) {
const execution = await Execution.query().insert({ const execution = await Execution.query().insert({
flowId: this.flow.id, flowId: this.flow.id,
testRun: true, testRun: this.testRun,
}); });
executions.push(execution);
let previousExecutionStep: ExecutionStep; let previousExecutionStep: ExecutionStep;
let fetchedData;
const priorExecutionSteps: ExecutionSteps = {}; const priorExecutionSteps: ExecutionSteps = {};
let fetchedActionData = {};
for await (const step of steps) { for await (const step of steps) {
const appData = App.findOneByKey(step.appKey); const appData = App.findOneByKey(step.appKey);
const { const {
appKey, appKey,
connection, connection,
@@ -43,37 +58,66 @@ class Processor {
parameters: rawParameters = {}, parameters: rawParameters = {},
id, id,
} = step; } = step;
const isTrigger = type === 'trigger'; const isTrigger = type === 'trigger';
const AppClass = (await import(`../apps/${appKey}`)).default; const AppClass = (await import(`../apps/${appKey}`)).default;
const computedParameters = Processor.computeParameters( const computedParameters = Processor.computeParameters(
rawParameters, rawParameters,
priorExecutionSteps priorExecutionSteps
); );
const appInstance = new AppClass( const appInstance = new AppClass(
appData, appData,
connection.formattedData, connection.formattedData,
computedParameters computedParameters
); );
const commands = isTrigger ? appInstance.triggers : appInstance.actions;
const command = commands[key]; if (!isTrigger) {
fetchedData = await command.run(); const command = appInstance.actions[key];
fetchedActionData = await command.run();
}
previousExecutionStep = await execution previousExecutionStep = await execution
.$relatedQuery('executionSteps') .$relatedQuery('executionSteps')
.insertAndFetch({ .insertAndFetch({
stepId: id, stepId: id,
status: 'success', status: 'success',
dataIn: previousExecutionStep?.dataOut, dataIn: isTrigger ? rawParameters : previousExecutionStep?.dataOut,
dataOut: fetchedData, dataOut: isTrigger ? data : fetchedActionData,
}); });
priorExecutionSteps[id] = previousExecutionStep; priorExecutionSteps[id] = previousExecutionStep;
if (id === this.untilStep.id) { if (id === this.untilStep.id) {
return fetchedData; break;
}
} }
} }
if (!this.testRun) return;
const lastExecutionStepFromFirstExecution = await executions[0]
.$relatedQuery('executionSteps')
.orderBy('created_at', 'desc')
.first();
return lastExecutionStepFromFirstExecution.dataOut;
}
async getInitialTriggerData(step: Step) {
const appData = App.findOneByKey(step.appKey);
const { appKey, connection, key, parameters: rawParameters = {} } = step;
const AppClass = (await import(`../apps/${appKey}`)).default;
const appInstance = new AppClass(
appData,
connection.formattedData,
rawParameters
);
const command = appInstance.triggers[key];
const fetchedData = await command.run();
return fetchedData; return fetchedData;
} }