user service edited without stupid base service
This commit is contained in:
parent
0fa43f7d9c
commit
d377e37dbc
@ -3,20 +3,17 @@ import { InjectRepository } from "@nestjs/typeorm";
|
|||||||
import { RoleType, Users } from "src/entities/users.entity";
|
import { RoleType, Users } from "src/entities/users.entity";
|
||||||
import { Repository } from "typeorm";
|
import { Repository } from "typeorm";
|
||||||
import { CreateUserDto } from "./dto/create_user.dto";
|
import { CreateUserDto } from "./dto/create_user.dto";
|
||||||
import * as bcrypt from 'bcrypt';
|
|
||||||
import { UpdateUserDto } from "./dto/update_user.dto";
|
import { UpdateUserDto } from "./dto/update_user.dto";
|
||||||
import { BaseService } from "src/common/base.service";
|
import * as bcrypt from 'bcrypt';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UserService extends BaseService<Users> {
|
export class UserService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(Users)
|
@InjectRepository(Users)
|
||||||
private readonly usersRepository: Repository<Users>
|
private readonly usersRepository: Repository<Users>
|
||||||
) {
|
) { }
|
||||||
super(usersRepository);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Creation utilisateur
|
// Création utilisateur
|
||||||
async createUser(dto: CreateUserDto, currentUser?: Users): Promise<Users> {
|
async createUser(dto: CreateUserDto, currentUser?: Users): Promise<Users> {
|
||||||
// Sécuriser le rôle
|
// Sécuriser le rôle
|
||||||
if (!currentUser || currentUser.role !== RoleType.SUPER_ADMIN) {
|
if (!currentUser || currentUser.role !== RoleType.SUPER_ADMIN) {
|
||||||
@ -26,13 +23,11 @@ export class UserService extends BaseService<Users> {
|
|||||||
// 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.consent_photo && dto.consent_photo_at) {
|
||||||
if (dto.consent_photo_at) {
|
|
||||||
const parsed = new Date(dto.consent_photo_at);
|
const parsed = new Date(dto.consent_photo_at);
|
||||||
if (!isNaN(parsed.getTime())) {
|
if (!isNaN(parsed.getTime())) {
|
||||||
consentDate = parsed;
|
consentDate = parsed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Hash mot de passe
|
// Hash mot de passe
|
||||||
const salt = await bcrypt.genSalt();
|
const salt = await bcrypt.genSalt();
|
||||||
@ -58,11 +53,18 @@ export class UserService extends BaseService<Users> {
|
|||||||
return this.findOne(saved.id);
|
return this.findOne(saved.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOneBy(where: Partial<Users>) {
|
//Trouver tous les utilisateurs
|
||||||
return this.usersRepository.findOne({ where })
|
async findAll(): Promise<Users[]> {
|
||||||
|
return this.usersRepository.find();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Trouver utilisateur par ID
|
|
||||||
|
// Trouver utilisateur par condition
|
||||||
|
async findOneBy(where: Partial<Users>): Promise<Users | null> {
|
||||||
|
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) {
|
||||||
@ -71,28 +73,28 @@ export class UserService extends BaseService<Users> {
|
|||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Trouver utilisateur par email
|
// Trouver utilisateur par email
|
||||||
async findByEmailOrNull(email: string): Promise<Users | null> {
|
async findByEmailOrNull(email: string): Promise<Users | null> {
|
||||||
const user = await this.usersRepository.findOne({ where: { email } });
|
return this.usersRepository.findOne({ where: { email } });
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Mettre a jour un utilisateur
|
// 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);
|
||||||
|
|
||||||
//Tant que pas super_admin interdir changement de role
|
// Interdire changement de rôle si pas super admin
|
||||||
if (dto.role && currentUser.role !== RoleType.SUPER_ADMIN) {
|
if (dto.role && currentUser.role !== RoleType.SUPER_ADMIN) {
|
||||||
throw new ForbiddenException('Acces reserve aux super admins');
|
throw new ForbiddenException('Accès réservé aux super admins');
|
||||||
}
|
}
|
||||||
|
|
||||||
//Gestion de changement 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_hash = await bcrypt.hash(dto.password, salt);
|
||||||
delete (dto as any).password;
|
delete (dto as any).password;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Conversion de la date de consentement
|
||||||
if (dto.consent_photo_at !== undefined) {
|
if (dto.consent_photo_at !== undefined) {
|
||||||
user.consent_photo_at = dto.consent_photo_at
|
user.consent_photo_at = dto.consent_photo_at
|
||||||
? new Date(dto.consent_photo_at)
|
? new Date(dto.consent_photo_at)
|
||||||
@ -104,7 +106,7 @@ export class UserService extends BaseService<Users> {
|
|||||||
return this.usersRepository.save(user);
|
return this.usersRepository.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Supprimer un utilisateur
|
// 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