Manage Appointments DONE!

This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 21:29:18 +03:00
parent 8663a186a0
commit 13bfa2bdd9
91 changed files with 762 additions and 52 deletions
@@ -0,0 +1,8 @@
namespace Application.Endpoints.Appointments;
public class AppointmentManagementDto
{
public Guid DoctorId { get; set; }
public Guid PatientId { get; set; }
public DateTime Appointment { get; set; }
}
@@ -0,0 +1,121 @@
using Application.Services.Database;
using Application.Services.Database.MongoDB;
using Core.Entities;
namespace Application.Endpoints.Appointments;
public class AppointmentManagementHandler
{
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
private readonly IPatientRepository _patientRepository;
private readonly IDoctorRepository _doctorRepository;
public AppointmentManagementHandler(IAppointmentsMongoDbService appointmentsMongoDbService,
IPatientRepository patientRepository, IDoctorRepository doctorRepository)
{
_appointmentsMongoDbService = appointmentsMongoDbService;
_patientRepository = patientRepository;
_doctorRepository = doctorRepository;
}
public async Task<BaseResponse> HandleCreateAppointment(AppointmentManagementDto dto)
{
var validation = new CreateAppointmentValidator(_appointmentsMongoDbService,
_doctorRepository, _patientRepository);
var validationResult = await validation.ValidateAsync(dto);
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 appointmentId = IdentifierGenerator.GenerateId(dto.DoctorId, dto.PatientId);
var criteria = new List<(string FieldName, string Value)>
{
("_id", appointmentId),
};
var appointments = await _appointmentsMongoDbService.FindAsync<Appointment>(criteria);
if(!appointments.Any())
{
var appointment = new Appointment();
appointment.SetId(appointmentId);
appointment.SetDoctorIid(dto.DoctorId.ToString());
appointment.SetPatientId(dto.PatientId.ToString());
appointment.AddAppointment(dto.Appointment);
await _appointmentsMongoDbService.AddAsync(appointment);
}
else
{
var appointment = appointments[0];
appointment.AddAppointment(dto.Appointment);
await _appointmentsMongoDbService.ModifyAsync("_id", appointmentId, appointment);
}
return new BaseResponse()
{
StatusCode = HttpStatusCodes.Created,
Message = "Appointment successfully created.",
Data = null
};
}
public async Task<BaseResponse> HandleDeleteAppointment(AppointmentManagementDto dto)
{
var validation = new DeleteAppointmentValidator(_appointmentsMongoDbService,
_doctorRepository, _patientRepository);
var validationResult = await validation.ValidateAsync(dto);
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 appointmentId = IdentifierGenerator.GenerateId(dto.DoctorId, dto.PatientId);
var criteria = new List<(string FieldName, string Value)>
{
("_id", appointmentId),
};
var appointments = await _appointmentsMongoDbService.FindAsync<Appointment>(criteria);
if(!appointments.Any())
{
return new BaseResponse()
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Appointment not found in system.",
Data = null
};
}
var appointment = appointments[0];
appointment.RemoveAppointment(dto.Appointment);
await _appointmentsMongoDbService.ModifyAsync("_id", appointmentId, appointment);
return new BaseResponse()
{
StatusCode = HttpStatusCodes.OK,
Message = "Appointment successfully removed.",
Data = null
};
}
}
@@ -0,0 +1,66 @@
using Application.Services.Database;
using Application.Services.Database.MongoDB;
using FluentValidation;
namespace Application.Endpoints.Appointments;
public class CreateAppointmentValidator : AbstractValidator<AppointmentManagementDto>
{
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
private readonly IDoctorRepository _doctorRepository;
private readonly IPatientRepository _patientRepository;
public CreateAppointmentValidator(IAppointmentsMongoDbService appointmentsMongoDbService,
IDoctorRepository doctorRepository, IPatientRepository patientRepository)
{
RuleFor(x => x.DoctorId)
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(IsDoctorRegistered).WithMessage("Doctor is not registered in system")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.PatientId)
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(IsPatientRegistered).WithMessage("Patient is not registered in system")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.Appointment)
.Must(IsAppointmentValidFormat).WithMessage("Appointment is not valid")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x)
.MustAsync(IsAppointmentUnique).WithMessage("Appointment already in system.")
.WithErrorCode(HttpStatusCodes.Conflict.ToString());
_appointmentsMongoDbService = appointmentsMongoDbService;
_doctorRepository = doctorRepository;
_patientRepository = patientRepository;
}
private async Task<bool> IsDoctorRegistered(Guid id, CancellationToken cancellationToken)
{
var doctor = await _doctorRepository.GetByIdAsync(id);
return doctor != null;
}
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken cancellationToken)
{
var patient = await _patientRepository.GetByIdAsync(id);
return patient != null;
}
private bool IsAppointmentValidFormat(DateTime appointment)
{
if (appointment == default(DateTime))
return false;
if (appointment.Date < DateTime.UtcNow.Date)
return false;
return true;
}
private async Task<bool> IsAppointmentUnique(AppointmentManagementDto dto, CancellationToken cancellationToken)
{
return await _appointmentsMongoDbService.IsAppointmentUnique(dto);
}
}
@@ -0,0 +1,67 @@
using Application.Services.Database;
using Application.Services.Database.MongoDB;
using Core.Entities;
using FluentValidation;
namespace Application.Endpoints.Appointments;
public class DeleteAppointmentValidator : AbstractValidator<AppointmentManagementDto>
{
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
private readonly IDoctorRepository _doctorRepository;
private readonly IPatientRepository _patientRepository;
public DeleteAppointmentValidator(IAppointmentsMongoDbService appointmentsMongoDbService,
IDoctorRepository doctorRepository, IPatientRepository patientRepository)
{
RuleFor(x => x.DoctorId)
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(IsDoctorRegistered).WithMessage("Doctor is not registered in system")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.PatientId)
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(IsPatientRegistered).WithMessage("Patient is not registered in system")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.Appointment)
.Must(IsAppointmentValidFormat).WithMessage("Appointment is not valid")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x)
.MustAsync(DoesAppointmentExists).WithMessage("Appointment not found in system")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
_appointmentsMongoDbService = appointmentsMongoDbService;
_doctorRepository = doctorRepository;
_patientRepository = patientRepository;
}
private async Task<bool> IsDoctorRegistered(Guid id, CancellationToken cancellationToken)
{
var doctor = await _doctorRepository.GetByIdAsync(id);
return doctor != null;
}
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken cancellationToken)
{
var patient = await _patientRepository.GetByIdAsync(id);
return patient != null;
}
private bool IsAppointmentValidFormat(DateTime appointment)
{
if (appointment == default(DateTime))
return false;
if (appointment.Date < DateTime.UtcNow.Date)
return false;
return true;
}
private async Task<bool> DoesAppointmentExists(AppointmentManagementDto dto, CancellationToken cancellationToken)
{
return await _appointmentsMongoDbService.DoesAppointmentExists(dto);
}
}
@@ -47,7 +47,7 @@ public class ChatHandler
};
}
var chatId = ChatIdentifier.GenerateChatId(sendMessageDto.Sender, sendMessageDto.Receiver);
var chatId = IdentifierGenerator.GenerateId(sendMessageDto.Sender, sendMessageDto.Receiver);
var criteria = new List<(string, string)>();
criteria.Add(("_id", chatId));
@@ -109,7 +109,7 @@ public class ChatHandler
};
}
var chatId = ChatIdentifier.GenerateChatId(getConversationDto.IdUser1, getConversationDto.IdUser2);
var chatId = IdentifierGenerator.GenerateId(getConversationDto.IdUser1, getConversationDto.IdUser2);
var criteria = new List<(string, string)>();
criteria.Add(("_id", chatId));
@@ -13,7 +13,7 @@ public class DoctorProfileValidation : AbstractValidator<DoctorProfileUpdateDto>
RuleFor(x => x.Id)
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(IsDoctorRegistered).WithMessage("Doctor is registered in system")
.MustAsync(IsDoctorRegistered).WithMessage("Doctor is not registered in system")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.Email)
@@ -40,7 +40,7 @@ public class DoctorProfileValidation : AbstractValidator<DoctorProfileUpdateDto>
private async Task<bool> IsDoctorRegistered(Guid id, CancellationToken cancellationToken)
{
var doctor = await _doctorRepository.GetByIdAsync(id);
return doctor == null;
return doctor != null;
}
private async Task<bool> BeUniqueEmail(string email, CancellationToken cancellationToken)
@@ -1,8 +1,8 @@
namespace Application.Endpoints.Chats;
namespace Application.Endpoints;
public class ChatIdentifier
public class IdentifierGenerator
{
public static string GenerateChatId(Guid id1, Guid id2)
public static string GenerateId(Guid id1, Guid id2)
{
// Convert GUIDs to strings
string strId1 = id1.ToString();
@@ -31,7 +31,7 @@ public class MedicalHistoryFileManagementHandler
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
StatusCode = HttpStatusCodes.NotFound,
Message = "Medical histories not found",
Data = null
};
@@ -13,7 +13,7 @@ public class PatientProfileValidation : AbstractValidator<PatientProfileDto>
RuleFor(x => x.Id)
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(IsPatientRegistered).WithMessage("Patient is registered in system")
.MustAsync(IsPatientRegistered).WithMessage("Patient is not registered in system")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.Email)
@@ -35,8 +35,8 @@ public class PatientProfileValidation : AbstractValidator<PatientProfileDto>
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken cancellationToken)
{
var doctor = await _patientRepository.GetByIdAsync(id);
return doctor == null;
var patient = await _patientRepository.GetByIdAsync(id);
return patient != null;
}
private async Task<bool> BeUniqueEmail(string email, CancellationToken cancellationToken)