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