refactor: Introduce IActionOutput and ITriggerOutput types

This commit is contained in:
Faruk AYDIN
2022-10-12 21:10:44 +02:00
committed by Ali BARIN
parent 6895378d33
commit d9192f6e6b
22 changed files with 327 additions and 279 deletions

View File

@@ -24,13 +24,13 @@ export default {
options: [
{
label: 'Yes',
value: true
value: true,
},
{
label: 'No',
value: false
}
]
value: false,
},
],
},
{
label: 'Time of day',
@@ -42,111 +42,111 @@ export default {
options: [
{
label: '00:00',
value: 0
value: 0,
},
{
label: '01:00',
value: 1
value: 1,
},
{
label: '02:00',
value: 2
value: 2,
},
{
label: '03:00',
value: 3
value: 3,
},
{
label: '04:00',
value: 4
value: 4,
},
{
label: '05:00',
value: 5
value: 5,
},
{
label: '06:00',
value: 6
value: 6,
},
{
label: '07:00',
value: 7
value: 7,
},
{
label: '08:00',
value: 8
value: 8,
},
{
label: '09:00',
value: 9
value: 9,
},
{
label: '10:00',
value: 10
value: 10,
},
{
label: '11:00',
value: 11
value: 11,
},
{
label: '12:00',
value: 12
value: 12,
},
{
label: '13:00',
value: 13
value: 13,
},
{
label: '14:00',
value: 14
value: 14,
},
{
label: '15:00',
value: 15
value: 15,
},
{
label: '16:00',
value: 16
value: 16,
},
{
label: '17:00',
value: 17
value: 17,
},
{
label: '18:00',
value: 18
value: 18,
},
{
label: '19:00',
value: 19
value: 19,
},
{
label: '20:00',
value: 20
value: 20,
},
{
label: '21:00',
value: 21
value: 21,
},
{
label: '22:00',
value: 22
value: 22,
},
{
label: '23:00',
value: 23
}
]
}
]
value: 23,
},
],
},
],
},
{
key: 'testStep',
name: 'Test trigger'
}
name: 'Test trigger',
},
],
getInterval(parameters: IGlobalVariable["db"]["step"]["parameters"]) {
getInterval(parameters: IGlobalVariable['step']['parameters']) {
if (parameters.triggersOnWeekend as boolean) {
return cronTimes.everyDayAt(parameters.hour as number);
}
@@ -156,14 +156,20 @@ export default {
async run($: IGlobalVariable, startDateTime: Date) {
const dateTime = DateTime.fromJSDate(startDateTime);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(dateTime) as IJSONValue;
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
dateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] };
},
async testRun($: IGlobalVariable) {
const nextCronDateTime = getNextCronDateTime(this.getInterval($.db.step.parameters));
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(nextCronDateTime) as IJSONValue;
const nextCronDateTime = getNextCronDateTime(
this.getInterval($.step.parameters)
);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
nextCronDateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] };
},

View File

@@ -24,25 +24,25 @@ export default {
options: [
{
label: 'Yes',
value: true
value: true,
},
{
label: 'No',
value: false
}
]
}
]
value: false,
},
],
},
],
},
{
key: 'testStep',
name: 'Test trigger'
}
name: 'Test trigger',
},
],
getInterval(parameters: IGlobalVariable["db"]["step"]["parameters"]) {
getInterval(parameters: IGlobalVariable['step']['parameters']) {
if (parameters.triggersOnWeekend) {
return cronTimes.everyHour
return cronTimes.everyHour;
}
return cronTimes.everyHourExcludingWeekends;
@@ -50,14 +50,20 @@ export default {
async run($: IGlobalVariable, startDateTime: Date) {
const dateTime = DateTime.fromJSDate(startDateTime);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(dateTime) as IJSONValue;
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
dateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] };
},
async testRun($: IGlobalVariable) {
const nextCronDateTime = getNextCronDateTime(this.getInterval($.db.step.parameters));
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(nextCronDateTime) as IJSONValue;
const nextCronDateTime = getNextCronDateTime(
this.getInterval($.step.parameters)
);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
nextCronDateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] };
},

