public async Task <IActionResult> UpdateOutbreak([FromBody] UpdateOutbreak command) { await rules .RequireSettingInformation() .RequireDocumentsWhenAfterActionReportCompleted() .RequireModeOfTransmission() .RequireHumanSpecimenConfirmation() .Apply(command, ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await service.Execute(command); return(Ok()); }
private async Task SaveOutbreak(UpdateOutbreak command) { var outbreak = await writeContext.Outbreak .Include(ob => ob.OutbreakEvents) .Include(ob => ob.OutbreakSymptoms) .FirstAsync(ob => ob.IdOutbreak == command.OutbreakId); using (var transaction = writeContext.Database.BeginTransaction()) { //calculate the event date outbreak.DtOutbreakEvent = new DateTime?[] { outbreak.DtEarliestOnset, outbreak.DtEarliestExposure, outbreak.DtChdNotified, outbreak.DtOutbreakEvent } .Where(d => d.HasValue) .OrderBy(d => d) .First(); //so all events have the same timestamp var eventDate = DateTime.Now; await ReplaceEvents(outbreak, OutbreakEventType.Investigation, command.InvestigationMethods, eventDate, CodeType.OutbreakInvestigation); await ReplaceEvents(outbreak, OutbreakEventType.StudyDesign, command.StudyDesigns, eventDate, CodeType.OutbreakStudyDesign); await ReplaceEvents(outbreak, OutbreakEventType.MultiCounty, command.OtherCountiesList, eventDate, CodeType.County); await ReplaceEvents(outbreak, OutbreakEventType.MultiCountry, command.OtherCountriesList, eventDate, CodeType.Country, CodeType.Region); await ReplaceEvents(outbreak, OutbreakEventType.MultiState, command.OtherStatesList, eventDate, CodeType.State); await ReplaceEvents(outbreak, OutbreakEventType.Regulatory, command.RegulatoryAgencies, eventDate, CodeType.OutbreakRegulatory); await CreateEvent(outbreak, OutbreakEventType.CaseDefinition, command.CaseDefinition, eventDate); await CreateEvent(outbreak, OutbreakEventType.LabFindings, command.LabFindings, eventDate); await CreateEvent(outbreak, OutbreakEventType.Closure, command.Decisions, eventDate, OutbreakEventType.ConclusionNotes); await CreateEvent(outbreak, OutbreakEventType.Closure, command.Recommendations, eventDate, OutbreakEventType.Recommendations); await CreateEvent(outbreak, OutbreakEventType.ProcessImprovements, command.ImprovementAreas, eventDate); await CreateEvent(outbreak, OutbreakEventType.EpiComPostId, command.PostId.ToString(), eventDate); ReplaceSymptoms(outbreak, command.Symptom, command.OtherSymptom); outbreak.NmOutbreak = command.EventName; //if ICD9 changes update all associated cases if (outbreak.CdIcd9 != command.DiseaseHazard) { string sql = "update dbo.epi_case set CD_ICD9 = @icd9 where id_outbreak = @outbreakId"; var connection = writeContext.Database.GetDbConnection().EnsureOpen(); await connection.ExecuteAsync(sql, new { icd9 = command.DiseaseHazard, outbreakId = command.OutbreakId }, transaction.GetDbTransaction()); outbreak.CdIcd9 = command.DiseaseHazard; //only need to ensure a survey is created/exists when ICD9 changes await EnsureOutbreakSurvey(outbreak); } outbreak.CdCountyInitiating = command.County; outbreak.InMultiCounty = command.OtherCountiesAffected; outbreak.InMultiState = command.OtherStatesAffected; outbreak.DsStateConsulted = command.StaffConsulted; outbreak.NmInvestigator = command.Investigator; outbreak.CdModeTransmission = command.TransmissionMode; outbreak.InFoodWater = command.FoodOrWaterRelated; outbreak.InVehicleFw = command.IsVehicleIdentified; outbreak.DsVehicleFw = command.Vehicle; outbreak.InHealthcare = command.HealthRelated; outbreak.CdVehicleIdentified = command.CauseForOutbreak; outbreak.DsVehicleHai = command.Description; outbreak.CdModeTransmission = command.TransmissionMode; outbreak.InFoodWater = command.FoodOrWaterRelated; outbreak.InVehicleFw = command.IsVehicleIdentified; outbreak.DsVehicleFw = command.Vehicle; outbreak.InHealthcare = command.HealthRelated; outbreak.CdVehicleIdentified = command.CauseForOutbreak; outbreak.DsVehicleHai = command.Description; outbreak.CdErDeptVisitsType = command.EmergencyVisitsType; outbreak.AmErDeptVisits = command.EmergencyVisits; outbreak.CdInpatientHosptialType = command.InpatientHospitalizationsType; outbreak.AmInpatientHospital = command.InpatientHospitalizations; outbreak.AmDeaths = command.Deaths; outbreak.CdDeathsType = command.DeathsType; outbreak.DtChdNotified = command.NotifiedDate; outbreak.CdReporterType = command.FirstNotified; outbreak.NmReporter = command.ReporterName ?? string.Empty; outbreak.DtInvestigated = command.InvestigationStarted; outbreak.DtClosed = command.InvestigationClosed; outbreak.CdStatus = outbreak.DtClosed != null ? "CLOSED" : "OPEN"; outbreak.CdSyndromes = command.Syndrome; outbreak.DsSyndromeOther = command.OtherSyndrome; outbreak.DsDiseaseHazardOther = command.OtherDiseaseHazard; outbreak.AmEstimatedIll = command.EstimatedNumber.GetValueOrDefault(); outbreak.InInvestigated = command.IsInvestigated; outbreak.InOutbreak = command.IsOutbreak; outbreak.InRecommendations = command.IsRecProvided; outbreak.CdRecommendationsHow = command.MethodofRec; //TODO: this does not look correct outbreak.InImplemented = command.WereRecommendationsImplemented; outbreak.InInternalAction = command.IsReportCompleted; outbreak.DtEarliestExposure = command.FirstExposureDate; outbreak.DtLastExposure = command.LastExposureDate; outbreak.DtEarliestOnset = command.FirstOnsetDate; outbreak.DtLastOnset = command.LastOnsetDate; outbreak.AmMedianDuration = command.Duration; outbreak.CdMedianDurationUnit = command.TimeUnit; outbreak.InLabConfirmed = command.IsHumanSpecimens; outbreak.InLabConducted = command.IsLabTestingConducted; outbreak.AmLabConfirmed = (short?)command.NoOfCases; //TODO: verify this does not look correct outbreak.InLabConfirmedFood = command.IsFoodSpecimens; outbreak.AmTotalCases = command.TotalCases; outbreak.CdTotalCaseType = command.TotalCaseType; outbreak.AmNonStaffCases = command.NonStaffCases; outbreak.AmStaffCases = command.StaffCases; outbreak.AmUnknownCases = command.UnknownCases; if (!string.IsNullOrWhiteSpace(command.Comments)) { await CreateEvent(outbreak, OutbreakEventType.Review, command.Comments, eventDate); } if (outbreak.CdReviewStatus != command.ReviewStatus) { outbreak.IdReviewer = usernameProvider.GetUsername(); outbreak.DtReview = DateTime.Now; outbreak.CdReviewStatus = command.ReviewStatus; } await writeContext.SaveChangesAsync(); transaction.Commit(); } //save survey //NOTE: This is not transacted with outbreak save //if can be, but it requires detecting a current pending transaction inside survey answer save if (command.SurveyUid != null) { await surveyService.Execute(new SaveSurveyAnswers { OutbreakId = outbreak.IdOutbreak, Answers = command.SurveyAnswers, SurveyId = command.SurveyUid.Value }); } }
public async Task Execute(UpdateOutbreak command) { await SaveOutbreak(command); ///**********Results****************/ //outbreak.AmTotalCases = command.Results.TotalCaseResults.TotalCases; //outbreak.AmTotalExposed = command.Results.TotalCaseResults.TotalExposed; //outbreak.AmMale = command.Results.GenderResults.MaleCases; //outbreak.AmFemale = command.Results.GenderResults.FemaleCases; //outbreak.AmGenderUnknown = command.Results.GenderResults.UnknownGenderCases; //outbreak.AmStaffCases = command.Results.StaffExposureResults.StaffCases; //outbreak.AmStaffExposed = command.Results.StaffExposureResults.StaffExposed; //outbreak.AmNonStaffCases = command.Results.StaffExposureResults.NonstaffCases; //outbreak.AmNonStaffExposed = command.Results.StaffExposureResults.NonstaffExposed; //outbreak.AmUnknownCases = command.Results.StaffExposureResults.UnknownCases; //outbreak.AmUnknownExposed = command.Results.StaffExposureResults.UnknownExposed; //outbreak.AmAgeLess1 = command.Results.AgeResults.AgeLessthan1; //outbreak.AmAge14 = command.Results.AgeResults.Age1to4; //outbreak.AmAge59 = command.Results.AgeResults.Age5to9; //outbreak.AmAge1019 = command.Results.AgeResults.Age10to19; //outbreak.AmAge2049 = command.Results.AgeResults.Age20to49; //outbreak.AmAge5074 = command.Results.AgeResults.Age50to74; //outbreak.AmAgeGreater74 = command.Results.AgeResults.AgeGreater74; //outbreak.AmAgeUnknown = command.Results.AgeResults.AgeUnknown; //outbreak.AmWithInfoSought = command.Results.OutComeResults.InfoAvailable; //outbreak.AmSoughtHealthcare = command.Results.OutComeResults.SoughtCases; //outbreak.AmErVisit = command.Results.OutComeResults.ErCases; //outbreak.AmHospitalization = command.Results.OutComeResults.InpatientCases; //outbreak.AmDied = command.Results.OutComeResults.DiedCases; //outbreak.AmWithInfoSought = command.Results.OutComeResults.SoughtCasesWithInfo; //outbreak.AmWithInfoErVisit = command.Results.OutComeResults.ErCasesWithInfo; //outbreak.AmWithInfoHospitalization = command.Results.OutComeResults.InpatientCasesWithInfo; //outbreak.AmWithInfoDied = command.Results.OutComeResults.DiedCasesWithInfo; ///**********Clinical results****************/ //var firstOnsetDateOrMaxValue = String.IsNullOrEmpty(command.ClinicalResults.FirstOnsetDate) ? DateTime.MaxValue : Convert.ToDateTime(command.ClinicalResults.FirstOnsetDate); //var firstExposureDateOrMaxValue = String.IsNullOrEmpty(command.ClinicalResults.FirstExposureDate) ? DateTime.MaxValue : Convert.ToDateTime(command.ClinicalResults.FirstExposureDate); //var notifiedDate = Convert.ToDateTime(command.Background.NotifiedDate); //var minDate = new DateTime(Math.Min(((DateTime)firstExposureDateOrMaxValue).Ticks, (new DateTime(Math.Min(((DateTime)firstOnsetDateOrMaxValue).Ticks, ((DateTime)notifiedDate).Ticks))).Ticks)); //bool noOutbreakLevelSurveyExists = !writeContext.Survey // .Any(survey => survey.CdSurveyType == "OUTBREAK_LEVEL" // && survey.IdOutbreak.Equals(command.OutbreakId) // && (survey.IdSurveyVersion == 1)); //bool diseaseCodeExists = !String.IsNullOrEmpty(command.Background.DiseaseHazard); //if (noOutbreakLevelSurveyExists && diseaseCodeExists) //{ // var existingOutbreakTemplateSurvey = await writeContext.Survey // .Where(survey => survey.CdSurveyType.Equals("OUTBREAK_TEMPLATE") // && survey.CdIcd9.Equals(command.Background.DiseaseHazard) // && (survey.DtEffective <= minDate) // && (survey.IdSurveyVersion == 1)) // .FirstOrDefaultAsync(); // if (existingOutbreakTemplateSurvey != null) // { // var newSurveyId = (int)await sequenceGenerator.GetNextAsync(SequenceType.Survey); // Merlin.Core.Data.Survey newSurvey = new Merlin.Core.Data.Survey // { // IdSurvey = newSurveyId, // NmSurvey = existingOutbreakTemplateSurvey.NmSurvey + " " + command.Background.EventName, // IdOutbreak = command.OutbreakId, // DtEffective = minDate, // IdAdded = command.IdUser, // DtAdded = DateTime.Now, // UidLayout = existingOutbreakTemplateSurvey.UidLayout, // CdSurveyType = "OUTBREAK_LEVEL", // IdSurveyVersion = 1, // UID = Guid.NewGuid() // }; // writeContext.Survey.Add(newSurvey); // } //} ///**********EpiCom****************/ ////EpiCom post submitted separately //if (command.Epicom.PostId.HasValue) //{ // var currentPostId = await writeContext.OutbreakEvents // .Where(x => x.CdEventType.Equals("EPICOM_POST_ID") && x.IdOutbreak == command.OutbreakId) // .OrderByDescending(x => x.IdEvent) // .Select(x => x.DsDesc) // .FirstOrDefaultAsync(); // if (!command.Epicom.PostId.Equals(currentPostId)) // { // EventId = (int)await sequenceGenerator.GetNextAsync(SequenceType.OutbreakEventId); // OutbreakEvents newEvent = new OutbreakEvents // { // IdOutbreak = command.OutbreakId, // CdEventType = "EPICOM_POST_ID", // IdEvent = EventId, // CdSubType = "", // DsDesc = command.Epicom.PostId.ToString(), // IdSequence = 0, // IdAdded = command.Epicom.Author // }; // writeContext.OutbreakEvents.Add(newEvent); // } //} }