auth/src/modules/authentication/domain/usecases/update-password.usecase.ts

35 lines
1.1 KiB
TypeScript

import { CommandHandler } from '@nestjs/cqrs';
import { AuthenticationRepository } from '../../adapters/secondaries/authentication.repository';
import { Authentication } from '../entities/authentication';
import * as bcrypt from 'bcrypt';
import { UpdatePasswordCommand } from '../../commands/update-password.command';
import { Messager } from '../../adapters/secondaries/messager';
@CommandHandler(UpdatePasswordCommand)
export class UpdatePasswordUseCase {
constructor(
private readonly _authenticationRepository: AuthenticationRepository,
private readonly _messager: Messager,
) {}
execute = async (command: UpdatePasswordCommand): Promise<Authentication> => {
const { uuid, password } = command.updatePasswordRequest;
const hash = await bcrypt.hash(password, 10);
try {
return await this._authenticationRepository.update(uuid, {
password: hash,
});
} catch (error) {
this._messager.publish(
'logging.auth.password.update.warning',
JSON.stringify({
command,
error,
}),
);
throw error;
}
};
}