41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
|
|
import { AggregateRoot, Result, UniqueID } from "@common/domain";
|
||
|
|
import { UserAuthenticatedEvent } from "../events";
|
||
|
|
import { EmailAddress, Username } from "../value-objects";
|
||
|
|
|
||
|
|
export interface IAuthenticatedUserProps {
|
||
|
|
username: Username;
|
||
|
|
email: EmailAddress;
|
||
|
|
roles: string[];
|
||
|
|
token: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export class AuthenticatedUser extends AggregateRoot<IAuthenticatedUserProps> {
|
||
|
|
static create(props: IAuthenticatedUserProps, id?: UniqueID): Result<AuthenticatedUser, Error> {
|
||
|
|
const { username, email, roles, token } = props;
|
||
|
|
|
||
|
|
if (!id || !username || !email || !token) {
|
||
|
|
return Result.fail(new Error("Invalid authenticated user data"));
|
||
|
|
}
|
||
|
|
|
||
|
|
const user = new AuthenticatedUser({ username, email, roles, token }, id);
|
||
|
|
|
||
|
|
// 🔹 Disparar evento de dominio "UserAuthenticatedEvent"
|
||
|
|
user.addDomainEvent(new UserAuthenticatedEvent(id, email.toString()));
|
||
|
|
|
||
|
|
return Result.ok(user);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 🔹 Devuelve una representación lista para persistencia
|
||
|
|
*/
|
||
|
|
public toPersistenceData(): any {
|
||
|
|
return {
|
||
|
|
id: this._id.toString(),
|
||
|
|
username: this._props.username.toString(),
|
||
|
|
email: this._props.email.toString(),
|
||
|
|
roles: JSON.stringify(this._props.roles),
|
||
|
|
token: this._props.token,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|