From 5a52f592cb787b98977a677d8d84beaa7203ba0e Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Mon, 9 Nov 2020 18:18:45 +0100 Subject: [PATCH] isLockedByMe --- src/datasources/db/utils.ts | 8 +++++--- src/resolvers/cargoBikeResolver.ts | 6 +++++- src/resolvers/contactInformationResolvers.ts | 4 +++- src/resolvers/lendingStationResolvers.ts | 4 +++- src/resolvers/participantResolvers.ts | 4 +++- src/resolvers/providerResolvers.ts | 4 +++- src/resolvers/workshopResolvers.ts | 3 ++- src/schema/type-defs.ts | 16 ++++++++++++++++ 8 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/datasources/db/utils.ts b/src/datasources/db/utils.ts index 6c720a3..88a4f55 100644 --- a/src/datasources/db/utils.ts +++ b/src/datasources/db/utils.ts @@ -25,9 +25,11 @@ export function genDateRange (struct: any) { * @param req user request */ export function isLocked (parent: any, { dataSources, req }: { dataSources: any; req: any }) { - return dataSources.userAPI.getUserId(LockUtils.getToken(req)).then((value: number) => { - return value !== 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 }) { + return req.userId === parent.lockedBy && new Date() <= new Date(parent.lockedUntil); } export async function deleteEntity (connection: Connection, target: ObjectType, alias: string, id: number, userId: number): Promise { diff --git a/src/resolvers/cargoBikeResolver.ts b/src/resolvers/cargoBikeResolver.ts index 608b5d3..4e290eb 100644 --- a/src/resolvers/cargoBikeResolver.ts +++ b/src/resolvers/cargoBikeResolver.ts @@ -1,6 +1,6 @@ import { Permission } from '../datasources/userserver/permission'; import { GraphQLError } from 'graphql'; -import { isLocked } from '../datasources/db/utils'; +import { isLocked, isLockedByMe } from '../datasources/db/utils'; export default { Query: { @@ -119,6 +119,7 @@ export default { } }, 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 }), timeFrames (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { if (req.permissions.includes(Permission.ReadTimeFrame)) { return dataSources.lendingStationAPI.timeFramesByCargoBikeId(parent.id); @@ -150,6 +151,7 @@ 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 }) }, BikeEvent: { @@ -181,9 +183,11 @@ 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 }) }, 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 }) }, Mutation: { diff --git a/src/resolvers/contactInformationResolvers.ts b/src/resolvers/contactInformationResolvers.ts index 1e4fce5..0430680 100644 --- a/src/resolvers/contactInformationResolvers.ts +++ b/src/resolvers/contactInformationResolvers.ts @@ -1,7 +1,7 @@ import { GraphQLError } from 'graphql'; import { Permission } from '../datasources/userserver/permission'; import { Person } from '../model/Person'; -import { isLocked } from '../datasources/db/utils'; +import { isLocked, isLockedByMe } from '../datasources/db/utils'; export default { Query: { @@ -42,6 +42,7 @@ 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 }) }, ContactInformation: { @@ -52,6 +53,7 @@ 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 }) }, Mutation: { diff --git a/src/resolvers/lendingStationResolvers.ts b/src/resolvers/lendingStationResolvers.ts index 118914b..8652530 100644 --- a/src/resolvers/lendingStationResolvers.ts +++ b/src/resolvers/lendingStationResolvers.ts @@ -1,7 +1,7 @@ import { Permission } from '../datasources/userserver/permission'; import { GraphQLError } from 'graphql'; import { LendingStation } from '../model/LendingStation'; -import { isLocked } from '../datasources/db/utils'; +import { isLocked, isLockedByMe } from '../datasources/db/utils'; export default { Query: { @@ -77,6 +77,7 @@ 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 }) }, LoanPeriod: { @@ -106,6 +107,7 @@ 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 }) }, Mutation: { diff --git a/src/resolvers/participantResolvers.ts b/src/resolvers/participantResolvers.ts index 9968bb8..1d3a8c0 100644 --- a/src/resolvers/participantResolvers.ts +++ b/src/resolvers/participantResolvers.ts @@ -1,6 +1,6 @@ import { GraphQLError } from 'graphql'; import { Permission } from '../datasources/userserver/permission'; -import { isLocked } from '../datasources/db/utils'; +import { isLocked, isLockedByMe } from '../datasources/db/utils'; export default { Query: { @@ -69,6 +69,7 @@ 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 }) }, Engagement: { @@ -100,6 +101,7 @@ 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 }) }, Mutation: { diff --git a/src/resolvers/providerResolvers.ts b/src/resolvers/providerResolvers.ts index e1b7f0b..29fc9ac 100644 --- a/src/resolvers/providerResolvers.ts +++ b/src/resolvers/providerResolvers.ts @@ -1,6 +1,6 @@ import { GraphQLError } from 'graphql'; import { Permission } from '../datasources/userserver/permission'; -import { isLocked } from '../datasources/db/utils'; +import { isLocked, isLockedByMe } from '../datasources/db/utils'; export default { Query: { @@ -55,6 +55,7 @@ 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 }) }, Organisation: { @@ -79,6 +80,7 @@ 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 }) }, Mutation: { diff --git a/src/resolvers/workshopResolvers.ts b/src/resolvers/workshopResolvers.ts index 58e2ef3..fe71587 100644 --- a/src/resolvers/workshopResolvers.ts +++ b/src/resolvers/workshopResolvers.ts @@ -1,6 +1,6 @@ import { Permission } from '../datasources/userserver/permission'; import { GraphQLError } from 'graphql'; -import { isLocked } from '../datasources/db/utils'; +import { isLocked, isLockedByMe } from '../datasources/db/utils'; import { Participant } from '../model/Participant'; export default { @@ -56,6 +56,7 @@ 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 }) }, WorkshopType: { diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index 933c0eb..c03ebfa 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -48,6 +48,7 @@ type CargoBike { engagement(offset: Int!, limit: Int!): [Engagement] timeFrames: [TimeFrame] isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -327,6 +328,7 @@ type Participant { engagement: [Engagement] workshops: [Workshop] isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -376,6 +378,7 @@ type Workshop { trainer2: Participant participants: [Participant] isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -405,6 +408,7 @@ type WorkshopType { id: ID! name: String! isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -424,6 +428,7 @@ type EngagementType { name: String! description: String! isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -449,6 +454,7 @@ type Engagement { participant: Participant! cargoBike: CargoBike! isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -504,6 +510,7 @@ type Equipment { description: String cargoBike: CargoBike isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -531,6 +538,7 @@ type EquipmentType { name: String! description: String! isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -563,6 +571,7 @@ type BikeEvent { documents: [String]! remark: String isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -601,6 +610,7 @@ input BikeEventUpdateInput { type BikeEventType { id: ID! name: String! + isLockedByMe: Boolean! isLocked: Boolean! lockedUntil: Date } @@ -619,6 +629,7 @@ type Provider { organisation: Organisation cargoBikes: [CargoBike] isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -652,6 +663,7 @@ type Person { firstName: String! contactInformation: [ContactInformation] isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -678,6 +690,7 @@ type ContactInformation { email2: String note: String isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -717,6 +730,7 @@ type Organisation { contactInformation: ContactInformation otherData: String isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -760,6 +774,7 @@ type LendingStation { numCargoBikes: Int! organisation: Organisation isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date @@ -830,6 +845,7 @@ type TimeFrame { lendingStation: LendingStation! cargoBike: CargoBike! isLocked: Boolean! + isLockedByMe: Boolean! "null if not locked by other user" lockedBy: ID lockedUntil: Date