forked from Ynov/ptitspas-ynov-back
89 lines
2.9 KiB
TypeScript
89 lines
2.9 KiB
TypeScript
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 d’un 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 d’un 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 d’un 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);
|
||
}
|
||
}
|