user service corrected

This commit is contained in:
sdraris 2025-09-12 12:00:29 +02:00
parent e038cab520
commit b0f3214a34

View File

@ -22,8 +22,8 @@ export class UserService {
// Nettoyage / validation consentement photo
let consentDate: Date | undefined;
if (dto.consent_photo && dto.consent_photo_at) {
const parsed = new Date(dto.consent_photo_at);
if (dto.consentement_photo && dto.date_consentement_photo) {
const parsed = new Date(dto.date_consentement_photo);
if (!isNaN(parsed.getTime())) {
consentDate = parsed;
}
@ -31,40 +31,38 @@ export class UserService {
// Hash mot de passe
const salt = await bcrypt.genSalt();
const password_hash = await bcrypt.hash(dto.password, salt);
const hashedPassword = await bcrypt.hash(dto.password, salt);
const entity = this.usersRepository.create({
email: dto.email,
password_hash,
first_name: dto.first_name,
last_name: dto.last_name,
password: hashedPassword,
prenom: dto.prenom,
nom: dto.nom,
role: dto.role,
status: dto.status,
gender: dto.gender,
phone: dto.phone,
address: dto.address,
statut: dto.statut,
genre: dto.genre,
telephone: dto.telephone,
ville: dto.ville,
code_postal: dto.code_postal,
adresse: dto.adresse,
photo_url: dto.photo_url,
consent_photo: dto.consent_photo ?? false,
consent_photo_at: consentDate,
must_change_password: dto.must_change_password ?? false
consentement_photo: dto.consentement_photo ?? false,
date_consentement_photo: consentDate,
changement_mdp_obligatoire: dto.changement_mdp_obligatoire ?? false
});
const saved = await this.usersRepository.save(entity);
return this.findOne(saved.id);
}
//Trouver tous les utilisateurs
async findAll(): Promise<Users[]> {
return this.usersRepository.find();
}
// Trouver utilisateur par condition
async findOneBy(where: Partial<Users>): Promise<Users | null> {
return this.usersRepository.findOne({ where });
}
// Trouver utilisateur par ID
async findOne(id: string): Promise<Users> {
const user = await this.usersRepository.findOne({ where: { id } });
if (!user) {
@ -73,12 +71,10 @@ export class UserService {
return user;
}
// Trouver utilisateur par email
async findByEmailOrNull(email: string): Promise<Users | null> {
return this.usersRepository.findOne({ where: { email } });
}
// Mettre à jour un utilisateur
async updateUser(id: string, dto: UpdateUserDto, currentUser: Users): Promise<Users> {
const user = await this.findOne(id);
@ -90,23 +86,22 @@ export class UserService {
// Gestion du mot de passe
if (dto.password) {
const salt = await bcrypt.genSalt();
user.password_hash = await bcrypt.hash(dto.password, salt);
user.password = await bcrypt.hash(dto.password, salt);
delete (dto as any).password;
}
// Conversion de la date de consentement
if (dto.consent_photo_at !== undefined) {
user.consent_photo_at = dto.consent_photo_at
? new Date(dto.consent_photo_at)
if (dto.date_consentement_photo !== undefined) {
user.date_consentement_photo = dto.date_consentement_photo
? new Date(dto.date_consentement_photo)
: undefined;
delete (dto as any).consent_photo_at;
delete (dto as any).date_consentement_photo;
}
Object.assign(user, dto);
return this.usersRepository.save(user);
}
// Supprimer un utilisateur
async remove(id: string): Promise<void> {
const result = await this.usersRepository.delete(id);
if (result.affected === 0) {