50 lines
1.8 KiB
C#
50 lines
1.8 KiB
C#
using Application.Services.Database;
|
|
using Application.Services.HashingAlgorithms;
|
|
|
|
namespace Application.Endpoints.Patients.ResetPassword;
|
|
|
|
public class PatientResetPasswordHandler
|
|
{
|
|
private readonly IHashingAlgorithms _hashingAlgorithms;
|
|
private readonly IPatientRepository _patientRepository;
|
|
|
|
public PatientResetPasswordHandler(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms)
|
|
{
|
|
_patientRepository = patientRepository;
|
|
_hashingAlgorithms = hashingAlgorithms;
|
|
}
|
|
|
|
public async Task<BaseResponse> Handle(PatientResetPasswordDto patientResetPasswordDto)
|
|
{
|
|
patientResetPasswordDto.Password = _hashingAlgorithms.SHA256Algorithm(patientResetPasswordDto.Password);
|
|
var validation = new PatientResetPasswordValidation(_patientRepository);
|
|
var validationResult = await validation.ValidateAsync(patientResetPasswordDto);
|
|
|
|
if (!validationResult.IsValid)
|
|
{
|
|
var firstError = validationResult.Errors.FirstOrDefault();
|
|
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
|
|
var errorMessage = firstError.ErrorMessage;
|
|
|
|
return new BaseResponse
|
|
{
|
|
StatusCode = errorCode,
|
|
Message = errorMessage,
|
|
Data = null
|
|
};
|
|
}
|
|
|
|
var currentPatient = await _patientRepository.FindByEmailAsync(patientResetPasswordDto.Email);
|
|
var updatedPatient = currentPatient;
|
|
updatedPatient.SetPassword(_hashingAlgorithms.SHA256Algorithm(patientResetPasswordDto.Password));
|
|
|
|
await _patientRepository.UpdateAsync(updatedPatient);
|
|
|
|
return new BaseResponse
|
|
{
|
|
StatusCode = HttpStatusCodes.OK,
|
|
Message = "Password successfully changed!",
|
|
Data = null
|
|
};
|
|
}
|
|
} |