refactor: Redesign list channels for the slack actions
This commit is contained in:
36
packages/backend/src/apps/slack/data/list-channels/index.ts
Normal file
36
packages/backend/src/apps/slack/data/list-channels/index.ts
Normal 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;
|
||||||
|
},
|
||||||
|
};
|
@@ -33,7 +33,7 @@ const updateFlowStatus = async (
|
|||||||
|
|
||||||
const triggerStep = await flow.getTriggerStep();
|
const triggerStep = await flow.getTriggerStep();
|
||||||
const trigger = await triggerStep.getTrigger();
|
const trigger = await triggerStep.getTrigger();
|
||||||
const interval = trigger.getInterval(triggerStep.parameters);
|
const interval = trigger?.getInterval(triggerStep.parameters);
|
||||||
const repeatOptions = {
|
const repeatOptions = {
|
||||||
cron: interval || EVERY_15_MINUTES_CRON,
|
cron: interval || EVERY_15_MINUTES_CRON,
|
||||||
};
|
};
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { IJSONObject } from '@automatisch/types';
|
import { IData, IJSONObject } from '@automatisch/types';
|
||||||
import Context from '../../types/express/context';
|
import Context from '../../types/express/context';
|
||||||
import App from '../../models/app';
|
import App from '../../models/app';
|
||||||
import globalVariable from '../../helpers/global-variable';
|
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;
|
if (!connection || !step.appKey) return null;
|
||||||
|
|
||||||
const app = await App.findOneByKey(step.appKey);
|
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($);
|
const fetchedData = await command.run($);
|
||||||
|
|
||||||
return fetchedData;
|
if (fetchedData.error) {
|
||||||
|
throw new Error(JSON.stringify(fetchedData.error));
|
||||||
|
}
|
||||||
|
|
||||||
|
return fetchedData.data;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default getData;
|
export default getData;
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { join } from 'path';
|
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');
|
const appsPath = join(__dirname, '../apps');
|
||||||
|
|
||||||
@@ -9,12 +9,13 @@ async function getDefaultExport(path: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function stripFunctions<C>(data: C): C {
|
function stripFunctions<C>(data: C): C {
|
||||||
return JSON.parse(
|
return JSON.parse(JSON.stringify(data));
|
||||||
JSON.stringify(data)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getFileContent<C>(path: string, stripFuncs: boolean): Promise<C> {
|
async function getFileContent<C>(
|
||||||
|
path: string,
|
||||||
|
stripFuncs: boolean
|
||||||
|
): Promise<C> {
|
||||||
try {
|
try {
|
||||||
const fileContent = await getDefaultExport(path);
|
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 appSubdirectory = join(appsPath, path);
|
||||||
const childrenContent = [];
|
const childrenContent = [];
|
||||||
|
|
||||||
@@ -51,9 +55,22 @@ async function getChildrenContentInDirectory<C>(path: string, stripFuncs: boolea
|
|||||||
const getApp = async (appKey: string, stripFuncs = true) => {
|
const getApp = async (appKey: string, stripFuncs = true) => {
|
||||||
const appData: IApp = await getDefaultExport(`../apps/${appKey}`);
|
const appData: IApp = await getDefaultExport(`../apps/${appKey}`);
|
||||||
|
|
||||||
appData.auth = await getFileContent<IAuth>(`../apps/${appKey}/auth/index.ts`, stripFuncs);
|
appData.auth = await getFileContent<IAuth>(
|
||||||
appData.triggers = await getChildrenContentInDirectory<ITrigger>(`${appKey}/triggers`, stripFuncs);
|
`../apps/${appKey}/auth/index.ts`,
|
||||||
appData.actions = await getChildrenContentInDirectory<IAction>(`${appKey}/actions`, stripFuncs);
|
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;
|
return appData;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user