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 #43

Merged
merged 4 commits into from
Oct 13, 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
27 changes: 12 additions & 15 deletions backend/code/src/rooms/dto/change-owner.dto.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
// import { ApiProperty } from '@nestjs/swagger';
import {
IsNotEmpty,
IsString,
} from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';

export class ChangeOwnerDto{
// @ApiProperty()
@IsNotEmpty()
@IsString()
roomId: string;
@IsNotEmpty()
@IsString()
NewOwnerId: string;

}
export class ChangeOwnerDto {
@ApiProperty()
@IsNotEmpty()
@IsString()
roomId: string;
@ApiProperty()
@IsNotEmpty()
@IsString()
NewOwnerId: string;
}
13 changes: 13 additions & 0 deletions backend/code/src/rooms/dto/set-admin.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';

export class SetAdminDto {
@ApiProperty()
@IsNotEmpty()
@IsString()
roomId: string;
@ApiProperty()
@IsNotEmpty()
@IsString()
newAdmin: string;
}
12 changes: 11 additions & 1 deletion backend/code/src/rooms/rooms.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { LeaveRoomDto } from './dto/leave-room.dto';
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 { ApiCookieAuth, ApiTags } from '@nestjs/swagger';

@ApiTags('rooms')
Expand Down Expand Up @@ -77,7 +78,16 @@ export class RoomsController {
async changeOwner(
@Body() roomdata: ChangeOwnerDto,
@GetCurrentUser('userId') userId: string,
){
) {
return await this.roomsService.changeOwner(roomdata, userId);
}
@Post('setAdmin')
@HttpCode(HttpStatus.OK)
@UseGuards(AtGuard)
async setAdmin(
@Body() roomdata: SetAdminDto,
@GetCurrentUser('userId') userId: string,
) {
return await this.roomsService.setAdmin(roomdata, userId);
}
}
47 changes: 40 additions & 7 deletions backend/code/src/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { JoinRoomDto } from './dto/join-room.dto';
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 * as bcrypt from 'bcrypt';
import { UpdateRoomDto } from './dto/update-room.dto';

Expand Down Expand Up @@ -125,17 +126,49 @@ export class RoomsService {
});
}
async changeOwner(roomData: ChangeOwnerDto, userId: string) {
const { ownerId } = await this.prisma.room.findUnique({
const room = await this.prisma.room.findUnique({
where: { id: roomData.roomId },
select: { ownerId: true },
});
if (ownerId !== userId)
const member = await this.prisma.roomMember.findUnique({
where: { unique_user_room: { userId: userId, roomId: roomData.roomId } },
select: { id: true },
});
if (room.ownerId !== userId)
throw new UnauthorizedException('You are not the owner of this room');
const updateRoomOwner = await this.prisma.room.update({
if (!member)
throw new UnauthorizedException('new owner is not a member of this room');
await this.prisma.room.update({
where: { id: roomData.roomId },
data: { owner: { connect: { userId: roomData.NewOwnerId } } }
data: { owner: { connect: { userId: roomData.NewOwnerId } } },
});
return { message: 'change roomOwner successfully' };
}

return { message: 'roomOwner changed successfully' };
}
async setAdmin(roomData: SetAdminDto, 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 },
});
if (room.ownerId !== userId)
throw new UnauthorizedException('You are not the owner of this room');
if (!user)
throw new UnauthorizedException('new admin is not a member of this room');
if (user.is_admin || room.ownerId === roomData.newAdmin)
throw new UnauthorizedException(
'new admin is already an admin of this room',
);
return await this.prisma.roomMember.update({
where: {
unique_user_room: {
userId: roomData.newAdmin,
roomId: roomData.roomId,
},
},
data: { is_admin: true },
});
}
}
Loading