diff --git a/backend/code/src/profile/dto/update-profile.dto.ts b/backend/code/src/profile/dto/update-profile.dto.ts index fa9e9ba..425fdbb 100644 --- a/backend/code/src/profile/dto/update-profile.dto.ts +++ b/backend/code/src/profile/dto/update-profile.dto.ts @@ -1,30 +1,41 @@ import { ApiProperty } from '@nestjs/swagger'; -import { IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator'; +import { + IsBoolean, + IsByteLength, + IsEmail, + IsNotEmpty, + IsOptional, + IsString, + MinLength, +} from 'class-validator'; export class UpdateProfileDto { @ApiProperty({ required: false }) @IsOptional() @IsString() @IsNotEmpty() - @IsEmail() + @IsEmail({}, { message: 'invalid email' }) email?: string; @ApiProperty({ required: false }) @IsOptional() @IsString() @IsNotEmpty() + @IsByteLength(8, 20) password?: string; @ApiProperty({ required: false }) @IsOptional() @IsString() @IsNotEmpty() + @MinLength(4) firstName?: string; @ApiProperty({ required: false }) @IsOptional() @IsString() @IsNotEmpty() + @MinLength(4) lastName?: string; @ApiProperty({ required: false }) @@ -32,4 +43,9 @@ export class UpdateProfileDto { @IsString() @IsNotEmpty() discreption?: string; + + @ApiProperty({ required: true }) + @IsOptional() + @IsBoolean() + finishProfile: boolean; } diff --git a/backend/code/src/profile/profile.service.ts b/backend/code/src/profile/profile.service.ts index 326a944..23e35c8 100644 --- a/backend/code/src/profile/profile.service.ts +++ b/backend/code/src/profile/profile.service.ts @@ -31,7 +31,22 @@ export class ProfileService { userId: string, update_data: UpdateProfileDto, ): Promise { - const user = await this.usersService.updateUser(userId, update_data); + let finishProfile = false; + if ('finishProfile' in update_data) { + finishProfile = update_data.finishProfile; + delete update_data.finishProfile; + } + let user = await this.usersService.updateUser(userId, update_data); + if (finishProfile) { + if (user.firstName === null || user.lastName === null) + throw new HttpException( + 'First name and last name are required', + HttpStatus.BAD_REQUEST, + ); + user = await this.usersService.updateUser(userId, { + profileFinished: true, + }); + } return new ProfileDto(user); } diff --git a/backend/code/src/rooms/dto/room-data.dto.ts b/backend/code/src/rooms/dto/room-data.dto.ts new file mode 100644 index 0000000..b702c37 --- /dev/null +++ b/backend/code/src/rooms/dto/room-data.dto.ts @@ -0,0 +1,16 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { Room } from '@prisma/client'; + +export class RoomDataDto { + constructor(room_dta: Room) { + this.id = room_dta.id; + this.name = room_dta.name; + this.type = room_dta.type; + } + @ApiProperty({ example: 'cln8xxhut0000stofkkf' }) + id: string; + @ApiProperty({ example: 'test room' }) + name: string; + @ApiProperty({ example: 'public' }) + type: string; +} diff --git a/backend/code/src/rooms/rooms.controller.ts b/backend/code/src/rooms/rooms.controller.ts index c780300..424e9ab 100644 --- a/backend/code/src/rooms/rooms.controller.ts +++ b/backend/code/src/rooms/rooms.controller.ts @@ -18,7 +18,8 @@ import { ChangeOwnerDto } from './dto/change-owner.dto'; import { SetAdminDto } from './dto/set-admin.dto'; import { KickMemberDto } from './dto/kick-member.dto'; import { MuteMemberDto } from './dto/mute-member.dto'; -import { ApiCookieAuth, ApiTags } from '@nestjs/swagger'; +import { ApiCookieAuth, ApiResponse, ApiTags } from '@nestjs/swagger'; +import { RoomDataDto } from './dto/room-data.dto'; @ApiTags('rooms') @ApiCookieAuth('X-Access-Token') @@ -26,6 +27,7 @@ import { ApiCookieAuth, ApiTags } from '@nestjs/swagger'; export class RoomsController { constructor(private readonly roomsService: RoomsService) {} + @ApiResponse({ type: RoomDataDto }) @Post('create') @UseGuards(AtGuard) async createRoom( @@ -45,6 +47,10 @@ export class RoomsController { return await this.roomsService.joinRoom(memberdata, userId); } + @ApiResponse({ + status: HttpStatus.OK, + schema: { default: { message: 'left room successfully' } }, + }) @Post('leave') @HttpCode(HttpStatus.OK) @UseGuards(AtGuard) @@ -55,6 +61,12 @@ export class RoomsController { return await this.roomsService.leaveRoom(memberdata, userId); } + @ApiResponse({ + status: HttpStatus.OK, + schema: { + default: { message: 'deleted room successfully' }, + }, + }) @Post('delete') @HttpCode(HttpStatus.OK) @UseGuards(AtGuard) @@ -65,6 +77,10 @@ export class RoomsController { return await this.roomsService.deleteRoom(roomdata, userId); } + @ApiResponse({ + status: HttpStatus.OK, + type: RoomDataDto, + }) @Post('update') @HttpCode(HttpStatus.OK) @UseGuards(AtGuard) @@ -74,6 +90,7 @@ export class RoomsController { ) { return await this.roomsService.updateRoom(roomdata, userId); } + @Post('changeOwner') @HttpCode(HttpStatus.OK) @UseGuards(AtGuard) @@ -83,6 +100,7 @@ export class RoomsController { ) { return await this.roomsService.changeOwner(roomdata, userId); } + @Post('setAdmin') @HttpCode(HttpStatus.OK) @UseGuards(AtGuard) @@ -92,6 +110,7 @@ export class RoomsController { ) { return await this.roomsService.setAdmin(roomdata, userId); } + @Post('kick') @HttpCode(HttpStatus.OK) @UseGuards(AtGuard) @@ -101,6 +120,7 @@ export class RoomsController { ) { return await this.roomsService.kickMember(memberdata, userId); } + @Post('mute') @HttpCode(HttpStatus.OK) @UseGuards(AtGuard) diff --git a/backend/code/src/rooms/rooms.service.ts b/backend/code/src/rooms/rooms.service.ts index c45cef3..7e5bfb9 100644 --- a/backend/code/src/rooms/rooms.service.ts +++ b/backend/code/src/rooms/rooms.service.ts @@ -15,6 +15,7 @@ import { KickMemberDto } from './dto/kick-member.dto'; import { MuteMemberDto } from './dto/mute-member.dto'; import * as bcrypt from 'bcrypt'; import { UpdateRoomDto } from './dto/update-room.dto'; +import { RoomDataDto } from './dto/room-data.dto'; @Injectable() export class RoomsService { @@ -38,7 +39,7 @@ export class RoomsService { }, }, }); - return await this.prisma.roomMember.create({ + await this.prisma.roomMember.create({ data: { user: { connect: { userId: roomOwnerId }, @@ -49,6 +50,7 @@ export class RoomsService { is_admin: true, }, }); + return new RoomDataDto(room); } async joinRoom(roomData: JoinRoomDto, userId: string) { @@ -87,7 +89,7 @@ export class RoomsService { }); if (ownerId === userId) throw new UnauthorizedException('You are the owner of this room'); - return await this.prisma.roomMember.delete({ + await this.prisma.roomMember.delete({ where: { unique_user_room: { userId, @@ -95,6 +97,7 @@ export class RoomsService { }, }, }); + return { message: 'left room successfully' }; } async deleteRoom(roomData: DeleteRoomDto, userId: string) { @@ -133,16 +136,26 @@ export class RoomsService { if (roomData.type == 'public' || roomData.type == 'private') { roomData.password = null; } - return await this.prisma.room.update({ + const room_updated = await this.prisma.room.update({ where: { id: roomId }, data: roomData, }); + return new RoomDataDto(room_updated); } + async changeOwner(roomData: ChangeOwnerDto, userId: string) { const room = await this.prisma.room.findUnique({ where: { id: roomData.roomId }, - select: { ownerId: true }, + select: { + ownerId: true, + members: { + where: { + userId: roomData.NewOwnerId, + }, + }, + }, }); + //NOTE: test the members const member = await this.prisma.roomMember.findUnique({ where: { unique_user_room: {