auth/src/modules/authentication/core/domain/authentication.entity.ts

59 lines
1.8 KiB
TypeScript

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<AuthenticationProps> {
protected readonly _id: AggregateID;
static create = async (
create: CreateAuthenticationProps,
): Promise<AuthenticationEntity> => {
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<void> => {
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<boolean> =>
await passwordVerifier.verify(password, this.props.password);
validate(): void {
// entity business rules validation to protect it's invariant before saving entity to a database
}
}