11using Microsoft . AspNetCore . Authorization ;
2- using Microsoft . AspNetCore . Http ;
3- using Microsoft . Extensions . Configuration ;
42using Microsoft . Extensions . Logging ;
53using System ;
64using System . Collections . Generic ;
2321using Volo . Abp . Domain . Entities ;
2422using Volo . Abp . Features ;
2523using Volo . Abp . SettingManagement ;
24+ using Microsoft . AspNetCore . Http ;
2625using Volo . Abp . Users ;
26+ using Unity . GrantManager . Notifications ;
2727
2828namespace Unity . Notifications . EmailNotifications ;
2929
30-
3130[ Dependency ( ReplaceServices = false ) ]
3231[ ExposeServices ( typeof ( EmailNotificationService ) , typeof ( IEmailNotificationService ) ) ]
33- public class EmailNotificationService : ApplicationService , IEmailNotificationService
34- {
35- private readonly IChesClientService _chesClientService ;
36- private readonly IConfiguration _configuration ;
37- private readonly EmailQueueService _emailQueueService ;
38- private readonly IEmailLogsRepository _emailLogsRepository ;
39- private readonly IExternalUserLookupServiceProvider _externalUserLookupServiceProvider ;
40- private readonly ISettingManager _settingManager ;
41- private readonly IFeatureChecker _featureChecker ;
42- private readonly IHttpContextAccessor _httpContextAccessor ;
43-
44- public EmailNotificationService (
32+ #pragma warning disable S107 // Methods should not have too many parameters
33+ public class EmailNotificationService (
34+ INotificationsAppService notificationAppService ,
4535 IEmailLogsRepository emailLogsRepository ,
46- IConfiguration configuration ,
4736 IChesClientService chesClientService ,
4837 EmailQueueService emailQueueService ,
4938 IExternalUserLookupServiceProvider externalUserLookupServiceProvider ,
5039 ISettingManager settingManager ,
5140 IFeatureChecker featureChecker ,
52- IHttpContextAccessor httpContextAccessor
53- )
54- {
55- _emailLogsRepository = emailLogsRepository ;
56- _configuration = configuration ;
57- _chesClientService = chesClientService ;
58- _emailQueueService = emailQueueService ;
59- _externalUserLookupServiceProvider = externalUserLookupServiceProvider ;
60- _settingManager = settingManager ;
61- _featureChecker = featureChecker ;
62- _httpContextAccessor = httpContextAccessor ;
63- }
41+ IHttpContextAccessor httpContextAccessor ) : ApplicationService , IEmailNotificationService
42+ #pragma warning restore S107 // Methods should not have too many parameters
43+ {
6444
6545 public async Task DeleteEmail ( Guid id )
6646 {
67- await _emailLogsRepository . DeleteAsync ( id ) ;
68- }
47+ await emailLogsRepository . DeleteAsync ( id ) ;
48+ }
6949
7050 public async Task < int > GetEmailsChesWithNoResponseCountAsync ( )
7151 {
@@ -77,7 +57,7 @@ public async Task<int> GetEmailsChesWithNoResponseCountAsync()
7757 ( x . Status == EmailStatus . Initialized && x . CreationTime . AddMinutes ( 10 ) < dbNow ) ;
7858
7959 // Fetch all email logs and apply the filter using LINQ
80- var allEmailLogs = await _emailLogsRepository . GetListAsync ( ) ;
60+ var allEmailLogs = await emailLogsRepository . GetListAsync ( ) ;
8161 var emailLogs = allEmailLogs . Where ( filter . Compile ( ) ) . ToList ( ) ;
8262
8363 // Ensure we're returning 0 if no logs are found
@@ -90,16 +70,16 @@ public async Task<int> GetEmailsChesWithNoResponseCountAsync()
9070 {
9171 return null ;
9272 }
93-
94- var emailObject = await GetEmailObjectAsync ( emailTo , body , subject , emailFrom , "html" , emailTemplateName , emailCC , emailBCC ) ;
95- EmailLog emailLog = await _emailLogsRepository . GetAsync ( emailId ) ;
73+
74+ var emailObject = await GetEmailObjectAsync ( emailTo , body , subject , emailFrom , "html" , emailTemplateName ) ;
75+ EmailLog emailLog = await emailLogsRepository . GetAsync ( emailId ) ;
9676 emailLog = UpdateMappedEmailLog ( emailLog , emailObject ) ;
9777 emailLog . ApplicationId = applicationId ;
9878 emailLog . Id = emailId ;
9979 emailLog . Status = status ?? EmailStatus . Initialized ;
10080
10181 // When being called here the current tenant is in context - verified by looking at the tenant id
102- EmailLog loggedEmail = await _emailLogsRepository . UpdateAsync ( emailLog , autoSave : true ) ;
82+ EmailLog loggedEmail = await emailLogsRepository . UpdateAsync ( emailLog , autoSave : true ) ;
10383 return loggedEmail ;
10484 }
10585
@@ -122,7 +102,7 @@ public async Task<int> GetEmailsChesWithNoResponseCountAsync()
122102 emailLog . Status = status ?? EmailStatus . Initialized ;
123103
124104 // When being called here the current tenant is in context - verified by looking at the tenant id
125- EmailLog loggedEmail = await _emailLogsRepository . InsertAsync ( emailLog , autoSave : true ) ;
105+ EmailLog loggedEmail = await emailLogsRepository . InsertAsync ( emailLog , autoSave : true ) ;
126106 return loggedEmail ;
127107 }
128108
@@ -131,21 +111,19 @@ protected virtual async Task NotifyTeamsChannel(string chesEmailError)
131111 string ? envInfo = Environment . GetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" ) ;
132112 string activityTitle = "CHES Email error: " + chesEmailError ;
133113 string activitySubtitle = "Environment: " + envInfo ;
134- string teamsChannel = _configuration [ "Notifications:TeamsNotificationsWebhook" ] ?? "" ;
135- List < Fact > facts = new ( ) { } ;
136- await TeamsNotificationService . PostToTeamsAsync ( teamsChannel , activityTitle , activitySubtitle , facts ) ;
114+ await notificationAppService . PostToTeamsAsync ( activityTitle , activitySubtitle ) ;
137115 }
138116
139117 public async Task < HttpResponseMessage > SendCommentNotification ( EmailCommentDto input )
140118 {
141119 HttpResponseMessage res = new ( ) ;
142120 try
143121 {
144- if ( await _featureChecker . IsEnabledAsync ( "Unity.Notifications" ) )
122+ if ( await featureChecker . IsEnabledAsync ( "Unity.Notifications" ) )
145123 {
146124 var defaultFromAddress = await SettingProvider . GetOrNullAsync ( NotificationsSettings . Mailing . DefaultFromAddress ) ;
147125 var scheme = "https" ;
148- var request = _httpContextAccessor . HttpContext ? . Request ;
126+ var request = httpContextAccessor . HttpContext ? . Request ;
149127
150128 if ( request == null )
151129 {
@@ -211,7 +189,6 @@ public async Task<HttpResponseMessage> SendCommentNotification(EmailCommentDto i
211189 return res ;
212190 }
213191
214-
215192 /// <summary>
216193 /// Send Email Notfication
217194 /// </summary>
@@ -238,8 +215,8 @@ public async Task<HttpResponseMessage> SendEmailNotification(string emailTo, str
238215
239216 }
240217 // Send the email using the CHES client service
241- var emailObject = await GetEmailObjectAsync ( emailTo , body , subject , emailFrom , emailBodyType , emailTemplateName , emailCC , emailBCC ) ;
242- var response = await _chesClientService . SendAsync ( emailObject ) ;
218+ var emailObject = await GetEmailObjectAsync ( emailTo , body , subject , emailFrom , emailBodyType , emailTemplateName ) ;
219+ var response = await chesClientService . SendAsync ( emailObject ) ;
243220
244221 // Assuming SendAsync returns a HttpResponseMessage or equivalent:
245222 return response ;
@@ -259,7 +236,7 @@ public async Task<HttpResponseMessage> SendEmailNotification(string emailTo, str
259236 EmailLog emailLog = new EmailLog ( ) ;
260237 try
261238 {
262- emailLog = await _emailLogsRepository . GetAsync ( id ) ;
239+ emailLog = await emailLogsRepository . GetAsync ( id ) ;
263240 }
264241 catch ( EntityNotFoundException ex )
265242 {
@@ -272,7 +249,7 @@ public async Task<HttpResponseMessage> SendEmailNotification(string emailTo, str
272249 [ Authorize ]
273250 public virtual async Task < List < EmailHistoryDto > > GetHistoryByApplicationId ( Guid applicationId )
274251 {
275- var entityList = await _emailLogsRepository . GetByApplicationIdAsync ( applicationId ) ;
252+ var entityList = await emailLogsRepository . GetByApplicationIdAsync ( applicationId ) ;
276253 var dtoList = ObjectMapper . Map < List < EmailLog > , List < EmailHistoryDto > > ( entityList ) ;
277254
278255 var sentByUserIds = dtoList
@@ -284,7 +261,7 @@ public virtual async Task<List<EmailHistoryDto>> GetHistoryByApplicationId(Guid
284261
285262 foreach ( var userId in sentByUserIds )
286263 {
287- var userInfo = await _externalUserLookupServiceProvider . FindByIdAsync ( userId ) ;
264+ var userInfo = await externalUserLookupServiceProvider . FindByIdAsync ( userId ) ;
288265 if ( userInfo != null )
289266 {
290267 userDictionary [ userId ] = ObjectMapper . Map < IUserData , EmailHistoryUserDto > ( userInfo ) ;
@@ -313,10 +290,18 @@ public async Task SendEmailToQueue(EmailLog emailLog)
313290 emailNotificationEvent . Id = emailLog . Id ;
314291 emailNotificationEvent . TenantId = emailLog . TenantId ;
315292 emailNotificationEvent . RetryAttempts = emailLog . RetryAttempts ;
316- await _emailQueueService . SendToEmailEventQueueAsync ( emailNotificationEvent ) ;
293+ await emailQueueService . SendToEmailEventQueueAsync ( emailNotificationEvent ) ;
317294 }
318295
319- protected virtual async Task < dynamic > GetEmailObjectAsync ( string emailTo , string body , string subject , string ? emailFrom , string ? emailBodyType , string ? emailTemplateName , string ? emailCC = null , string ? emailBCC = null )
296+ protected virtual async Task < dynamic > GetEmailObjectAsync (
297+ string emailTo ,
298+ string body ,
299+ string subject ,
300+ string ? emailFrom ,
301+ string ? emailBodyType ,
302+ string ? emailTemplateName ,
303+ string ? emailCC = null ,
304+ string ? emailBCC = null )
320305 {
321306 var toList = emailTo . ParseEmailList ( ) ?? [ ] ;
322307 var ccList = emailCC . ParseEmailList ( ) ;
@@ -365,7 +350,7 @@ private async Task UpdateTenantSettings(string settingKey, string valueString)
365350 {
366351 if ( ! valueString . IsNullOrWhiteSpace ( ) )
367352 {
368- await _settingManager . SetForCurrentTenantAsync ( settingKey , valueString ) ;
353+ await settingManager . SetForCurrentTenantAsync ( settingKey , valueString ) ;
369354 }
370355 }
371356}
0 commit comments