|
|
@ -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( |
|
|
@ -14,9 +15,9 @@ 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<Admin> { |
|
|
|
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("Email is already registered.", HttpStatus.CONFLICT); |
|
|
|
} |
|
|
|
} |
|
|
@ -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; |
|
|
|
} |
|
|
|
} |
|
|
@ -38,16 +39,16 @@ export class AdminService { |
|
|
|
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", 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", HttpStatus.UNAUTHORIZED); |
|
|
|
throw new HttpException("Invalid email or password", HttpStatus.UNAUTHORIZED); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const token = this.jwtService.sign( |
|
|
|
const token = this.jwtService.sign( |
|
|
|
{ id: admin.id, role: admin.role }, |
|
|
|
{ id: admin.id, role: admin.role }, |
|
|
|
{ |
|
|
|
{ |
|
|
@ -55,12 +56,27 @@ export class AdminService { |
|
|
|
expiresIn: this.configService.get<string | number>("JWT_EXPIRES") || "1h", |
|
|
|
expiresIn: this.configService.get<string | number>("JWT_EXPIRES") || "1h", |
|
|
|
}, |
|
|
|
}, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return { token }; |
|
|
|
return { token }; |
|
|
|
} catch (error) { |
|
|
|
} catch (error) { |
|
|
|
console.log(error); |
|
|
|
console.log(error); |
|
|
|
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}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|