ptitspas-ynov-back/src/routes/gestionnaires/gestionnaires.service.ts
2025-09-09 16:39:36 +02:00

89 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
ConflictException,
ForbiddenException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { RoleType, Users } from 'src/entities/users.entity';
import { CreateGestionnaireDto } from '../user/dto/create_gestionnaire.dto';
import { UpdateGestionnaireDto } from '../user/dto/update_gestionnaire.dto';
import * as bcrypt from 'bcrypt';
@Injectable()
export class GestionnairesService {
constructor(
@InjectRepository(Users)
private readonly gestionnaireRepository: Repository<Users>,
) { }
// Création dun gestionnaire
async create(dto: CreateGestionnaireDto): Promise<Users> {
const exist = await this.gestionnaireRepository.findOneBy({ email: dto.email });
if (exist) throw new ConflictException('Email déjà utilisé');
const salt = await bcrypt.genSalt();
const password_hash = await bcrypt.hash(dto.password, salt);
const entity = this.gestionnaireRepository.create({
email: dto.email,
password_hash,
first_name: dto.first_name,
last_name: dto.last_name,
gender: dto.gender,
status: dto.status,
phone: dto.phone,
address: dto.address,
photo_url: dto.photo_url,
consent_photo: dto.consent_photo ?? false,
consent_photo_at: dto.consent_photo_at ? new Date(dto.consent_photo_at) : undefined,
must_change_password: dto.must_change_password ?? false,
role: RoleType.GESTIONNAIRE,
});
return this.gestionnaireRepository.save(entity);
}
// Liste des gestionnaires
async findAll(): Promise<Users[]> {
return this.gestionnaireRepository.find({ where: { role: RoleType.GESTIONNAIRE } });
}
// Récupérer un gestionnaire par ID
async findOne(id: string): Promise<Users> {
const gestionnaire = await this.gestionnaireRepository.findOne({
where: { id, role: RoleType.GESTIONNAIRE },
});
if (!gestionnaire) throw new NotFoundException('Gestionnaire introuvable');
return gestionnaire;
}
// Mise à jour dun gestionnaire
async update(id: string, dto: UpdateGestionnaireDto): Promise<Users | null> {
const gestionnaire = await this.findOne(id);
if (!gestionnaire) throw new NotFoundException('Gestionnaire introuvable');
if (dto.password) {
const salt = await bcrypt.genSalt();
gestionnaire.password_hash = await bcrypt.hash(dto.password, salt);
}
if (dto.consent_photo_at !== undefined) {
gestionnaire.consent_photo_at = dto.consent_photo_at
? new Date(dto.consent_photo_at)
: undefined;
}
Object.assign(gestionnaire, dto);
return this.gestionnaireRepository.save(gestionnaire);
}
// Suppression dun gestionnaire
async remove(id: string): Promise<void> {
const gestionnaire = await this.findOne(id);
if (!gestionnaire) throw new NotFoundException('Gestionnaire introuvable');
await this.gestionnaireRepository.delete(id);
}
}