src/resolvers: code clean up

pull/14/head
leonnicolas 4 years ago
parent 57e369528e
commit d31690ab58
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -1,6 +1,5 @@
import { Connection, EntityManager, ObjectType } from 'typeorm'; import { Connection, EntityManager, ObjectType } from 'typeorm';
import { Lockable } from '../../model/CargoBike'; import { Lockable } from '../../model/CargoBike';
import { GraphQLError } from 'graphql';
import { ActionLog, Actions } from '../../model/ActionLog'; import { ActionLog, Actions } from '../../model/ActionLog';
import { UserInputError } from 'apollo-server-express'; import { UserInputError } from 'apollo-server-express';
@ -24,11 +23,11 @@ export function genDateRange (struct: any) {
* @param dataSources * @param dataSources
* @param req user request * @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); 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); return req.userId === parent.lockedBy && new Date() <= new Date(parent.lockedUntil);
} }
@ -47,10 +46,6 @@ export async function deleteEntity (connection: Connection, target: ObjectType<L
} }
export class LockUtils { export class LockUtils {
static getToken (req: any) : string {
return req.headers.authorization?.replace('Bearer ', '');
}
static async findById (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number): Promise<Lockable> { static async findById (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number): Promise<Lockable> {
return await connection.getRepository(target) return await connection.getRepository(target)
.createQueryBuilder(alias) .createQueryBuilder(alias)
@ -89,40 +84,6 @@ export class LockUtils {
return await this.findById(connection, target, alias, id); return await this.findById(connection, target, alias, id);
} }
static async unlockEntity_old (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number, userId: number): Promise<boolean> {
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<Lockable>, alias: string, id: number, userId: number): Promise<Lockable> { static async unlockEntity (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number, userId: number): Promise<Lockable> {
await connection.getRepository(target) await connection.getRepository(target)
.createQueryBuilder(alias) .createQueryBuilder(alias)
@ -173,7 +134,8 @@ export class ActionLogger {
} }
const ret :string[] = []; const ret :string[] = [];
Object.keys(updates).forEach(value => { 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 => { Object.keys(updates[value]).forEach(subValue => {
ret.push(alias + '."' + value + subValue[0].toUpperCase() + subValue.substr(1).toLowerCase() + '"'); ret.push(alias + '."' + value + subValue[0].toUpperCase() + subValue.substr(1).toLowerCase() + '"');
}); });

@ -118,8 +118,8 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
isLocked: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }), isLockedByMe: (parent: any, __: any, { req }: { req: any }) => isLockedByMe(parent, { req }),
isLockedByMe: (parent: any, __: any, { dataSources, req }: { dataSources: any; req: any }) => isLockedByMe(parent, { dataSources, req }), isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req }),
timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
if (req.permissions.includes(Permission.ReadTimeFrame)) { if (req.permissions.includes(Permission.ReadTimeFrame)) {
return dataSources.lendingStationAPI.timeFramesByCargoBikeId(parent.id); return dataSources.lendingStationAPI.timeFramesByCargoBikeId(parent.id);
@ -151,8 +151,8 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
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, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
}, },
BikeEvent: { BikeEvent: {
cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { cargoBike (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) {
@ -183,12 +183,12 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
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, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
}, },
BikeEventType: { BikeEventType: {
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, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
}, },
Mutation: { Mutation: {
createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }: { dataSources: any, req: any }) => { createCargoBike: (_: any, { cargoBike }: { cargoBike: any }, { dataSources, req }: { dataSources: any, req: any }) => {

@ -42,8 +42,8 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
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, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
}, },
ContactInformation: { ContactInformation: {
person: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => { person: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => {
@ -53,8 +53,8 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
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, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
}, },
Mutation: { Mutation: {
createPerson: (_: any, { person }: { person: any }, { dataSources, req }: { dataSources: any, req: any }) => { createPerson: (_: any, { person }: { person: any }, { dataSources, req }: { dataSources: any, req: any }) => {

@ -77,8 +77,8 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
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, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
}, },
LoanPeriod: { LoanPeriod: {
loanTimes (parent: any) { loanTimes (parent: any) {
@ -107,8 +107,8 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
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, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
}, },
Mutation: { Mutation: {
createLendingStation: (_: any, { lendingStation }:{ lendingStation: LendingStation }, { dataSources, req }:{dataSources: any, req: any }) => { createLendingStation: (_: any, { lendingStation }:{ lendingStation: LendingStation }, { dataSources, req }:{dataSources: any, req: any }) => {

@ -69,8 +69,8 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
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, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
}, },
Engagement: { Engagement: {
cargoBike (parent: any, _: any, { dataSources, req }: { dataSources: any, req: any }) { 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(')', ''); const str = (parent.dateRange as string).split(',')[1].replace(')', '');
return (str.length > 0) ? str : null; return (str.length > 0) ? str : null;
}, },
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, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
}, },
Mutation: { Mutation: {
createParticipant: (_: any, { participant }: { participant: any }, { dataSources, req }: { dataSources: any, req: any }) => { createParticipant: (_: any, { participant }: { participant: any }, { dataSources, req }: { dataSources: any, req: any }) => {

@ -55,8 +55,8 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
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, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
}, },
Organisation: { Organisation: {
provider: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => { provider: (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) => {
@ -80,8 +80,8 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
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, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
}, },
Mutation: { Mutation: {
createProvider: (_: any, { provider }: { provider: any }, { dataSources, req }: { dataSources: any, req: any }) => { createProvider: (_: any, { provider }: { provider: any }, { dataSources, req }: { dataSources: any, req: any }) => {

@ -56,11 +56,12 @@ export default {
return new GraphQLError('Insufficient Permissions'); return new GraphQLError('Insufficient Permissions');
} }
}, },
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, { dataSources, req }: { dataSources: any; req: any }) => isLocked(parent, { dataSources, req }) isLocked: (parent: any, __: any, { req }: { req: any }) => isLocked(parent, { req })
}, },
WorkshopType: { 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: { Mutation: {
createWorkshop: (_: any, { workshop }: { workshop: number }, { dataSources, req }: { dataSources: any, req: any }) => { createWorkshop: (_: any, { workshop }: { workshop: number }, { dataSources, req }: { dataSources: any, req: any }) => {

Loading…
Cancel
Save