From 412fe8cc74dd5255b82de7a03ca5b76f5ebe64e4 Mon Sep 17 00:00:00 2001 From: Navaneethakrishnan Date: Tue, 23 Jul 2024 17:31:17 +0530 Subject: [PATCH] feat: introduced admin can add new guest and delete guests --- .../src/pl-events/pl-events.controller.ts | 26 ++++++-- .../src/pl-events/pl-events.service.ts | 59 ++++++++++++++++--- libs/contracts/src/lib/contract-pl-events.ts | 9 +++ 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/apps/web-api/src/pl-events/pl-events.controller.ts b/apps/web-api/src/pl-events/pl-events.controller.ts index 7fb9ce7a6..57cd7785c 100644 --- a/apps/web-api/src/pl-events/pl-events.controller.ts +++ b/apps/web-api/src/pl-events/pl-events.controller.ts @@ -66,9 +66,10 @@ export class PLEventsController { ): Promise { const userEmail = request["userEmail"]; const member: any = await this.memberService.findMemberByEmail(request["userEmail"]); - const result = await this.memberService.isMemberPartOfTeams(member, [body.teamUid]); + const result = await this.memberService.isMemberPartOfTeams(member, [body.teamUid]) || + await this.memberService.checkIfAdminUser(member); if (!result) { - throw new ForbiddenException(`Member with email ${userEmail} is not part of team with uid ${body.teamUid}`); + throw new ForbiddenException(`Member with email ${userEmail} is not part of team with uid ${body.teamUid} or isn't admin`); } return await this.eventService.createPLEventGuest(body as any, slug, member); } @@ -84,13 +85,30 @@ export class PLEventsController { ) { const userEmail = request["userEmail"]; const member: any = await this.memberService.findMemberByEmail(request["userEmail"]); - const result = await this.memberService.isMemberPartOfTeams(member, [body.teamUid]); + const result = await this.memberService.isMemberPartOfTeams(member, [body.teamUid]) || + await this.memberService.checkIfAdminUser(member); if (!result) { - throw new ForbiddenException(`Member with email ${userEmail} is not part of team with uid ${body.teamUid}`); + throw new ForbiddenException(`Member with email ${userEmail} is not part of team with uid ${body.teamUid} or isn't admin`); } return await this.eventService.modifyPLEventGuestByUid(uid, body as any, slug, member); } + @Api(server.route.deletePLEventGuests) + @UsePipes(ZodValidationPipe) + @UseGuards(UserTokenValidation) + async deletePLEventGuests( + @Body() body, + @Req() request + ) { + const userEmail = request["userEmail"]; + const member: any = await this.memberService.findMemberByEmail(request["userEmail"]); + const result = await this.memberService.checkIfAdminUser(member); + if (!result) { + throw new ForbiddenException(`Member with email ${userEmail} is not admin `); + } + return await this.eventService.deletePLEventGuests(body.guests); + } + @Api(server.route.getPLEventsByLoggedInMember) @ApiQueryFromZod(PLEventQueryParams) @ApiOkResponseFromZod(ResponsePLEventSchemaWithRelationsSchema.array()) diff --git a/apps/web-api/src/pl-events/pl-events.service.ts b/apps/web-api/src/pl-events/pl-events.service.ts index 3bb30389d..af4a62b0c 100644 --- a/apps/web-api/src/pl-events/pl-events.service.ts +++ b/apps/web-api/src/pl-events/pl-events.service.ts @@ -53,7 +53,17 @@ export class PLEventsService { name: true, image: true, telegramHandler: true, - teamMemberRoles: true, + teamMemberRoles: { + select:{ + team: { + select:{ + uid: true, + name: true, + logo: true + } + } + } + }, preferences: true, officeHours: true, projectContributions: { @@ -150,13 +160,13 @@ export class PLEventsService { member ) { try { - const event: any = await this.getPLEventBySlug(slug, true); - await this.memberService.updateTelegramIfChanged(member, guest.telegramId); - await this.memberService.updateOfficeHoursIfChanged(member, guest.officeHours); + const event: any = await this.getPLEventBySlug(slug, true); + const isAdmin = this.memberService.checkIfAdminUser(member); + await this.updateMemberDetails(guest, member, isAdmin); await this.prisma.pLEventGuest.create({ data:{ ...guest, - memberUid: member?.uid, + memberUid: isAdmin ? guest.memberUid : member.uid, eventUid: event?.uid } }); @@ -176,14 +186,14 @@ export class PLEventsService { member ) { try { - const event: any = await this.getPLEventBySlug(slug, true); - await this.memberService.updateTelegramIfChanged(member, guest.telegramId); - await this.memberService.updateOfficeHoursIfChanged(member, guest.officeHours); + const event: any = await this.getPLEventBySlug(slug, true); + const isAdmin = this.memberService.checkIfAdminUser(member); + await this.updateMemberDetails(guest, member, isAdmin); return await this.prisma.pLEventGuest.update({ where:{ uid }, data:{ ...guest, - memberUid: member?.uid, + memberUid: this.memberService.checkIfAdminUser(member) ? guest.memberUid : member.uid, eventUid: event?.uid } }); @@ -192,6 +202,26 @@ export class PLEventsService { } }; + async deletePLEventGuests( + guestUids, + ) { + try { + await this.prisma.pLEventGuest.deleteMany({ + where: { + uid: { + in: guestUids ? guestUids : [] + } + } + }); + await this.cacheService.reset(); + return { + msg: `success` + }; + } catch(err) { + this.handleErrors(err); + } + } + async getPLEventsByMember(member) { try { return this.prisma.pLEvent.findMany({ @@ -208,6 +238,17 @@ export class PLEventsService { } } + async updateMemberDetails(guest, member, isAdmin) { + if (isAdmin) { + const guestMember = await this.memberService.findOne(guest.memberUid); + await this.memberService.updateTelegramIfChanged(guestMember, guest.telegramId); + await this.memberService.updateOfficeHoursIfChanged(guestMember, guest.officeHours); + } else { + await this.memberService.updateTelegramIfChanged(member, guest.telegramId); + await this.memberService.updateOfficeHoursIfChanged(member, guest.officeHours); + } + } + private handleErrors(error, message?) { this.logger.error(error); if (error instanceof Prisma.PrismaClientKnownRequestError) { diff --git a/libs/contracts/src/lib/contract-pl-events.ts b/libs/contracts/src/lib/contract-pl-events.ts index 2911f5757..b34b23a1f 100644 --- a/libs/contracts/src/lib/contract-pl-events.ts +++ b/libs/contracts/src/lib/contract-pl-events.ts @@ -45,6 +45,15 @@ export const apiEvents = contract.router({ }, summary: 'Modify a guest in pl event', }, + deletePLEventGuests: { + method: 'POST', + path: `${getAPIVersionAsPath('1')}/irl/events/:slug/guests`, + body: contract.body(), + responses: { + 200: contract.response(), + }, + summary: 'delete a list of guests in pl event', + }, getPLEventsByLoggedInMember: { method: 'GET', path: `${getAPIVersionAsPath('1')}/irl/me/events`,