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

129 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LCU.API.Collections.CollectionsApiLib.Models;
using LCU.API.Collections.RepoApi.Domain.AlertChecker;
using LCU.API.Collections.RepoApi.Domain.Processing;
using LCU.Shared.Collections.RepoApiLib.Models;
using Serilog;
namespace LCU.API.Collections.RepoApi.Services.Producer
{
public class AccountAlertCheckService : IAccountAlertCheckService
{
private const string O4RStatus = "O4R";
private const string NoStatus = "";
private const string RepoStatus = "REPO";
private const string RecaStatus = "RECA";
private const string RehoStatus = "REHO";
private const string RedeemedStatus = "REDEEMED";
private readonly IEventAlertService _eventAlertService;
private readonly IEnumerable<IAlertChecker> _accountAlertPreStatusCheckers;
private readonly IEnumerable<IAlertChecker> _accountAlertPrimaryCheckers;
private readonly IEnumerable<IAlertChecker> _accountAlertPrimaryIgnoreWrittenOffCheckers;
public AccountAlertCheckService( IEventAlertService eventAlertService)
{
_eventAlertService = eventAlertService;
_accountAlertPreStatusCheckers = new List<IAlertChecker>()
{
new SkipPaidChecker()
};
_accountAlertPrimaryCheckers = new List<IAlertChecker>()
{
new RepoNotMovedChecker( 7),
new RepoNotSoldChecker( 60, 30),
new StatusChangeChecker( RecaStatus),
new StatusChangeChecker( RehoStatus),
new TimeInStatusChecker( RehoStatus, 30, 30),
new StatusChangeChecker( O4RStatus, new List<string>() { NoStatus, RedeemedStatus, RehoStatus, RecaStatus}),
new O4rNoRepoChecker(RepoType.Voluntary),
new O4rNoRepoChecker(RepoType.Involuntary),
new O4rNoRepoChecker(RepoType.PreSkip),
new SkipEndChecker( 91),
new SkipNotPaidChecker( 106, 15),
new DocumentNotFoundChecker("Notice of Plan to Sell", "LTR35", "701", RepoStatus),
new DocumentNotFoundChecker("Notice of Repossession", "LTR34", "701", RepoStatus),
new RtcLetterNotFoundChecker("RTC Letter", new List<string> { "RTC 1", "RTC 3" }),
new SoldNoProceedsChecker(),
new AuctionOnHoldChecker(),
new AuctionVinDifferentChecker(),
new RedemptionEndedNoTitleOrderedChecker()
};
_accountAlertPrimaryIgnoreWrittenOffCheckers = new List<IAlertChecker>()
{
new DebitAdjustmentChecker(),
new PaymentReceivedChecker(),
new CreditAdjustmentChecker(),
new SaleFundsReceivedChecker()
};
}
public Task AlertChecksPreStatusReject(RepoAccountProcessingSlip slip)
{
return CheckForAlerts(_accountAlertPreStatusCheckers, slip);
}
public async Task<EventAlert> ManualAlert(EventAlert alert)
{
return await _eventAlertService.SaveAsync(alert);
}
public async Task MaybeAlertOnInvalidStatus(RepoAccountProcessingSlip slip)
{
var allowedStatuses = slip.CurrentSettings?.AllowedStatusChanges.Select(x => x.CurrentStatus);
if (slip.UpdatedRepoAccount != null && allowedStatuses != null &&
!allowedStatuses.Contains(slip.UpdatedRepoAccount.Status))
{
await _eventAlertService.SaveAsync(new EventAlert()
{
RepoId = slip.UpdatedRepoAccount.RepoId,
AccountNo = slip.UpdatedRepoAccount.AccountNo,
CreatedBy = "Importer",
DateAdded = DateTime.Now,
Message = $"Collections System Status {slip.UpdatedRepoAccount.Status} Not Valid"
});
}
}
public async Task AlertChecksPrimary( RepoAccountProcessingSlip slip)
{
if (slip.TransactionHistory != null && slip.TransactionHistory.Any())
{
await CheckForAlerts(_accountAlertPrimaryIgnoreWrittenOffCheckers, slip);
}
else
{
Log.Error($"No Transactions Available, skipping Transaction Alerts for {slip.AccountNo}");
}
await CheckForAlerts(_accountAlertPrimaryCheckers, slip);
}
private async Task CheckForAlerts(IEnumerable<IAlertChecker> alertCheckers, RepoAccountProcessingSlip slip)
{
var eventAlerts = new List<EventAlert>();
foreach (var alertChecker in alertCheckers)
{
try
{
var createdAlerts = alertChecker.CheckForAlert(slip);
if (createdAlerts != null)
{
eventAlerts.AddRange(createdAlerts);
}
}
catch (Exception ex)
{
var accountNumber = slip.AccountNo ?? "Account Number Not Found";
Log.Error(ex, $"Error Checking Alert {alertChecker.GetType()} For Account: {accountNumber}");
}
}
await _eventAlertService.SaveRangeAsync(eventAlerts);
}
}
}