2024-05-15 19:56:22 +00:00
|
|
|
import {
|
|
|
|
|
AggregateRoot,
|
|
|
|
|
Email,
|
|
|
|
|
IDomainError,
|
2024-05-16 11:56:46 +00:00
|
|
|
Name,
|
2024-05-19 22:04:23 +00:00
|
|
|
Password,
|
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-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-15 19:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-19 22:04:23 +00:00
|
|
|
//type ISecuredUserProps = Omit<IUserProps, "password">;
|
|
|
|
|
|
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-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-15 19:56:22 +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-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-05-16 18:16:00 +00:00
|
|
|
get isUser(): boolean {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get isAdmin(): boolean {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-05-15 19:56:22 +00:00
|
|
|
}
|