/// <summary> /// Prepare the model for the condition /// </summary> private ConditionDetail PrepareConditionDetail(PatientConditionForUpdateDto conditionForUpdate) { var conditionDetail = new ConditionDetail(); conditionDetail.CustomAttributes = _modelExtensionBuilder.BuildModelExtension <PatientCondition>(); //conditionDetail = _mapper.Map<ConditionDetail>(conditionForUpdate); foreach (var newAttribute in conditionForUpdate.Attributes) { var customAttribute = _customAttributeRepository.Get(ca => ca.Id == newAttribute.Key); if (customAttribute != null) { // Validate attribute exists for household entity and is a PMT attribute var attributeDetail = conditionDetail.CustomAttributes.SingleOrDefault(ca => ca.AttributeKey == customAttribute.AttributeKey); if (attributeDetail == null) { ModelState.AddModelError("Message", $"Unable to locate custom attribute on patient condition {newAttribute.Key}"); } else { attributeDetail.Value = newAttribute.Value; } } else { ModelState.AddModelError("Message", $"Unable to locate custom attribute {newAttribute.Key}"); } } // Update patient custom attributes from source return(conditionDetail); }
/// <summary> /// Validate the input model for updating a condition /// </summary> private void ValidateConditionForUpdateModel(Patient patientFromRepo, PatientConditionForUpdateDto conditionForUpdateDto, long patientConditionId) { if (Regex.Matches(conditionForUpdateDto.SourceDescription, @"[-a-zA-Z0-9 .,()']").Count < conditionForUpdateDto.SourceDescription.Length) { ModelState.AddModelError("Message", "Source description contains invalid characters (Enter A-Z, a-z, 0-9, hyphen, space, period, comma, parentheses, apostrophe)"); } if (conditionForUpdateDto.StartDate > DateTime.Today) { ModelState.AddModelError("Message", "Start Date should be before current date"); } if (conditionForUpdateDto.StartDate < patientFromRepo.DateOfBirth) { ModelState.AddModelError("Message", "Start Date should be after Date Of Birth"); } if (conditionForUpdateDto.OutcomeDate.HasValue) { if (conditionForUpdateDto.OutcomeDate > DateTime.Today) { ModelState.AddModelError("Message", "Outcome Date should be before current date"); } if (conditionForUpdateDto.OutcomeDate < patientFromRepo.DateOfBirth) { ModelState.AddModelError("Message", "Outcome Date should be after Date Of Birth"); } if (conditionForUpdateDto.OutcomeDate < conditionForUpdateDto.StartDate) { ModelState.AddModelError("Message", "Outcome Date should be after Start Date"); } } }
public async Task <IActionResult> UpdatePatientCondition(int patientId, int id, [FromBody] PatientConditionForUpdateDto conditionForUpdate) { if (conditionForUpdate == null) { ModelState.AddModelError("Message", "Unable to locate payload for new request"); } var command = new ChangeConditionDetailsCommand(patientId, id, conditionForUpdate.SourceTerminologyMedDraId, conditionForUpdate.StartDate, conditionForUpdate.OutcomeDate, conditionForUpdate.Outcome, conditionForUpdate.TreatmentOutcome, conditionForUpdate.CaseNumber, conditionForUpdate.Comments, conditionForUpdate.Attributes); _logger.LogInformation( "----- Sending command: ChangeConditionDetailsCommand - {patientId}: {patientConditionId}", command.PatientId, command.PatientConditionId); var commandResult = await _mediator.Send(command); if (!commandResult) { return(BadRequest("Command not created")); } return(Ok()); }
public async Task <IActionResult> CreatePatientCondition(int patientId, [FromBody] PatientConditionForUpdateDto conditionForUpdate) { if (conditionForUpdate == null) { ModelState.AddModelError("Message", "Unable to locate payload for new condition"); return(BadRequest(ModelState)); } var patientFromRepo = await _patientRepository.GetAsync(f => f.Id == patientId); if (patientFromRepo == null) { return(NotFound()); } var sourceTermFromRepo = _terminologyMeddraRepository.Get(conditionForUpdate.SourceTerminologyMedDraId); if (sourceTermFromRepo == null) { ModelState.AddModelError("Message", "Unable to locate source term"); } Outcome outcomeFromRepo = null; if (!String.IsNullOrWhiteSpace(conditionForUpdate.Outcome)) { outcomeFromRepo = _outcomeRepository.Get(o => o.Description == conditionForUpdate.Outcome); if (outcomeFromRepo == null) { ModelState.AddModelError("Message", "Unable to locate outcome"); } } TreatmentOutcome treatmentOutcomeFromRepo = null; if (!String.IsNullOrWhiteSpace(conditionForUpdate.TreatmentOutcome)) { treatmentOutcomeFromRepo = _treatmentOutcomeRepository.Get(to => to.Description == conditionForUpdate.TreatmentOutcome); if (treatmentOutcomeFromRepo == null) { ModelState.AddModelError("Message", "Unable to locate treatment outcome"); } } ValidateConditionForUpdateModel(patientFromRepo, conditionForUpdate, 0); // Custom validation if (outcomeFromRepo != null && treatmentOutcomeFromRepo != null) { if (outcomeFromRepo.Description == "Fatal" && treatmentOutcomeFromRepo.Description != "Died") { ModelState.AddModelError("Message", "Treatment Outcome not consistent with Condition Outcome"); } if (outcomeFromRepo.Description != "Fatal" && treatmentOutcomeFromRepo.Description == "Died") { ModelState.AddModelError("Message", "Condition Outcome not consistent with Treatment Outcome"); } } if (ModelState.IsValid) { var conditionDetail = PrepareConditionDetail(conditionForUpdate); if (!conditionDetail.IsValid()) { conditionDetail.InvalidAttributes.ForEach(element => ModelState.AddModelError("Message", element)); } if (ModelState.IsValid) { var patientCondition = patientFromRepo.AddOrUpdatePatientCondition(0, sourceTermFromRepo, conditionForUpdate.StartDate, conditionForUpdate.OutcomeDate, outcomeFromRepo, treatmentOutcomeFromRepo, conditionForUpdate.CaseNumber, conditionForUpdate.Comments, conditionForUpdate.SourceDescription, _patientStatusRepository.Get(ps => ps.Description == "Died")); //throw new Exception(JsonConvert.SerializeObject(patientCondition)); _modelExtensionBuilder.UpdateExtendable(patientCondition, conditionDetail.CustomAttributes, "Admin"); _patientConditionRepository.Save(patientCondition); await _unitOfWork.CompleteAsync(); var mappedPatientCondition = _mapper.Map <PatientConditionIdentifierDto>(patientCondition); if (mappedPatientCondition == null) { return(StatusCode(500, "Unable to locate newly added condition")); } return(CreatedAtAction("GetPatientConditionByIdentifier", new { id = mappedPatientCondition.Id }, CreateLinksForPatientCondition <PatientConditionIdentifierDto>(mappedPatientCondition))); } } return(BadRequest(ModelState)); }