import { AggregateRoot, AggregateID } from '@mobicoop/ddd-library'; import { AuthenticationProps, CreateAuthenticationProps, } from './authentication.types'; import { AuthenticationCreatedDomainEvent } from './events/authentication-created.domain-event'; import { AuthenticationDeletedDomainEvent } from './events/authentication-deleted.domain-event'; import { PasswordUpdatedDomainEvent } from './events/password-updated.domain-event'; import { PasswordVerifierPort } from '../application/ports/password-verifier.port'; export class AuthenticationEntity extends AggregateRoot { protected readonly _id: AggregateID; static create = async ( create: CreateAuthenticationProps, ): Promise => { const props: AuthenticationProps = { ...create }; const authentication = new AuthenticationEntity({ id: props.userId, props: { userId: props.userId, password: props.password, usernames: props.usernames, }, }); authentication.addEvent( new AuthenticationCreatedDomainEvent({ aggregateId: props.userId }), ); return authentication; }; updatePassword = async (password: string): Promise => { this.props.password = password; this.addEvent( new PasswordUpdatedDomainEvent({ aggregateId: this.id, }), ); }; delete(): void { this.addEvent( new AuthenticationDeletedDomainEvent({ aggregateId: this.id, }), ); } authenticate = async ( password: string, passwordVerifier: PasswordVerifierPort, ): Promise => await passwordVerifier.verify(password, this.props.password); validate(): void { // entity business rules validation to protect it's invariant before saving entity to a database } }