46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
export class AccountEntity {
|
|
constructor(
|
|
public readonly id: string,
|
|
public readonly email: string,
|
|
private password: string,
|
|
public readonly roleId: number,
|
|
public readonly createdAt: Date,
|
|
public readonly updatedAt: Date,
|
|
) {
|
|
this.validateEmail(email);
|
|
this.validatePassword(password);
|
|
}
|
|
|
|
// Logique métier : validation de l'email
|
|
private validateEmail(email: string): void {
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
if (!emailRegex.test(email)) {
|
|
throw new Error("Format d'email invalide");
|
|
}
|
|
}
|
|
|
|
private validatePassword(password: string): void {
|
|
if (password.length < 8) {
|
|
throw new Error("Mot de passe trop court");
|
|
}
|
|
}
|
|
|
|
// Logique métier : vérification du mot de passe
|
|
public verifyPassword(plainPassword: string): boolean {
|
|
// Dans un vrai projet, on utiliserait bcrypt
|
|
return this.password === plainPassword;
|
|
}
|
|
|
|
// Factory method pour créer un nouveau compte
|
|
static create(email: string, password: string): AccountEntity {
|
|
const now = new Date();
|
|
const id = crypto.randomUUID();
|
|
return new AccountEntity(id, email, password, 1, now, now);
|
|
}
|
|
}
|
|
|
|
export type CreateAccountDto = {
|
|
email: string;
|
|
password: string;
|
|
};
|