From 0098d9b3bf203e6a7cb74c3e3adc12890475576e Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Fri, 30 Oct 2020 17:04:15 +0100 Subject: [PATCH] src: bug fix --- src/datasources/db/participantAPI.ts | 34 ++++++++++++++++++++-------- src/resolvers/cargobikeResolver.ts | 11 +++++++-- src/schema/type-defs.ts | 6 +++-- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/datasources/db/participantAPI.ts b/src/datasources/db/participantAPI.ts index b982325..026e61a 100644 --- a/src/datasources/db/participantAPI.ts +++ b/src/datasources/db/participantAPI.ts @@ -40,17 +40,21 @@ export class ParticipantAPI extends DataSource { .loadOne(); } - async participantByCargoBikeId (id:number) { - return await this.connection.getRepository(Engagement) + async participantsByCargoBikeId (id:number) { + return await this.connection.getRepository(Engagement)// TODO do this with a sub query .createQueryBuilder('e') - .relation(Engagement, 'participantId') - .of((await this.connection.getRepository(Engagement)// TODO do this with a sub query - .createQueryBuilder('e') - .select() - .where('"dateRange" && daterange(CURRENT_DATE,CURRENT_DATE,\'[]\')') - .andWhere('"cargoBikeId" = :id', { id: id }) - .getOne())?.id - ).loadOne(); + .select() + .where('"dateRange" && daterange(CURRENT_DATE,CURRENT_DATE,\'[]\')') + .andWhere('"cargoBikeId" = :id', { id: id }) + .getMany() + .then(async (value: Engagement[]) => { + return value?.map(async (engagement: Engagement) => { + return await this.connection.getRepository(Engagement) + .createQueryBuilder('e') + .relation(Engagement, 'participantId') + .of(engagement.id).loadOne(); + }); + }); } async engagementByParticipantId (id: number) { @@ -74,6 +78,16 @@ export class ParticipantAPI extends DataSource { .getMany(); } + async currentEngagementByCargoBikeId (id: number) { + return await this.connection.getRepository(Engagement) + .createQueryBuilder('engagement') + .select() + .where('engagement."cargoBikeId" = :id', { id: id }) + .where('"dateRange" && daterange(CURRENT_DATE,CURRENT_DATE,\'[]\')') + .orderBy('engagement."dateRange"', 'DESC') + .getMany(); + } + async engagements (offset: number, limit: number) { return await this.connection.getRepository(Engagement) .createQueryBuilder('e') diff --git a/src/resolvers/cargobikeResolver.ts b/src/resolvers/cargobikeResolver.ts index 6ca00ae..608b5d3 100644 --- a/src/resolvers/cargobikeResolver.ts +++ b/src/resolvers/cargobikeResolver.ts @@ -76,6 +76,13 @@ export default { } }, CargoBike: { + currentEngagements (parent: any, __: any, { dataSources, req }: { dataSources: any, req: any }) { + if (req.permissions.includes(Permission.ReadEngagement)) { + return dataSources.participantAPI.currentEngagementByCargoBikeId(parent.id); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, engagement (parent: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) { if (req.permissions.includes(Permission.ReadEngagement)) { return dataSources.participantAPI.engagementByCargoBikeId(offset, limit, parent.id); @@ -83,9 +90,9 @@ export default { return new GraphQLError('Insufficient Permissions'); } }, - coordinator (parent: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) { // TODO should be done with engagements + participants (parent: any, { offset, limit }: { offset: number, limit: number }, { dataSources, req }: { dataSources: any, req: any }) { // TODO should be done with engagements if (req.permissions.includes(Permission.ReadParticipant)) { - return dataSources.participantAPI.participantByCargoBikeId(parent.id); + return dataSources.participantAPI.participantsByCargoBikeId(parent.id); } else { return new GraphQLError('Insufficient Permissions'); } diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index 97e4ffd..6677b98 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -39,10 +39,12 @@ type CargoBike { stickerBikeNameState: StickerBikeNameState note: String provider: Provider - coordinator: Participant + "all participants currently engaged with the cargoBike" + participants: [Participant] insuranceData: InsuranceData! lendingStation: LendingStation taxes: Taxes + currentEngagements: [Engagement] engagement(offset: Int!, limit: Int!): [Engagement] timeFrames: [TimeFrame] isLocked: Boolean! @@ -129,7 +131,7 @@ input CargoBikeUpdateInput { type InsuranceData { """ - Eventuelly, this field will become an enum or a separate data table and user can choose from a pool of insurance companies. + Eventually, this field will become an enum or a separate data table and user can choose from a pool of insurance companies. """ name: String! benefactor: String!