-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEmailNotificationService.cs
More file actions
371 lines (331 loc) · 16.2 KB
/
EmailNotificationService.cs
File metadata and controls
371 lines (331 loc) · 16.2 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Unity.Modules.Shared.Utils;
using Unity.Notifications.Emails;
using Unity.Notifications.Events;
using Unity.Notifications.Integrations.Ches;
using Unity.Notifications.Integrations.RabbitMQ;
using Unity.Notifications.Permissions;
using Unity.Notifications.Settings;
using Unity.Notifications.TeamsNotifications;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Features;
using Volo.Abp.SettingManagement;
using Volo.Abp.Users;
namespace Unity.Notifications.EmailNotifications;
[Dependency(ReplaceServices = false)]
[ExposeServices(typeof(EmailNotificationService), typeof(IEmailNotificationService))]
public class EmailNotificationService : ApplicationService, IEmailNotificationService
{
private readonly IChesClientService _chesClientService;
private readonly IConfiguration _configuration;
private readonly EmailQueueService _emailQueueService;
private readonly IEmailLogsRepository _emailLogsRepository;
private readonly IExternalUserLookupServiceProvider _externalUserLookupServiceProvider;
private readonly ISettingManager _settingManager;
private readonly IFeatureChecker _featureChecker;
private readonly IHttpContextAccessor _httpContextAccessor;
public EmailNotificationService(
IEmailLogsRepository emailLogsRepository,
IConfiguration configuration,
IChesClientService chesClientService,
EmailQueueService emailQueueService,
IExternalUserLookupServiceProvider externalUserLookupServiceProvider,
ISettingManager settingManager,
IFeatureChecker featureChecker,
IHttpContextAccessor httpContextAccessor
)
{
_emailLogsRepository = emailLogsRepository;
_configuration = configuration;
_chesClientService = chesClientService;
_emailQueueService = emailQueueService;
_externalUserLookupServiceProvider = externalUserLookupServiceProvider;
_settingManager = settingManager;
_featureChecker = featureChecker;
_httpContextAccessor = httpContextAccessor;
}
public async Task DeleteEmail(Guid id)
{
await _emailLogsRepository.DeleteAsync(id);
}
public async Task<int> GetEmailsChesWithNoResponseCountAsync()
{
var dbNow = DateTime.UtcNow;
// Create the expression to filter the email logs
Expression<Func<EmailLog, bool>> filter = x =>
(x.Status == EmailStatus.Sent && x.ChesResponse == null) ||
(x.Status == EmailStatus.Initialized && x.CreationTime.AddMinutes(10) < dbNow);
// Fetch all email logs and apply the filter using LINQ
var allEmailLogs = await _emailLogsRepository.GetListAsync();
var emailLogs = allEmailLogs.Where(filter.Compile()).ToList();
// Ensure we're returning 0 if no logs are found
return emailLogs?.Count ?? 0;
}
public async Task<EmailLog?> UpdateEmailLog(Guid emailId, string emailTo, string body, string subject, Guid applicationId, string? emailFrom, string? status, string? emailTemplateName, string? emailCC = null, string? emailBCC = null)
{
if (string.IsNullOrEmpty(emailTo))
{
return null;
}
var emailObject = await GetEmailObjectAsync(emailTo, body, subject, emailFrom, "html", emailTemplateName, emailCC, emailBCC);
EmailLog emailLog = await _emailLogsRepository.GetAsync(emailId);
emailLog = UpdateMappedEmailLog(emailLog, emailObject);
emailLog.ApplicationId = applicationId;
emailLog.Id = emailId;
emailLog.Status = status ?? EmailStatus.Initialized;
// When being called here the current tenant is in context - verified by looking at the tenant id
EmailLog loggedEmail = await _emailLogsRepository.UpdateAsync(emailLog, autoSave: true);
return loggedEmail;
}
public async Task<EmailLog?> InitializeEmailLog(string emailTo, string body, string subject, Guid applicationId, string? emailFrom, string? emailTemplateName, string? emailCC = null, string? emailBCC = null)
{
return await InitializeEmailLog(emailTo, body, subject, applicationId, emailFrom, EmailStatus.Initialized, emailTemplateName, emailCC, emailBCC);
}
[RemoteService(false)]
public async Task<EmailLog?> InitializeEmailLog(string emailTo, string body, string subject, Guid applicationId, string? emailFrom, string? status, string? emailTemplateName, string? emailCC = null, string? emailBCC = null)
{
if (string.IsNullOrEmpty(emailTo))
{
return null;
}
var emailObject = await GetEmailObjectAsync(emailTo, body, subject, emailFrom, "html", emailTemplateName, emailCC, emailBCC);
EmailLog emailLog = new EmailLog();
emailLog = UpdateMappedEmailLog(emailLog, emailObject);
emailLog.ApplicationId = applicationId;
emailLog.Status = status ?? EmailStatus.Initialized;
// When being called here the current tenant is in context - verified by looking at the tenant id
EmailLog loggedEmail = await _emailLogsRepository.InsertAsync(emailLog, autoSave: true);
return loggedEmail;
}
protected virtual async Task NotifyTeamsChannel(string chesEmailError)
{
string? envInfo = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
string activityTitle = "CHES Email error: " + chesEmailError;
string activitySubtitle = "Environment: " + envInfo;
string teamsChannel = _configuration["Notifications:TeamsNotificationsWebhook"] ?? "";
List<Fact> facts = new() { };
await TeamsNotificationService.PostToTeamsAsync(teamsChannel, activityTitle, activitySubtitle, facts);
}
public async Task<HttpResponseMessage> SendCommentNotification(EmailCommentDto input)
{
HttpResponseMessage res = new();
try
{
if (await _featureChecker.IsEnabledAsync("Unity.Notifications"))
{
var defaultFromAddress = await SettingProvider.GetOrNullAsync(NotificationsSettings.Mailing.DefaultFromAddress);
var scheme = "https";
var request = _httpContextAccessor.HttpContext?.Request;
if (request == null)
{
throw new InvalidOperationException("HttpContext or Request is null.");
}
var host = request.Host.ToUriComponent();
var pathBase = "/GrantApplications/Details?ApplicationId=";
var baseUrl = $"{scheme}://{host}{pathBase}";
var commentLink = $"{baseUrl}{input.ApplicationId}";
var subject = $"Unity-Comment: {input.Subject}";
var fromEmail = defaultFromAddress ?? "NoReply@gov.bc.ca";
string htmlBody = $@"
<html lang='en' xmlns='http://www.w3.org/1999/xhtml' xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office'>
<body style='font-family: Arial, sans-serif;'>
<h3 style='color: #0a58ca;'>{input.From} mentioned you in a comment.</h3>
<table style='width: 100%; background-color: #f9f9f9; border-left: 3px solid #ccc;'>
<tr>
<td style='padding: 15px;'>
<p>{input.Body}</p>
</td>
</tr>
</table>
<br />
<table style='background-color: #255a90;'>
<tr>
<td style='padding: 5px 10px; color: #fff; border: 1px solid #2d63c8'>
<a href='{commentLink}' target='_blank'
style='display: inline-block;
font-size: 14px;
color: #fff;
text-decoration: none;'>View Comment</a>
</td>
</tr>
</table>
<p style='font-size: 12px; color: #999;'>*Note - Please do not reply to this email as it is an automated notification.</p>
</body>
</html>";
foreach (var email in input.MentionNamesEmail)
{
var toEmail = email;
res = await SendEmailNotification(toEmail, htmlBody, subject, fromEmail, "html", input.EmailTemplateName);
}
}
else
{
res = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("Feature is not enabled.")
};
}
}
catch (Exception ex)
{
Logger.LogError(ex, "EmailNotificationService->SendEmailCommentNotification: Exception occurred while sending email.");
res = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent($"An exception occurred while sending the email: {ex.Message}")
};
}
return res;
}
/// <summary>
/// Send Email Notfication
/// </summary>
/// <param name="emailTo">The email address to send to</param>
/// <param name="body">The body of the email</param>
/// <param name="subject">Subject Message</param>
/// <param name="emailFrom">From Email Address</param>
/// <param name="emailBodyType">Type of body email: html or text</param>
/// <param name="emailTemplateName">Template name for the email</param>
/// <param name="emailCC">CC email addresses</param>
/// <param name="emailBCC">BCC email addresses</param>
/// <returns>HttpResponseMessage indicating the result of the operation</returns>
public async Task<HttpResponseMessage> SendEmailNotification(string emailTo, string body, string subject, string? emailFrom, string? emailBodyType, string? emailTemplateName, string? emailCC = null, string? emailBCC = null)
{
try
{
if (string.IsNullOrEmpty(emailTo))
{
Logger.LogError("EmailNotificationService->SendEmailNotification: The 'emailTo' parameter is null or empty.");
return new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("'emailTo' cannot be null or empty.")
};
}
// Send the email using the CHES client service
var emailObject = await GetEmailObjectAsync(emailTo, body, subject, emailFrom, emailBodyType, emailTemplateName, emailCC, emailBCC);
var response = await _chesClientService.SendAsync(emailObject);
// Assuming SendAsync returns a HttpResponseMessage or equivalent:
return response;
}
catch (Exception ex)
{
Logger.LogError(ex, "EmailNotificationService->SendEmailNotification: Exception occurred while sending email.");
return new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent($"An exception occurred while sending the email: {ex.Message}")
};
}
}
public async Task<EmailLog?> GetEmailLogById(Guid id)
{
EmailLog emailLog = new EmailLog();
try
{
emailLog = await _emailLogsRepository.GetAsync(id);
}
catch (EntityNotFoundException ex)
{
string ExceptionMessage = ex.Message;
Logger.LogInformation(ex, "Entity Not found for Email Log Must be in wrong context: {ExceptionMessage}", ExceptionMessage);
}
return emailLog;
}
[Authorize]
public virtual async Task<List<EmailHistoryDto>> GetHistoryByApplicationId(Guid applicationId)
{
var entityList = await _emailLogsRepository.GetByApplicationIdAsync(applicationId);
var dtoList = ObjectMapper.Map<List<EmailLog>, List<EmailHistoryDto>>(entityList);
var sentByUserIds = dtoList
.Where(d => d.CreatorId.HasValue)
.Select(d => d.CreatorId!.Value)
.Distinct();
var userDictionary = new Dictionary<Guid, EmailHistoryUserDto>();
foreach (var userId in sentByUserIds)
{
var userInfo = await _externalUserLookupServiceProvider.FindByIdAsync(userId);
if (userInfo != null)
{
userDictionary[userId] = ObjectMapper.Map<IUserData, EmailHistoryUserDto>(userInfo);
}
}
foreach (var item in dtoList)
{
if (item.CreatorId.HasValue && userDictionary.TryGetValue(item.CreatorId.Value, out var userDto))
{
item.SentBy = userDto;
}
}
return dtoList;
}
/// <summary>
/// Send Email To Queue
/// </summary>
/// <param name="EmailLog">The email log to send to q</param>
public async Task SendEmailToQueue(EmailLog emailLog)
{
EmailNotificationEvent emailNotificationEvent = new EmailNotificationEvent();
emailNotificationEvent.Id = emailLog.Id;
emailNotificationEvent.TenantId = emailLog.TenantId;
emailNotificationEvent.RetryAttempts = emailLog.RetryAttempts;
await _emailQueueService.SendToEmailEventQueueAsync(emailNotificationEvent);
}
protected virtual async Task<dynamic> GetEmailObjectAsync(string emailTo, string body, string subject, string? emailFrom, string? emailBodyType, string? emailTemplateName, string? emailCC = null, string? emailBCC = null)
{
var toList = emailTo.ParseEmailList() ?? [];
var ccList = emailCC.ParseEmailList();
var bccList = emailBCC.ParseEmailList();
var defaultFromAddress = await SettingProvider.GetOrNullAsync(NotificationsSettings.Mailing.DefaultFromAddress);
var emailObject = new
{
body,
bodyType = emailBodyType ?? "text",
cc = ccList,
bcc = bccList,
encoding = "utf-8",
from = emailFrom ?? defaultFromAddress ?? "NoReply@gov.bc.ca",
priority = "normal",
subject,
tag = "tag",
to = toList,
templateName = emailTemplateName,
};
return emailObject;
}
protected virtual EmailLog UpdateMappedEmailLog(EmailLog emailLog, dynamic emailDynamicObject)
{
emailLog.Body = emailDynamicObject.body;
emailLog.Subject = emailDynamicObject.subject;
emailLog.BodyType = emailDynamicObject.bodyType;
emailLog.FromAddress = emailDynamicObject.from;
emailLog.ToAddress = string.Join(",", emailDynamicObject.to);
emailLog.CC = emailDynamicObject.cc != null ? string.Join(",", (IEnumerable<string>)emailDynamicObject.cc) : string.Empty;
emailLog.BCC = emailDynamicObject.bcc != null ? string.Join(",", (IEnumerable<string>)emailDynamicObject.bcc) : string.Empty;
emailLog.TemplateName = emailDynamicObject.templateName;
return emailLog;
}
[Authorize(NotificationsPermissions.Settings)]
public async Task UpdateSettings(NotificationsSettingsDto settingsDto)
{
await UpdateTenantSettings(NotificationsSettings.Mailing.DefaultFromAddress, settingsDto.DefaultFromAddress);
await UpdateTenantSettings(NotificationsSettings.Mailing.EmailMaxRetryAttempts, settingsDto.MaximumRetryAttempts);
}
private async Task UpdateTenantSettings(string settingKey, string valueString)
{
if (!valueString.IsNullOrWhiteSpace())
{
await _settingManager.SetForCurrentTenantAsync(settingKey, valueString);
}
}
}