You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.8 KiB
81 lines
2.8 KiB
import { HttpException, HttpStatus, Injectable } from "@nestjs/common"; |
|
import { InjectModel } from "@nestjs/sequelize"; |
|
import { Admin } from "./entities/admin.entity"; |
|
import * as bcrypt from "bcrypt"; |
|
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( |
|
@InjectModel(Admin) private readonly adminModel: typeof Admin, |
|
private readonly jwtService: JwtService, |
|
private readonly configService: ConfigService, |
|
) {} |
|
//register method |
|
async register(createAdminDto: CreateAdminDto): Promise<Admin> { |
|
try { |
|
const existingAdmin = await this.adminModel.findOne({ where: { email: createAdminDto.email } }); |
|
if (existingAdmin) { |
|
throw new HttpException("Email is already registered.", HttpStatus.CONFLICT); |
|
} |
|
|
|
createAdminDto.password = await bcrypt.hash(createAdminDto.password, 10); |
|
|
|
const admin = await this.adminModel.create(createAdminDto); |
|
|
|
return admin; |
|
} catch (error) { |
|
console.log(error); |
|
if (error instanceof HttpException) { |
|
throw error; |
|
} |
|
throw new HttpException("An error occurred during registration.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
} |
|
} |
|
//login method |
|
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 or username", HttpStatus.UNAUTHORIZED); |
|
} |
|
|
|
const isValidPassword = await bcrypt.compare(loginAdminDto.password, admin.password); |
|
if (!isValidPassword) { |
|
throw new HttpException("Invalid email or password or username", HttpStatus.UNAUTHORIZED); |
|
} |
|
|
|
const token = this.jwtService.sign( |
|
{ id: admin.id, role: admin.role }, |
|
{ |
|
secret: this.configService.get<string>("JWT_SECRET"), |
|
expiresIn: this.configService.get<string | number>("JWT_EXPIRES") || "1h", |
|
}, |
|
); |
|
|
|
return { token }; |
|
} catch (error) { |
|
throw new HttpException(error.response, 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}`); |
|
} |
|
} |
|
}
|
|
|