View File

@@ -23,129 +23,129 @@ export default {
options: [
{
label: 1,
value: 1
value: 1,
},
{
label: 2,
value: 2
value: 2,
},
{
label: 3,
value: 3
value: 3,
},
{
label: 4,
value: 4
value: 4,
},
{
label: 5,
value: 5
value: 5,
},
{
label: 6,
value: 6
value: 6,
},
{
label: 7,
value: 7
value: 7,
},
{
label: 8,
value: 8
value: 8,
},
{
label: 9,
value: 9
value: 9,
},
{
label: 10,
value: 10
value: 10,
},
{
label: 11,
value: 11
value: 11,
},
{
label: 12,
value: 12
value: 12,
},
{
label: 13,
value: 13
value: 13,
},
{
label: 14,
value: 14
value: 14,
},
{
label: 15,
value: 15
value: 15,
},
{
label: 16,
value: 16
value: 16,
},
{
label: 17,
value: 17
value: 17,
},
{
label: 18,
value: 18
value: 18,
},
{
label: 19,
value: 19
value: 19,
},
{
label: 20,
value: 20
value: 20,
},
{
label: 21,
value: 21
value: 21,
},
{
label: 22,
value: 22
value: 22,
},
{
label: 23,
value: 23
value: 23,
},
{
label: 24,
value: 24
value: 24,
},
{
label: 25,
value: 25
value: 25,
},
{
label: 26,
value: 26
value: 26,
},
{
label: 27,
value: 27
value: 27,
},
{
label: 28,
value: 28
value: 28,
},
{
label: 29,
value: 29
value: 29,
},
{
label: 30,
value: 30
value: 30,
},
{
label: 31,
value: 31
}
]
value: 31,
},
],
},
{
label: 'Time of day',
@@ -157,126 +157,135 @@ export default {
options: [
{
label: '00:00',
value: 0
value: 0,
},
{
label: '01:00',
value: 1
value: 1,
},
{
label: '02:00',
value: 2
value: 2,
},
{
label: '03:00',
value: 3
value: 3,
},
{
label: '04:00',
value: 4
value: 4,
},
{
label: '05:00',
value: 5
value: 5,
},
{
label: '06:00',
value: 6
value: 6,
},
{
label: '07:00',
value: 7
value: 7,
},
{
label: '08:00',
value: 8
value: 8,
},
{
label: '09:00',
value: 9
value: 9,
},
{
label: '10:00',
value: 10
value: 10,
},
{
label: '11:00',
value: 11
value: 11,
},
{
label: '12:00',
value: 12
value: 12,
},
{
label: '13:00',
value: 13
value: 13,
},
{
label: '14:00',
value: 14
value: 14,
},
{
label: '15:00',
value: 15
value: 15,
},
{
label: '16:00',
value: 16
value: 16,
},
{
label: '17:00',
value: 17
value: 17,
},
{
label: '18:00',
value: 18
value: 18,
},
{
label: '19:00',
value: 19
value: 19,
},
{
label: '20:00',
value: 20
value: 20,
},
{
label: '21:00',
value: 21
value: 21,
},
{
label: '22:00',
value: 22
value: 22,
},
{
label: '23:00',
value: 23
}
]
}
]
value: 23,
},
],
},
],
},
{
key: 'testStep',
name: 'Test trigger'
}
name: 'Test trigger',
},
],
getInterval(parameters: IGlobalVariable["db"]["step"]["parameters"]) {
const interval = cronTimes.everyMonthOnAndAt(parameters.day as number, parameters.hour as number);
getInterval(parameters: IGlobalVariable['step']['parameters']) {
const interval = cronTimes.everyMonthOnAndAt(
parameters.day as number,
parameters.hour as number
);
return interval;
},
async run($: IGlobalVariable, startDateTime: Date) {
const dateTime = DateTime.fromJSDate(startDateTime);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(dateTime) as IJSONValue;
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
dateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] };
},
async testRun($: IGlobalVariable) {
const nextCronDateTime = getNextCronDateTime(this.getInterval($.db.step.parameters));
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(nextCronDateTime) as IJSONValue;
const nextCronDateTime = getNextCronDateTime(
this.getInterval($.step.parameters)
);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
nextCronDateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] };
},

