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: search for users #50

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
2 changes: 1 addition & 1 deletion backend/code/src/profile/dto/profile.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ProfileDtoProps = Partial<User> &
tfa:boolean,
*/

type NAME = {
export type NAME = {
first: string;
last: string;
};
Expand Down
2 changes: 1 addition & 1 deletion backend/code/src/rooms/dto/change-owner.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export class ChangeOwnerDto {
@ApiProperty()
@IsNotEmpty()
@IsString()
NewOwnerId: string;
memberId: string;
}
14 changes: 0 additions & 14 deletions backend/code/src/rooms/dto/kick-member.dto.ts

This file was deleted.

13 changes: 0 additions & 13 deletions backend/code/src/rooms/dto/mute-member.dto.ts

This file was deleted.

13 changes: 0 additions & 13 deletions backend/code/src/rooms/dto/set-admin.dto.ts

This file was deleted.

19 changes: 13 additions & 6 deletions backend/code/src/rooms/rooms.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
// }
}
30 changes: 13 additions & 17 deletions backend/code/src/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -151,7 +148,7 @@ export class RoomsService {
ownerId: true,
members: {
where: {
userId: roomData.NewOwnerId,
userId: roomData.memberId,
},
},
},
Expand All @@ -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,
},
},
Expand All @@ -172,23 +169,24 @@ 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 },
});
const user = await this.prisma.roomMember.findUnique({
where: {
unique_user_room: {
userId: roomData.newAdmin,
userId: roomData.memberId,
roomId: roomData.roomId,
},
},
Expand All @@ -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 },
Expand Down Expand Up @@ -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 },
Expand Down
9 changes: 9 additions & 0 deletions backend/code/src/users/dto/search-user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsNotEmpty } from 'class-validator';

export class usersSearchDto {
@ApiProperty()
@IsString()
@IsNotEmpty()
q: string;
}
23 changes: 23 additions & 0 deletions backend/code/src/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions backend/code/src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
48 changes: 48 additions & 0 deletions backend/code/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 };
});
}
}
Loading