Uecko_ERP/apps/server/src/contexts/auth/domain/aggregates/authenticated-user.ts

92 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-04-22 15:09:57 +00:00
import { Result } from "core/common/helpers";
2025-02-21 10:06:27 +00:00
2025-04-22 15:09:57 +00:00
import { AggregateRoot, EmailAddress, UniqueID } from "core/common/domain";
2025-02-01 21:48:13 +00:00
import { UserAuthenticatedEvent } from "../events";
2025-02-21 10:06:27 +00:00
import { HashPassword, PlainPassword, Username } from "../value-objects";
2025-02-01 21:48:13 +00:00
export interface IAuthenticatedUserProps {
username: Username;
email: EmailAddress;
2025-02-05 20:40:59 +00:00
hashPassword: HashPassword;
2025-02-01 21:48:13 +00:00
roles: string[];
}
2025-02-03 21:54:51 +00:00
export interface IAuthenticatedUser {
username: Username;
email: EmailAddress;
2025-02-21 10:06:27 +00:00
hashPassword: HashPassword;
2025-02-03 21:54:51 +00:00
accessToken: string;
refreshToken: string;
isUser: boolean;
isAdmin: boolean;
2025-02-05 20:40:59 +00:00
verifyPassword(candidatePassword: PlainPassword): Promise<boolean>;
2025-02-07 11:14:36 +00:00
hasRole(role: string): boolean;
hasRoles(roles: string[]): boolean;
2025-02-03 21:54:51 +00:00
getRoles(): string[];
toPersistenceData(): any;
}
export class AuthenticatedUser
extends AggregateRoot<IAuthenticatedUserProps>
implements IAuthenticatedUser
{
public accessToken: string = "";
public refreshToken: string = "";
2025-02-03 13:12:36 +00:00
static create(props: IAuthenticatedUserProps, id: UniqueID): Result<AuthenticatedUser, Error> {
const user = new AuthenticatedUser(props, id);
2025-02-01 21:48:13 +00:00
// 🔹 Disparar evento de dominio "UserAuthenticatedEvent"
2025-02-03 13:12:36 +00:00
const { email } = props;
2025-02-01 21:48:13 +00:00
user.addDomainEvent(new UserAuthenticatedEvent(id, email.toString()));
return Result.ok(user);
}
2025-02-05 20:40:59 +00:00
verifyPassword(candidatePassword: PlainPassword): Promise<boolean> {
2025-02-20 18:55:24 +00:00
return this.props.hashPassword.verifyPassword(candidatePassword.toString());
2025-02-03 18:03:23 +00:00
}
2025-02-03 21:54:51 +00:00
getRoles(): string[] {
2025-02-20 18:55:24 +00:00
return this.props.roles;
2025-02-03 19:50:16 +00:00
}
2025-02-07 11:14:36 +00:00
hasRole(role: string): boolean {
2025-02-20 18:55:24 +00:00
return (this.props.roles || []).some((r) => r === role);
2025-02-07 11:14:36 +00:00
}
hasRoles(roles: string[]): boolean {
2025-02-16 19:30:20 +00:00
return roles && roles.map((rol) => this.hasRole(rol)).some((value) => value != false);
2025-02-07 11:14:36 +00:00
}
2025-02-03 21:54:51 +00:00
get username(): Username {
2025-02-20 18:55:24 +00:00
return this.props.username;
2025-02-03 21:54:51 +00:00
}
get email(): EmailAddress {
2025-02-20 18:55:24 +00:00
return this.props.email;
2025-02-03 21:54:51 +00:00
}
2025-02-21 10:06:27 +00:00
get hashPassword(): HashPassword {
return this.props.hashPassword;
}
2025-02-03 19:50:16 +00:00
get isUser(): boolean {
2025-02-07 11:14:36 +00:00
return this.hasRole("user");
2025-02-03 19:50:16 +00:00
}
get isAdmin(): boolean {
2025-02-07 11:14:36 +00:00
return this.hasRole("admin");
2025-02-03 19:50:16 +00:00
}
2025-02-01 21:48:13 +00:00
/**
* 🔹 Devuelve una representación lista para persistencia
*/
2025-02-03 13:12:36 +00:00
toPersistenceData(): any {
2025-02-21 10:06:27 +00:00
return;
2025-02-01 21:48:13 +00:00
}
}