View File

@@ -23,33 +23,33 @@ export default {
options: [
{
label: 'Monday',
value: 1
value: 1,
},
{
label: 'Tuesday',
value: 2
value: 2,
},
{
label: 'Wednesday',
value: 3
value: 3,
},
{
label: 'Thursday',
value: 4
value: 4,
},
{
label: 'Friday',
value: 5
value: 5,
},
{
label: 'Saturday',
value: 6
value: 6,
},
{
label: 'Sunday',
value: 0
}
]
value: 0,
},
],
},
{
label: 'Time of day',
@@ -61,126 +61,135 @@ export default {
options: [
{
label: '00:00',
value: 0
value: 0,
},
{
label: '01:00',
value: 1
value: 1,
},
{
label: '02:00',
value: 2
value: 2,
},
{
label: '03:00',
value: 3
value: 3,
},
{
label: '04:00',
value: 4
value: 4,
},
{
label: '05:00',
value: 5
value: 5,
},
{
label: '06:00',
value: 6
value: 6,
},
{
label: '07:00',
value: 7
value: 7,
},
{
label: '08:00',
value: 8
value: 8,
},
{
label: '09:00',
value: 9
value: 9,
},
{
label: '10:00',
value: 10
value: 10,
},
{
label: '11:00',
value: 11
value: 11,
},
{
label: '12:00',
value: 12
value: 12,
},
{
label: '13:00',
value: 13
value: 13,
},
{
label: '14:00',
value: 14
value: 14,
},
{
label: '15:00',
value: 15
value: 15,
},
{
label: '16:00',
value: 16
value: 16,
},
{
label: '17:00',
value: 17
value: 17,
},
{
label: '18:00',
value: 18
value: 18,
},
{
label: '19:00',
value: 19
value: 19,
},
{
label: '20:00',
value: 20
value: 20,
},
{
label: '21:00',
value: 21
value: 21,
},
{
label: '22:00',
value: 22
value: 22,
},
{
label: '23:00',
value: 23
}
]
}
]
value: 23,
},
],
},
],
},
{
key: 'testStep',
name: 'Test trigger'
}
name: 'Test trigger',
},
],
getInterval(parameters: IGlobalVariable["db"]["step"]["parameters"]) {
const interval = cronTimes.everyWeekOnAndAt(parameters.weekday as number, parameters.hour as number);
getInterval(parameters: IGlobalVariable['step']['parameters']) {
const interval = cronTimes.everyWeekOnAndAt(
parameters.weekday as number,
parameters.hour as number
);
return interval;
},
async run($: IGlobalVariable, startDateTime: Date) {
const dateTime = DateTime.fromJSDate(startDateTime);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(dateTime) as IJSONValue;
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
dateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] };
},
async testRun($: IGlobalVariable) {
const nextCronDateTime = getNextCronDateTime(this.getInterval($.db.step.parameters));
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(nextCronDateTime) as IJSONValue;
const nextCronDateTime = getNextCronDateTime(
this.getInterval($.step.parameters)
);
const dateTimeObjectRepresentation = getDateTimeObjectRepresentation(
nextCronDateTime
) as IJSONValue;
return { data: [dateTimeObjectRepresentation] };
},

View File

