Merge pull request #476 from automatisch/refactor/slack-list-channels

refactor: Adjust slack list channels to use slack client
This commit is contained in:
Ömer Faruk Aydın
2022-09-01 22:24:21 +03:00
committed by GitHub
4 changed files with 27 additions and 12 deletions

View File

@@ -1,10 +1,12 @@
import ListChannels from './data/list-channels';
import { IJSONObject } from '@automatisch/types';
import SlackClient from './client';
export default class Data {
client: SlackClient;
listChannels: ListChannels;
constructor(connectionData: IJSONObject) {
this.listChannels = new ListChannels(connectionData);
constructor(client: SlackClient) {
this.client = client;
this.listChannels = new ListChannels(client);
}
}

View File

@@ -1,17 +1,27 @@
import type { IJSONObject } from '@automatisch/types';
import { WebClient } from '@slack/web-api';
import { IJSONObject } from '@automatisch/types';
import SlackClient from '../client';
export default class ListChannels {
client: WebClient;
client: SlackClient;
constructor(connectionData: IJSONObject) {
this.client = new WebClient(connectionData.accessToken as string);
constructor(client: SlackClient) {
this.client = client;
}
async run() {
const { channels } = await this.client.conversations.list();
const response = await this.client.httpClient.get('/conversations.list', {
headers: {
Authorization: `Bearer ${this.client.connection.formattedData.accessToken}`,
},
});
return channels.map((channel) => {
if (response.data.ok === 'false') {
throw new Error(
`Error occured while fetching slack channels: ${response.data.error}`
);
}
return response.data.channels.map((channel: IJSONObject) => {
return {
value: channel.id,
name: channel.name,

View File

@@ -26,6 +26,6 @@ export default class Slack implements IService {
// this.triggers = new Triggers(this.client);
// this.actions = new Actions(this.client);
// this.data = new Data(this.client);
this.data = new Data(this.client);
}
}

View File

@@ -10,7 +10,10 @@ type Params = {
const getData = async (_parent: unknown, params: Params, context: Context) => {
const step = await context.currentUser
.$relatedQuery('steps')
.withGraphFetched('connection, flow')
.withGraphFetched({
connection: true,
flow: true,
})
.findById(params.stepId);
if (!step) return null;