Add functionality to update admin profile in admin module

master
nicekid1 2 months ago
parent 4506469b9b
commit 6945420e27
  1. 7
      src/admin/admin.controller.ts
  2. 18
      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

@ -17,5 +17,10 @@ export class AdminController {
async login(@Body() loginAdminDto: LoginAdminDto): Promise<{ token: string }> { async login(@Body() loginAdminDto: LoginAdminDto): Promise<{ token: string }> {
return this.adminService.login(loginAdminDto); 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 { ConfigService } from "@nestjs/config";
import { CreateAdminDto } from "./dto/create-Admin.dto"; import { CreateAdminDto } from "./dto/create-Admin.dto";
import { LoginAdminDto } from "./dto/login-Admin.dto"; import { LoginAdminDto } from "./dto/login-Admin.dto";
import { UpdateUserDto } from "./dto/update-user.dto";
@Injectable() @Injectable()
export class AdminService { export class AdminService {
constructor( constructor(
@ -27,7 +28,7 @@ export class AdminService {
return admin; return admin;
} catch (error) { } catch (error) {
console.log(error) console.log(error);
if (error instanceof HttpException) { if (error instanceof HttpException) {
throw error; throw error;
} }
@ -62,5 +63,20 @@ export class AdminService {
throw new HttpException("An error occurred during login.", HttpStatus.INTERNAL_SERVER_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 { export class LoginAdminDto {
@IsEmail({}, { message: "Invalid email format" }) @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; gender: string;
} }
export enum Gender {
Male = "male",
Female = "female",
}

Loading…
Cancel
Save