Improve error handling structure in admin module

master
nicekid1 2 months ago
parent 72665cebda
commit c1149c035c
  1. 8
      src/admin/admin.controller.ts
  2. 35
      src/admin/admin.service.ts

@ -3,23 +3,23 @@ import { AdminService } from "./admin.service";
import { Admin } from "./entities/admin.entity"; import { Admin } from "./entities/admin.entity";
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 { JwtAuthGuard } from "src/guard/auth.guard";
import { UpdateUserDto } from "./dto/update-user.dto"; import { UpdateUserDto } from "./dto/update-user.dto";
import { RoleGuard } from "src/guard/role.guard";
@Controller("admin") @Controller("admin")
export class AdminController { export class AdminController {
constructor(private readonly adminService: AdminService) {} constructor(private readonly adminService: AdminService) {}
@Post("register") @Post("register")
async register(@Body() createAdminDto: CreateAdminDto): Promise<Admin> { async register(@Body() createAdminDto: CreateAdminDto): Promise<{message}> {
return this.adminService.register(createAdminDto); return this.adminService.register(createAdminDto);
} }
@Post("login") @Post("login")
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) @UseGuards(RoleGuard)
@Put() @Put()
async editAdminProfile(@Request() req, @Body() updateAdminDto: UpdateUserDto): Promise<Admin> { async editAdminProfile(@Request() req, @Body() updateAdminDto: UpdateUserDto): Promise<{message}>{
const userId = req.user.id; const userId = req.user.id;
return this.adminService.editAdminProfile(userId, updateAdminDto); return this.adminService.editAdminProfile(userId, updateAdminDto);
} }

@ -15,38 +15,38 @@ export class AdminService {
private readonly configService: ConfigService, private readonly configService: ConfigService,
) {} ) {}
//register method //register method
async register(createAdminDto: CreateAdminDto): Promise<Admin> { async register(createAdminDto: CreateAdminDto): Promise<{ message }> {
try { try {
const existingAdmin = await this.adminModel.findOne({ where: { email: createAdminDto.email } }); const existingAdmin = await this.adminModel.findOne({
where: { email: createAdminDto.email },
});
if (existingAdmin) { if (existingAdmin) {
throw new HttpException("Email is already registered.", HttpStatus.CONFLICT); throw new HttpException("The provided email is already registered.", HttpStatus.CONFLICT);
} }
createAdminDto.password = await bcrypt.hash(createAdminDto.password, 10); createAdminDto.password = await bcrypt.hash(createAdminDto.password, 10);
await this.adminModel.create(createAdminDto);
const admin = await this.adminModel.create(createAdminDto); return { message: "admin register is successful" };
return admin;
} catch (error) { } catch (error) {
console.log(error);
if (error instanceof HttpException) { if (error instanceof HttpException) {
throw error; throw error;
} }
throw new HttpException("An error occurred during registration.", HttpStatus.INTERNAL_SERVER_ERROR); throw new HttpException("An unexpected error occurred. Please try again later.", HttpStatus.INTERNAL_SERVER_ERROR);
} }
} }
//login method //login method
async login(loginAdminDto: LoginAdminDto): Promise<{ token: string }> { async login(loginAdminDto: LoginAdminDto): Promise<{ token: string }> {
try { try {
const admin = await this.adminModel.findOne({ where: { email: loginAdminDto.email } }); const admin = await this.adminModel.findOne({
where: { email: loginAdminDto.email },
});
if (!admin) { if (!admin) {
throw new HttpException("Invalid email or password or username", HttpStatus.UNAUTHORIZED); throw new HttpException("Invalid email or password.", HttpStatus.UNAUTHORIZED);
} }
const isValidPassword = await bcrypt.compare(loginAdminDto.password, admin.password); const isValidPassword = await bcrypt.compare(loginAdminDto.password, admin.password);
if (!isValidPassword) { if (!isValidPassword) {
throw new HttpException("Invalid email or password or username", HttpStatus.UNAUTHORIZED); throw new HttpException("Invalid email or password.", HttpStatus.UNAUTHORIZED);
} }
const token = this.jwtService.sign( const token = this.jwtService.sign(
@ -59,11 +59,14 @@ export class AdminService {
return { token }; return { token };
} catch (error) { } catch (error) {
throw new HttpException(error.response, HttpStatus.INTERNAL_SERVER_ERROR); if (error instanceof HttpException) {
throw error;
}
throw new HttpException("An unexpected error occurred. Please try again later.", HttpStatus.INTERNAL_SERVER_ERROR);
} }
} }
//edit admin profile method //edit admin profile method
async editAdminProfile(userId: number, updateAdminDto: UpdateUserDto): Promise<Admin> { async editAdminProfile(userId: number, updateAdminDto: UpdateUserDto): Promise<{ message }> {
try { try {
const user = await this.adminModel.findOne({ where: { id: userId } }); const user = await this.adminModel.findOne({ where: { id: userId } });
@ -73,7 +76,7 @@ export class AdminService {
await user.update(updateAdminDto); await user.update(updateAdminDto);
return user; return { message: "admin account updated successful" };
} catch (error) { } catch (error) {
throw new Error(`An error occurred while updating admin: ${error.message}`); throw new Error(`An error occurred while updating admin: ${error.message}`);
} }

Loading…
Cancel
Save