2025-05-02 21:43:51 +00:00
|
|
|
import { AggregateRoot, EmailAddress, UniqueID } from "@/core/common/domain";
|
2025-05-09 10:45:32 +00:00
|
|
|
import { Result } from "@repo/rdx-utils";
|
2025-02-15 21:30:12 +00:00
|
|
|
import { UserAuthenticatedEvent } from "../events";
|
2025-02-20 18:55:24 +00:00
|
|
|
import { Username } from "../value-objects";
|
2025-02-15 21:30:12 +00:00
|
|
|
|
|
|
|
|
export interface IUserProps {
|
|
|
|
|
username: Username;
|
|
|
|
|
email: EmailAddress;
|
|
|
|
|
roles: string[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface IUser {
|
|
|
|
|
username: Username;
|
|
|
|
|
email: EmailAddress;
|
|
|
|
|
|
|
|
|
|
isUser: boolean;
|
|
|
|
|
isAdmin: boolean;
|
2025-02-21 10:06:27 +00:00
|
|
|
isActive: boolean;
|
2025-02-15 21:30:12 +00:00
|
|
|
|
|
|
|
|
hasRole(role: string): boolean;
|
|
|
|
|
hasRoles(roles: string[]): boolean;
|
|
|
|
|
getRoles(): string[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class User extends AggregateRoot<IUserProps> implements IUser {
|
|
|
|
|
static create(props: IUserProps, id: UniqueID): Result<User, Error> {
|
|
|
|
|
const user = new User(props, id);
|
|
|
|
|
|
|
|
|
|
// 🔹 Disparar evento de dominio "UserAuthenticatedEvent"
|
|
|
|
|
const { email } = props;
|
|
|
|
|
user.addDomainEvent(new UserAuthenticatedEvent(id, email.toString()));
|
|
|
|
|
|
|
|
|
|
return Result.ok(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getRoles(): string[] {
|
2025-02-20 18:55:24 +00:00
|
|
|
return this.props.roles;
|
2025-02-15 21:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasRole(role: string): boolean {
|
2025-02-20 18:55:24 +00:00
|
|
|
return (this.props.roles || []).some((r) => r === role);
|
2025-02-15 21:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasRoles(roles: string[]): boolean {
|
|
|
|
|
return roles.map((rol) => this.hasRole(rol)).some((value) => value != false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get username(): Username {
|
2025-02-20 18:55:24 +00:00
|
|
|
return this.props.username;
|
2025-02-15 21:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get email(): EmailAddress {
|
2025-02-20 18:55:24 +00:00
|
|
|
return this.props.email;
|
2025-02-15 21:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get isUser(): boolean {
|
|
|
|
|
return this.hasRole("user");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get isAdmin(): boolean {
|
|
|
|
|
return this.hasRole("admin");
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 10:06:27 +00:00
|
|
|
get isActive(): boolean {
|
|
|
|
|
return true;
|
2025-02-15 21:30:12 +00:00
|
|
|
}
|
|
|
|
|
}
|