This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 00:03:24 +03:00
parent 2ccec617a5
commit b48d5ee19e
168 changed files with 2877 additions and 1146 deletions
@@ -0,0 +1,7 @@
namespace Application.Endpoints.Doctors.ResetPassword;
public class DoctorResetPasswordDto
{
public string? Email { get; set; }
public string? Password { get; set; }
}
@@ -1,58 +1,49 @@
using Application.Endpoints.Doctors.Login;
using Application.Services.Database;
using Application.Services.Database;
using Application.Services.HashingAlgorithms;
namespace Application.Endpoints.Doctors.ResetPassword;
public class DoctorResetPasswordHandler
{
private readonly IDoctorRepository _doctorRepository;
private readonly IHashingAlgorithms _hashingAlgorithms;
public DoctorResetPasswordHandler(IDoctorRepository doctorRepository)
public DoctorResetPasswordHandler(IDoctorRepository doctorRepository, IHashingAlgorithms hashingAlgorithms)
{
_doctorRepository = doctorRepository;
_hashingAlgorithms = hashingAlgorithms;
}
public async Task<BaseResponse> Handle(DoctorLoginDTO resetDoctorDto)
public async Task<BaseResponse> Handle(DoctorResetPasswordDto resetDoctorDto)
{
var validation = new DoctorResetPasswordValidation(_doctorRepository);
var validationResult = await validation.ValidateAsync(resetDoctorDto);
if (!validationResult.IsValid)
{
var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
var firstError = validationResult.Errors.FirstOrDefault();
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
var errorMessage = firstError.ErrorMessage;
return new BaseResponse
{
Success = false,
Message = string.Join(", ", errorMessage),
StatusCode = errorCode,
Message = errorMessage,
Data = null
};
}
var currentDoctor = await _doctorRepository.FindByEmailAsync(resetDoctorDto.Email);
var updatedDoctor = currentDoctor;
updatedDoctor.Password = resetDoctorDto.Password;
updatedDoctor.SetPassword(_hashingAlgorithms.SHA256Algorithm(resetDoctorDto.Password));
await _doctorRepository.UpdateAsync(updatedDoctor);
updatedDoctor = await _doctorRepository.GetByIdAsync(currentDoctor.Id);
if (updatedDoctor.Password != resetDoctorDto.Password)
{
return new BaseResponse
{
Success = false,
Message = $"Failed to update passwor for doctor {updatedDoctor.Name}",
Data = null
};
}
return new BaseResponse
{
Success = true,
Message = $"Password of doctor {updatedDoctor.Name} has been reset succesfully",
Data = updatedDoctor.Email
StatusCode = HttpStatusCodes.OK,
Message = "Password successfully changed!",
Data = null
};
}
}
}
@@ -1,9 +1,9 @@
using Application.Services.Database;
using FluentValidation;
namespace Application.Endpoints.Doctors.Login;
namespace Application.Endpoints.Doctors.ResetPassword;
public class DoctorResetPasswordValidation : AbstractValidator<DoctorLoginDTO>
public class DoctorResetPasswordValidation : AbstractValidator<DoctorResetPasswordDto>
{
private readonly IDoctorRepository _doctorRepository;
@@ -12,15 +12,19 @@ public class DoctorResetPasswordValidation : AbstractValidator<DoctorLoginDTO>
_doctorRepository = doctorRepository;
RuleFor(x => x.Email)
.NotEmpty().WithMessage("Email is required.")
.EmailAddress().WithMessage("Invalid email format.")
.MustAsync(BeExistingDoctor).WithMessage("Doctor with this email does not exist.");
.NotEmpty().WithMessage("Email is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.EmailAddress().WithMessage("Invalid email format.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(BeExistingDoctor).WithMessage("Doctor with this email does not exist.")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.Password)
.NotEmpty().WithMessage("Password is required.")
.NotEmpty().WithMessage("Password is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.")
.MustAsync((dto, password, context, cancellationToken) => BeDifferentFromOldPassword(dto.Email, password, cancellationToken))
.WithMessage("New password cannot be the same as old password.");
.WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync((dto, password, context, cancellationToken) =>
BeDifferentFromOldPassword(dto.Email, password, cancellationToken))
.WithMessage("New password cannot be the same as old password.")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
private async Task<bool> BeExistingDoctor(string email, CancellationToken cancellationToken)
@@ -29,9 +33,10 @@ public class DoctorResetPasswordValidation : AbstractValidator<DoctorLoginDTO>
return doctor != null;
}
private async Task<bool> BeDifferentFromOldPassword(string email, string newPassword, CancellationToken cancellationToken)
private async Task<bool> BeDifferentFromOldPassword(string email, string newPassword,
CancellationToken cancellationToken)
{
var currentDoctor = await _doctorRepository.FindByEmailAsync(email);
return !newPassword.Equals(currentDoctor?.Password, StringComparison.Ordinal);
}
}
}