Presupuestador_web/server/src/contexts/users/domain/entities/User.ts
2024-05-20 09:47:44 +02:00

73 lines
1.4 KiB
TypeScript

import { Password } from "@/contexts/common/domain";
import {
AggregateRoot,
Email,
IDomainError,
Name,
Result,
UniqueID,
handleDomainError,
} from "@shared/contexts";
import { UserHasName } from "./User.specifications";
export interface IUserProps {
name: Name;
email: Email;
password: Password;
}
//type ISecuredUserProps = Omit<IUserProps, "password">;
export interface IUser {
id: UniqueID;
name: Name;
email: Email;
password: Password;
isUser: boolean;
isAdmin: boolean;
}
export class User extends AggregateRoot<IUserProps> implements IUser {
static readonly ERROR_USER_WITHOUT_NAME = "ERROR_USER_WITHOUT_NAME";
public static create(
props: IUserProps,
id?: UniqueID,
): Result<User, IDomainError> {
const user = new User(props, id);
// Reglas de negocio / validaciones
const isValidUser = new UserHasName().isSatisfiedBy(user);
if (!isValidUser) {
return Result.fail(handleDomainError(User.ERROR_USER_WITHOUT_NAME));
}
return Result.ok<User>(user);
}
public static async hashPassword(password): Promise<string> {
return Password.hashPassword(password);
}
get name(): Name {
return this.props.name;
}
get email(): Email {
return this.props.email;
}
get password(): Password {
return this.props.password;
}
get isUser(): boolean {
return true;
}
get isAdmin(): boolean {
return true;
}
}