refactor: Redesign list channels for the slack actions

This commit is contained in:
Faruk AYDIN
2022-10-07 14:00:58 +03:00
parent c958abdfcf
commit 2862953136
4 changed files with 72 additions and 14 deletions

View File

@@ -0,0 +1,36 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
export default {
name: 'List channels',
key: 'listChannels',
async run($: IGlobalVariable) {
const channels: {
data: IJSONObject[];
error: IJSONObject | null;
} = {
data: [],
error: null,
};
const response = await $.http.get('/conversations.list', {
headers: {
Authorization: `Bearer ${$.auth.data.accessToken}`,
},
});
if (response.integrationError || response.data.ok === 'false') {
channels.error = response.integrationError;
return channels;
}
channels.data = response.data.channels.map((channel: IJSONObject) => {
return {
value: channel.id,
name: channel.name,
};
});
return channels;
},
};

View File

@@ -33,7 +33,7 @@ const updateFlowStatus = async (
const triggerStep = await flow.getTriggerStep();
const trigger = await triggerStep.getTrigger();
const interval = trigger.getInterval(triggerStep.parameters);
const interval = trigger?.getInterval(triggerStep.parameters);
const repeatOptions = {
cron: interval || EVERY_15_MINUTES_CRON,
};

View File

@@ -1,4 +1,4 @@
import { IJSONObject } from '@automatisch/types';
import { IData, IJSONObject } from '@automatisch/types';
import Context from '../../types/express/context';
import App from '../../models/app';
import globalVariable from '../../helpers/global-variable';
@@ -25,12 +25,17 @@ 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, step.flow, step);
const command = app.data.find((data: IData) => data.key === params.key);
const command = app.data[params.key];
const fetchedData = await command.run($);
return fetchedData;
if (fetchedData.error) {
throw new Error(JSON.stringify(fetchedData.error));
}
return fetchedData.data;
};
export default getData;

View File

@@ -1,6 +1,6 @@
import fs from 'fs';
import { join } from 'path';
import { IApp, IAuth, IAction, ITrigger } from '@automatisch/types';
import { IApp, IAuth, IAction, ITrigger, IData } from '@automatisch/types';
const appsPath = join(__dirname, '../apps');
@@ -9,12 +9,13 @@ async function getDefaultExport(path: string) {
}
function stripFunctions<C>(data: C): C {
return JSON.parse(
JSON.stringify(data)
);
return JSON.parse(JSON.stringify(data));
}
async function getFileContent<C>(path: string, stripFuncs: boolean): Promise<C> {
async function getFileContent<C>(
path: string,
stripFuncs: boolean
): Promise<C> {
try {
const fileContent = await getDefaultExport(path);
@@ -28,7 +29,10 @@ async function getFileContent<C>(path: string, stripFuncs: boolean): Promise<C>
}
}
async function getChildrenContentInDirectory<C>(path: string, stripFuncs: boolean): Promise<C[]> {
async function getChildrenContentInDirectory<C>(
path: string,
stripFuncs: boolean
): Promise<C[]> {
const appSubdirectory = join(appsPath, path);
const childrenContent = [];
@@ -51,9 +55,22 @@ async function getChildrenContentInDirectory<C>(path: string, stripFuncs: boolea
const getApp = async (appKey: string, stripFuncs = true) => {
const appData: IApp = await getDefaultExport(`../apps/${appKey}`);
appData.auth = await getFileContent<IAuth>(`../apps/${appKey}/auth/index.ts`, stripFuncs);
appData.triggers = await getChildrenContentInDirectory<ITrigger>(`${appKey}/triggers`, stripFuncs);
appData.actions = await getChildrenContentInDirectory<IAction>(`${appKey}/actions`, stripFuncs);
appData.auth = await getFileContent<IAuth>(
`../apps/${appKey}/auth/index.ts`,
stripFuncs
);
appData.triggers = await getChildrenContentInDirectory<ITrigger>(
`${appKey}/triggers`,
stripFuncs
);
appData.actions = await getChildrenContentInDirectory<IAction>(
`${appKey}/actions`,
stripFuncs
);
appData.data = await getChildrenContentInDirectory<IData>(
`${appKey}/data`,
stripFuncs
);
return appData;
};