diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index e644773..25bd7cc 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -3,23 +3,23 @@ import { AdminService } from "./admin.service"; import { Admin } from "./entities/admin.entity"; import { CreateAdminDto } from "./dto/create-Admin.dto"; import { LoginAdminDto } from "./dto/login-Admin.dto"; -import { JwtAuthGuard } from "src/guard/auth.guard"; import { UpdateUserDto } from "./dto/update-user.dto"; +import { RoleGuard } from "src/guard/role.guard"; @Controller("admin") export class AdminController { constructor(private readonly adminService: AdminService) {} @Post("register") - async register(@Body() createAdminDto: CreateAdminDto): Promise { + async register(@Body() createAdminDto: CreateAdminDto): Promise<{message}> { return this.adminService.register(createAdminDto); } @Post("login") async login(@Body() loginAdminDto: LoginAdminDto): Promise<{ token: string }> { return this.adminService.login(loginAdminDto); } - @UseGuards(JwtAuthGuard) + @UseGuards(RoleGuard) @Put() - async editAdminProfile(@Request() req, @Body() updateAdminDto: UpdateUserDto): Promise { + async editAdminProfile(@Request() req, @Body() updateAdminDto: UpdateUserDto): Promise<{message}>{ 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 8fc6a09..c8d8b91 100644 --- a/src/admin/admin.service.ts +++ b/src/admin/admin.service.ts @@ -15,38 +15,38 @@ export class AdminService { private readonly configService: ConfigService, ) {} //register method - async register(createAdminDto: CreateAdminDto): Promise { + async register(createAdminDto: CreateAdminDto): Promise<{ message }> { 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); + throw new HttpException("The provided email is already registered.", HttpStatus.CONFLICT); } - createAdminDto.password = await bcrypt.hash(createAdminDto.password, 10); - - const admin = await this.adminModel.create(createAdminDto); - - return admin; + await this.adminModel.create(createAdminDto); + return { message: "admin register is successful" }; } catch (error) { - console.log(error); if (error instanceof HttpException) { 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 async login(loginAdminDto: LoginAdminDto): Promise<{ token: string }> { try { - const admin = await this.adminModel.findOne({ where: { email: loginAdminDto.email } }); + const admin = await this.adminModel.findOne({ + where: { email: loginAdminDto.email }, + }); 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); 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( @@ -59,11 +59,14 @@ export class AdminService { return { token }; } 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 - async editAdminProfile(userId: number, updateAdminDto: UpdateUserDto): Promise { + async editAdminProfile(userId: number, updateAdminDto: UpdateUserDto): Promise<{ message }> { try { const user = await this.adminModel.findOne({ where: { id: userId } }); @@ -73,7 +76,7 @@ export class AdminService { await user.update(updateAdminDto); - return user; + return { message: "admin account updated successful" }; } catch (error) { throw new Error(`An error occurred while updating admin: ${error.message}`); }