import { ForbiddenException, Injectable, NotFoundException } from "@nestjs/common"; import { InjectRepository } from "@nestjs/typeorm"; import { RoleType, StatutUtilisateurType, Users } from "src/entities/users.entity"; import { Repository } from "typeorm"; import { CreateUserDto } from "./dto/create_user.dto"; import { UpdateUserDto } from "./dto/update_user.dto"; import * as bcrypt from 'bcrypt'; import { StatutValidationType, Validation } from "src/entities/validations.entity"; @Injectable() export class UserService { constructor( @InjectRepository(Users) private readonly usersRepository: Repository, @InjectRepository(Validation) private readonly validationRepository: Repository ) { } // Création utilisateur async createUser(dto: CreateUserDto, currentUser?: Users): Promise { // Sécuriser le rôle if (!currentUser || currentUser.role !== RoleType.SUPER_ADMIN) { dto.role = RoleType.PARENT; } // Nettoyage / validation consentement photo let consentDate: Date | undefined; if (dto.consentement_photo && dto.date_consentement_photo) { const parsed = new Date(dto.date_consentement_photo); if (!isNaN(parsed.getTime())) { consentDate = parsed; } } // Hash mot de passe const salt = await bcrypt.genSalt(); const hashedPassword = await bcrypt.hash(dto.password, salt); const entity = this.usersRepository.create({ email: dto.email, password: hashedPassword, prenom: dto.prenom, nom: dto.nom, role: dto.role, 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, 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); } async findAll(): Promise { return this.usersRepository.find(); } async findOneBy(where: Partial): Promise { return this.usersRepository.findOne({ where }); } async findOne(id: string): Promise { const user = await this.usersRepository.findOne({ where: { id } }); if (!user) { throw new NotFoundException('Utilisateur introuvable'); } return user; } async findByEmailOrNull(email: string): Promise { return this.usersRepository.findOne({ where: { email } }); } async updateUser(id: string, dto: UpdateUserDto, currentUser: Users): Promise { const user = await this.findOne(id); // Interdire changement de rôle si pas super admin if (dto.role && currentUser.role !== RoleType.SUPER_ADMIN) { throw new ForbiddenException('Accès réservé aux super admins'); } // Gestion du mot de passe if (dto.password) { const salt = await bcrypt.genSalt(); user.password = await bcrypt.hash(dto.password, salt); delete (dto as any).password; } // Conversion de la date de consentement 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).date_consentement_photo; } Object.assign(user, dto); return this.usersRepository.save(user); } async validateUser(user_id: string, currentUser: Users, comment?: string): Promise { if (![RoleType.SUPER_ADMIN, RoleType.ADMINISTRATEUR, RoleType.GESTIONNAIRE].includes(currentUser.role)) { throw new ForbiddenException('Accès réservé aux super admins, administrateurs et gestionnaires'); } const user = await this.usersRepository.findOne({ where: { id: user_id } }); if (!user) throw new NotFoundException('Utilisateur introuvable'); user.statut = StatutUtilisateurType.ACTIF; const savedUser = await this.usersRepository.save(user); const validation = this.validationRepository.create({ user: savedUser, type: 'validation_compte', status: StatutValidationType.VALIDE, validated_by: currentUser, comment }); await this.validationRepository.save(validation); return savedUser; } async remove(id: string): Promise { const result = await this.usersRepository.delete(id); if (result.affected === 0) { throw new NotFoundException('Utilisateur introuvable'); } } }