private async Task UpdateHealthRisks(Project projectToUpdate, ProjectRequestDto projectRequestDto) { var projectHealthRiskIdsFromDto = projectRequestDto.HealthRisks.Where(ar => ar.Id.HasValue).Select(ar => ar.Id.Value).ToList(); var projectHealthRisksToDelete = await _nyssContext.ProjectHealthRisks .Where(phr => phr.Project.Id == projectToUpdate.Id && !projectHealthRiskIdsFromDto.Contains(phr.Id)) .Select(phr => new { ProjectHealthRisk = phr, ReportCount = phr.Reports.Count }) .ToListAsync(); if (projectHealthRisksToDelete.Any(phr => phr.ReportCount > 0)) { throw new ResultException(ResultKey.Project.HealthRiskContainsReports); } _nyssContext.ProjectHealthRisks.RemoveRange(projectHealthRisksToDelete.Select(phr => phr.ProjectHealthRisk)); var projectHealthRisksToAdd = projectRequestDto.HealthRisks.Where(ar => ar.Id == null); foreach (var projectHealthRisk in projectHealthRisksToAdd) { var projectHealthRiskToAdd = new ProjectHealthRisk { HealthRiskId = projectHealthRisk.HealthRiskId, CaseDefinition = projectHealthRisk.CaseDefinition, FeedbackMessage = projectHealthRisk.FeedbackMessage, AlertRule = new AlertRule { //ToDo: make CountThreshold nullable or change validation CountThreshold = projectHealthRisk.AlertRuleCountThreshold ?? 0, DaysThreshold = projectHealthRisk.AlertRuleDaysThreshold, KilometersThreshold = projectHealthRisk.AlertRuleKilometersThreshold } }; projectToUpdate.ProjectHealthRisks.Add(projectHealthRiskToAdd); } var projectHealthRisksToUpdate = projectRequestDto.HealthRisks.Where(ar => ar.Id.HasValue); foreach (var projectHealthRisk in projectHealthRisksToUpdate) { var projectHealthRiskToUpdate = projectToUpdate.ProjectHealthRisks.FirstOrDefault(ar => ar.Id == projectHealthRisk.Id.Value); if (projectHealthRiskToUpdate != null) { projectHealthRiskToUpdate.CaseDefinition = projectHealthRisk.CaseDefinition; projectHealthRiskToUpdate.FeedbackMessage = projectHealthRisk.FeedbackMessage; //ToDo: make CountThreshold nullable or change validation projectHealthRiskToUpdate.AlertRule.CountThreshold = projectHealthRisk.AlertRuleCountThreshold ?? 0; projectHealthRiskToUpdate.AlertRule.DaysThreshold = projectHealthRisk.AlertRuleDaysThreshold; projectHealthRiskToUpdate.AlertRule.KilometersThreshold = projectHealthRisk.AlertRuleKilometersThreshold; } } }
private async Task <Alert> CreateNewAlert(ProjectHealthRisk projectHealthRisk) { var newAlert = new Alert { ProjectHealthRisk = projectHealthRisk, CreatedAt = DateTime.UtcNow, Status = AlertStatus.Pending }; await _nyssContext.Alerts.AddAsync(newAlert); return(newAlert); }
public Report CreateNewReport(ProjectHealthRisk projectHealthRisk, DataCollector dataCollector, bool isTraining = false, Point location = null, DateTime?receivedAt = null) { return(new Report { Id = _numerator.Next, Status = ReportStatus.New, ProjectHealthRisk = projectHealthRisk, DataCollector = dataCollector, IsTraining = isTraining, ReceivedAt = receivedAt ?? default, Location = location });
public LabeledReportGroup AddReport(ReportStatus status, ProjectHealthRisk projectHealthRisk, DataCollector dataCollector, bool isTraining = false, Point location = null, DateTime?receivedAt = null, Village village = null) { Reports.Add(new Report { Id = _reportNumerator.Next, ReportGroupLabel = _label, Status = status, ProjectHealthRisk = projectHealthRisk, DataCollector = dataCollector, IsTraining = isTraining, ReceivedAt = receivedAt ?? default, Location = location, RawReport = new RawReport { Village = village ?? default } });
public async Task Handle(string queryString) { var parsedQueryString = HttpUtility.ParseQueryString(queryString); var sender = parsedQueryString[SenderParameterName]; var timestamp = parsedQueryString[TimestampParameterName]; var text = parsedQueryString[TextParameterName]; var incomingMessageId = parsedQueryString[IncomingMessageIdParameterName].ParseToNullableInt(); var outgoingMessageId = parsedQueryString[OutgoingMessageIdParameterName].ParseToNullableInt(); var modemNumber = parsedQueryString[ModemNumberParameterName].ParseToNullableInt(); var apiKey = parsedQueryString[ApiKeyParameterName]; ErrorReportData reportErrorData = null; try { Alert triggeredAlert = null; ProjectHealthRisk projectHealthRisk = null; GatewaySetting gatewaySetting = null; using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var rawReport = new RawReport { Sender = sender, Timestamp = timestamp, ReceivedAt = _dateTimeProvider.UtcNow, Text = text, IncomingMessageId = incomingMessageId, OutgoingMessageId = outgoingMessageId, ModemNumber = modemNumber, ApiKey = apiKey }; await _nyssContext.AddAsync(rawReport); var reportValidationResult = await ParseAndValidateReport(rawReport, parsedQueryString); if (reportValidationResult.IsSuccess) { gatewaySetting = reportValidationResult.GatewaySetting; projectHealthRisk = reportValidationResult.ReportData.ProjectHealthRisk; var epiDate = _dateTimeProvider.GetEpiDate(reportValidationResult.ReportData.ReceivedAt); var report = new Report { IsTraining = reportValidationResult.ReportData.DataCollector.IsInTrainingMode, ReportType = reportValidationResult.ReportData.ParsedReport.ReportType, Status = ReportStatus.New, ReceivedAt = reportValidationResult.ReportData.ReceivedAt, CreatedAt = _dateTimeProvider.UtcNow, DataCollector = reportValidationResult.ReportData.DataCollector, EpiWeek = epiDate.EpiWeek, EpiYear = epiDate.EpiYear, PhoneNumber = sender, Location = reportValidationResult.ReportData.DataCollector.Location, ReportedCase = reportValidationResult.ReportData.ParsedReport.ReportedCase, KeptCase = new ReportCase { CountMalesBelowFive = null, CountMalesAtLeastFive = null, CountFemalesBelowFive = null, CountFemalesAtLeastFive = null }, DataCollectionPointCase = reportValidationResult.ReportData.ParsedReport.DataCollectionPointCase, ProjectHealthRisk = projectHealthRisk, ReportedCaseCount = projectHealthRisk.HealthRisk.HealthRiskType == HealthRiskType.Human ? (reportValidationResult.ReportData.ParsedReport.ReportedCase.CountFemalesAtLeastFive ?? 0) + (reportValidationResult.ReportData.ParsedReport.ReportedCase.CountFemalesBelowFive ?? 0) + (reportValidationResult.ReportData.ParsedReport.ReportedCase.CountMalesAtLeastFive ?? 0) + (reportValidationResult.ReportData.ParsedReport.ReportedCase.CountMalesBelowFive ?? 0) : 1 }; rawReport.Report = report; await _nyssContext.Reports.AddAsync(report); triggeredAlert = await _alertService.ReportAdded(report); } else { reportErrorData = reportValidationResult.ErrorReportData; gatewaySetting = reportValidationResult.GatewaySetting; } await _nyssContext.SaveChangesAsync(); transactionScope.Complete(); } if (reportErrorData == null) { if (!string.IsNullOrEmpty(gatewaySetting?.EmailAddress) && projectHealthRisk != null) { var recipients = new List <string> { sender }; await _queuePublisherService.SendSMSesViaEagle(gatewaySetting.EmailAddress, gatewaySetting.Name, recipients, projectHealthRisk.FeedbackMessage); } if (triggeredAlert != null) { await _alertService.SendNotificationsForNewAlert(triggeredAlert, gatewaySetting); } } else { await SendFeedbackOnError(reportErrorData, gatewaySetting); } } catch (ReportValidationException e) { _loggerAdapter.Warn(e.Message); } }
private static List <Report> BuildReports(DataCollector dataCollector, List <int> ids, ProjectHealthRisk projectHealthRisk, bool?isTraining = false) { var reports = ids .Select(i => new Report { Id = i, DataCollector = dataCollector, Status = ReportStatus.Pending, ProjectHealthRisk = projectHealthRisk, ReportedCase = new ReportCase(), DataCollectionPointCase = new DataCollectionPointCase(), CreatedAt = new DateTime(2020, 1, 1), IsTraining = isTraining ?? false, ReportType = dataCollector.DataCollectorType == DataCollectorType.CollectionPoint ? ReportType.DataCollectionPoint : ReportType.Single }) .ToList(); return(reports); }