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

add: modify swagger for room | add finish profile checking #48

Merged
merged 1 commit into from
Oct 16, 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
20 changes: 18 additions & 2 deletions backend/code/src/profile/dto/update-profile.dto.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
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 })
@IsOptional()
@IsString()
@IsNotEmpty()
discreption?: string;

@ApiProperty({ required: true })
@IsOptional()
@IsBoolean()
finishProfile: boolean;
}
17 changes: 16 additions & 1 deletion backend/code/src/profile/profile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,22 @@ export class ProfileService {
userId: string,
update_data: UpdateProfileDto,
): Promise<ProfileDto> {
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);
}

Expand Down
16 changes: 16 additions & 0 deletions backend/code/src/rooms/dto/room-data.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 21 additions & 1 deletion backend/code/src/rooms/rooms.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ 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')
@Controller('rooms')
export class RoomsController {
constructor(private readonly roomsService: RoomsService) {}

@ApiResponse({ type: RoomDataDto })
@Post('create')
@UseGuards(AtGuard)
async createRoom(
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -74,6 +90,7 @@ export class RoomsController {
) {
return await this.roomsService.updateRoom(roomdata, userId);
}

@Post('changeOwner')
@HttpCode(HttpStatus.OK)
@UseGuards(AtGuard)
Expand All @@ -83,6 +100,7 @@ export class RoomsController {
) {
return await this.roomsService.changeOwner(roomdata, userId);
}

@Post('setAdmin')
@HttpCode(HttpStatus.OK)
@UseGuards(AtGuard)
Expand All @@ -92,6 +110,7 @@ export class RoomsController {
) {
return await this.roomsService.setAdmin(roomdata, userId);
}

@Post('kick')
@HttpCode(HttpStatus.OK)
@UseGuards(AtGuard)
Expand All @@ -101,6 +120,7 @@ export class RoomsController {
) {
return await this.roomsService.kickMember(memberdata, userId);
}

@Post('mute')
@HttpCode(HttpStatus.OK)
@UseGuards(AtGuard)
Expand Down
21 changes: 17 additions & 4 deletions backend/code/src/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -38,7 +39,7 @@ export class RoomsService {
},
},
});
return await this.prisma.roomMember.create({
await this.prisma.roomMember.create({
data: {
user: {
connect: { userId: roomOwnerId },
Expand All @@ -49,6 +50,7 @@ export class RoomsService {
is_admin: true,
},
});
return new RoomDataDto(room);
}

async joinRoom(roomData: JoinRoomDto, userId: string) {
Expand Down Expand Up @@ -87,14 +89,15 @@ 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,
roomId: memberData.roomId,
},
},
});
return { message: 'left room successfully' };
}

async deleteRoom(roomData: DeleteRoomDto, userId: string) {
Expand Down Expand Up @@ -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: {
Expand Down
Loading