public async Task <bool> UpdateSample(SampleUpdateDatabaseModel sample, int loggedInUserId) { var storedProcedure = "[dbo].[usp_UpdateSampleWithCin]"; sample.UserId = loggedInUserId; return(await SelectInsertOrUpdateAsync <bool, SampleUpdateDatabaseModel>(storedProcedure, sample)); }
public async Task <bool> ConfirmRequestAsync(AnalysisRequestDataModel request, int loggedInUserId, bool isSamplePriority = false) { RequestDataStatus requestSampleStatus = RequestDataStatus.New; RequestDataStatus patientStatus = RequestDataStatus.New; RequestDataStatus clinicalDetailsStatus = RequestDataStatus.New; int InsertedPatientId = 0; List <TestsModel> TestsToInsert = new List <TestsModel>(); List <TestsModel> TestsToRemove = new List <TestsModel>(); List <ClinicalDetailsDatabaseModel> clinicalDetails = new List <ClinicalDetailsDatabaseModel>(); List <ResultsDatabaseModel> databaseTestsForRequest = new List <ResultsDatabaseModel>(); #region Request Rejection Criteria - Primary //Requests with no Cin are invalid if (string.IsNullOrEmpty(request.Cin)) { throw new ArgumentNullException("CIN", "COVID Identification number cannot be null! Analysis request not saved!"); } //Requests with not tests are not valid. if (request.Tests.Count == 0) { throw new ArgumentException("A minimum of one test is mandatory to confirm the request!"); } //Checking whether the current request exists on the database. If it does, any edits for the request should not change the patient to a new patient. var PatientDataValidityForAnalysisRequest = await IsPatientDifferentForAnalysisRequest(request); //if the data update criteria for existing request fails, throw an exception saying so... if (PatientDataValidityForAnalysisRequest.IsAnalysisRequestsPatientChanged) { throw new Exception("Cannot confirm request. Cannot change patient for a previously confirmed request!\n" + $"Previous details: {request.Cin}: {PatientDataValidityForAnalysisRequest.DatabasePatient.NidPp} ( {PatientDataValidityForAnalysisRequest.DatabasePatient.Fullname} )\n" + $"Current details: {request.Cin}: {request.NationalIdPassport} ( {request.Fullname} )"); } #endregion #region Fetch Present data //load the sample from database, if exists var requestAndSample = await GetSampleByIdAsync(request.Cin); //fetch patient data specific to the now handlng request PatientModel patient = (await patientData.GetPatientByNidPp(request.NationalIdPassport)).FirstOrDefault(); //if the request and sample exist on the database... if (requestAndSample != null) { //Get clinical details saved on the database for request clinicalDetails = await clinicalDetailsData.GetClinicalDetailsByRequestId(requestAndSample.RequestId); //Get requested tests saved on the database databaseTestsForRequest = await GetRequestedTestsByRequestIdAsync(requestAndSample.RequestId); } #endregion #region Determine request, sample and , clinical details status | Determine Tests to insert / delete //Checking whether the current handling request is new, clean, or dirty. requestSampleStatus = AssesRequestAndSampleStatus(requestAndSample, request); //checking whether the patient for the current request is new, clean or dirty. patientStatus = AssesPatientStatus(patient, request); //if the request and sample was previously saved on database.... if (requestAndSample != null) { //determine the status of clinical details... New, Clean or dirty. clinicalDetailsStatus = AssesClinicalDetailStatus(request, clinicalDetails); //Generate a list of tests to insert for the request TestsToInsert = GetTestsToInsert(request.Tests, databaseTestsForRequest); //Make a list of tests for the request that exists on the database but does not exist on the request. //NOTE: If no results exists for these tests, they will be removed. TestsToRemove = GetTestsToRemove(request.Tests, databaseTestsForRequest); } #endregion #region Insert / Update / Delete from database #region Database IO #region insert / update patient if (patientStatus == RequestDataStatus.Dirty) { var patientToUpdate = new PatientUpdateDatabaseModel() { Id = patient.Id, Fullname = request.Fullname, NidPp = request.NationalIdPassport, Birthdate = DateHelper.GetCD4FormatJustDateNoTime(request.Birthdate), GenderId = request.GenderId, AtollId = request.AtollId, CountryId = request.CountryId, Address = request.Address, PhoneNumber = request.PhoneNumber, InstituteAssignedPatientId = request.InstituteAssignedPatientId }; var isPatientUpdated = await patientData.UpdatePatientById(patientToUpdate); if (!isPatientUpdated) { throw new Exception("Cannot update patient data!"); } } if (patientStatus == RequestDataStatus.New) { //insert patient var patientToInsert = new PatientInsertDatabaseModel() { Fullname = request.Fullname, NidPp = request.NationalIdPassport, Birthdate = DateHelper.GetCD4FormatJustDateNoTime(request.Birthdate), GenderId = request.GenderId, AtollId = request.AtollId, CountryId = request.CountryId, Address = request.Address, PhoneNumber = request.PhoneNumber, InstituteAssignedPatientId = request.InstituteAssignedPatientId }; InsertedPatientId = await patientData.InsertPatient(patientToInsert); if (InsertedPatientId == 0) { throw new Exception("An error occured. Cannot insert the patient!"); } } #endregion #region Case New Request, Everything below the tree is new, Insert all if (requestSampleStatus == RequestDataStatus.New) { //IF REQUEST IS NEW:: CLINICAL DETAILS, SAMPLE AND REQUESTED TESTS //WILL BE NEW FOR SURE. SO HANDLE THEM ALL AT THE SAME TIME var output = await InsertNewCompleteRequest(GetPatientId(patient, InsertedPatientId), request, loggedInUserId); await InsertUpdateSamplePriorityAsync(request.Cin, isSamplePriority, loggedInUserId); return(output); } #endregion #region Update request if (requestSampleStatus == RequestDataStatus.Dirty) { var requestToUpdate = new AnalysisRequestUpdateDatabaseModel() { Id = requestAndSample.RequestId, PatientId = patient.Id, EpisodeNumber = request.EpisodeNumber, Age = request.Age }; var isRequestUpdated = await UpdateRequestAsync(requestToUpdate, loggedInUserId); if (!isRequestUpdated) { throw new Exception("Cannot update request data! [ either episode number, age or patient associated with request was not updated ]"); } } #endregion #region Update Sample if (requestSampleStatus == RequestDataStatus.Dirty) { var sampleToUpdate = new SampleUpdateDatabaseModel() { Cin = request.Cin, SiteId = request.SiteId, CollectionDate = request.SampleCollectionDate, ReceivedDate = request.SampleReceivedDate }; var output = await sampleDataAccess.UpdateSample(sampleToUpdate, loggedInUserId); if (!output) { throw new Exception("Cannot sample details. [May include: Site, collected date or received date]. Please verify!"); } } #endregion #region Update Results Table [Test data | NOT ACTUAL SAMPLE RESULTS] if (TestsToInsert.Count > 0 || TestsToRemove.Count > 0) { var output = await resultDataAccess.ManageRequestedTestsDataAsync(TestsToInsert, TestsToRemove, request.Cin, loggedInUserId); if (!output) { throw new Exception("Error adding or removing requested tests!"); } } #endregion #region Sync Clinical details data if (clinicalDetailsStatus == RequestDataStatus.Dirty) { //Sync string csvClinicalDetails = ClinicalDetailsDataAccess.GetCsvClinicalDetails(request.ClinicalDetails); var output = await clinicalDetailsData.SyncClinicalDetails (csvClinicalDetails, requestAndSample.RequestId); if (!output) { throw new Exception("Cannot update clinical details for the request."); } } #endregion #region Update Sample and Test Priority await InsertUpdateSamplePriorityAsync(request.Cin, isSamplePriority, loggedInUserId); #endregion #endregion #endregion return(true); }