chore: Introduce has many steps through flows for user model

This commit is contained in:
Faruk AYDIN
2022-01-29 19:48:59 +03:00
committed by Ömer Faruk Aydın
parent 700d0bfd32
commit 3429784309
5 changed files with 32 additions and 24 deletions

View File

@@ -10,8 +10,8 @@ const deleteStepResolver = async (
params: Params, params: Params,
req: RequestWithCurrentUser req: RequestWithCurrentUser
) => { ) => {
// TODO: This logic should be revised by using current user await req.currentUser
await Step.query() .$relatedQuery('steps')
.delete() .delete()
.findOne({ .findOne({
id: params.id, id: params.id,

View File

@@ -1,27 +1,25 @@
import { GraphQLString, GraphQLNonNull } from 'graphql'; import { GraphQLString, GraphQLNonNull } from 'graphql';
import Connection from '../../models/connection';
import Step from '../../models/step';
import stepType from '../types/step'; import stepType from '../types/step';
import RequestWithCurrentUser from '../../types/express/request-with-current-user';
type Params = { type Params = {
id: string; id: string;
data: Record<string, unknown>; data: Record<string, unknown>;
}; };
const executeStepResolver = async (params: Params): Promise<any> => { const executeStepResolver = async (
const step = await Step.query() params: Params,
req: RequestWithCurrentUser
): Promise<any> => {
const step = await req.currentUser
.$relatedQuery('steps')
.withGraphFetched('connection')
.findOne({ .findOne({
id: params.id, 'steps.id': params.id,
})
.throwIfNotFound();
const connection = await Connection.query()
.findOne({
id: step.connectionId,
}) })
.throwIfNotFound(); .throwIfNotFound();
const appClass = (await import(`../../apps/${step.appKey}`)).default; const appClass = (await import(`../../apps/${step.appKey}`)).default;
const appInstance = new appClass(connection.data); const appInstance = new appClass(step.connection.data);
await appInstance.triggers[step.key].run(); await appInstance.triggers[step.key].run();
return step; return step;
@@ -32,7 +30,8 @@ const executeStep = {
args: { args: {
id: { type: GraphQLNonNull(GraphQLString) }, id: { type: GraphQLNonNull(GraphQLString) },
}, },
resolve: (_: any, params: Params) => executeStepResolver(params), resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
executeStepResolver(params, req),
}; };
export default executeStep; export default executeStep;

View File

@@ -23,17 +23,11 @@ const updateStepResolver = async (
) => { ) => {
const { input } = params; const { input } = params;
const flow = await req.currentUser let step = await req.currentUser
.$relatedQuery('flows')
.findOne({
id: input.flow.id,
})
.throwIfNotFound();
let step = await flow
.$relatedQuery('steps') .$relatedQuery('steps')
.findOne({ .findOne({
id: input.id, 'steps.id': input.id,
flow_id: input.flow.id,
}) })
.throwIfNotFound(); .throwIfNotFound();

View File

@@ -16,6 +16,7 @@ class Step extends Base {
connectionId: number; connectionId: number;
position: number; position: number;
parameters: string; parameters: string;
connection?: Connection;
static tableName = 'steps'; static tableName = 'steps';

View File

@@ -2,6 +2,7 @@ import { QueryContext, ModelOptions } from 'objection';
import Base from './base'; import Base from './base';
import Connection from './connection'; import Connection from './connection';
import Flow from './flow'; import Flow from './flow';
import Step from './step';
import bcrypt from 'bcrypt'; import bcrypt from 'bcrypt';
class User extends Base { class User extends Base {
@@ -10,6 +11,7 @@ class User extends Base {
password!: string; password!: string;
connections?: [Connection]; connections?: [Connection];
flows?: [Flow]; flows?: [Flow];
steps?: [Step];
static tableName = 'users'; static tableName = 'users';
@@ -41,6 +43,18 @@ class User extends Base {
to: 'flows.user_id', to: 'flows.user_id',
}, },
}, },
steps: {
relation: Base.ManyToManyRelation,
modelClass: Step,
join: {
from: 'users.id',
through: {
from: 'flows.user_id',
to: 'flows.id',
},
to: 'steps.flow_id',
},
},
}); });
login(password: string) { login(password: string) {