wip tests update password

This commit is contained in:
sbriat
2023-07-10 17:37:50 +02:00
parent 55c4367702
commit de81325750
9 changed files with 182 additions and 8 deletions

View File

@@ -6,6 +6,7 @@ import {
} 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';
export class AuthenticationEntity extends AggregateRoot<AuthenticationProps> {
protected readonly _id: AggregateID;
@@ -14,7 +15,7 @@ export class AuthenticationEntity extends AggregateRoot<AuthenticationProps> {
create: CreateAuthenticationProps,
): Promise<AuthenticationEntity> => {
const props: AuthenticationProps = { ...create };
const hash = await bcrypt.hash(props.password, 10);
const hash = await AuthenticationEntity.encryptPassword(props.password);
const authentication = new AuthenticationEntity({
id: props.userId,
props: {
@@ -29,6 +30,15 @@ export class AuthenticationEntity extends AggregateRoot<AuthenticationProps> {
return authentication;
};
updatePassword = async (password: string): Promise<void> => {
this.props.password = await AuthenticationEntity.encryptPassword(password);
this.addEvent(
new PasswordUpdatedDomainEvent({
aggregateId: this.id,
}),
);
};
delete(): void {
this.addEvent(
new AuthenticationDeletedDomainEvent({
@@ -40,4 +50,7 @@ export class AuthenticationEntity extends AggregateRoot<AuthenticationProps> {
validate(): void {
// entity business rules validation to protect it's invariant before saving entity to a database
}
private static encryptPassword = async (password: string): Promise<string> =>
await bcrypt.hash(password, 10);
}

View File

@@ -0,0 +1,7 @@
import { DomainEvent, DomainEventProps } from '@mobicoop/ddd-library';
export class PasswordUpdatedDomainEvent extends DomainEvent {
constructor(props: DomainEventProps<PasswordUpdatedDomainEvent>) {
super(props);
}
}