-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFsbPaymentNotifier.cs
More file actions
541 lines (468 loc) · 20.5 KB
/
FsbPaymentNotifier.cs
File metadata and controls
541 lines (468 loc) · 20.5 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Unity.Notifications.Emails;
using Unity.Notifications.Events;
using Unity.Notifications.Settings;
using Unity.Payments.Domain.PaymentRequests;
using Unity.Payments.Enums;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Local;
using Volo.Abp.Identity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Settings;
using Volo.Abp.TenantManagement;
namespace Unity.Payments.PaymentRequests.Notifications
{
/// <summary>
/// Service responsible for sending email notifications when payments reach FSB status
/// </summary>
public class FsbPaymentNotifier : ISingletonDependency
{
private readonly IIdentityUserRepository _identityUserRepository;
private readonly ITenantRepository _tenantRepository;
private readonly ICurrentTenant _currentTenant;
private readonly ILocalEventBus _localEventBus;
private readonly ISettingProvider _settingProvider;
private readonly FsbApEmailGroupStrategy _fsbApEmailGroupStrategy;
private readonly ILogger<FsbPaymentNotifier> _logger;
public FsbPaymentNotifier(
IIdentityUserRepository identityUserRepository,
ITenantRepository tenantRepository,
ICurrentTenant currentTenant,
ILocalEventBus localEventBus,
ISettingProvider settingProvider,
FsbApEmailGroupStrategy fsbApEmailGroupStrategy,
ILogger<FsbPaymentNotifier> logger)
{
_identityUserRepository = identityUserRepository;
_tenantRepository = tenantRepository;
_currentTenant = currentTenant;
_localEventBus = localEventBus;
_settingProvider = settingProvider;
_fsbApEmailGroupStrategy = fsbApEmailGroupStrategy;
_logger = logger;
}
/// <summary>
/// Sends email notification with Excel attachment for FSB payments
/// </summary>
/// <param name="fsbPayments">List of payments that have reached FSB status</param>
public async Task NotifyFsbPayments(List<PaymentRequest> fsbPayments)
{
if (fsbPayments == null || fsbPayments.Count == 0)
{
_logger.LogDebug("NotifyFsbPayments: No FSB payments to notify");
return;
}
try
{
// Get recipients from FSB-AP email group
var recipients = await _fsbApEmailGroupStrategy.GetEmailRecipientsAsync();
if (recipients == null || recipients.Count == 0)
{
_logger.LogWarning("NotifyFsbPayments: No recipients found in FSB-AP email group. Email not sent.");
return;
}
// Group payments by batch name (treating null/empty as "Unknown")
var batchGroups = fsbPayments
.GroupBy(p => string.IsNullOrWhiteSpace(p.BatchName) ? "Unknown" : p.BatchName)
.ToList();
_logger.LogInformation(
"NotifyFsbPayments: Grouped {TotalPayments} payments into {BatchCount} batches",
fsbPayments.Count,
batchGroups.Count);
// Get tenant name for email body
string tenantName = "N/A";
if (_currentTenant.Id.HasValue)
{
var tenant = await _tenantRepository.GetAsync(_currentTenant.Id.Value);
tenantName = tenant?.Name ?? "N/A";
}
// Get from address
var defaultFromAddress = await _settingProvider.GetOrNullAsync(NotificationsSettings.Mailing.DefaultFromAddress);
string fromAddress = defaultFromAddress ?? "NoReply@gov.bc.ca";
// Process each batch
int successCount = 0;
int failureCount = 0;
foreach (var batchGroup in batchGroups)
{
string batchName = batchGroup.Key;
var batchPayments = batchGroup.ToList();
try
{
await SendBatchNotification(
batchName,
batchPayments,
recipients,
tenantName,
fromAddress);
successCount++;
_logger.LogInformation(
"NotifyFsbPayments: Successfully sent notification for batch '{BatchName}' with {PaymentCount} payments",
batchName,
batchPayments.Count);
}
catch (Exception ex)
{
failureCount++;
_logger.LogError(
ex,
"NotifyFsbPayments: Failed to send notification for batch '{BatchName}' with {PaymentCount} payments. Continuing with other batches.",
batchName,
batchPayments.Count);
// Continue processing other batches (resilient processing)
}
}
_logger.LogInformation(
"NotifyFsbPayments: Completed processing {TotalBatches} batches. Success: {SuccessCount}, Failed: {FailureCount}",
batchGroups.Count,
successCount,
failureCount);
}
catch (Exception ex)
{
_logger.LogError(ex, "NotifyFsbPayments: Critical error during batch processing");
throw new InvalidOperationException(
$"Failed to process FSB payment notifications. See inner exception for details.",
ex);
}
}
/// <summary>
/// Collects detailed payment data from database for Excel export
/// </summary>
private async Task<List<FsbPaymentData>> CollectPaymentData(List<PaymentRequest> fsbPayments)
{
var paymentDataList = new List<FsbPaymentData>();
try
{
var userNameDict = await BuildUserNameDictionaryAsync(fsbPayments);
// Process each payment
foreach (var payment in fsbPayments)
{
try
{
var paymentData = CreateBasePaymentData(payment);
ApplySiteData(paymentData, payment);
ApplyPaymentRequester(paymentData, payment, userNameDict);
ApplyApprovals(paymentData, payment, userNameDict);
paymentDataList.Add(paymentData);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "NotifyFsbPayments: Error collecting data for payment {PaymentId}", payment.Id);
// Continue processing other payments
}
}
_logger.LogInformation("NotifyFsbPayments: Successfully collected data for {Count} payments", paymentDataList.Count);
}
catch (Exception ex)
{
_logger.LogError(ex, "NotifyFsbPayments: Error collecting payment data");
}
return paymentDataList;
}
private async Task<Dictionary<Guid, string>> BuildUserNameDictionaryAsync(List<PaymentRequest> fsbPayments)
{
var allApproverIds = fsbPayments
.SelectMany(p => p.ExpenseApprovals)
.Where(ea => ea.DecisionUserId.HasValue)
.Select(ea => ea.DecisionUserId!.Value)
.Distinct()
.ToList();
var allCreatorIds = fsbPayments
.Select(p => p.CreatorId)
.Where(id => id.HasValue)
.Select(id => id!.Value)
.Distinct()
.ToList();
var allUserIds = allApproverIds.Concat(allCreatorIds).Distinct().ToList();
if (allUserIds.Count == 0)
{
return [];
}
var users = await _identityUserRepository.GetListByIdsAsync(allUserIds);
return users.ToDictionary(
u => u.Id,
u => $"{u.Name} {u.Surname}".Trim()
);
}
private static FsbPaymentData CreateBasePaymentData(PaymentRequest payment)
{
return new FsbPaymentData
{
// Column 1: Batch # - Use BatchName instead of BatchNumber
BatchName = payment.BatchName ?? "N/A",
// Column 2: Contract Number
ContractNumber = payment.ContractNumber,
// Column 3: Payee Name
PayeeName = payment.PayeeName ?? "N/A",
// Column 7: Invoice Number
InvoiceNumber = payment.InvoiceNumber,
// Column 8: Amount
Amount = payment.Amount,
// Column 15: CAS Cheque Stub Description
CasCheckStubDescription = payment.Description,
// Column 16: Account Coding
AccountCoding = AccountCodingFormatter.Format(payment.AccountCoding),
// Column 18: Requested On
RequestedOn = payment.CreationTime
};
}
private static void ApplySiteData(FsbPaymentData paymentData, PaymentRequest payment)
{
if (payment.Site == null)
{
paymentData.CasSupplierSiteNumber = "N/A/N/A";
paymentData.PayeeAddress = "N/A";
paymentData.PayGroup = "N/A";
return;
}
string supplierNumber = payment.Site.Supplier?.Number ?? "N/A";
string siteNumber = payment.Site.Number ?? "N/A";
// Column 4: CAS Supplier/Site Number - Combined format
paymentData.CasSupplierSiteNumber = $"{supplierNumber}/{siteNumber}";
// Column 5: Payee Address - Combined address string
paymentData.PayeeAddress = FormatAddress(
payment.Site.AddressLine1,
payment.Site.AddressLine2,
payment.Site.AddressLine3,
payment.Site.City,
payment.Site.Province,
payment.Site.PostalCode
);
// Column 9: Pay Group - Convert enum to string
paymentData.PayGroup = FormatPayGroup(payment.Site.PaymentGroup);
}
private static string FormatPayGroup(PaymentGroup paymentGroup)
{
return paymentGroup switch
{
PaymentGroup.EFT => "EFT",
PaymentGroup.Cheque => "Cheque",
_ => "N/A"
};
}
private static void ApplyPaymentRequester(
FsbPaymentData paymentData,
PaymentRequest payment,
Dictionary<Guid, string> userNameDict)
{
// Column 17: Payment Requester
if (!payment.CreatorId.HasValue)
{
return;
}
if (userNameDict.TryGetValue(payment.CreatorId.Value, out var requesterName))
{
paymentData.PaymentRequester = requesterName;
}
}
private static void ApplyApprovals(
FsbPaymentData paymentData,
PaymentRequest payment,
IReadOnlyDictionary<Guid, string> userNameDict)
{
ApplyLevel1Approval(paymentData, payment, userNameDict);
ApplyLevel2Approval(paymentData, payment, userNameDict);
ApplyLevel3Approval(paymentData, payment, userNameDict);
}
private static void ApplyLevel1Approval(
FsbPaymentData paymentData,
PaymentRequest payment,
IReadOnlyDictionary<Guid, string> userNameDict)
{
var l1Approval = payment.ExpenseApprovals.FirstOrDefault(ea => ea.Type == ExpenseApprovalType.Level1);
if (l1Approval == null)
{
return;
}
// Columns 6, 10, 12: All use L1 Approval Date
paymentData.InvoiceDate = l1Approval.DecisionDate;
paymentData.GoodsServicesReceivedDate = l1Approval.DecisionDate;
paymentData.QRApprovalDate = l1Approval.DecisionDate;
// Column 11: Qualifier Receiver (L1 Approver name)
if (l1Approval.DecisionUserId.HasValue
&& userNameDict.TryGetValue(l1Approval.DecisionUserId.Value, out var l1Name))
{
paymentData.QualifierReceiver = l1Name;
}
}
private static void ApplyLevel2Approval(
FsbPaymentData paymentData,
PaymentRequest payment,
IReadOnlyDictionary<Guid, string> userNameDict)
{
var l2Approval = payment.ExpenseApprovals.FirstOrDefault(ea => ea.Type == ExpenseApprovalType.Level2);
if (l2Approval == null)
{
return;
}
// Column 14: EA-Approval Date
paymentData.EAApprovalDate = l2Approval.DecisionDate;
// Column 13: Expense Authority (L2 Approver name)
if (l2Approval.DecisionUserId.HasValue
&& userNameDict.TryGetValue(l2Approval.DecisionUserId.Value, out var l2Name))
{
paymentData.ExpenseAuthority = l2Name;
}
}
private static void ApplyLevel3Approval(
FsbPaymentData paymentData,
PaymentRequest payment,
IReadOnlyDictionary<Guid, string> userNameDict)
{
var l3Approval = payment.ExpenseApprovals.FirstOrDefault(ea => ea.Type == ExpenseApprovalType.Level3);
if (l3Approval == null)
{
return;
}
// Column 20: L3 Approval Date
paymentData.L3ApprovalDate = l3Approval.DecisionDate;
// Column 19: L3 Approver
if (l3Approval.DecisionUserId.HasValue
&& userNameDict.TryGetValue(l3Approval.DecisionUserId.Value, out var l3Name))
{
paymentData.L3Approver = l3Name;
}
}
/// <summary>
/// Generates HTML email body
/// </summary>
private static string GenerateEmailBody(string tenantName)
{
return $@"
<html>
<body>
<p>Hello,</p>
<p>Please see the attached spreadsheet for the payment processing request from the {tenantName} program. If you have any questions, please contact payment requester.</p>
<p>Kind regards.</p>
<br/>
<br/>
<p><em>*ATTENTION - Please do not reply to this email as it is an automated notification which is unable to receive replies.</em></p>
</body>
</html>";
}
/// <summary>
/// Formats address components into a single string
/// </summary>
private static string FormatAddress(
string? addressLine1,
string? addressLine2,
string? addressLine3,
string? city,
string? province,
string? postalCode)
{
var addressParts = new List<string>();
if (!string.IsNullOrWhiteSpace(addressLine1))
addressParts.Add(addressLine1);
if (!string.IsNullOrWhiteSpace(addressLine2))
addressParts.Add(addressLine2);
if (!string.IsNullOrWhiteSpace(addressLine3))
addressParts.Add(addressLine3);
// Combine city, province, postal code on one line
var cityProvincePostal = new List<string>();
if (!string.IsNullOrWhiteSpace(city))
cityProvincePostal.Add(city);
if (!string.IsNullOrWhiteSpace(province))
cityProvincePostal.Add(province);
if (!string.IsNullOrWhiteSpace(postalCode))
cityProvincePostal.Add(postalCode);
if (cityProvincePostal.Count > 0)
addressParts.Add(string.Join(", ", cityProvincePostal));
return addressParts.Count > 0 ? string.Join(", ", addressParts) : "N/A";
}
/// <summary>
/// Sends email notification for a single batch of payments
/// </summary>
/// <param name="batchName">Name of the batch (already normalized for "Unknown")</param>
/// <param name="batchPayments">List of payment requests in this batch</param>
/// <param name="recipients">Email recipients list</param>
/// <param name="tenantName">Current tenant name for email body</param>
/// <param name="fromAddress">Email from address</param>
private async Task SendBatchNotification(
string batchName,
List<PaymentRequest> batchPayments,
List<string> recipients,
string tenantName,
string fromAddress)
{
// Collect payment data for this batch
var paymentDataList = await CollectPaymentData(batchPayments);
if (paymentDataList.Count == 0)
{
_logger.LogWarning(
"SendBatchNotification: Failed to collect payment data for batch '{BatchName}'. Email not sent.",
batchName);
return;
}
// Generate Excel file
byte[] excelBytes = FsbPaymentExcelGenerator.GenerateExcelFile(paymentDataList);
// Generate filename with sanitized batch name
string sanitizedBatchName = SanitizeFileName(batchName);
string fileName = $"FSB_Payments_{sanitizedBatchName}_{DateTime.UtcNow:yyyyMMdd_HHmmssfff}.xlsx";
// Generate email body (reuse existing method)
string emailBody = GenerateEmailBody(tenantName);
// Generate email subject per requirement
string subject = batchName;
// Extract payment IDs for tracking
var paymentIds = batchPayments.Select(p => p.Id).ToList();
// Publish email event with attachment
await _localEventBus.PublishAsync(
new EmailNotificationEvent
{
Action = EmailAction.SendFsbNotification,
TenantId = _currentTenant.Id,
RetryAttempts = 0,
Body = emailBody,
Subject = subject, // Batch-specific subject
EmailFrom = fromAddress,
EmailAddressList = recipients,
ApplicationId = Guid.Empty,
EmailAttachments =
[
new() {
FileName = fileName, // Batch-specific filename
Content = excelBytes,
ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
}
],
PaymentRequestIds = paymentIds // Track which payments are in this email
}
);
}
/// <summary>
/// Sanitizes batch name for use in filenames by removing invalid characters
/// </summary>
/// <param name="batchName">Original batch name</param>
/// <returns>Sanitized batch name safe for filenames</returns>
private static string SanitizeFileName(string batchName)
{
if (string.IsNullOrWhiteSpace(batchName))
{
return "Unknown";
}
// Get OS-specific invalid filename characters
char[] invalidChars = Path.GetInvalidFileNameChars();
// Replace invalid characters with underscore
string sanitized = batchName;
foreach (char c in invalidChars)
{
sanitized = sanitized.Replace(c, '_');
}
// Replace spaces with underscores for cleaner filenames
sanitized = sanitized.Replace(' ', '_');
// Trim to reasonable length (Windows has 255 char limit)
// Reserve space for: "FSB_Payments_" (13) + "_yyyyMMdd_HHmmssfff.xlsx" (25) = 38 chars
const int maxBatchNameLength = 217; // Conservative limit (255 - 38 = 217)
if (sanitized.Length > maxBatchNameLength)
{
sanitized = sanitized.Substring(0, maxBatchNameLength);
}
return sanitized;
}
}
}