116 lines
4.8 KiB
C#
116 lines
4.8 KiB
C#
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
|
using LCU.Library.Internal.IncentiveApiModels.Requests;
|
|
using LCU.Library.Internal.IncentiveApiModels.Results;
|
|
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Services.NightlyIncentiveProcesses;
|
|
using LCU.Library.Internal.IncentiveApiModels.Common;
|
|
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
|
|
|
namespace LCU.API.Internal.Operations.IncentiveApi.Services.NightlyDecisionServices;
|
|
|
|
public class DirectDepositActivityDecisionService : NightlyIncentiveProcessesService, INightlyIncentiveProcessesService
|
|
{
|
|
private readonly IServiceScopeFactory _serviceScopeFactory;
|
|
private readonly ProcessResult _processResult;
|
|
private const int PageItemLimit = 1000;
|
|
private static readonly List<string> TransactionCodes = new List<string> { "XDEP" };
|
|
|
|
public DirectDepositActivityDecisionService(ServicesFactoryConfiguration servicesFactoryConfiguration,
|
|
IServiceScopeFactory serviceScopeFactory)
|
|
: base(servicesFactoryConfiguration)
|
|
{
|
|
_serviceScopeFactory = serviceScopeFactory;
|
|
_processResult = new ProcessResult();
|
|
}
|
|
|
|
public async Task<ProcessResult> RunProductDecisionProcess(NightlyIncentiveProcessRequest request)
|
|
{
|
|
try
|
|
{
|
|
var result = await UpdateAndDecideActivities();
|
|
var message = $"{result.Count()} Direct Deposit activities were updated";
|
|
Log.Information(message);
|
|
_processResult.MessageStack.Add(message);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
var message = $"There was an issue with the Direct Deposit Update Process. Exception Message: {exception.Message}.";
|
|
_processResult.MessageStack.Add(message);
|
|
_processResult.Success = false;
|
|
}
|
|
return _processResult;
|
|
}
|
|
|
|
public async Task<IReadOnlyCollection<ActivityStateChange>> UpdateAndDecideActivities()
|
|
{
|
|
using var scope = _serviceScopeFactory.CreateScope();
|
|
|
|
var activityPage = await Task.Run(async () => await scope.ServiceProvider.GetRequiredService<IActivityService>().ListByFilterAsync(new ActivityFilter
|
|
{
|
|
Limit = PageItemLimit,
|
|
Page = 1,
|
|
State = new List<ActivityState> { ActivityState.PendingReferral },
|
|
Product = new List<Product> { Product.DirectDeposit },
|
|
Type = new List<ActivityType> { ActivityType.Referral }
|
|
}));
|
|
|
|
if (activityPage?.Items == null)
|
|
{
|
|
return new List<ActivityStateChange>();
|
|
}
|
|
|
|
if (activityPage.Total > PageItemLimit)
|
|
{
|
|
Log.Warning($"More than {PageItemLimit} items to process in the Update And Decide Direct Deposit Pending Referrals!");
|
|
}
|
|
|
|
var activities = activityPage.Items.ToList();
|
|
var currentDateTime = DateTime.Now;
|
|
var changes = new List<ActivityStateChange>();
|
|
|
|
foreach (var activity in activities)
|
|
{
|
|
try
|
|
{
|
|
var activityHistory = await Task.Run(async () => await scope.ServiceProvider.GetRequiredService<IDnaService>().GetTransactionHistory(activity.Detail.AccountNumber, activity.Created, currentDateTime, TransactionCodes));
|
|
|
|
if (activityHistory == null)
|
|
{
|
|
var decision = new ActivityStateChange
|
|
{
|
|
ActivityId = activity.Id,
|
|
State = ActivityState.Rejected,
|
|
Note = "No Account Was Found With The Given Account Number",
|
|
AcceptedDate = activity.Accepted.Date.AddHours(12)
|
|
};
|
|
|
|
await Task.Run(async () => await scope.ServiceProvider.GetRequiredService<IActivityService>().ApplyStateChange(decision));
|
|
changes.Add(decision);
|
|
}
|
|
|
|
if (activityHistory != null && activityHistory.Any())
|
|
{
|
|
var decision = new ActivityStateChange
|
|
{
|
|
ActivityId = activity.Id,
|
|
State = ActivityState.Accepted,
|
|
Note = "Account Had At Least One External Deposit Transaction",
|
|
AcceptedDate = activity.Accepted.Date.AddHours(12)
|
|
};
|
|
|
|
await Task.Run(async () => await scope.ServiceProvider.GetRequiredService<IActivityService>().ApplyStateChange(decision));
|
|
changes.Add(decision);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failed to save direct deposit referral state as accepted for activity:{activity.Id}");
|
|
}
|
|
}
|
|
|
|
return changes;
|
|
}
|
|
}
|
|
|