From 6945420e27b8a2313e9f39821e52cb0a4f3589b9 Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Sat, 4 Jan 2025 14:26:49 +0330 Subject: [PATCH] Add functionality to update admin profile in admin module --- src/admin/admin.controller.ts | 13 ++++++++---- src/admin/admin.service.ts | 32 +++++++++++++++++++++-------- src/admin/dto/login-Admin.dto.ts | 2 +- src/admin/dto/update-user.dto.ts | 33 ++++++++++++++++++++++++++++++ src/admin/entities/admin.entity.ts | 5 ++++- 5 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 src/admin/dto/update-user.dto.ts diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index 6df3a71..e644773 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Body,Request, Put, UseGuards } from "@nestjs/common"; +import { Controller, Get, Post, Body, Request, Put, UseGuards } from "@nestjs/common"; import { AdminService } from "./admin.service"; import { Admin } from "./entities/admin.entity"; import { CreateAdminDto } from "./dto/create-Admin.dto"; @@ -10,12 +10,17 @@ import { UpdateUserDto } from "./dto/update-user.dto"; export class AdminController { constructor(private readonly adminService: AdminService) {} @Post("register") - async register(@Body()createAdminDto:CreateAdminDto): Promise { + async register(@Body() createAdminDto: CreateAdminDto): Promise { return this.adminService.register(createAdminDto); } @Post("login") - async login(@Body() loginAdminDto:LoginAdminDto ): Promise<{ token: string }> { + async login(@Body() loginAdminDto: LoginAdminDto): Promise<{ token: string }> { return this.adminService.login(loginAdminDto); } - + @UseGuards(JwtAuthGuard) + @Put() + async editAdminProfile(@Request() req, @Body() updateAdminDto: UpdateUserDto): Promise { + const userId = req.user.id; + return this.adminService.editAdminProfile(userId, updateAdminDto); + } } diff --git a/src/admin/admin.service.ts b/src/admin/admin.service.ts index 7441bfe..4990536 100644 --- a/src/admin/admin.service.ts +++ b/src/admin/admin.service.ts @@ -6,6 +6,7 @@ import { JwtService } from "@nestjs/jwt"; import { ConfigService } from "@nestjs/config"; import { CreateAdminDto } from "./dto/create-Admin.dto"; import { LoginAdminDto } from "./dto/login-Admin.dto"; +import { UpdateUserDto } from "./dto/update-user.dto"; @Injectable() export class AdminService { constructor( @@ -14,9 +15,9 @@ export class AdminService { private readonly configService: ConfigService, ) {} //register method - async register(createAdminDto:CreateAdminDto): Promise { + async register(createAdminDto: CreateAdminDto): Promise { try { - const existingAdmin = await this.adminModel.findOne({ where: { email:createAdminDto.email } }); + const existingAdmin = await this.adminModel.findOne({ where: { email: createAdminDto.email } }); if (existingAdmin) { throw new HttpException("Email is already registered.", HttpStatus.CONFLICT); } @@ -27,7 +28,7 @@ export class AdminService { return admin; } catch (error) { - console.log(error) + console.log(error); if (error instanceof HttpException) { throw error; } @@ -38,16 +39,16 @@ export class AdminService { async login(loginAdminDto: LoginAdminDto): Promise<{ token: string }> { try { const admin = await this.adminModel.findOne({ where: { email: loginAdminDto.email } }); - + if (!admin) { throw new HttpException("Invalid email or password", HttpStatus.UNAUTHORIZED); } - + const isValidPassword = await bcrypt.compare(loginAdminDto.password, admin.password); if (!isValidPassword) { throw new HttpException("Invalid email or password", HttpStatus.UNAUTHORIZED); } - + const token = this.jwtService.sign( { id: admin.id, role: admin.role }, { @@ -55,12 +56,27 @@ export class AdminService { expiresIn: this.configService.get("JWT_EXPIRES") || "1h", }, ); - + return { token }; } catch (error) { console.log(error); throw new HttpException("An error occurred during login.", HttpStatus.INTERNAL_SERVER_ERROR); } } - + //edit admin profile method + async editAdminProfile(userId: number, updateAdminDto: UpdateUserDto): Promise { + try { + const user = await this.adminModel.findOne({ where: { id: userId } }); + + if (!user) { + throw new Error("Admin not found"); + } + + await user.update(updateAdminDto); + + return user; + } catch (error) { + throw new Error(`An error occurred while updating admin: ${error.message}`); + } + } } diff --git a/src/admin/dto/login-Admin.dto.ts b/src/admin/dto/login-Admin.dto.ts index 275e57d..f444f9a 100644 --- a/src/admin/dto/login-Admin.dto.ts +++ b/src/admin/dto/login-Admin.dto.ts @@ -1,4 +1,4 @@ -import { IsString, IsEmail, IsEnum, IsNotEmpty, IsOptional, Matches } from "class-validator"; +import { IsString, IsEmail,IsNotEmpty} from "class-validator"; export class LoginAdminDto { @IsEmail({}, { message: "Invalid email format" }) diff --git a/src/admin/dto/update-user.dto.ts b/src/admin/dto/update-user.dto.ts new file mode 100644 index 0000000..3fbabfd --- /dev/null +++ b/src/admin/dto/update-user.dto.ts @@ -0,0 +1,33 @@ + +import { IsOptional, IsString, IsEmail, IsEnum } from 'class-validator'; +import {Gender } from '../entities/admin.entity'; + +export class UpdateUserDto { + @IsOptional() + @IsString() + username?: string; + + @IsOptional() + @IsEmail() + email?: string; + + @IsOptional() + @IsString() + password?: string; + + @IsOptional() + @IsString() + firstName?: string; + + @IsOptional() + @IsString() + lastName?: string; + + @IsOptional() + @IsString() + phoneNumber?: string; + + @IsOptional() + @IsEnum(Gender) + gender?: Gender; +} diff --git a/src/admin/entities/admin.entity.ts b/src/admin/entities/admin.entity.ts index ac6898d..c92a01e 100644 --- a/src/admin/entities/admin.entity.ts +++ b/src/admin/entities/admin.entity.ts @@ -29,4 +29,7 @@ export class Admin extends Model { }) gender: string; } - +export enum Gender { + Male = "male", + Female = "female", +}