Add functionality to update admin profile in admin module

master
nicekid1 2 months ago
parent 4506469b9b
commit 6945420e27
  1. 13
      src/admin/admin.controller.ts
  2. 32
      src/admin/admin.service.ts
  3. 2
      src/admin/dto/login-Admin.dto.ts
  4. 33
      src/admin/dto/update-user.dto.ts
  5. 5
      src/admin/entities/admin.entity.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<Admin> {
async register(@Body() createAdminDto: CreateAdminDto): Promise<Admin> {
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<Admin> {
const userId = req.user.id;
return this.adminService.editAdminProfile(userId, updateAdminDto);
}
}

@ -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<Admin> {
async register(createAdminDto: CreateAdminDto): Promise<Admin> {
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<string | number>("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<Admin> {
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}`);
}
}
}

@ -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" })

@ -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;
}

@ -29,4 +29,7 @@ export class Admin extends Model<Admin> {
})
gender: string;
}
export enum Gender {
Male = "male",
Female = "female",
}

Loading…
Cancel
Save