medicalHistory: deletion when patient is deleted

This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 22:06:01 +03:00
parent 13bfa2bdd9
commit 7b24c178b3
62 changed files with 321 additions and 356 deletions
@@ -1,7 +1,7 @@
using Application.Endpoints;
using Application.Endpoints.Appointments;
using Application.Services.Database;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
@@ -9,8 +9,8 @@ namespace API.Controllers;
public class AppointmentsController : BaseApiController
{
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
private readonly IPatientRepository _patientRepository;
private readonly IDoctorRepository _doctorRepository;
private readonly IPatientRepository _patientRepository;
public AppointmentsController(IAppointmentsMongoDbService appointmentsMongoDbService,
IPatientRepository patientRepository, IDoctorRepository doctorRepository)
@@ -23,7 +23,8 @@ public class AppointmentsController : BaseApiController
[HttpPost]
public async Task<ActionResult<BaseResponse>> CreateAppointment(AppointmentManagementDto dto)
{
var handler = new AppointmentManagementHandler(_appointmentsMongoDbService, _patientRepository, _doctorRepository);
var handler =
new AppointmentManagementHandler(_appointmentsMongoDbService, _patientRepository, _doctorRepository);
var response = await handler.HandleCreateAppointment(dto).ConfigureAwait(false);
return StatusCode(response.StatusCode, response);
}
@@ -31,7 +32,8 @@ public class AppointmentsController : BaseApiController
[HttpDelete]
public async Task<ActionResult<BaseResponse>> DeleteAppointment(AppointmentManagementDto dto)
{
var handler = new AppointmentManagementHandler(_appointmentsMongoDbService, _patientRepository, _doctorRepository);
var handler =
new AppointmentManagementHandler(_appointmentsMongoDbService, _patientRepository, _doctorRepository);
var response = await handler.HandleDeleteAppointment(dto).ConfigureAwait(false);
return StatusCode(response.StatusCode, response);
}
+4 -4
View File
@@ -1,7 +1,7 @@
using Application.Endpoints;
using Application.Endpoints.Chats;
using Application.Services.Database;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
@@ -9,8 +9,8 @@ namespace API.Controllers;
public class ChatController : BaseApiController
{
private readonly IChatMongoDbService _chatMongoDbService;
private readonly IPatientRepository _patientRepository;
private readonly IDoctorRepository _doctorRepository;
private readonly IPatientRepository _patientRepository;
public ChatController(IChatMongoDbService chatMongoDbService, IPatientRepository patientRepository,
IDoctorRepository doctorRepository)
@@ -19,7 +19,7 @@ public class ChatController : BaseApiController
_patientRepository = patientRepository;
_doctorRepository = doctorRepository;
}
[HttpPost("send_message")]
public async Task<ActionResult<BaseResponse>> SendMessage(SendMessageDto sendMessageDto)
{
@@ -27,7 +27,7 @@ public class ChatController : BaseApiController
var response = await handler.HandleSendMessage(sendMessageDto).ConfigureAwait(false);
return StatusCode(response.StatusCode, response);
}
[HttpPost("get_conversation")]
public async Task<ActionResult<BaseResponse>> GetConversation(GetConversationDto getConversationDto)
{
+18 -30
View File
@@ -3,8 +3,8 @@ using Application.Endpoints.Doctors.Login;
using Application.Endpoints.Doctors.Profile;
using Application.Endpoints.Doctors.Registration;
using Application.Endpoints.Doctors.ResetPassword;
using Application.Services.Database;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Application.Services.HashingAlgorithms;
using Application.Services.Jwt;
using Core.Entities;
@@ -16,10 +16,10 @@ namespace API.Controllers;
[Route("api/[controller]")]
public class DoctorsController : ControllerBase
{
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
private readonly IDoctorRepository _database;
private readonly IHashingAlgorithms _hashingAlgorithms;
private readonly IJwtService _jwtService;
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
public DoctorsController(IDoctorRepository database, IHashingAlgorithms hashingAlgorithms,
IJwtService jwtService, IAppointmentsMongoDbService appointmentsMongoDbService)
@@ -48,11 +48,11 @@ public class DoctorsController : ControllerBase
{
Doctor doctor = (Doctor)response.Data;
var authToken = _jwtService.GenerateJwtToken(doctor.Email);
HttpContext.Response.Headers.Add("Authorization", $"Bearer {authToken}");
}
*/
return StatusCode(response.StatusCode, response);
}
@@ -61,36 +61,30 @@ public class DoctorsController : ControllerBase
{
var authorizationHeader = Request.Headers["Authorization"].FirstOrDefault();
if (string.IsNullOrEmpty(authorizationHeader) || !authorizationHeader.StartsWith("Bearer "))
{
return StatusCode(HttpStatusCodes.BadRequest, new BaseResponse
{
StatusCode = HttpStatusCodes.BadRequest,
StatusCode = HttpStatusCodes.BadRequest,
Message = "Invalid request header format.",
Data = null
});
}
var oldToken = authorizationHeader.Substring("Bearer ".Length).Trim();
if (!_jwtService.ValidateJwtToken(oldToken))
{
return StatusCode(HttpStatusCodes.Unauthorized, new BaseResponse
{
StatusCode = HttpStatusCodes.Unauthorized,
Message = "Invalid JWT token.",
StatusCode = HttpStatusCodes.Unauthorized,
Message = "Invalid JWT token.",
Data = null
});
}
else
var newToken = _jwtService.RefreshToken(oldToken);
return StatusCode(HttpStatusCodes.OK, new BaseResponse
{
var newToken = _jwtService.RefreshToken(oldToken);
return StatusCode(HttpStatusCodes.OK, new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Token refreshed successfully.",
Data = new { Token = newToken }
});
}
StatusCode = HttpStatusCodes.OK,
Message = "Token refreshed successfully.",
Data = new { Token = newToken }
});
}
[HttpPost("reset_password")]
@@ -131,11 +125,8 @@ public class DoctorsController : ControllerBase
var handler = new DoctorProfileHandler(_database, _hashingAlgorithms);
var response = await handler.HandleDelete(id).ConfigureAwait(false);
if (response.StatusCode < HttpStatusCodes.BadRequest)
{
DeleteDoctorAppointments(id);
}
if (response.StatusCode < HttpStatusCodes.BadRequest) DeleteDoctorAppointments(id);
return StatusCode(response.StatusCode, response);
}
@@ -146,10 +137,7 @@ public class DoctorsController : ControllerBase
("DoctorId", doctorId.ToString())
};
var appointments = await _appointmentsMongoDbService.FindAsync<Appointment>(criteria);
if (!appointments.Any())
{
return;
}
if (!appointments.Any()) return;
await _appointmentsMongoDbService.DeleteByIdAsync<Appointment>(appointments[0].Id);
}
}
@@ -1,8 +1,8 @@
using Application.Endpoints;
using Application.Endpoints.MedicalHistories.FileManagement;
using Application.Endpoints.MedicalHistories.ManageAuthorization;
using Application.Services.Database;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
@@ -11,10 +11,10 @@ namespace API.Controllers;
[Route("api/[controller]")]
public class MedicalHistoryController : ControllerBase
{
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
private readonly IMedicalHistoryMongoDbService _medicalHistoryMongoDbService;
private readonly IPatientRepository _patientRepository;
private readonly IDoctorRepository _doctorRepository;
private readonly IMedicalHistoryMongoDbService _medicalHistoryMongoDbService;
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
private readonly IPatientRepository _patientRepository;
public MedicalHistoryController(IMedicalHistoryRepository medicalHistoryRepository,
IPatientRepository patientRepository, IMedicalHistoryMongoDbService mongoDbService,
@@ -81,7 +81,7 @@ public class MedicalHistoryController : ControllerBase
var response = await handler.HandleGrantDoctorAccess(infoDto);
return StatusCode(response.StatusCode, response);
}
[HttpPut("revoke_access")]
public async Task<ActionResult<BaseResponse>> RevokeAccessToMedicalHistory(
MedicalHistoryManageAuthorizationDoctorDto infoDto)
+32 -23
View File
@@ -3,10 +3,9 @@ using Application.Endpoints.Patients.Login;
using Application.Endpoints.Patients.Profile;
using Application.Endpoints.Patients.Registration;
using Application.Endpoints.Patients.ResetPassword;
using Application.Services.Database;
using Application.Services.Database.PostgreSQL;
using Application.Services.HashingAlgorithms;
using Application.Services.Jwt;
using Core.Entities;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
@@ -16,15 +15,17 @@ namespace API.Controllers;
public class PatientsController : ControllerBase
{
private readonly IHashingAlgorithms _hashingAlgorithms;
private readonly IPatientRepository _patientRepository;
private readonly IJwtService _jwtService;
private readonly IMedicalHistoryRepository _medicalHistory;
private readonly IPatientRepository _patientRepository;
public PatientsController(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms,
IJwtService jwtService)
IJwtService jwtService, IMedicalHistoryRepository medicalHistory)
{
_patientRepository = patientRepository;
_hashingAlgorithms = hashingAlgorithms;
_jwtService = jwtService;
_medicalHistory = medicalHistory;
}
[HttpGet]
@@ -53,48 +54,42 @@ public class PatientsController : ControllerBase
{
Patient patient = (Patient)response.Data;
var authToken = _jwtService.GenerateJwtToken(patient.Email);
HttpContext.Response.Headers.Add("Authorization", $"Bearer {authToken}");
}
*/
return StatusCode(response.StatusCode, response);
}
[HttpPost("refresh_token")]
public async Task<ActionResult<BaseResponse>> RefreshToken()
{
var authorizationHeader = Request.Headers["Authorization"].FirstOrDefault();
if (string.IsNullOrEmpty(authorizationHeader) || !authorizationHeader.StartsWith("Bearer "))
{
return StatusCode(HttpStatusCodes.BadRequest, new BaseResponse
{
StatusCode = HttpStatusCodes.BadRequest,
StatusCode = HttpStatusCodes.BadRequest,
Message = "Invalid request header format.",
Data = null
});
}
var oldToken = authorizationHeader.Substring("Bearer ".Length).Trim();
if (!_jwtService.ValidateJwtToken(oldToken))
{
return StatusCode(HttpStatusCodes.Unauthorized, new BaseResponse
{
StatusCode = HttpStatusCodes.Unauthorized,
Message = "Invalid JWT token.",
StatusCode = HttpStatusCodes.Unauthorized,
Message = "Invalid JWT token.",
Data = null
});
}
else
var newToken = _jwtService.RefreshToken(oldToken);
return StatusCode(HttpStatusCodes.OK, new BaseResponse
{
var newToken = _jwtService.RefreshToken(oldToken);
return StatusCode(HttpStatusCodes.OK, new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Token refreshed successfully.",
Data = new { Token = newToken }
});
}
StatusCode = HttpStatusCodes.OK,
Message = "Token refreshed successfully.",
Data = new { Token = newToken }
});
}
[HttpPost("register")]
@@ -126,6 +121,20 @@ public class PatientsController : ControllerBase
{
var handler = new PatientProfileHandler(_patientRepository, _hashingAlgorithms);
var response = await handler.HandleDelete(id).ConfigureAwait(false);
if (response.StatusCode < HttpStatusCodes.BadRequest) DeleteMedicalHistory(id);
return StatusCode(response.StatusCode, response);
}
private async void DeleteMedicalHistory(Guid patientId)
{
var medicalHistoryList = await _medicalHistory.GetAllAsync();
foreach (var med in medicalHistoryList)
if (med.UserId == patientId)
{
await _medicalHistory.DeleteAsync(med);
return;
}
}
}