From a44695e5fd4c830e628f8b5d40bbe7c14ffeea04 Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Sat, 4 Jan 2025 12:19:21 +0330 Subject: [PATCH] Implement functionality to view list of users by admin --- src/users/users.controller.ts | 7 +++++++ src/users/users.service.ts | 37 ++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index e546514..90ea697 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -27,10 +27,17 @@ export class UsersController { const userId = req.user.id; return this.usersService.getProfile(userId); } + //edit user profile @UseGuards(JwtAuthGuard) @Put() async editProfile(@Request() req, @Body() updateUserDto: UpdateUserDto): Promise { const userId = req.user.id; return this.usersService.editProfile(userId, updateUserDto); } + //get users list (admin) + @UseGuards(JwtAuthGuard) + @Get("users") + async findAll(): Promise { + return this.usersService.findAll(); + } } diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 41b377a..9ecded9 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -19,10 +19,7 @@ export class UsersService { // Register method async register(createUserDto: CreateUserDto): Promise { try { - createUserDto.password = await bcrypt.hash( - createUserDto.password, - parseInt(process.env.BCRYPT_SALT_ROUNDS || "10", 10) - ); + createUserDto.password = await bcrypt.hash(createUserDto.password, parseInt(process.env.BCRYPT_SALT_ROUNDS || "10", 10)); const userExists = await this.userModel.findOne({ where: { email: createUserDto.email }, @@ -34,19 +31,14 @@ export class UsersService { const user = await this.userModel.create(createUserDto); return user; } catch (error) { - throw new HttpException( - `An error occurred: ${error.message}`, - HttpStatus.INTERNAL_SERVER_ERROR - ); + throw new HttpException(`An error occurred: ${error.message}`, HttpStatus.INTERNAL_SERVER_ERROR); } } - // Login method - async login(loginUserDto:LoginUserDto): Promise<{ token: string }> { - + async login(loginUserDto: LoginUserDto): Promise<{ token: string }> { const user = await this.userModel.findOne({ - where: { email:loginUserDto.email }, + where: { email: loginUserDto.email }, }); if (!user) { throw new UnauthorizedException("Invalid email or password"); @@ -71,20 +63,21 @@ export class UsersService { async getProfile(userId: number): Promise { const user = await this.userModel.findOne({ where: { id: userId }, - attributes: { exclude: ['password'] }, + attributes: { exclude: ["password"] }, }); - + if (!user) { - throw new Error('User not found'); + throw new Error("User not found"); } return user; } - async editProfile(userId:number, updateUserDto:UpdateUserDto){ - const user = await this.userModel.findOne({ where: { id:userId } }); + //edit profile user method + async editProfile(userId: number, updateUserDto: UpdateUserDto) { + const user = await this.userModel.findOne({ where: { id: userId } }); if (!user) { - throw new NotFoundException('User not found'); + throw new NotFoundException("User not found"); } if (updateUserDto.password) { @@ -95,4 +88,12 @@ export class UsersService { return user; } + //get users list + async findAll(): Promise { + try { + return await this.userModel.findAll(); + } catch (error) { + throw new Error("An error occurred while fetching users"); + } + } }