@@ -1,4 +1,4 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import { IGlobalVariable, IActionOutput } from '@automatisch/types';
type FindMessageOptions = {
query: string;
@@ -8,11 +8,6 @@ type FindMessageOptions = {
};
const findMessage = async ($: IGlobalVariable, options: FindMessageOptions) => {
const message: {
data?: IJSONObject;
error?: IJSONObject;
} = {};
const headers = {
Authorization: `Bearer ${$.auth.data.accessToken}`,
};
@@ -29,20 +24,14 @@ const findMessage = async ($: IGlobalVariable, options: FindMessageOptions) => {
params,
});
if (response.integrationError) {
message.error = response.integrationError;
return message;
}
const data = response.data;
if (!data.ok) {
message.error = data;
return message;
}
const messages = data.messages.matches;
message.data = messages?.[0];
const message: IActionOutput = {
data: {
raw: data?.data?.messages.matches[0],
},
error: response?.integrationError || (!data.ok && data),
};
return message;
};

View File

@@ -72,7 +72,7 @@ export default {
],
async run($: IGlobalVariable) {
const parameters = $.db.step.parameters;
const parameters = $.step.parameters;
const query = parameters.query as string;
const sortBy = parameters.sortBy as string;
const sortDirection = parameters.sortDirection as string;

View File

@@ -49,8 +49,8 @@ export default {
],
async run($: IGlobalVariable) {
const channelId = $.db.step.parameters.channel as string;
const text = $.db.step.parameters.message as string;
const channelId = $.step.parameters.channel as string;
const text = $.step.parameters.message as string;
const message = await postMessage($, channelId, text);

View File

@@ -1,18 +1,10 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import { IGlobalVariable, IActionOutput } from '@automatisch/types';
const postMessage = async (
$: IGlobalVariable,
channelId: string,
text: string
) => {
const message: {
data: IJSONObject | null | undefined;
error: IJSONObject | null | undefined;
} = {
data: null,
error: null,
};
const headers = {
Authorization: `Bearer ${$.auth.data.accessToken}`,
};
@@ -24,8 +16,12 @@ const postMessage = async (
const response = await $.http.post('/chat.postMessage', params, { headers });
message.error = response?.integrationError;
message.data = response?.data?.message;
const message: IActionOutput = {
data: {
raw: response?.data?.message,
},
error: response?.integrationError,
};
if (response.data.ok === false) {
message.error = response.data;

View File

@@ -1,4 +1,8 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import {
IGlobalVariable,
IJSONObject,
ITriggerOutput,
} from '@automatisch/types';
import { URLSearchParams } from 'url';
import { omitBy, isEmpty } from 'lodash';
import generateRequest from './generate-request';
@@ -14,12 +18,8 @@ const getUserFollowers = async (
) => {
let response;
const followers: {
data: IJSONObject[];
error: IJSONObject | null;
} = {
const followers: ITriggerOutput = {
data: [],
error: null,
};
do {
@@ -54,7 +54,10 @@ const getUserFollowers = async (
!options.lastInternalId ||
Number(tweet.id) > Number(options.lastInternalId)
) {
followers.data.push(tweet);
followers.data.push({
raw: tweet,
meta: { internalId: tweet.id as string },
});
} else {
return;
}

View File

@@ -1,4 +1,8 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import {
IGlobalVariable,
IJSONObject,
ITriggerOutput,
} from '@automatisch/types';
import { URLSearchParams } from 'url';
import omitBy from 'lodash/omitBy';
import isEmpty from 'lodash/isEmpty';
@@ -22,19 +26,15 @@ const getUserTweets = async (
const currentUser = await getCurrentUser($);
username = currentUser.username as string;
} else {
username = $.db.step.parameters.username as string;
username = $.step.parameters.username as string;
}
const user = await getUserByUsername($, username);
let response;
const tweets: {
data: IJSONObject[];
error: IJSONObject | null;
} = {
const tweets: ITriggerOutput = {
data: [],
error: null,
};
do {
@@ -65,7 +65,10 @@ const getUserTweets = async (
!options.lastInternalId ||
Number(tweet.id) > Number(options.lastInternalId)
) {
tweets.data.push(tweet);
tweets.data.push({
raw: tweet,
meta: { internalId: tweet.id as string },
});
} else {
return;
}

View File

@@ -20,7 +20,7 @@ export default {
async run($: IGlobalVariable) {
return await getUserTweets($, {
currentUser: true,
lastInternalId: $.db.flow.lastInternalId,
lastInternalId: $.flow.lastInternalId,
});
},

View File

@@ -18,7 +18,7 @@ export default {
],
async run($: IGlobalVariable) {
return await myFollowers($, $.db.flow.lastInternalId);
return await myFollowers($, $.flow.lastInternalId);
},
async testRun($: IGlobalVariable) {

View File

@@ -32,14 +32,14 @@ export default {
async run($: IGlobalVariable) {
return await searchTweets($, {
searchTerm: $.db.step.parameters.searchTerm as string,
lastInternalId: $.db.flow.lastInternalId,
searchTerm: $.step.parameters.searchTerm as string,
lastInternalId: $.flow.lastInternalId,
});
},
async testRun($: IGlobalVariable) {
return await searchTweets($, {
searchTerm: $.db.step.parameters.searchTerm as string,
searchTerm: $.step.parameters.searchTerm as string,
});
},
};

View File

@@ -1,4 +1,8 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import {
IGlobalVariable,
IJSONObject,
ITriggerOutput,
} from '@automatisch/types';
import qs from 'qs';
import generateRequest from '../../common/generate-request';
import { omitBy, isEmpty } from 'lodash';
@@ -14,12 +18,8 @@ const searchTweets = async (
) => {
let response;
const tweets: {
data: IJSONObject[];
error: IJSONObject | null;
} = {
const tweets: ITriggerOutput = {
data: [],
error: null,
};
do {
@@ -56,7 +56,14 @@ const searchTweets = async (
!options.lastInternalId ||
Number(tweet.id) > Number(options.lastInternalId)
) {
tweets.data.push(tweet);
const dataItem = {
raw: tweet,
meta: {
internalId: tweet.id as string,
},
};
tweets.data.push(dataItem);
} else {
return;
}

View File

@@ -32,15 +32,15 @@ export default {
async run($: IGlobalVariable) {
return await getUserTweets($, {
currentUser: false,
userId: $.db.step.parameters.username as string,
lastInternalId: $.db.flow.lastInternalId,
userId: $.step.parameters.username as string,
lastInternalId: $.flow.lastInternalId,
});
},
async testRun($: IGlobalVariable) {
return await getUserTweets($, {
currentUser: false,
userId: $.db.step.parameters.username as string,
userId: $.step.parameters.username as string,
});
},
};

View File

@@ -29,7 +29,7 @@ const createAuthData = async (
.default;
const app = await App.findOneByKey(connection.key);
const $ = await globalVariable(connection, app);
const $ = await globalVariable({ connection, app });
await authInstance.createAuthData($);
try {

View File

@@ -21,7 +21,7 @@ const verifyConnection = async (
.throwIfNotFound();
const app = await App.findOneByKey(connection.key);
const $ = await globalVariable(connection, app);
const $ = await globalVariable({ connection, app });
await app.auth.verifyCredentials($);
connection = await connection.$query().patchAndFetch({

View File

@@ -25,7 +25,7 @@ const getData = async (_parent: unknown, params: Params, context: Context) => {
if (!connection || !step.appKey) return null;
const app = await App.findOneByKey(step.appKey);
const $ = await globalVariable(connection, app, step.flow, step);
const $ = await globalVariable({ connection, app, flow: step.flow, step });
const command = app.data.find((data: IData) => data.key === params.key);

View File

@@ -20,10 +20,9 @@ const testConnection = async (
.throwIfNotFound();
const app = await App.findOneByKey(connection.key, false);
const $ = await globalVariable(connection, app);
const $ = await globalVariable({ connection, app });
const isStillVerified =
await app.auth.isStillVerified($);
const isStillVerified = await app.auth.isStillVerified($);
connection = await connection.$query().patchAndFetch({
formattedData: connection.formattedData,

View File

@@ -4,12 +4,18 @@ import Flow from '../models/flow';
import Step from '../models/step';
import { IJSONObject, IApp, IGlobalVariable } from '@automatisch/types';
type GlobalVariableOptions = {
connection?: Connection;
app: IApp;
flow?: Flow;
step?: Step;
};
const globalVariable = async (
connection: Connection,
appData: IApp,
flow?: Flow,
currentStep?: Step
options: GlobalVariableOptions
): Promise<IGlobalVariable> => {
const { connection, app, flow, step } = options;
const lastInternalId = await flow?.lastInternalId();
return {
@@ -28,15 +34,13 @@ const globalVariable = async (
},
data: connection?.formattedData,
},
app: appData,
http: createHttpClient({ baseURL: appData.baseUrl }),
db: {
flow: {
lastInternalId,
},
step: {
parameters: currentStep?.parameters || {},
},
app: app,
http: createHttpClient({ baseURL: app.baseUrl }),
flow: {
lastInternalId,
},
step: {
parameters: step?.parameters || {},
},
};
};

View File

@@ -1,5 +1,5 @@
import get from 'lodash.get';
import { IJSONObject } from '@automatisch/types';
import { IActionOutput } from '@automatisch/types';
import App from '../models/app';
import Flow from '../models/flow';
@@ -61,8 +61,8 @@ class Processor {
if (initialTriggerData.data.length > 1) {
initialTriggerData.data = initialTriggerData.data.sort(
(item: IJSONObject, nextItem: IJSONObject) => {
return (item.id as number) - (nextItem.id as number);
(item, nextItem) => {
return (item.raw.id as number) - (nextItem.raw.id as number);
}
);
}
@@ -73,7 +73,7 @@ class Processor {
const execution = await Execution.query().insert({
flowId: this.flow.id,
testRun: this.testRun,
internalId: data.id as string,
internalId: data.meta.internalId as string,
});
executions.push(execution);
@@ -81,12 +81,8 @@ class Processor {
let previousExecutionStep: ExecutionStep;
const priorExecutionSteps: ExecutionSteps = {};
let fetchedActionData: {
data: IJSONObject | null;
error: IJSONObject | null;
} = {
let fetchedActionData: IActionOutput = {
data: null,
error: null,
};
for await (const step of steps) {
@@ -105,12 +101,12 @@ class Processor {
const clonedStep = Object.assign({}, step);
clonedStep.parameters = computedParameters;
const $ = await globalVariable(
step.connection,
const $ = await globalVariable({
connection: step.connection,
app,
this.flow,
clonedStep
);
flow: this.flow,
step: clonedStep,
});
if (!isTrigger && key) {
const command = app.actions.find((action) => action.key === key);
@@ -135,7 +131,7 @@ class Processor {
stepId: id,
status: 'success',
dataIn: isTrigger ? rawParameters : computedParameters,
dataOut: isTrigger ? data : fetchedActionData.data,
dataOut: isTrigger ? data.raw : fetchedActionData.data.raw,
});
priorExecutionSteps[id] = previousExecutionStep;
@@ -176,12 +172,12 @@ class Processor {
if (!step.appKey || !step.key) return null;
const app = await App.findOneByKey(step.appKey);
const $ = await globalVariable(
step.connection,
const $ = await globalVariable({
connection: step.connection,
app,
this.flow,
flow: this.flow,
step,
)
});
const command = app.triggers.find((trigger) => trigger.key === step.key);