示例#1
0
        private async Task SendFeedbackOnError(ErrorReportData errorReport, GatewaySetting gatewaySetting)
        {
            if (gatewaySetting != null && !string.IsNullOrEmpty(errorReport.Sender))
            {
                var feedbackMessage = errorReport.ReportErrorType switch
                {
                    ReportErrorType.FormatError => await GetFeedbackMessageContent(SmsContentKey.ReportError.FormatError, errorReport.LanguageCode),
                    ReportErrorType.HealthRiskNotFound => await GetFeedbackMessageContent(SmsContentKey.ReportError.HealthRiskNotFound, errorReport.LanguageCode),
                    ReportErrorType.DataCollectorNotFound => null,
                    _ => await GetFeedbackMessageContent(SmsContentKey.ReportError.Other, errorReport.LanguageCode)
                };

                if (string.IsNullOrEmpty(feedbackMessage))
                {
                    _loggerAdapter.Warn($"No feedback message found for error type {errorReport.ReportErrorType}");
                    return;
                }

                var senderList = new List <string>(new[] { errorReport.Sender });
                await _queuePublisherService.SendSMSesViaEagle(gatewaySetting.EmailAddress, gatewaySetting.Name, senderList, feedbackMessage);
            }
        }
示例#2
0
        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);
            }
        }