From 3bb41ca8bcea7b725a3e2e74e903a93222a47d25 Mon Sep 17 00:00:00 2001 From: Ayoub Essabiry Date: Mon, 16 Oct 2023 18:27:11 +0100 Subject: [PATCH] ADD: search for users --- backend/code/src/profile/dto/profile.dto.ts | 2 +- .../code/src/rooms/dto/change-owner.dto.ts | 2 +- backend/code/src/rooms/dto/kick-member.dto.ts | 14 ------ backend/code/src/rooms/dto/mute-member.dto.ts | 13 ----- backend/code/src/rooms/dto/set-admin.dto.ts | 13 ----- backend/code/src/rooms/rooms.controller.ts | 19 +++++--- backend/code/src/rooms/rooms.service.ts | 30 +++++------- backend/code/src/users/dto/search-user.dto.ts | 9 ++++ backend/code/src/users/users.controller.ts | 23 +++++++++ backend/code/src/users/users.module.ts | 2 + backend/code/src/users/users.service.ts | 48 +++++++++++++++++++ 11 files changed, 110 insertions(+), 65 deletions(-) delete mode 100644 backend/code/src/rooms/dto/kick-member.dto.ts delete mode 100644 backend/code/src/rooms/dto/mute-member.dto.ts delete mode 100644 backend/code/src/rooms/dto/set-admin.dto.ts create mode 100644 backend/code/src/users/dto/search-user.dto.ts create mode 100644 backend/code/src/users/users.controller.ts diff --git a/backend/code/src/profile/dto/profile.dto.ts b/backend/code/src/profile/dto/profile.dto.ts index d26ffac..956d17f 100644 --- a/backend/code/src/profile/dto/profile.dto.ts +++ b/backend/code/src/profile/dto/profile.dto.ts @@ -26,7 +26,7 @@ type ProfileDtoProps = Partial & tfa:boolean, */ -type NAME = { +export type NAME = { first: string; last: string; }; diff --git a/backend/code/src/rooms/dto/change-owner.dto.ts b/backend/code/src/rooms/dto/change-owner.dto.ts index d95a09d..15a4562 100644 --- a/backend/code/src/rooms/dto/change-owner.dto.ts +++ b/backend/code/src/rooms/dto/change-owner.dto.ts @@ -9,5 +9,5 @@ export class ChangeOwnerDto { @ApiProperty() @IsNotEmpty() @IsString() - NewOwnerId: string; + memberId: string; } diff --git a/backend/code/src/rooms/dto/kick-member.dto.ts b/backend/code/src/rooms/dto/kick-member.dto.ts deleted file mode 100644 index 6e305da..0000000 --- a/backend/code/src/rooms/dto/kick-member.dto.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { ApiProperty } from '@nestjs/swagger'; -import { IsNotEmpty, IsString } from 'class-validator'; - -export class KickMemberDto { - @ApiProperty() - @IsString() - @IsNotEmpty() - roomId: string; - - @ApiProperty() - @IsString() - @IsNotEmpty() - memberId: string; -} diff --git a/backend/code/src/rooms/dto/mute-member.dto.ts b/backend/code/src/rooms/dto/mute-member.dto.ts deleted file mode 100644 index 4f9775e..0000000 --- a/backend/code/src/rooms/dto/mute-member.dto.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { IsNotEmpty, IsString } from 'class-validator'; -import { ApiProperty } from '@nestjs/swagger'; - -export class MuteMemberDto { - @ApiProperty() - @IsNotEmpty() - @IsString() - roomId: string; - @ApiProperty() - @IsNotEmpty() - @IsString() - memberId: string; -} diff --git a/backend/code/src/rooms/dto/set-admin.dto.ts b/backend/code/src/rooms/dto/set-admin.dto.ts deleted file mode 100644 index 6c9600c..0000000 --- a/backend/code/src/rooms/dto/set-admin.dto.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { ApiProperty } from '@nestjs/swagger'; -import { IsNotEmpty, IsString } from 'class-validator'; - -export class SetAdminDto { - @ApiProperty() - @IsNotEmpty() - @IsString() - roomId: string; - @ApiProperty() - @IsNotEmpty() - @IsString() - newAdmin: string; -} diff --git a/backend/code/src/rooms/rooms.controller.ts b/backend/code/src/rooms/rooms.controller.ts index 34096fd..77f45d8 100644 --- a/backend/code/src/rooms/rooms.controller.ts +++ b/backend/code/src/rooms/rooms.controller.ts @@ -17,9 +17,6 @@ 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 { KickMemberDto } from './dto/kick-member.dto'; -import { MuteMemberDto } from './dto/mute-member.dto'; import { RoomSearchDto } from './dto/room-search.dto'; import { ApiCookieAuth, ApiResponse, ApiTags } from '@nestjs/swagger'; import { RoomDataDto } from './dto/room-data.dto'; @@ -108,7 +105,7 @@ export class RoomsController { @HttpCode(HttpStatus.OK) @UseGuards(AtGuard) async setAdmin( - @Body() roomdata: SetAdminDto, + @Body() roomdata: ChangeOwnerDto, @GetCurrentUser('userId') userId: string, ) { return await this.roomsService.setAdmin(roomdata, userId); @@ -118,7 +115,7 @@ export class RoomsController { @HttpCode(HttpStatus.OK) @UseGuards(AtGuard) async kickMember( - @Body() memberdata: KickMemberDto, + @Body() memberdata: ChangeOwnerDto, @GetCurrentUser('userId') userId: string, ) { return await this.roomsService.kickMember(memberdata, userId); @@ -128,7 +125,7 @@ export class RoomsController { @HttpCode(HttpStatus.OK) @UseGuards(AtGuard) async muteMember( - @Body() memberdata: MuteMemberDto, + @Body() memberdata: ChangeOwnerDto, @GetCurrentUser('userId') userId: string, ) { return await this.roomsService.muteMember(memberdata, userId); @@ -140,4 +137,14 @@ export class RoomsController { async getRooms(@Query() query: RoomSearchDto) { return await this.roomsService.getRooms(query); } + + // @Get('members') + // @HttpCode(HttpStatus.OK) + // @UseGuards(AtGuard) + // async getRoomMembers( + // @Query() query: RoomSearchDto, + // @GetCurrentUser('userId') userId: string + // ){ + // return await this.roomsService.getRoomMembers(query, userId); + // } } diff --git a/backend/code/src/rooms/rooms.service.ts b/backend/code/src/rooms/rooms.service.ts index 488452f..dddd95e 100644 --- a/backend/code/src/rooms/rooms.service.ts +++ b/backend/code/src/rooms/rooms.service.ts @@ -10,9 +10,6 @@ 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 { KickMemberDto } from './dto/kick-member.dto'; -import { MuteMemberDto } from './dto/mute-member.dto'; import { RoomSearchDto } from './dto/room-search.dto'; import * as bcrypt from 'bcrypt'; import { UpdateRoomDto } from './dto/update-room.dto'; @@ -151,7 +148,7 @@ export class RoomsService { ownerId: true, members: { where: { - userId: roomData.NewOwnerId, + userId: roomData.memberId, }, }, }, @@ -160,7 +157,7 @@ export class RoomsService { const member = await this.prisma.roomMember.findUnique({ where: { unique_user_room: { - userId: roomData.NewOwnerId, + userId: roomData.memberId, roomId: roomData.roomId, }, }, @@ -172,15 +169,16 @@ export class RoomsService { 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.memberId } } }, }); await this.prisma.roomMember.update({ - where: { id: roomData.NewOwnerId }, + where: { id: roomData.memberId }, data: { is_admin: true }, }); return { message: 'roomOwner changed successfully' }; } - async setAdmin(roomData: SetAdminDto, userId: string) { + + async setAdmin(roomData: ChangeOwnerDto, userId: string) { const room = await this.prisma.room.findUnique({ where: { id: roomData.roomId }, select: { ownerId: true }, @@ -188,7 +186,7 @@ export class RoomsService { const user = await this.prisma.roomMember.findUnique({ where: { unique_user_room: { - userId: roomData.newAdmin, + userId: roomData.memberId, roomId: roomData.roomId, }, }, @@ -199,21 +197,22 @@ export class RoomsService { 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) + if (user.is_admin || room.ownerId === roomData.memberId) 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, + userId: roomData.memberId, roomId: roomData.roomId, }, }, data: { is_admin: true }, }); } - async kickMember(roomData: KickMemberDto, userId: string) { + + async kickMember(roomData: ChangeOwnerDto, userId: string) { const room = await this.prisma.room.findUnique({ where: { id: roomData.roomId }, select: { ownerId: true }, @@ -252,11 +251,8 @@ export class RoomsService { }, }); } - async muteMember(roomData: MuteMemberDto, userId: string) { - // check if user is admin or owner - // check if member is in room - // check if member is not admin or owner - // check if member is not muted + + async muteMember(roomData: ChangeOwnerDto, userId: string) { const room = await this.prisma.room.findUnique({ where: { id: roomData.roomId }, select: { ownerId: true }, diff --git a/backend/code/src/users/dto/search-user.dto.ts b/backend/code/src/users/dto/search-user.dto.ts new file mode 100644 index 0000000..11619d2 --- /dev/null +++ b/backend/code/src/users/dto/search-user.dto.ts @@ -0,0 +1,9 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString, IsNotEmpty } from 'class-validator'; + +export class usersSearchDto { + @ApiProperty() + @IsString() + @IsNotEmpty() + q: string; +} diff --git a/backend/code/src/users/users.controller.ts b/backend/code/src/users/users.controller.ts new file mode 100644 index 0000000..1feedf9 --- /dev/null +++ b/backend/code/src/users/users.controller.ts @@ -0,0 +1,23 @@ +import { + Controller, + Get, + HttpCode, + HttpStatus, + Query, + UseGuards, +} from '@nestjs/common'; +import { UsersService } from './users.service'; +import { AtGuard } from 'src/auth/guards/at.guard'; +import { usersSearchDto } from './dto/search-user.dto'; + +@Controller('users') +export class UsersController { + constructor(private readonly usersService: UsersService) {} + + @Get('search') + @HttpCode(HttpStatus.OK) + @UseGuards(AtGuard) + async getUsers(@Query() query: usersSearchDto) { + return this.usersService.getUsers(query.q); + } +} diff --git a/backend/code/src/users/users.module.ts b/backend/code/src/users/users.module.ts index 8fa904f..b401e16 100644 --- a/backend/code/src/users/users.module.ts +++ b/backend/code/src/users/users.module.ts @@ -1,8 +1,10 @@ import { Module } from '@nestjs/common'; import { UsersService } from './users.service'; +import { UsersController } from './users.controller'; @Module({ providers: [UsersService], exports: [UsersService], + controllers: [UsersController], }) export class UsersModule {} diff --git a/backend/code/src/users/users.service.ts b/backend/code/src/users/users.service.ts index a0b3da5..6221fc7 100644 --- a/backend/code/src/users/users.service.ts +++ b/backend/code/src/users/users.service.ts @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'; import { PrismaService } from 'src/prisma/prisma.service'; import { CreateUserDto } from './dto/create-user.dto'; import { UpdateUserDto } from './dto/update-user.dto'; +import { NAME } from '../profile/dto/profile.dto'; @Injectable() export class UsersService { @@ -89,4 +90,51 @@ export class UsersService { }, }); } + + async getUsers(name: string) { + const users = await this.prisma.user.findMany({ + where: { + OR: [ + { + firstName: { + contains: name, + mode: 'insensitive', + }, + }, + { + lastName: { + contains: name, + mode: 'insensitive', + }, + }, + ], + NOT: { + blocked_by: { + some: { + Blcoked_by: { + OR: [ + { + firstName: { + contains: name, + mode: 'insensitive', + }, + }, + { + lastName: { + contains: name, + mode: 'insensitive', + }, + }, + ], + }, + }, + }, + }, + }, + }); + return users.map((user) => { + const name: NAME = { first: user.firstName, last: user.lastName }; + return { name: name, id: user.userId, avatar: user.avatar }; + }); + } }