Presupuestador_web/server/src/contexts/users/domain/entities/User.ts

105 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-05-20 07:47:44 +00:00
import { Password } from "@/contexts/common/domain";
2024-05-15 19:56:22 +00:00
import {
AggregateRoot,
Email,
IDomainError,
2024-06-10 17:16:39 +00:00
Language,
2024-05-16 11:56:46 +00:00
Name,
2024-05-15 19:56:22 +00:00
Result,
UniqueID,
2024-05-19 22:04:23 +00:00
handleDomainError,
2024-05-15 19:56:22 +00:00
} from "@shared/contexts";
2024-05-19 22:04:23 +00:00
import { UserHasName } from "./User.specifications";
2024-05-27 09:55:04 +00:00
import { UserRole } from "./UserRole";
2024-05-15 19:56:22 +00:00
export interface IUserProps {
2024-05-16 11:56:46 +00:00
name: Name;
2024-05-15 19:56:22 +00:00
email: Email;
2024-05-19 22:04:23 +00:00
password: Password;
2024-05-27 09:55:04 +00:00
roles: UserRole[];
2024-06-10 17:16:39 +00:00
language: Language;
2024-05-15 19:56:22 +00:00
}
2024-05-27 09:55:04 +00:00
//type ISecuredUserProps = ;
2024-05-19 22:04:23 +00:00
2024-05-15 19:56:22 +00:00
export interface IUser {
id: UniqueID;
2024-05-16 11:56:46 +00:00
name: Name;
2024-05-15 19:56:22 +00:00
email: Email;
2024-05-20 07:36:28 +00:00
password: Password;
2024-06-10 17:16:39 +00:00
language: Language;
2024-05-16 18:16:00 +00:00
isUser: boolean;
isAdmin: boolean;
2024-05-15 19:56:22 +00:00
}
export class User extends AggregateRoot<IUserProps> implements IUser {
2024-05-19 22:04:23 +00:00
static readonly ERROR_USER_WITHOUT_NAME = "ERROR_USER_WITHOUT_NAME";
2024-05-27 09:55:04 +00:00
public static create(props: IUserProps, id?: UniqueID): Result<User, IDomainError> {
2024-05-19 22:04:23 +00:00
const user = new User(props, id);
2024-05-15 19:56:22 +00:00
2024-05-19 22:04:23 +00:00
// Reglas de negocio / validaciones
const isValidUser = new UserHasName().isSatisfiedBy(user);
2024-05-15 19:56:22 +00:00
2024-05-19 22:04:23 +00:00
if (!isValidUser) {
return Result.fail(handleDomainError(User.ERROR_USER_WITHOUT_NAME));
}
2024-05-15 19:56:22 +00:00
return Result.ok<User>(user);
}
public static async hashPassword(password): Promise<string> {
2024-05-19 22:04:23 +00:00
return Password.hashPassword(password);
2024-05-15 19:56:22 +00:00
}
2024-05-27 09:55:04 +00:00
private roles: UserRole[];
constructor(props: IUserProps, id?: UniqueID) {
const { roles } = props;
super(props, id);
this.roles = roles;
}
2024-05-16 11:56:46 +00:00
get name(): Name {
return this.props.name;
}
2024-05-15 19:56:22 +00:00
get email(): Email {
return this.props.email;
}
2024-05-20 07:36:28 +00:00
get password(): Password {
return this.props.password;
2024-05-15 19:56:22 +00:00
}
2024-06-10 17:16:39 +00:00
get language(): Language {
return this.props.language;
}
2024-05-16 18:16:00 +00:00
get isUser(): boolean {
2024-05-27 09:55:04 +00:00
return this.hasRole(UserRole.ROLE_USER);
2024-05-16 18:16:00 +00:00
}
get isAdmin(): boolean {
2024-05-27 09:55:04 +00:00
return this.hasRole(UserRole.ROLE_ADMIN);
}
public hasRole(role: UserRole): boolean {
return this.roles.some((r) => r.equals(role));
}
public getRoles(): UserRole[] {
return this.roles;
}
public addRole(role: UserRole): void {
if (!this.roles.some((r) => r.equals(role))) {
this.roles.push(role);
}
}
public removeRole(role: UserRole): void {
this.roles = this.roles.filter((r) => !r.equals(role));
2024-05-16 18:16:00 +00:00
}
2024-05-15 19:56:22 +00:00
}