Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduced admin can add new guest and delete guests #1904

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions apps/web-api/src/pl-events/pl-events.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ export class PLEventsController {
): Promise<any> {
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);
}
Expand All @@ -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())
Expand Down
59 changes: 50 additions & 9 deletions apps/web-api/src/pl-events/pl-events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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
}
});
Expand All @@ -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
}
});
Expand All @@ -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({
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions libs/contracts/src/lib/contract-pl-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>(),
responses: {
200: contract.response<unknown>(),
},
summary: 'delete a list of guests in pl event',
},
getPLEventsByLoggedInMember: {
method: 'GET',
path: `${getAPIVersionAsPath('1')}/irl/me/events`,
Expand Down
Loading