예제 #1
0
        public async Task <Result> Close(int alertId, string comments)
        {
            using var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);

            var alertData = await _nyssContext.Alerts
                            .Where(a => a.Id == alertId)
                            .Select(alert => new
            {
                Alert                      = alert,
                LanguageCode               = alert.ProjectHealthRisk.Project.NationalSociety.ContentLanguage.LanguageCode,
                NotificationEmails         = alert.ProjectHealthRisk.Project.EmailAlertRecipients.Select(ar => ar.EmailAddress).ToList(),
                CountThreshold             = alert.ProjectHealthRisk.AlertRule.CountThreshold,
                MaximumAcceptedReportCount = alert.AlertReports.Count(r => r.Report.Status == ReportStatus.Accepted || r.Report.Status == ReportStatus.Pending)
            })
                            .SingleAsync();

            if (alertData.Alert.Status != AlertStatus.Escalated)
            {
                return(Error(ResultKey.Alert.CloseAlert.WrongStatus));
            }

            alertData.Alert.Status   = AlertStatus.Closed;
            alertData.Alert.ClosedAt = _dateTimeProvider.UtcNow;
            alertData.Alert.ClosedBy = _authorizationService.GetCurrentUser();
            alertData.Alert.Comments = comments;

            FormattableString updateReportsCommand = $@"UPDATE Nyss.Reports SET Status = {ReportStatus.Closed.ToString()} WHERE Status = {ReportStatus.Pending.ToString()}
                                AND Id IN (SELECT ReportId FROM Nyss.AlertReports WHERE AlertId = {alertData.Alert.Id}) ";
            await _nyssContext.ExecuteSqlInterpolatedAsync(updateReportsCommand);

            await _nyssContext.SaveChangesAsync();

            transactionScope.Complete();

            return(Success());
        }