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

Dev init backend #45

Merged
merged 2 commits into from
Oct 15, 2023
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
14 changes: 14 additions & 0 deletions backend/code/src/rooms/dto/kick-member.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions backend/code/src/rooms/rooms.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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);
}
}
72 changes: 69 additions & 3 deletions backend/code/src/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -28,14 +29,25 @@ 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: {
connect: { userId: roomOwnerId },
},
},
});
return await this.prisma.roomMember.create({
data: {
user: {
connect: { userId: roomOwnerId },
},
room: {
connect: { id: room.id },
},
is_admin: true,
},
});
}

async joinRoom(roomData: JoinRoomDto, userId: string) {
Expand Down Expand Up @@ -131,7 +143,12 @@ 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)
Expand All @@ -142,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) {
Expand All @@ -150,9 +171,15 @@ 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) 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)
Expand All @@ -171,4 +198,43 @@ 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) 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',
);
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,
},
},
});
}
}
Loading