public async Task <PatientClinicalEventIdentifierDto> Handle(AddClinicalEventToPatientCommand message, CancellationToken cancellationToken) { var patientFromRepo = await _patientRepository.GetAsync(f => f.Id == message.PatientId, new string[] { "PatientClinicalEvents.SourceTerminologyMedDra", "PatientMedications.Concept", "PatientFacilities.Facility" }); if (patientFromRepo == null) { throw new KeyNotFoundException("Unable to locate patient"); } TerminologyMedDra sourceTermFromRepo = null; if (message.SourceTerminologyMedDraId.HasValue) { if (message.SourceTerminologyMedDraId > 0) { sourceTermFromRepo = await _terminologyMeddraRepository.GetAsync(message.SourceTerminologyMedDraId);; if (sourceTermFromRepo == null) { throw new KeyNotFoundException("Unable to locate terminology for MedDRA"); } } } var clinicalEventDetail = await PrepareClinicalEventDetailAsync(message.Attributes); if (!clinicalEventDetail.IsValid()) { clinicalEventDetail.InvalidAttributes.ForEach(element => throw new DomainException(element)); } var newPatientClinicalEvent = patientFromRepo.AddClinicalEvent(message.OnsetDate, message.ResolutionDate, sourceTermFromRepo, message.SourceDescription); _modelExtensionBuilder.UpdateExtendable(newPatientClinicalEvent, clinicalEventDetail.CustomAttributes, "Admin"); _patientRepository.Update(patientFromRepo); // TODO Move to domain event await _workFlowService.CreateWorkFlowInstanceAsync( workFlowName : "New Active Surveilliance Report", contextGuid : newPatientClinicalEvent.PatientClinicalEventGuid, patientIdentifier : String.IsNullOrWhiteSpace(message.PatientIdentifier)?patientFromRepo.FullName : $"{patientFromRepo.FullName} ({message.PatientIdentifier})", sourceIdentifier : newPatientClinicalEvent.SourceTerminologyMedDra?.DisplayName ?? newPatientClinicalEvent.SourceDescription, facilityIdentifier : patientFromRepo.CurrentFacilityCode); await LinkMedicationsToClinicalEvent(patientFromRepo, newPatientClinicalEvent.OnsetDate, newPatientClinicalEvent.PatientClinicalEventGuid); await _unitOfWork.CompleteAsync(); _logger.LogInformation($"----- Clinical Event {message.SourceDescription} created"); var mappedPatientClinicalEvent = _mapper.Map <PatientClinicalEventIdentifierDto>(newPatientClinicalEvent); return(CreateLinks(mappedPatientClinicalEvent)); }