diff --git a/packages/backend/src/graphql/mutations/create-auth-data.ts b/packages/backend/src/graphql/mutations/create-auth-data.ts index 54292881..973b9dc3 100644 --- a/packages/backend/src/graphql/mutations/create-auth-data.ts +++ b/packages/backend/src/graphql/mutations/create-auth-data.ts @@ -4,20 +4,25 @@ import authLinkType from '../types/auth-link'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; type Params = { - id: number, -} + id: number; +}; -const createAuthDataResolver = async (params: Params, req: RequestWithCurrentUser) => { - const connection = await Connection.query().findOne({ - user_id: req.currentUser.id, - id: params.id - }).throwIfNotFound(); +const createAuthDataResolver = async ( + params: Params, + req: RequestWithCurrentUser +) => { + const connection = await req.currentUser + .$relatedQuery('connections') + .findOne({ + id: params.id, + }) + .throwIfNotFound(); const appClass = (await import(`../../apps/${connection.key}`)).default; const appInstance = new appClass({ consumerKey: connection.data.consumerKey, - consumerSecret: connection.data.consumerSecret + consumerSecret: connection.data.consumerSecret, }); const authLink = await appInstance.authenticationClient.createAuthData(); @@ -25,19 +30,20 @@ const createAuthDataResolver = async (params: Params, req: RequestWithCurrentUse await connection.$query().patch({ data: { ...connection.data, - ...authLink - } - }) + ...authLink, + }, + }); return authLink; -} +}; const createAuthData = { type: authLinkType, args: { id: { type: GraphQLNonNull(GraphQLInt) }, }, - resolve: (_: any, params: Params, req: RequestWithCurrentUser) => createAuthDataResolver(params, req) + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => + createAuthDataResolver(params, req), }; export default createAuthData; diff --git a/packages/backend/src/graphql/mutations/create-connection.ts b/packages/backend/src/graphql/mutations/create-connection.ts index 6b4518c0..0ac12fd1 100644 --- a/packages/backend/src/graphql/mutations/create-connection.ts +++ b/packages/backend/src/graphql/mutations/create-connection.ts @@ -1,5 +1,4 @@ import { GraphQLNonNull } from 'graphql'; -import Connection from '../../models/connection'; import App from '../../models/app'; import connectionType from '../types/connection'; import availableAppsEnumType from '../types/available-apps-enum-type'; @@ -7,31 +6,34 @@ import RequestWithCurrentUser from '../../types/express/request-with-current-use import { GraphQLJSONObject } from 'graphql-type-json'; type Params = { - key: string, - data: object -} -const createConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { + key: string; + data: object; +}; +const createConnectionResolver = async ( + params: Params, + req: RequestWithCurrentUser +) => { const app = App.findOneByKey(params.key); - const connection = await Connection.query().insert({ + const connection = await req.currentUser.$relatedQuery('connections').insert({ key: params.key, data: params.data, - userId: req.currentUser.id }); return { ...connection, app, }; -} +}; const createConnection = { type: connectionType, args: { key: { type: GraphQLNonNull(availableAppsEnumType) }, - data: { type: GraphQLNonNull(GraphQLJSONObject) } + data: { type: GraphQLNonNull(GraphQLJSONObject) }, }, - resolve: (_: any, params: Params, req: RequestWithCurrentUser) => createConnectionResolver(params, req) + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => + createConnectionResolver(params, req), }; export default createConnection; diff --git a/packages/backend/src/graphql/mutations/create-flow.ts b/packages/backend/src/graphql/mutations/create-flow.ts index f65579e5..a38d403c 100644 --- a/packages/backend/src/graphql/mutations/create-flow.ts +++ b/packages/backend/src/graphql/mutations/create-flow.ts @@ -1,12 +1,9 @@ -import Flow from '../../models/flow'; import Step from '../../models/step'; import flowType from '../types/flow'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; const createFlowResolver = async (req: RequestWithCurrentUser) => { - const flow = await Flow.query().insert({ - userId: req.currentUser.id, - }); + const flow = await req.currentUser.$relatedQuery('flows').insert(); await Step.query().insert({ flowId: flow.id, diff --git a/packages/backend/src/graphql/mutations/create-step.ts b/packages/backend/src/graphql/mutations/create-step.ts index e332623a..a7c69295 100644 --- a/packages/backend/src/graphql/mutations/create-step.ts +++ b/packages/backend/src/graphql/mutations/create-step.ts @@ -1,6 +1,4 @@ import { GraphQLNonNull } from 'graphql'; -import Step from '../../models/step'; -import Flow from '../../models/flow'; import stepType, { stepInputType } from '../types/step'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; @@ -26,33 +24,30 @@ const createStepResolver = async ( ) => { const { input } = params; - const flow = await Flow.query() + const flow = await req.currentUser + .$relatedQuery('flows') .findOne({ id: input.flow.id, - user_id: req.currentUser.id, }) .throwIfNotFound(); - const previousStep = await Step.query() + const previousStep = await flow + .$relatedQuery('steps') .findOne({ id: input.previousStep.id, - flow_id: flow.id, }) .throwIfNotFound(); - const step = await Step.query().insertAndFetch({ - flowId: flow.id, + const step = await flow.$relatedQuery('steps').insertAndFetch({ key: input.key, appKey: input.appKey, type: 'action', position: previousStep.position + 1, }); - const nextSteps = await Step.query() - .where({ - flow_id: flow.id, - }) - .andWhere('position', '>=', step.position) + const nextSteps = await flow + .$relatedQuery('steps') + .where('position', '>=', step.position) .whereNot('id', step.id); const nextStepQueries = nextSteps.map(async (nextStep, index) => { diff --git a/packages/backend/src/graphql/mutations/delete-connection.ts b/packages/backend/src/graphql/mutations/delete-connection.ts index a8fd8d51..686a1cd5 100644 --- a/packages/backend/src/graphql/mutations/delete-connection.ts +++ b/packages/backend/src/graphql/mutations/delete-connection.ts @@ -1,26 +1,32 @@ import { GraphQLInt, GraphQLNonNull, GraphQLBoolean } from 'graphql'; -import Connection from '../../models/connection'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; type Params = { - id: number, - data: object -} -const deleteConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { - await Connection.query().delete().findOne({ - user_id: req.currentUser.id, - id: params.id - }).throwIfNotFound(); + id: number; + data: object; +}; +const deleteConnectionResolver = async ( + params: Params, + req: RequestWithCurrentUser +) => { + await req.currentUser + .$relatedQuery('connections') + .delete() + .findOne({ + id: params.id, + }) + .throwIfNotFound(); return; -} +}; const deleteConnection = { type: GraphQLBoolean, args: { - id: { type: GraphQLNonNull(GraphQLInt) } + id: { type: GraphQLNonNull(GraphQLInt) }, }, - resolve: (_: any, params: Params, req: RequestWithCurrentUser) => deleteConnectionResolver(params, req) + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => + deleteConnectionResolver(params, req), }; export default deleteConnection; diff --git a/packages/backend/src/graphql/mutations/reset-connection.ts b/packages/backend/src/graphql/mutations/reset-connection.ts index fdfe68c3..ea50a920 100644 --- a/packages/backend/src/graphql/mutations/reset-connection.ts +++ b/packages/backend/src/graphql/mutations/reset-connection.ts @@ -1,31 +1,36 @@ import { GraphQLInt, GraphQLNonNull } from 'graphql'; -import Connection from '../../models/connection'; import connectionType from '../types/connection'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; type Params = { - id: number -} + id: number; +}; -const resetConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { - let connection = await Connection.query().findOne({ - user_id: req.currentUser.id, - id: params.id - }).throwIfNotFound(); +const resetConnectionResolver = async ( + params: Params, + req: RequestWithCurrentUser +) => { + let connection = await req.currentUser + .$relatedQuery('connections') + .findOne({ + id: params.id, + }) + .throwIfNotFound(); connection = await connection.$query().patchAndFetch({ - data: { screenName: connection.data.screenName } - }) + data: { screenName: connection.data.screenName }, + }); return connection; -} +}; const resetConnection = { type: connectionType, args: { id: { type: GraphQLNonNull(GraphQLInt) }, }, - resolve: (_: any, params: Params, req: RequestWithCurrentUser) => resetConnectionResolver(params, req) + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => + resetConnectionResolver(params, req), }; export default resetConnection; diff --git a/packages/backend/src/graphql/mutations/update-connection.ts b/packages/backend/src/graphql/mutations/update-connection.ts index e2114e2a..2b6da649 100644 --- a/packages/backend/src/graphql/mutations/update-connection.ts +++ b/packages/backend/src/graphql/mutations/update-connection.ts @@ -1,36 +1,41 @@ import { GraphQLInt, GraphQLNonNull } from 'graphql'; import { GraphQLJSONObject } from 'graphql-type-json'; -import Connection from '../../models/connection'; import connectionType from '../types/connection'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; type Params = { - id: number, - data: object -} -const updateConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { - let connection = await Connection.query().findOne({ - user_id: req.currentUser.id, - id: params.id - }).throwIfNotFound(); + id: number; + data: object; +}; +const updateConnectionResolver = async ( + params: Params, + req: RequestWithCurrentUser +) => { + let connection = await req.currentUser + .$relatedQuery('connections') + .findOne({ + id: params.id, + }) + .throwIfNotFound(); connection = await connection.$query().patchAndFetch({ data: { ...connection.data, - ...params.data - } - }) + ...params.data, + }, + }); return connection; -} +}; const updateConnection = { type: connectionType, args: { id: { type: GraphQLNonNull(GraphQLInt) }, - data: { type: GraphQLNonNull(GraphQLJSONObject) } + data: { type: GraphQLNonNull(GraphQLJSONObject) }, }, - resolve: (_: any, params: Params, req: RequestWithCurrentUser) => updateConnectionResolver(params, req) + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => + updateConnectionResolver(params, req), }; export default updateConnection; diff --git a/packages/backend/src/graphql/mutations/update-flow.ts b/packages/backend/src/graphql/mutations/update-flow.ts index e26682d4..79d86256 100644 --- a/packages/backend/src/graphql/mutations/update-flow.ts +++ b/packages/backend/src/graphql/mutations/update-flow.ts @@ -1,33 +1,38 @@ import { GraphQLInt, GraphQLString, GraphQLNonNull } from 'graphql'; -import Flow from '../../models/flow'; import flowType from '../types/flow'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; type Params = { - id: number, - name: string -} -const updateFlowResolver = async (params: Params, req: RequestWithCurrentUser) => { - let flow = await Flow.query().findOne({ - user_id: req.currentUser.id, - id: params.id - }).throwIfNotFound(); + id: number; + name: string; +}; +const updateFlowResolver = async ( + params: Params, + req: RequestWithCurrentUser +) => { + let flow = await req.currentUser + .$relatedQuery('flows') + .findOne({ + id: params.id, + }) + .throwIfNotFound(); flow = await flow.$query().patchAndFetch({ ...flow, - ...params - }) + ...params, + }); return flow; -} +}; const updateFlow = { type: flowType, args: { id: { type: GraphQLNonNull(GraphQLInt) }, - name: { type: GraphQLNonNull(GraphQLString) } + name: { type: GraphQLNonNull(GraphQLString) }, }, - resolve: (_: any, params: Params, req: RequestWithCurrentUser) => updateFlowResolver(params, req) + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => + updateFlowResolver(params, req), }; export default updateFlow; diff --git a/packages/backend/src/graphql/mutations/update-step.ts b/packages/backend/src/graphql/mutations/update-step.ts index fc866892..6817c10e 100644 --- a/packages/backend/src/graphql/mutations/update-step.ts +++ b/packages/backend/src/graphql/mutations/update-step.ts @@ -1,8 +1,6 @@ -import { GraphQLInt, GraphQLString, GraphQLNonNull } from 'graphql'; -import Flow from '../../models/flow'; +import { GraphQLNonNull } from 'graphql'; import Step from '../../models/step'; import stepType, { stepInputType } from '../types/step'; -import availableAppsEnumType from '../types/available-apps-enum-type'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; type Params = { @@ -17,20 +15,27 @@ type Params = { connection: { id: number; }; - } -} -const updateStepResolver = async (params: Params, req: RequestWithCurrentUser) => { + }; +}; +const updateStepResolver = async ( + params: Params, + req: RequestWithCurrentUser +) => { const { input } = params; - const flow = await Flow.query().findOne({ - user_id: req.currentUser.id, - id: input.flow.id - }).throwIfNotFound(); + const flow = await req.currentUser + .$relatedQuery('flows') + .findOne({ + id: input.flow.id, + }) + .throwIfNotFound(); - let step = await Step.query().findOne({ - flow_id: flow.id, - id: input.id - }).throwIfNotFound(); + let step = await flow + .$relatedQuery('steps') + .findOne({ + id: input.id, + }) + .throwIfNotFound(); step = await Step.query().patchAndFetchById(input.id, { key: input.key, @@ -40,14 +45,15 @@ const updateStepResolver = async (params: Params, req: RequestWithCurrentUser) = }); return step; -} +}; const updateStep = { type: stepType, args: { - input: { type: new GraphQLNonNull(stepInputType) } + input: { type: new GraphQLNonNull(stepInputType) }, }, - resolve: (_: any, params: Params, req: RequestWithCurrentUser) => updateStepResolver(params, req) + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => + updateStepResolver(params, req), }; export default updateStep; diff --git a/packages/backend/src/graphql/mutations/verify-connection.ts b/packages/backend/src/graphql/mutations/verify-connection.ts index b800a6e9..8531c200 100644 --- a/packages/backend/src/graphql/mutations/verify-connection.ts +++ b/packages/backend/src/graphql/mutations/verify-connection.ts @@ -1,39 +1,45 @@ import { GraphQLInt, GraphQLNonNull } from 'graphql'; -import Connection from '../../models/connection'; import connectionType from '../types/connection'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; type Params = { - id: number -} -const verifyConnectionResolver = async (params: Params, req: RequestWithCurrentUser) => { - let connection = await Connection.query().findOne({ - user_id: req.currentUser.id, - id: params.id - }).throwIfNotFound(); + id: number; +}; +const verifyConnectionResolver = 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 verifiedCredentials = await appInstance.authenticationClient.verifyCredentials(); + const appInstance = new appClass(connection.data); + const verifiedCredentials = + await appInstance.authenticationClient.verifyCredentials(); connection = await connection.$query().patchAndFetch({ data: { ...connection.data, - ...verifiedCredentials + ...verifiedCredentials, }, - verified: true - }) + verified: true, + }); return connection; -} +}; const verifyConnection = { type: connectionType, args: { - id: { type: GraphQLNonNull(GraphQLInt) } + id: { type: GraphQLNonNull(GraphQLInt) }, }, - resolve: (_: any, params: Params, req: RequestWithCurrentUser) => verifyConnectionResolver(params, req) + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => + verifyConnectionResolver(params, req), }; export default verifyConnection; diff --git a/packages/backend/src/models/flow.ts b/packages/backend/src/models/flow.ts index 2cf26d79..53356826 100644 --- a/packages/backend/src/models/flow.ts +++ b/packages/backend/src/models/flow.ts @@ -1,10 +1,11 @@ -import Base from './base' -import Step from './step' +import Base from './base'; +import Step from './step'; class Flow extends Base { - id!: number - userId!: number - active: boolean + id!: number; + userId!: number; + active: boolean; + steps?: [Step]; static tableName = 'flows'; @@ -14,9 +15,9 @@ class Flow extends Base { properties: { id: { type: 'integer' }, userId: { type: 'integer' }, - active: { type: 'boolean' } - } - } + active: { type: 'boolean' }, + }, + }; static relationMappings = () => ({ steps: { @@ -26,8 +27,8 @@ class Flow extends Base { from: 'flows.id', to: 'steps.flow_id', }, - } - }) + }, + }); } export default Flow;