public async Task <IActionResult> PostAttachSurveysAsync([FromBody] AttachSurveys command) { await rules .MustBeValidLayoutId() .Apply(command, ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await service.Execute(command); //really should return a 201 created, but we have no API for getting the survey object yet 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 }); } }