75 lines
1.7 KiB
TypeScript
75 lines
1.7 KiB
TypeScript
|
|
import { AggregateRoot, Result, UniqueID } from "@common/domain";
|
||
|
|
import { UserAuthenticatedEvent } from "../events";
|
||
|
|
import { EmailAddress, Username } from "../value-objects";
|
||
|
|
|
||
|
|
export interface IUserProps {
|
||
|
|
username: Username;
|
||
|
|
email: EmailAddress;
|
||
|
|
roles: string[];
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface IUser {
|
||
|
|
username: Username;
|
||
|
|
email: EmailAddress;
|
||
|
|
|
||
|
|
isUser: boolean;
|
||
|
|
isAdmin: boolean;
|
||
|
|
|
||
|
|
hasRole(role: string): boolean;
|
||
|
|
hasRoles(roles: string[]): boolean;
|
||
|
|
getRoles(): string[];
|
||
|
|
toPersistenceData(): any;
|
||
|
|
}
|
||
|
|
|
||
|
|
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[] {
|
||
|
|
return this._props.roles;
|
||
|
|
}
|
||
|
|
|
||
|
|
hasRole(role: string): boolean {
|
||
|
|
return (this._props.roles || []).some((r) => r === role);
|
||
|
|
}
|
||
|
|
|
||
|
|
hasRoles(roles: string[]): boolean {
|
||
|
|
return roles.map((rol) => this.hasRole(rol)).some((value) => value != false);
|
||
|
|
}
|
||
|
|
|
||
|
|
get username(): Username {
|
||
|
|
return this._props.username;
|
||
|
|
}
|
||
|
|
|
||
|
|
get email(): EmailAddress {
|
||
|
|
return this._props.email;
|
||
|
|
}
|
||
|
|
|
||
|
|
get isUser(): boolean {
|
||
|
|
return this.hasRole("user");
|
||
|
|
}
|
||
|
|
|
||
|
|
get isAdmin(): boolean {
|
||
|
|
return this.hasRole("admin");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 🔹 Devuelve una representación lista para persistencia
|
||
|
|
*/
|
||
|
|
toPersistenceData(): any {
|
||
|
|
return {
|
||
|
|
id: this._id.toString(),
|
||
|
|
username: this._props.username.toString(),
|
||
|
|
email: this._props.email.toString(),
|
||
|
|
roles: this._props.roles.map((role) => role.toString()),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|