diff --git a/packages/backend/src/graphql/queries/get-app-connections.ts b/packages/backend/src/graphql/queries/get-app-connections.ts index 3b35bfcf..a845fff3 100644 --- a/packages/backend/src/graphql/queries/get-app-connections.ts +++ b/packages/backend/src/graphql/queries/get-app-connections.ts @@ -1,31 +1,36 @@ import { GraphQLList, 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'; import availableAppsEnumType from '../types/available-apps-enum-type'; type Params = { - key: string -} + key: string; +}; -const getAppConnectionsResolver = async (params: Params, req: RequestWithCurrentUser) => { +const getAppConnectionsResolver = async ( + params: Params, + req: RequestWithCurrentUser +) => { const app = App.findOneByKey(params.key); - const connections = await Connection.query() - .where({ user_id: req.currentUser.id, key: params.key }) - return connections.map((connection: any) => ({ + const connections = await req.currentUser.$relatedQuery('connections').where({ + key: params.key, + }); + + return connections.map((connection) => ({ ...connection, app, })); -} +}; const getAppConnections = { type: GraphQLList(connectionType), args: { key: { type: GraphQLNonNull(availableAppsEnumType) }, }, - resolve: (_: any, params: Params, req: RequestWithCurrentUser) => getAppConnectionsResolver(params, req) -} + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => + getAppConnectionsResolver(params, req), +}; export default getAppConnections; diff --git a/packages/backend/src/graphql/queries/get-app.ts b/packages/backend/src/graphql/queries/get-app.ts index 317a26a3..805411e2 100644 --- a/packages/backend/src/graphql/queries/get-app.ts +++ b/packages/backend/src/graphql/queries/get-app.ts @@ -2,19 +2,21 @@ import { 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 availableAppsEnumType from '../types/available-apps-enum-type'; type Params = { - key: string -} + key: string; +}; const getAppResolver = async (params: Params, req: RequestWithCurrentUser) => { const app = App.findOneByKey(params.key); - if (req.currentUser?.id) { - const connections = await Connection.query() - .where({ user_id: req.currentUser.id, key: params.key }); + if (req.currentUser) { + const connections = await req.currentUser + .$relatedQuery('connections') + .where({ + key: params.key, + }); return { ...app, @@ -23,14 +25,15 @@ const getAppResolver = async (params: Params, req: RequestWithCurrentUser) => { } return app; -} +}; const getApp = { type: appType, args: { key: { type: GraphQLNonNull(availableAppsEnumType) }, }, - resolve: (_: any, params: Params, req: RequestWithCurrentUser) => getAppResolver(params, req) -} + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => + getAppResolver(params, req), +}; export default getApp; diff --git a/packages/backend/src/graphql/queries/get-connected-apps.ts b/packages/backend/src/graphql/queries/get-connected-apps.ts index e0036ce1..feea647a 100644 --- a/packages/backend/src/graphql/queries/get-connected-apps.ts +++ b/packages/backend/src/graphql/queries/get-connected-apps.ts @@ -1,43 +1,48 @@ import { GraphQLList, GraphQLString } from 'graphql'; -import Connection from '../../models/connection'; import App from '../../models/app'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import appType from '../types/app'; type Params = { - name: string -} + name: string; +}; -const getConnectedAppsResolver = async (params: Params, req: RequestWithCurrentUser) => { - let apps = App.findAll(params.name) +const getConnectedAppsResolver = async ( + params: Params, + req: RequestWithCurrentUser +) => { + let apps = App.findAll(params.name); - const connections = await Connection.query() + const connections = await req.currentUser + .$relatedQuery('connections') .select('connections.key') .count('connections.id as count') - .where({ user_id: req.currentUser.id, verified: true }) - .groupBy('connections.key') + .where({ verified: true }) + .groupBy('connections.key'); - const connectionKeys = connections.map(connection => connection.key) + const connectionKeys = connections.map((connection) => connection.key); apps = apps .filter((app: any) => connectionKeys.includes(app.key)) .map((app: any) => { - const connection = connections - .find((connection: any) => connection.key === app.key) + const connection = connections.find( + (connection: any) => connection.key === app.key + ); app.connectionCount = connection.count; return app; - }) + }); return apps; -} +}; const getConnectedApps = { type: GraphQLList(appType), args: { - name: { type: GraphQLString } + name: { type: GraphQLString }, }, - resolve: (_: any, params: Params, req: RequestWithCurrentUser) => getConnectedAppsResolver(params, req) -} + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => + getConnectedAppsResolver(params, req), +}; export default getConnectedApps; diff --git a/packages/backend/src/graphql/queries/get-flow.ts b/packages/backend/src/graphql/queries/get-flow.ts index ac980d76..2efe5dc9 100644 --- a/packages/backend/src/graphql/queries/get-flow.ts +++ b/packages/backend/src/graphql/queries/get-flow.ts @@ -1,5 +1,4 @@ import { GraphQLNonNull, GraphQLInt } from 'graphql'; -import Flow from '../../models/flow'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import flowType from '../types/flow'; @@ -8,10 +7,11 @@ type Params = { }; const getFlowResolver = async (params: Params, req: RequestWithCurrentUser) => { - const flow = await Flow.query() + const flow = await req.currentUser + .$relatedQuery('flows') .withGraphJoined('[steps.[connection]]') .orderBy('steps.position', 'asc') - .findOne({ 'flows.user_id': req.currentUser.id, 'flows.id': params.id }) + .findOne({ 'flows.id': params.id }) .throwIfNotFound(); return flow; diff --git a/packages/backend/src/graphql/queries/get-flows.ts b/packages/backend/src/graphql/queries/get-flows.ts index eb87515b..cd6ce182 100644 --- a/packages/backend/src/graphql/queries/get-flows.ts +++ b/packages/backend/src/graphql/queries/get-flows.ts @@ -1,19 +1,21 @@ import { GraphQLList } from 'graphql'; -import Flow from '../../models/flow'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import flowType from '../types/flow'; -const getFlowsResolver = async (req: RequestWithCurrentUser): Promise => { - const flows = await Flow.query() - .withGraphJoined('[steps.[connection]]') - .where({'flows.user_id': req.currentUser.id}); +const getFlowsResolver = async ( + req: RequestWithCurrentUser +): Promise => { + const flows = await req.currentUser + .$relatedQuery('flows') + .withGraphJoined('[steps.[connection]]'); return flows; -} +}; const getFlows = { type: GraphQLList(flowType), - resolve: (_: any, _params: any, req: RequestWithCurrentUser) => getFlowsResolver(req) -} + resolve: (_: any, _params: any, req: RequestWithCurrentUser) => + getFlowsResolver(req), +}; export default getFlows; diff --git a/packages/backend/src/graphql/queries/test-connection.ts b/packages/backend/src/graphql/queries/test-connection.ts index 085b9e0e..d035eea2 100644 --- a/packages/backend/src/graphql/queries/test-connection.ts +++ b/packages/backend/src/graphql/queries/test-connection.ts @@ -1,37 +1,43 @@ import { GraphQLString, GraphQLNonNull } from 'graphql'; -import Connection from '../../models/connection'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; -import connectionType from '../types/connection' +import connectionType from '../types/connection'; type Params = { - id: string, - data: object -} -const testConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { - let connection = await Connection.query().findOne({ - user_id: req.currentUser.id, - id: params.id - }).throwIfNotFound(); + id: string; + data: object; +}; +const testConnectionResolver = async ( + params: Params, + req: RequestWithCurrentUser +) => { + let connection = await req.currentUser + .$relatedQuery('connections') + .findOne({ + id: params.id, + }) + .throwIfNotFound(); const appClass = (await import(`../../apps/${connection.key}`)).default; const appInstance = new appClass(connection.data); - const isStillVerified = await appInstance.authenticationClient.isStillVerified(); + const isStillVerified = + await appInstance.authenticationClient.isStillVerified(); connection = await connection.$query().patchAndFetch({ data: connection.data, - verified: isStillVerified - }) + verified: isStillVerified, + }); return connection; -} +}; const testConnection = { type: connectionType, args: { - id: { type: GraphQLNonNull(GraphQLString) } + id: { type: GraphQLNonNull(GraphQLString) }, }, - resolve: (_: any, params: Params, req: RequestWithCurrentUser) => testConnectionResolver(params, req) + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => + testConnectionResolver(params, req), }; export default testConnection; diff --git a/packages/backend/src/models/user.ts b/packages/backend/src/models/user.ts index 5874994a..c78f450a 100644 --- a/packages/backend/src/models/user.ts +++ b/packages/backend/src/models/user.ts @@ -1,12 +1,15 @@ import { QueryContext, ModelOptions } from 'objection'; import Base from './base'; import Connection from './connection'; +import Flow from './flow'; import bcrypt from 'bcrypt'; class User extends Base { - id!: number - email!: string - password!: string + id!: number; + email!: string; + password!: string; + connections?: [Connection]; + flows?: [Flow]; static tableName = 'users'; @@ -18,8 +21,8 @@ class User extends Base { id: { type: 'integer' }, email: { type: 'string', format: 'email', minLength: 1, maxLength: 255 }, password: { type: 'string', minLength: 1, maxLength: 255 }, - } - } + }, + }; static relationMappings = () => ({ connections: { @@ -29,8 +32,16 @@ class User extends Base { from: 'users.id', to: 'connections.user_id', }, - } - }) + }, + flows: { + relation: Base.HasManyRelation, + modelClass: Flow, + join: { + from: 'users.id', + to: 'flows.user_id', + }, + }, + }); login(password: string) { return bcrypt.compare(password, this.password); @@ -42,14 +53,14 @@ class User extends Base { async $beforeInsert(queryContext: QueryContext) { await super.$beforeInsert(queryContext); - await this.generateHash() + await this.generateHash(); } async $beforeUpdate(opt: ModelOptions, queryContext: QueryContext) { await super.$beforeUpdate(opt, queryContext); - if(this.password) { - await this.generateHash() + if (this.password) { + await this.generateHash(); } } } diff --git a/packages/backend/src/types/express/request-with-current-user.d.ts b/packages/backend/src/types/express/request-with-current-user.d.ts index d2c7119c..cc869973 100644 --- a/packages/backend/src/types/express/request-with-current-user.d.ts +++ b/packages/backend/src/types/express/request-with-current-user.d.ts @@ -1,8 +1,8 @@ import { Request } from 'express'; -import User from '../user'; +import User from '../../models/user'; interface RequestWithCurrentUser extends Request { - currentUser: User + currentUser: User; } export default RequestWithCurrentUser; diff --git a/packages/backend/src/types/user.d.ts b/packages/backend/src/types/user.d.ts deleted file mode 100644 index 8bbc5bd8..00000000 --- a/packages/backend/src/types/user.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -type User = { - id: number, - email: string, - password: string, - createdAt: string, - updatedAt: string -} - -export default User;