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