import { Injectable, UnauthorizedException } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { PrismaService } from '../prisma/prisma.service'; import * as bcrypt from 'bcrypt'; @Injectable() export class AdminService { constructor( private prisma: PrismaService, private jwtService: JwtService, ) {} async changePassword(adminId: string, oldPassword: string, newPassword: string) { // Récupérer l'administrateur const admin = await this.prisma.admin.findUnique({ where: { id: adminId }, }); if (!admin) { throw new UnauthorizedException('Administrateur non trouvé'); } // Vérifier l'ancien mot de passe const isPasswordValid = await bcrypt.compare(oldPassword, admin.password); if (!isPasswordValid) { throw new UnauthorizedException('Ancien mot de passe incorrect'); } // Hasher le nouveau mot de passe const hashedPassword = await bcrypt.hash(newPassword, 10); // Mettre à jour le mot de passe await this.prisma.admin.update({ where: { id: adminId }, data: { password: hashedPassword }, }); return { message: 'Mot de passe modifié avec succès' }; } }