49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
using Application.Services.Database.PostgreSQL;
|
|
using Application.Services.HashingAlgorithms;
|
|
|
|
namespace Application.Endpoints.Authorization.Patient;
|
|
|
|
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(LoginDto patientResetPasswordDto)
|
|
{
|
|
var validation = new PatientResetPasswordValidation(_patientRepository, _hashingAlgorithms);
|
|
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
|
|
};
|
|
}
|
|
} |