From d31690ab588d6ab38838b3fcf9a888939d9d22c1 Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Tue, 10 Nov 2020 16:10:30 +0100 Subject: [PATCH] src/resolvers: code clean up --- src/datasources/db/utils.ts | 46 ++------------------ src/resolvers/cargoBikeResolver.ts | 16 +++---- src/resolvers/contactInformationResolvers.ts | 8 ++-- src/resolvers/lendingStationResolvers.ts | 8 ++-- src/resolvers/participantResolvers.ts | 8 ++-- src/resolvers/providerResolvers.ts | 8 ++-- src/resolvers/workshopResolvers.ts | 7 +-- 7 files changed, 32 insertions(+), 69 deletions(-) diff --git a/src/datasources/db/utils.ts b/src/datasources/db/utils.ts index 366ad83..1c9282b 100644 --- a/src/datasources/db/utils.ts +++ b/src/datasources/db/utils.ts @@ -1,6 +1,5 @@ import { Connection, EntityManager, ObjectType } from 'typeorm'; import { Lockable } from '../../model/CargoBike'; -import { GraphQLError } from 'graphql'; import { ActionLog, Actions } from '../../model/ActionLog'; import { UserInputError } from 'apollo-server-express'; @@ -24,11 +23,11 @@ export function genDateRange (struct: any) { * @param dataSources * @param req user request */ -export function isLocked (parent: any, { dataSources, req }: { dataSources: any; req: any }) { +export function isLocked (parent: any, { req }: { req: any }) { return req.userId !== parent.lockedBy && new Date() <= new Date(parent.lockedUntil); } -export function isLockedByMe (parent: any, { dataSources, req }: { dataSources: any; req: any }) { +export function isLockedByMe (parent: any, { req }: { req: any }) { return req.userId === parent.lockedBy && new Date() <= new Date(parent.lockedUntil); } @@ -47,10 +46,6 @@ export async function deleteEntity (connection: Connection, target: ObjectType, alias: string, id: number): Promise { return await connection.getRepository(target) .createQueryBuilder(alias) @@ -89,40 +84,6 @@ export class LockUtils { return await this.findById(connection, target, alias, id); } - static async unlockEntity_old (connection: Connection, target: ObjectType, alias: string, id: number, userId: number): Promise { - const lock = await connection.getRepository(target) - .createQueryBuilder(alias) - .select([ - alias + '.lockedUntil', - alias + '.lockedBy' - ]) - .where('id = :id', { - id: id - }) - .andWhere(alias + '.lockedUntil > CURRENT_TIMESTAMP') - .getOne(); - if (!lock?.lockedUntil) { - // no lock - return true; - // eslint-disable-next-line eqeqeq - } else if (lock?.lockedBy == userId) { - // user can unlock - await connection.getRepository(target) - .createQueryBuilder(alias) - .update() - .set({ - lockedUntil: null, - lockedBy: null - }) - .where('id = :id', { id: id }) - .execute(); - return true; - } else { - // entity is locked by other user - return false; - } - } - static async unlockEntity (connection: Connection, target: ObjectType, alias: string, id: number, userId: number): Promise { await connection.getRepository(target) .createQueryBuilder(alias) @@ -173,7 +134,8 @@ export class ActionLogger { } const ret :string[] = []; Object.keys(updates).forEach(value => { - if (typeof updates[value] === 'object' && !Array.isArray(updates[value]) && updates[value]) { + // sometimes updates[value] is an array, e.g. timePeriods that are saved as a simple array in postgres + if (updates[value] && typeof updates[value] === 'object' && !Array.isArray(updates[value])) { Object.keys(updates[value]).forEach(subValue => { ret.push(alias + '."' + value + subValue[0].toUpperCase() + subValue.substr(1).toLowerCase() + '"'); }); diff --git a/src/resolvers/cargoBikeResolver.ts b/src/resolvers/cargoBikeResolver.ts index 4e290eb..363739a 100644 --- a/src/resolvers/cargoBikeResolver.ts +++ b/src/resolvers/cargoBikeResolver.ts @@ -118,8 +118,8 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }), - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }), timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { if (req.permissions.includes(Permission.ReadTimeFrame)) { return dataSources.lendingStationAPI.timeFramesByCargoBikeId(parent.id); @@ -151,8 +151,8 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, BikeEvent: { cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { @@ -183,12 +183,12 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, BikeEventType: { - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, Mutation: { createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }: { dataSources: any, req: any }) => { diff --git a/src/resolvers/contactInformationResolvers.ts b/src/resolvers/contactInformationResolvers.ts index 0430680..46b215a 100644 --- a/src/resolvers/contactInformationResolvers.ts +++ b/src/resolvers/contactInformationResolvers.ts @@ -42,8 +42,8 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, ContactInformation: { person: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => { @@ -53,8 +53,8 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, Mutation: { createPerson: (_: any, { person }: { person: any }, { dataSources, req }: { dataSources: any, req: any }) => { diff --git a/src/resolvers/lendingStationResolvers.ts b/src/resolvers/lendingStationResolvers.ts index 8652530..adb6a41 100644 --- a/src/resolvers/lendingStationResolvers.ts +++ b/src/resolvers/lendingStationResolvers.ts @@ -77,8 +77,8 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, LoanPeriod: { loanTimes (parent: any) { @@ -107,8 +107,8 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, Mutation: { createLendingStation: (_: any, { lendingStation }:{ lendingStation: LendingStation }, { dataSources, req }:{dataSources: any, req: any }) => { diff --git a/src/resolvers/participantResolvers.ts b/src/resolvers/participantResolvers.ts index 1d3a8c0..70421b0 100644 --- a/src/resolvers/participantResolvers.ts +++ b/src/resolvers/participantResolvers.ts @@ -69,8 +69,8 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, Engagement: { cargoBike (parent: any, _: any, { dataSources, req }: { dataSources: any, req: any }) { @@ -101,8 +101,8 @@ export default { const str = (parent.dateRange as string).split(',')[1].replace(')', ''); return (str.length > 0) ? str : null; }, - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, Mutation: { createParticipant: (_: any, { participant }: { participant: any }, { dataSources, req }: { dataSources: any, req: any }) => { diff --git a/src/resolvers/providerResolvers.ts b/src/resolvers/providerResolvers.ts index 29fc9ac..0423cc8 100644 --- a/src/resolvers/providerResolvers.ts +++ b/src/resolvers/providerResolvers.ts @@ -55,8 +55,8 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, Organisation: { provider: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => { @@ -80,8 +80,8 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, Mutation: { createProvider: (_: any, { provider }: { provider: any }, { dataSources, req }: { dataSources: any, req: any }) => { diff --git a/src/resolvers/workshopResolvers.ts b/src/resolvers/workshopResolvers.ts index fe71587..6337a98 100644 --- a/src/resolvers/workshopResolvers.ts +++ b/src/resolvers/workshopResolvers.ts @@ -56,11 +56,12 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, WorkshopType: { - isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) + isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }), + isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }) }, Mutation: { createWorkshop: (_: any, { workshop }: { workshop: number }, { dataSources, req }: { dataSources: any, req: any }) => {