-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDataHealthCheckWorker.cs
More file actions
127 lines (111 loc) · 5.51 KB
/
DataHealthCheckWorker.cs
File metadata and controls
127 lines (111 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using Microsoft.Extensions.Logging;
using Quartz;
using System;
using System.Threading.Tasks;
using Unity.GrantManager.Settings;
using Unity.Modules.Shared.Utils;
using Unity.Notifications.EmailNotifications;
using Unity.Payments.Domain.PaymentRequests;
using Unity.Payments.Enums;
using Volo.Abp.BackgroundWorkers.Quartz;
using Volo.Abp.MultiTenancy;
using Volo.Abp.SettingManagement;
using Volo.Abp.TenantManagement;
namespace Unity.GrantManager.HealthChecks.BackgroundWorkers
{
[DisallowConcurrentExecution]
public class DataHealthCheckWorker : QuartzBackgroundWorkerBase
{
private readonly ICurrentTenant _currentTenant;
private readonly ITenantRepository _tenantRepository;
private readonly IEmailNotificationService _emailNotificationService;
private readonly IPaymentRequestRepository _paymentRequestsRepository;
public DataHealthCheckWorker(ICurrentTenant currentTenant,
ITenantRepository tenantRepository,
ISettingManager settingManager,
IEmailNotificationService emailNotificationService,
IPaymentRequestRepository paymentRequestsRepository)
{
_currentTenant = currentTenant;
_tenantRepository = tenantRepository;
_emailNotificationService = emailNotificationService;
_paymentRequestsRepository = paymentRequestsRepository;
string? envInfo = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (string.Equals(envInfo, "Production", StringComparison.OrdinalIgnoreCase))
{
string cronExpression = SettingDefinitions.GetSettingsValue(settingManager, SettingsConstants.BackgroundJobs.DataHealthCheckMonitor_Expression);
JobDetail = JobBuilder
.Create<DataHealthCheckWorker>()
.WithIdentity(nameof(DataHealthCheckWorker))
.Build();
Trigger = TriggerBuilder
.Create()
.WithIdentity(nameof(DataHealthCheckWorker))
.WithSchedule(CronScheduleBuilder.CronSchedule(cronExpression)
.WithMisfireHandlingInstructionIgnoreMisfires())
.Build();
}
}
public override async Task Execute(IJobExecutionContext context)
{
Logger.LogInformation("Executing DataHealthCheckWorker...");
var tenants = await _tenantRepository.GetListAsync();
bool sendEmail = false;
var emailBodyBuilder = new System.Text.StringBuilder();
foreach (var tenant in tenants)
{
using (_currentTenant.Change(tenant.Id, tenant.Name))
{
// Lookup the missing emails
var missingEmailsCount = await _emailNotificationService.GetEmailsChesWithNoResponseCountAsync();
if (missingEmailsCount > 0)
{
Logger.LogWarning("Tenant {TenantName} has {MissingEmailsCount} missing email(s) with a status of Initialized or Sent but no CHES Response.", tenant.Name, missingEmailsCount);
string missingEmailBody = $"Unity tenant {tenant.Name} has {missingEmailsCount} email(s) that were sent but have no CHES Response.";
sendEmail = true;
emailBodyBuilder.AppendLine($"{missingEmailBody}<br />");
}
// Lookup the missing payments
var missingPayments = await GetPaymentsSentWithoutResponseCountAsync();
if (missingPayments > 0)
{
Logger.LogWarning("Tenant {TenantName} has {MissingPaymentsCount} payments sent without a response.", tenant.Name, missingPayments);
string missingPaymentBody = $"Unity tenant {tenant.Name} has {missingPayments} payment(s) that are in Submitted status but have no CAS Response.";
sendEmail = true;
emailBodyBuilder.AppendLine($"{missingPaymentBody}<br />");
}
}
}
if (sendEmail)
{
string emailBody = emailBodyBuilder.ToString();
await SendEmailAlert(emailBody, "Data Health Check Alert - Emails/Payments Missing Responses");
}
Logger.LogInformation("DataHealthCheckWorker Executed...");
await Task.CompletedTask;
}
private async Task<int> GetPaymentsSentWithoutResponseCountAsync()
{
var payments = await _paymentRequestsRepository.GetListAsync(x => x.Status == PaymentRequestStatus.Submitted && x.CasHttpStatusCode == null);
return payments.Count;
}
private async Task SendEmailAlert(string emailBody, string subject)
{
string htmlBody = $@"
<html>
<body style='font-family: Arial, sans-serif;'>
<p>{emailBody}</p>
<br />
<p style='font-size: 12px; color: #999;'>*Note - Please do not reply to this email as it is an automated notification.</p>
</body>
</html>";
await _emailNotificationService.SendEmailNotification(
"grantmanagementsupport@gov.bc.ca",
htmlBody,
subject,
"NoReply@gov.bc.ca", "html",
"");
Logger.LogInformation("Missing Alerts Sent...");
}
}
}