2025-02-01 21:48:13 +00:00
|
|
|
import { AggregateRoot, Result, UniqueID } from "@common/domain";
|
|
|
|
|
import { UserAuthenticatedEvent } from "../events";
|
2025-02-03 13:12:36 +00:00
|
|
|
import { EmailAddress, PasswordHash, Username } from "../value-objects";
|
2025-02-01 21:48:13 +00:00
|
|
|
|
|
|
|
|
export interface IAuthenticatedUserProps {
|
|
|
|
|
username: Username;
|
|
|
|
|
email: EmailAddress;
|
2025-02-03 13:12:36 +00:00
|
|
|
passwordHash: PasswordHash;
|
2025-02-01 21:48:13 +00:00
|
|
|
roles: string[];
|
|
|
|
|
token: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class AuthenticatedUser extends AggregateRoot<IAuthenticatedUserProps> {
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 🔹 Devuelve una representación lista para persistencia
|
|
|
|
|
*/
|
2025-02-03 13:12:36 +00:00
|
|
|
toPersistenceData(): any {
|
2025-02-01 21:48:13 +00:00
|
|
|
return {
|
|
|
|
|
id: this._id.toString(),
|
|
|
|
|
username: this._props.username.toString(),
|
|
|
|
|
email: this._props.email.toString(),
|
2025-02-03 13:12:36 +00:00
|
|
|
password: this._props.passwordHash.toString(),
|
2025-02-01 21:48:13 +00:00
|
|
|
roles: JSON.stringify(this._props.roles),
|
|
|
|
|
token: this._props.token,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|