Entities
As mentioned in the Domain layer section, your entities should contain your business logic, and have any services that the action requires to be injected. For example, to reset a user's password:
class ResetPassword {
constructor(
public readonly userId: string,
public readonly password: string,
) {
}
}
@CommandHandler(ResetPassword)
export class ResetPasswordHandler implements ICommandHandler<ResetPassword, User> {
public constructor(
private repository: UserRepository
) { }
public async execute(command: ResetPassword) {
const user = await this.repository.findOne(command.userId);
if (!passwordValidator.isValid(newPassword)) {
throw new Error(...);
}
user.updatePassword(passwordHasher.hash(newPassword));
return await this.repository.save(user);
}
}
class User extends AggregateRoot {
public updatePassword(newPassword: string): void {
this.password = newPassword;
}
}