From 102cebd04545d336a52bd9cf94de43c4cd2c2e80 Mon Sep 17 00:00:00 2001 From: Ayoub Essabiry Date: Sun, 15 Oct 2023 11:43:22 +0100 Subject: [PATCH 1/2] ADD : kick room member --- backend/code/src/rooms/dto/kick-member.dto.ts | 14 ++++++ backend/code/src/rooms/rooms.controller.ts | 10 +++++ backend/code/src/rooms/rooms.service.ts | 44 +++++++++++++++++-- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 backend/code/src/rooms/dto/kick-member.dto.ts diff --git a/backend/code/src/rooms/dto/kick-member.dto.ts b/backend/code/src/rooms/dto/kick-member.dto.ts new file mode 100644 index 0000000..dba99c5 --- /dev/null +++ b/backend/code/src/rooms/dto/kick-member.dto.ts @@ -0,0 +1,14 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString } from 'class-validator'; + +export class KickMemberDto { + @ApiProperty() + @IsString() + @IsNotEmpty() + roomId: string; + + @ApiProperty() + @IsString() + @IsNotEmpty() + memberId: string; +} \ No newline at end of file diff --git a/backend/code/src/rooms/rooms.controller.ts b/backend/code/src/rooms/rooms.controller.ts index eb02b41..88e21ac 100644 --- a/backend/code/src/rooms/rooms.controller.ts +++ b/backend/code/src/rooms/rooms.controller.ts @@ -16,6 +16,7 @@ import { DeleteRoomDto } from './dto/delete-room.dto'; import { UpdateRoomDto } from './dto/update-room.dto'; import { ChangeOwnerDto } from './dto/change-owner.dto'; import { SetAdminDto } from './dto/set-admin.dto'; +import { KickMemberDto } from './dto/kick-member.dto'; import { ApiCookieAuth, ApiTags } from '@nestjs/swagger'; @ApiTags('rooms') @@ -90,4 +91,13 @@ export class RoomsController { ) { return await this.roomsService.setAdmin(roomdata, userId); } + @Post('kick') + @HttpCode(HttpStatus.OK) + @UseGuards(AtGuard) + async kickMember( + @Body() memberdata: KickMemberDto, + @GetCurrentUser('userId') userId: string, + ) { + return await this.roomsService.kickMember(memberdata, userId); + } } diff --git a/backend/code/src/rooms/rooms.service.ts b/backend/code/src/rooms/rooms.service.ts index c344dac..7922376 100644 --- a/backend/code/src/rooms/rooms.service.ts +++ b/backend/code/src/rooms/rooms.service.ts @@ -11,6 +11,7 @@ import { LeaveRoomDto } from './dto/leave-room.dto'; import { DeleteRoomDto } from './dto/delete-room.dto'; import { ChangeOwnerDto } from './dto/change-owner.dto'; import { SetAdminDto } from './dto/set-admin.dto'; +import { KickMemberDto } from './dto/kick-member.dto'; import * as bcrypt from 'bcrypt'; import { UpdateRoomDto } from './dto/update-room.dto'; @@ -28,7 +29,7 @@ export class RoomsService { roomData.password = await bcrypt.hash(roomData.password, 10); } - return await this.prisma.room.create({ + const room = await this.prisma.room.create({ data: { ...roomData, owner: { @@ -36,6 +37,16 @@ export class RoomsService { }, }, }); + return await this.prisma.roomMember.create({ + data: { + user: { + connect: { userId: roomOwnerId }, + }, + room: { + connect: { id: room.id }, + }, + }, + }); } async joinRoom(roomData: JoinRoomDto, userId: string) { @@ -131,7 +142,7 @@ export class RoomsService { select: { ownerId: true }, }); const member = await this.prisma.roomMember.findUnique({ - where: { unique_user_room: { userId: userId, roomId: roomData.roomId } }, + where: { unique_user_room: { userId: roomData.NewOwnerId, roomId: roomData.roomId } }, select: { id: true }, }); if (room.ownerId !== userId) @@ -150,7 +161,7 @@ export class RoomsService { select: { ownerId: true }, }); const user = await this.prisma.roomMember.findUnique({ - where: { unique_user_room: { userId: userId, roomId: roomData.roomId } }, + where: { unique_user_room: { userId: roomData.newAdmin, roomId: roomData.roomId } }, select: { is_admin: true }, }); if (room.ownerId !== userId) @@ -171,4 +182,31 @@ export class RoomsService { data: { is_admin: true }, }); } + async kickMember(roomData: KickMemberDto, userId: string) { + const room = await this.prisma.room.findUnique({ + where: { id: roomData.roomId }, + select: { ownerId: true }, + }); + const user = await this.prisma.roomMember.findUnique({ + where: { unique_user_room: { userId: userId, roomId: roomData.roomId } }, + select: { is_admin: true }, + }); + const member = await this.prisma.roomMember.findUnique({ + where: { unique_user_room: { userId: roomData.memberId, roomId: roomData.roomId } }, + }); + if (room.ownerId !== userId || !user.is_admin) + throw new UnauthorizedException('You are not owner or admin of this room'); + if (member.userId === room.ownerId) + throw new UnauthorizedException('You can not kick the owner of this room'); + if (member.userId === userId) + throw new UnauthorizedException('You can not kick yourself'); + return await this.prisma.roomMember.delete({ + where: { + unique_user_room: { + userId: roomData.memberId, + roomId: roomData.roomId, + }, + }, + }); + } } From 2baa6b8a419dbf95af604a1ec2be34b41dc625a2 Mon Sep 17 00:00:00 2001 From: Ayoub Essabiry Date: Sun, 15 Oct 2023 13:54:12 +0100 Subject: [PATCH 2/2] Update kick room member --- backend/code/src/rooms/dto/kick-member.dto.ts | 2 +- backend/code/src/rooms/rooms.service.ts | 40 ++++++++++++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/backend/code/src/rooms/dto/kick-member.dto.ts b/backend/code/src/rooms/dto/kick-member.dto.ts index dba99c5..6e305da 100644 --- a/backend/code/src/rooms/dto/kick-member.dto.ts +++ b/backend/code/src/rooms/dto/kick-member.dto.ts @@ -11,4 +11,4 @@ export class KickMemberDto { @IsString() @IsNotEmpty() memberId: string; -} \ No newline at end of file +} diff --git a/backend/code/src/rooms/rooms.service.ts b/backend/code/src/rooms/rooms.service.ts index 7922376..162f18e 100644 --- a/backend/code/src/rooms/rooms.service.ts +++ b/backend/code/src/rooms/rooms.service.ts @@ -45,6 +45,7 @@ export class RoomsService { room: { connect: { id: room.id }, }, + is_admin: true, }, }); } @@ -142,7 +143,12 @@ export class RoomsService { select: { ownerId: true }, }); const member = await this.prisma.roomMember.findUnique({ - where: { unique_user_room: { userId: roomData.NewOwnerId, roomId: roomData.roomId } }, + where: { + unique_user_room: { + userId: roomData.NewOwnerId, + roomId: roomData.roomId, + }, + }, select: { id: true }, }); if (room.ownerId !== userId) @@ -153,6 +159,10 @@ export class RoomsService { where: { id: roomData.roomId }, data: { owner: { connect: { userId: roomData.NewOwnerId } } }, }); + await this.prisma.roomMember.update({ + where: { id: roomData.NewOwnerId }, + data: { is_admin: true }, + }); return { message: 'roomOwner changed successfully' }; } async setAdmin(roomData: SetAdminDto, userId: string) { @@ -161,9 +171,15 @@ export class RoomsService { select: { ownerId: true }, }); const user = await this.prisma.roomMember.findUnique({ - where: { unique_user_room: { userId: roomData.newAdmin, roomId: roomData.roomId } }, + where: { + unique_user_room: { + userId: roomData.newAdmin, + roomId: roomData.roomId, + }, + }, select: { is_admin: true }, }); + if (!room) throw new HttpException('room not found', HttpStatus.NOT_FOUND); if (room.ownerId !== userId) throw new UnauthorizedException('You are not the owner of this room'); if (!user) @@ -192,12 +208,24 @@ export class RoomsService { select: { is_admin: true }, }); const member = await this.prisma.roomMember.findUnique({ - where: { unique_user_room: { userId: roomData.memberId, roomId: roomData.roomId } }, + where: { + unique_user_room: { + userId: roomData.memberId, + roomId: roomData.roomId, + }, + }, }); + if (!room) throw new HttpException('room not found', HttpStatus.NOT_FOUND); + if (!member) + throw new HttpException('member not found', HttpStatus.NOT_FOUND); if (room.ownerId !== userId || !user.is_admin) - throw new UnauthorizedException('You are not owner or admin of this room'); + throw new UnauthorizedException( + 'You are not owner or admin of this room', + ); if (member.userId === room.ownerId) - throw new UnauthorizedException('You can not kick the owner of this room'); + throw new UnauthorizedException( + 'You can not kick the owner of this room', + ); if (member.userId === userId) throw new UnauthorizedException('You can not kick yourself'); return await this.prisma.roomMember.delete({ @@ -207,6 +235,6 @@ export class RoomsService { roomId: roomData.roomId, }, }, - }); + }); } }