Files
2026-04-20 14:47:24 -05:00

95 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using LCU.API.Collections.RepoApi.Domain.Processing;
using LCU.Shared.Collections.RepoApiLib.Models;
using LCU.Shared.Collections.RepoApiLib.Models.Consumer;
namespace LCU.API.Collections.RepoApi.Domain.AlertChecker
{
public abstract class TransactionHistoryChecker : IAlertChecker
{
private readonly int _daysInPastToSearch;
protected TransactionHistoryChecker(int daysInPastToSearch = 3)
{
_daysInPastToSearch = daysInPastToSearch;
}
public abstract List<EventAlert> CheckForAlert(RepoAccountProcessingSlip slip);
protected List<EventAlert> StandardizedCheckForAlert(AlertProcessingSlip alertProcessingSlip)
{
var newEventAlerts = new List<EventAlert>();
var results = GetRecentTransactionsFromType
(
alertProcessingSlip.WatchedTransactionTypes,
alertProcessingSlip.AccountProcessingSlip.TransactionHistory
);
if (results != null && AccountStatusIsQualified(alertProcessingSlip.WatchedStatusTypes,
alertProcessingSlip.AccountProcessingSlip.UpdatedRepoAccount.Status))
{
foreach (var result in results)
{
newEventAlerts.Add(new EventAlert()
{
RepoId = alertProcessingSlip.AccountProcessingSlip.UpdatedRepoAccount.RepoId,
AccountNo = alertProcessingSlip.AccountProcessingSlip.UpdatedRepoAccount.AccountNo,
Message = alertProcessingSlip.Message,
SubMessage = FormatTransactionMessage(result),
CreatedBy = "Batch",
DateAdded = DateTime.Now
}
);
}
}
return newEventAlerts.Count > 0 ? newEventAlerts : null;
}
private static bool AccountStatusIsQualified(List<string> watchedStatusTypes, string status)
{
return watchedStatusTypes?.Contains(status.ToUpper()) ?? true;
}
public static string FormatTransactionMessage(CoreTransaction transaction)
{
var message = "";
if (transaction.transactionTypeCode.ToLower().Contains("undo"))
{
message += "***TRANSACTION UNDONE*** - ";
}
return message +
$"Transaction Type: \"{transaction.transactionTypeCode}\" | Posting: {transaction.postingDate} | Transaction: {transaction.amount?.ToString("C2")} | Balance: {transaction.runningBalance?.ToString("C2")} | Description: \"{transaction.transactionTypeDescription}\"";
}
protected List<CoreTransaction> GetRecentTransactionsFromType(List<string> transactionTypes, IReadOnlyCollection<CoreTransaction> transactions)
{
var returnItems = new List<CoreTransaction>();
if (transactions != null && transactions.Any())
{
foreach (var transactionType in transactionTypes)
{
returnItems.AddRange(GetRecentTransactionsFromType(transactionType, _daysInPastToSearch, transactions));
}
}
return returnItems.Count > 0 ? returnItems : null;
}
private static IEnumerable<CoreTransaction> GetRecentTransactionsFromType(string transactionType, int daysInPast, IReadOnlyCollection<CoreTransaction> transactions)
{
return (
from transaction in transactions
where (
transaction.postingDate > DateTime.Now.AddDays(-daysInPast)
&& transaction.transactionTypeCode.ToLower().Contains(transactionType.ToLower()
)
)
select transaction
).ToList();
}
}
}