feat: add app connections w/ testing and deleting functions
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import TwitterApi from 'twitter-api-v2';
|
||||
import App from '../../models/app';
|
||||
import Field from '../../types/field';
|
||||
import appData from './info';
|
||||
|
||||
export default class Twitter {
|
||||
client: any
|
||||
@@ -16,7 +17,7 @@ export default class Twitter {
|
||||
});
|
||||
|
||||
this.connectionData = connectionData;
|
||||
this.appData = App.findOneByKey('twitter');
|
||||
this.appData = appData;
|
||||
}
|
||||
|
||||
async createAuthLink() {
|
||||
|
@@ -1,5 +1,6 @@
|
||||
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
||||
import Connection from '../../models/connection';
|
||||
import App from '../../models/app';
|
||||
import connectionType from '../types/connection';
|
||||
import twitterCredentialInputType from '../types/twitter-credential-input';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
@@ -9,13 +10,17 @@ type Params = {
|
||||
data: object
|
||||
}
|
||||
const createConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
const app = await App.findOneByKey(params.key);
|
||||
const connection = await Connection.query().insert({
|
||||
key: params.key,
|
||||
data: params.data,
|
||||
userId: req.currentUser.id
|
||||
});
|
||||
|
||||
return connection;
|
||||
return {
|
||||
...connection,
|
||||
app,
|
||||
};
|
||||
}
|
||||
|
||||
const createConnection = {
|
||||
|
@@ -1,5 +1,6 @@
|
||||
import { GraphQLList, GraphQLString, GraphQLNonNull } from 'graphql';
|
||||
import Connection from '../../models/connection';
|
||||
import App from '../../models/app';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
import connectionType from '../types/connection';
|
||||
|
||||
@@ -8,10 +9,14 @@ type Params = {
|
||||
}
|
||||
|
||||
const getAppConnectionsResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
const app = await App.findOneByKey(params.key);
|
||||
const connections = await Connection.query()
|
||||
.where({ user_id: req.currentUser.id, verified: true, key: params.key })
|
||||
.where({ user_id: req.currentUser.id, key: params.key })
|
||||
|
||||
return connections;
|
||||
return connections.map((connection: any) => ({
|
||||
...connection,
|
||||
app,
|
||||
}));
|
||||
}
|
||||
|
||||
const getAppConnections = {
|
||||
|
@@ -1,17 +1,32 @@
|
||||
import { GraphQLString, GraphQLNonNull } from 'graphql';
|
||||
import App from '../../models/app';
|
||||
import appType from '../types/app';
|
||||
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
|
||||
import Connection from '../../models/connection';
|
||||
import connectionType from '../types/connection';
|
||||
|
||||
type Params = {
|
||||
key: string
|
||||
}
|
||||
|
||||
const getAppResolver = (params: Params) => {
|
||||
const getAppResolver = async (params: Params, req: RequestWithCurrentUser) => {
|
||||
if(!params.key) {
|
||||
throw new Error('No key provided.')
|
||||
}
|
||||
|
||||
return App.findOneByKey(params.key)
|
||||
const app = await App.findOneByKey(params.key);
|
||||
|
||||
if (req.currentUser?.id) {
|
||||
const connections = await Connection.query()
|
||||
.where({ user_id: req.currentUser.id, key: params.key });
|
||||
|
||||
return {
|
||||
...app,
|
||||
connections,
|
||||
};
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
const getApp = {
|
||||
@@ -19,9 +34,7 @@ const getApp = {
|
||||
args: {
|
||||
key: { type: GraphQLNonNull(GraphQLString) },
|
||||
},
|
||||
resolve: (_: any, params: Params) => getAppResolver(params)
|
||||
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => getAppResolver(params, req)
|
||||
}
|
||||
|
||||
|
||||
|
||||
export default getApp;
|
||||
|
@@ -15,7 +15,7 @@ const testConnectionResolver = async (params: Params, req: RequestWithCurrentUse
|
||||
|
||||
const appClass = (await import(`../../apps/${connection.key}`)).default;
|
||||
|
||||
const appInstance = new appClass(connection.data)
|
||||
const appInstance = new appClass(connection.data);
|
||||
const isStillVerified = await appInstance.isStillVerified();
|
||||
|
||||
connection = await connection.$query().patchAndFetch({
|
||||
|
@@ -4,15 +4,20 @@ import authenticationStepType from './authentication-step';
|
||||
|
||||
const appType = new GraphQLObjectType({
|
||||
name: 'App',
|
||||
fields: {
|
||||
name: { type: GraphQLString },
|
||||
key: { type: GraphQLString },
|
||||
connectionCount: { type: GraphQLInt },
|
||||
iconUrl: { type: GraphQLString },
|
||||
docUrl: { type: GraphQLString },
|
||||
primaryColor: { type: GraphQLString },
|
||||
fields: { type: GraphQLList(fieldType) },
|
||||
authenticationSteps: { type: GraphQLList(authenticationStepType) }
|
||||
fields: () => {
|
||||
const connectionType = require('./connection').default;
|
||||
|
||||
return {
|
||||
name: { type: GraphQLString },
|
||||
key: { type: GraphQLString },
|
||||
connectionCount: { type: GraphQLInt },
|
||||
iconUrl: { type: GraphQLString },
|
||||
docUrl: { type: GraphQLString },
|
||||
primaryColor: { type: GraphQLString },
|
||||
fields: { type: GraphQLList(fieldType) },
|
||||
authenticationSteps: { type: GraphQLList(authenticationStepType) },
|
||||
connections: { type: GraphQLList(connectionType) },
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
@@ -3,11 +3,16 @@ import connectionDataType from './connection-data';
|
||||
|
||||
const connectionType = new GraphQLObjectType({
|
||||
name: 'connection',
|
||||
fields: {
|
||||
id: { type: GraphQLString },
|
||||
key: { type: GraphQLString },
|
||||
data: { type: connectionDataType },
|
||||
verified: { type: GraphQLBoolean },
|
||||
fields: () => {
|
||||
const appType = require('./app').default;
|
||||
|
||||
return {
|
||||
id: { type: GraphQLString },
|
||||
key: { type: GraphQLString },
|
||||
data: { type: connectionDataType },
|
||||
verified: { type: GraphQLBoolean },
|
||||
app: { type: appType }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user