Skip to content

Commit

Permalink
Merge pull request #43 from ismail-kharrobi/DEV-INIT_BACKEND
Browse files Browse the repository at this point in the history
Dev init backend
  • Loading branch information
automerge-pingpong[bot] committed Oct 13, 2023
2 parents a6edd3e + f8a4942 commit fc0dc34
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
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 },
});
}
}

0 comments on commit fc0dc34

Please sign in to comment.