fix: Integration and auth problems wrt slack and twitter

This commit is contained in:
Faruk AYDIN
2022-10-07 16:05:13 +03:00
parent 2862953136
commit 11ef2ac4bc
8 changed files with 104 additions and 90 deletions

View File

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

View File

@@ -19,11 +19,16 @@ export default {
}, },
}); });
if (response.integrationError || response.data.ok === 'false') { if (response.integrationError) {
channels.error = response.integrationError; channels.error = response.integrationError;
return channels; return channels;
} }
if (response.data.ok === 'false') {
channels.error = response.data.error;
return channels;
}
channels.data = response.data.channels.map((channel: IJSONObject) => { channels.data = response.data.channels.map((channel: IJSONObject) => {
return { return {
value: channel.id, value: channel.id,

View File

@@ -25,7 +25,7 @@ const createAuthData = async (
return null; return null;
} }
const authInstance = (await import(`../../apps/${connection.key}2/auth`)) const authInstance = (await import(`../../apps/${connection.key}/auth`))
.default; .default;
const app = await App.findOneByKey(connection.key); const app = await App.findOneByKey(connection.key);

View File

@@ -104,14 +104,18 @@ type App {
authDocUrl: String authDocUrl: String
primaryColor: String primaryColor: String
supportsConnections: Boolean supportsConnections: Boolean
fields: [Field] auth: AppAuth
authenticationSteps: [AuthenticationStep]
reconnectionSteps: [ReconnectionStep]
triggers: [Trigger] triggers: [Trigger]
actions: [Action] actions: [Action]
connections: [Connection] connections: [Connection]
} }
type AppAuth {
fields: [Field]
authenticationSteps: [AuthenticationStep]
reconnectionSteps: [ReconnectionStep]
}
enum ArgumentEnumType { enum ArgumentEnumType {
integer integer
string string

View File

@@ -2,19 +2,21 @@ import type { IApp } from '@automatisch/types';
import appConfig from '../config/app'; import appConfig from '../config/app';
const appInfoConverter = (rawAppData: IApp) => { const appInfoConverter = (rawAppData: IApp) => {
const stringifiedRawAppData = JSON.stringify(rawAppData); rawAppData.iconUrl = rawAppData.iconUrl.replace(
let computedRawData = stringifiedRawAppData.replace(
'{BASE_URL}', '{BASE_URL}',
appConfig.baseUrl appConfig.baseUrl
); );
computedRawData = computedRawData.replace(
'{WEB_APP_URL}',
appConfig.webAppUrl
);
const computedJSONData: IApp = JSON.parse(computedRawData); if (rawAppData.auth?.fields) {
return computedJSONData; rawAppData.auth.fields = rawAppData.auth.fields.map((field) => {
return {
...field,
value: field.value?.replace('{WEB_APP_URL}', appConfig.webAppUrl),
};
});
}
return rawAppData;
}; };
export default appInfoConverter; export default appInfoConverter;

View File

@@ -10,16 +10,14 @@ class App {
// Temporaryly restrict the apps we expose until // Temporaryly restrict the apps we expose until
// their actions/triggers are implemented! // their actions/triggers are implemented!
static temporaryList = [ static temporaryList = ['slack', 'twitter', 'scheduler'];
'slack',
'twitter',
'scheduler'
];
static async findAll(name?: string, stripFuncs = true): Promise<IApp[]> { static async findAll(name?: string, stripFuncs = true): Promise<IApp[]> {
if (!name) if (!name)
return Promise.all( return Promise.all(
this.temporaryList.map(async (name) => await this.findOneByName(name, stripFuncs)) this.temporaryList.map(
async (name) => await this.findOneByName(name, stripFuncs)
)
); );
return Promise.all( return Promise.all(
@@ -32,16 +30,12 @@ class App {
static async findOneByName(name: string, stripFuncs = false): Promise<IApp> { static async findOneByName(name: string, stripFuncs = false): Promise<IApp> {
const rawAppData = await getApp(name.toLocaleLowerCase(), stripFuncs); const rawAppData = await getApp(name.toLocaleLowerCase(), stripFuncs);
if (!stripFuncs) return rawAppData;
return appInfoConverter(rawAppData); return appInfoConverter(rawAppData);
} }
static async findOneByKey(key: string, stripFuncs = false): Promise<IApp> { static async findOneByKey(key: string, stripFuncs = false): Promise<IApp> {
const rawAppData = await getApp(key, stripFuncs); const rawAppData = await getApp(key, stripFuncs);
if (!stripFuncs) return rawAppData;
return appInfoConverter(rawAppData); return appInfoConverter(rawAppData);
} }
} }

View File

@@ -10,6 +10,7 @@ export const GET_APP = gql`
authDocUrl authDocUrl
primaryColor primaryColor
supportsConnections supportsConnections
auth {
fields { fields {
key key
label label
@@ -49,6 +50,7 @@ export const GET_APP = gql`
} }
} }
} }
}
connections { connections {
id id
} }

View File

@@ -10,6 +10,7 @@ export const GET_APPS = gql`
primaryColor primaryColor
connectionCount connectionCount
supportsConnections supportsConnections
auth {
fields { fields {
key key
label label
@@ -50,6 +51,7 @@ export const GET_APPS = gql`
} }
} }
} }
}
triggers { triggers {
name name
key key