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 CheckForAlert(RepoAccountProcessingSlip slip); protected List StandardizedCheckForAlert(AlertProcessingSlip alertProcessingSlip) { var newEventAlerts = new List(); 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 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 GetRecentTransactionsFromType(List transactionTypes, IReadOnlyCollection transactions) { var returnItems = new List(); 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 GetRecentTransactionsFromType(string transactionType, int daysInPast, IReadOnlyCollection transactions) { return ( from transaction in transactions where ( transaction.postingDate > DateTime.Now.AddDays(-daysInPast) && transaction.transactionTypeCode.ToLower().Contains(transactionType.ToLower() ) ) select transaction ).ToList(); } } }