forked from Ynov/ptitspas-ynov-back
138 lines
5.0 KiB
TypeScript
138 lines
5.0 KiB
TypeScript
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<Users>,
|
|
|
|
@InjectRepository(Validation)
|
|
private readonly validationRepository: Repository<Validation>
|
|
) { }
|
|
|
|
// Création utilisateur
|
|
async createUser(dto: CreateUserDto, currentUser?: Users): Promise<Users> {
|
|
// 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<Users[]> {
|
|
return this.usersRepository.find();
|
|
}
|
|
|
|
async findOneBy(where: Partial<Users>): Promise<Users | null> {
|
|
return this.usersRepository.findOne({ where });
|
|
}
|
|
|
|
async findOne(id: string): Promise<Users> {
|
|
const user = await this.usersRepository.findOne({ where: { id } });
|
|
if (!user) {
|
|
throw new NotFoundException('Utilisateur introuvable');
|
|
}
|
|
return user;
|
|
}
|
|
|
|
async findByEmailOrNull(email: string): Promise<Users | null> {
|
|
return this.usersRepository.findOne({ where: { email } });
|
|
}
|
|
|
|
async updateUser(id: string, dto: UpdateUserDto, currentUser: Users): Promise<Users> {
|
|
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<Users> {
|
|
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<void> {
|
|
const result = await this.usersRepository.delete(id);
|
|
if (result.affected === 0) {
|
|
throw new NotFoundException('Utilisateur introuvable');
|
|
}
|
|
}
|
|
}
|