api cu adaugare/revocare acces

This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 04:02:36 +03:00
parent 9334fa50cb
commit 48eb2adcbd
327 changed files with 261 additions and 98308 deletions
@@ -1,4 +1,4 @@
namespace Application.Endpoints.MedicalHistories;
namespace Application.Endpoints.MedicalHistories.FileManagement;
public class MedicalHistoryCreateDto
{
@@ -1,7 +1,7 @@
using Application.Services.Database;
using FluentValidation;
namespace Application.Endpoints.MedicalHistories;
namespace Application.Endpoints.MedicalHistories.FileManagement;
public class MedicalHistoryCreateValidation : AbstractValidator<MedicalHistoryCreateDto>
{
@@ -2,15 +2,15 @@
using Application.Services.Database.MongoDB;
using Core.Entities;
namespace Application.Endpoints.MedicalHistories;
namespace Application.Endpoints.MedicalHistories.FileManagement;
public class MedicalHistoryHandler
public class MedicalHistoryFileManagementHandler
{
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
private readonly IMedicalHistoryMongoDbService _medicalHistoryMongoDbService;
private readonly IPatientRepository _patientRepository;
public MedicalHistoryHandler(IMedicalHistoryRepository medicalHistoryRepository,
public MedicalHistoryFileManagementHandler(IMedicalHistoryRepository medicalHistoryRepository,
IPatientRepository patientRepository, IMedicalHistoryMongoDbService medicalHistoryMongoDbService)
{
_medicalHistoryRepository = medicalHistoryRepository;
@@ -80,7 +80,7 @@ public class MedicalHistoryHandler
UserId = medicalHistoryCreateDto.UserId,
Content = medicalHistoryCreateDto.Content
};
var medicalHistoryId = medicalHistory.Id;
var newMedicalHistory = new MedicalHistoryAuthorisationModel
{
@@ -1,4 +1,4 @@
namespace Application.Endpoints.MedicalHistories;
namespace Application.Endpoints.MedicalHistories.FileManagement;
public class MedicalHistoryUpdateDto
{
@@ -1,7 +1,7 @@
using Application.Services.Database;
using FluentValidation;
namespace Application.Endpoints.MedicalHistories;
namespace Application.Endpoints.MedicalHistories.FileManagement;
public class MedicalHistoryUpdateValidation : AbstractValidator<MedicalHistoryUpdateDto>
{
@@ -0,0 +1,7 @@
namespace Application.Endpoints.MedicalHistories.ManageAuthorization;
public class MedicalHistoryManageAuthorizationDoctorDto
{
public Guid MedicalRecordId { get; set; }
public Guid DoctorId { get; set; }
}
@@ -0,0 +1,143 @@
using Application.Services.Database;
using Application.Services.Database.MongoDB;
using Core.Entities;
namespace Application.Endpoints.MedicalHistories.ManageAuthorization;
public class MedicalHistoryManageAuthorizationHandler
{
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
private readonly IDoctorRepository _doctorRepository;
private readonly IMedicalHistoryMongoDbService _medicalHistoryMongoDbService;
public MedicalHistoryManageAuthorizationHandler(IMedicalHistoryRepository medicalHistoryRepository,
IMedicalHistoryMongoDbService medicalHistoryMongoDbService, IDoctorRepository doctorRepository)
{
_medicalHistoryRepository = medicalHistoryRepository;
_medicalHistoryMongoDbService = medicalHistoryMongoDbService;
_doctorRepository = doctorRepository;
}
public async Task<BaseResponse> HandleGrantDoctorAccess(MedicalHistoryManageAuthorizationDoctorDto infoDto)
{
var validation = new MedicalHistoryManageAuthorizationValidation(_medicalHistoryRepository, _doctorRepository);
var validationResult = await validation.ValidateAsync(infoDto);
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 criteria = new List<(string, string)>();
criteria.Add(("_id", infoDto.MedicalRecordId.ToString()));
var documents = await _medicalHistoryMongoDbService.FindAsync<MedicalHistoryAuthorisationModel>(criteria);
if (!documents.Any())
{
return new BaseResponse()
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Access to medical history not found.",
Data = null
};
}
var authorisations = documents[0].Authorisation;
if (authorisations.Contains(infoDto.ToString()))
{
return new BaseResponse()
{
StatusCode = HttpStatusCodes.Conflict,
Message = "Access to medical history already granted.",
Data = null
};
}
authorisations.Add(infoDto.DoctorId.ToString());
var authorizationModel = new MedicalHistoryAuthorisationModel()
{
Id = infoDto.MedicalRecordId.ToString(),
Authorisation = authorisations
};
await _medicalHistoryMongoDbService.ModifyAsync("_id", infoDto.MedicalRecordId.ToString(), authorizationModel);
return new BaseResponse()
{
StatusCode = HttpStatusCodes.OK,
Message = "Access to medical history granted.",
Data = null
};
}
public async Task<BaseResponse> HandleRevokeDoctorAccess(MedicalHistoryManageAuthorizationDoctorDto infoDto)
{
var validation = new MedicalHistoryManageAuthorizationValidation(_medicalHistoryRepository, _doctorRepository);
var validationResult = await validation.ValidateAsync(infoDto);
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 criteria = new List<(string, string)>();
criteria.Add(("_id", infoDto.MedicalRecordId.ToString()));
var documents = await _medicalHistoryMongoDbService.FindAsync<MedicalHistoryAuthorisationModel>(criteria);
if (!documents.Any())
{
return new BaseResponse()
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Access to medical history not found.",
Data = null
};
}
var authorisations = documents[0].Authorisation;
Console.WriteLine(authorisations);
if (!authorisations.Contains(infoDto.DoctorId.ToString()))
{
return new BaseResponse()
{
StatusCode = HttpStatusCodes.Conflict,
Message = "Access to medical history already revoked.",
Data = null
};
}
authorisations.Remove(infoDto.DoctorId.ToString());
var authorizationModel = new MedicalHistoryAuthorisationModel()
{
Id = infoDto.MedicalRecordId.ToString(),
Authorisation = authorisations
};
await _medicalHistoryMongoDbService.ModifyAsync("_id", infoDto.MedicalRecordId.ToString(), authorizationModel);
return new BaseResponse()
{
StatusCode = HttpStatusCodes.OK,
Message = "Access to medical history granted.",
Data = null
};
}
}
@@ -0,0 +1,39 @@
using Application.Services.Database;
using FluentValidation;
namespace Application.Endpoints.MedicalHistories.ManageAuthorization;
public class MedicalHistoryManageAuthorizationValidation : AbstractValidator<MedicalHistoryManageAuthorizationDoctorDto>
{
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
private readonly IDoctorRepository _doctorRepository;
public MedicalHistoryManageAuthorizationValidation(IMedicalHistoryRepository medicalHistoryRepository,
IDoctorRepository doctorRepository)
{
RuleFor(x => x.MedicalRecordId)
.NotEmpty().WithMessage("Id is required").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(BeExistingMedicalHistoryRecord).WithMessage("Medical history record does not exist")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
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());
_medicalHistoryRepository = medicalHistoryRepository;
_doctorRepository = doctorRepository;
}
private async Task<bool> BeExistingMedicalHistoryRecord(Guid guid, CancellationToken token)
{
var record = await _medicalHistoryRepository.GetByIdAsync(guid);
return record != null;
}
private async Task<bool> IsDoctorRegistered(Guid id, CancellationToken cancellationToken)
{
var doctor = await _doctorRepository.GetByIdAsync(id);
return doctor != null;
}
}
@@ -5,13 +5,13 @@ namespace Application.Services.Database.MongoDB;
public interface IMedicalHistoryMongoDbService
{
IMongoCollection<MedicalHistoryAuthorisationModel> GetCollection<MedicalHistoryAuthorisationModel>();
IMongoCollection<T> GetCollection<T>();
Task<List<MedicalHistoryAuthorisationModel>> FindAsync<MedicalHistoryAuthorisationModel>(List<(string FieldName, string Value)> criteria);
Task<List<T>> FindAsync<T>(List<(string FieldName, string Value)> criteria);
Task AddAsync<MedicalHistoryAuthorisationModel>(MedicalHistoryAuthorisationModel document);
Task AddAsync<T>(T document);
Task ModifyAsync<MedicalHistoryAuthorisationModel>(string keyField, string keyValue, MedicalHistoryAuthorisationModel document);
Task ModifyAsync<T>(string keyField, string keyValue, T document);
Task DeleteAsync<MedicalHistoryAuthorisationModel>(string keyField, string keyValue);
Task DeleteAsync<T>(string keyField, string keyValue);
}
Binary file not shown.
Binary file not shown.
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Application")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+61a55fa7353346bdad2d677f0ec3c044c3aa87d5")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+b4eca98e0a616e4db63ed57610d69f27f43f4765")]
[assembly: System.Reflection.AssemblyProductAttribute("Application")]
[assembly: System.Reflection.AssemblyTitleAttribute("Application")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
ca8d9dbfc6e022b60b63524e70a3acb8ce1eaa86a2b066fce9c4864553b6125d
78ff498bf54e9d999affcd9081c61dde8845fd1e78f6485753e0ca4436dc7cdc
@@ -1 +1 @@
e99f7362e6da84368608067cdbb0c281eef010bca303f59d33c712edf6a9964f
003e629f8ad4defbd9bc4b4a250b4753763e3945de1c7fb70b833ff828554693
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/61a55fa7353346bdad2d677f0ec3c044c3aa87d5/*"}}
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/b4eca98e0a616e4db63ed57610d69f27f43f4765/*"}}