140 lines
5.4 KiB
C#
140 lines
5.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using LCU.API.Collections.RepoApi.Configuration;
|
|
using LCU.API.Collections.RepoApi.DataAccess;
|
|
using LCU.API.Collections.RepoApi.Services;
|
|
using LCU.Shared.Collections.RepoApiLib.Hosted;
|
|
using LCU.Shared.Collections.RepoApiLib.Models;
|
|
using LCU.Shared.Collections.RepoApiLib.Models.Producer;
|
|
using LCU.Shared.Collections.RepoApiLib.Repositories;
|
|
using LCU.Shared.Realtime.QueueLib.Models;
|
|
using LCU.Shared.Realtime.QueueLib.Queue;
|
|
using Serilog;
|
|
|
|
namespace LCU.API.Collections.RepoApi.HostedServices
|
|
{
|
|
public class RepoQueueProducerService : IRepoQueueProducerService
|
|
{
|
|
private readonly RepoApiConfiguration _configuration;
|
|
private readonly ISettingsService _settingsService;
|
|
private readonly ICollectionsDao _collectionsDao;
|
|
private readonly QueueItemProducer<ProcessingItem> _queueService;
|
|
private readonly IQueueConsumerConfig _consumerConfig;
|
|
private readonly IRepoAccountService _repoAccountService;
|
|
private readonly IProcessingReceiptService _processingReceiptService;
|
|
|
|
public RepoQueueProducerService(IQueueConnectionConfig connectionConfig,
|
|
IRepoAccountService repoAcountService,
|
|
RepoApiConfiguration configuration,
|
|
ISettingsService settingsService,
|
|
ICollectionsDao collectionsDao,
|
|
IProcessingReceiptService processingReceiptService)
|
|
{
|
|
_configuration = configuration;
|
|
_settingsService = settingsService;
|
|
_collectionsDao = collectionsDao;
|
|
_processingReceiptService = processingReceiptService;
|
|
_consumerConfig = configuration.QueueConsumerConfigs["RepoAccountQueue"];
|
|
_queueService = new QueueItemProducer<ProcessingItem>(connectionConfig);
|
|
_repoAccountService = repoAcountService;
|
|
}
|
|
|
|
public async Task ImportData(TaskSettings settings)
|
|
{
|
|
try
|
|
{
|
|
var reprocessItems = await _processingReceiptService.GetRetryListAsync();
|
|
var watchStatusItems = await RetrieveRepoAccountsOfInterest();
|
|
var combinedList = CreateCombinedList(reprocessItems, watchStatusItems);
|
|
|
|
Log.Information($"Repo Producer Queueing {combinedList.Count} results.");
|
|
if (combinedList.Any())
|
|
{
|
|
_queueService.EnqueueList(combinedList, _consumerConfig);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, "Error Deserializing. Repo Producer Failed to queue accounts.");
|
|
}
|
|
|
|
try
|
|
{
|
|
var fullConfig = await _collectionsDao.RetrieveTemenosConfigurationAsync();
|
|
var users = fullConfig.Users.Select(x => new ExternalUser()
|
|
{
|
|
Code = x.Code,
|
|
ReferenceId = x.Id,
|
|
Name = x.Description
|
|
}).ToList();
|
|
await _settingsService.SyncExternalUsersAsync(users);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, "Error Updating external user list from Temenos.");
|
|
}
|
|
}
|
|
|
|
public IReadOnlyCollection<ProcessingItem> CreateCombinedList(IReadOnlyCollection<ProcessingReceipt> listPriority,
|
|
IReadOnlyCollection<ProcessingItem> listSecondary)
|
|
{
|
|
var combinedList = new List<ProcessingItem>();
|
|
|
|
if (listPriority != null && listPriority.Any())
|
|
{
|
|
foreach (var processingItem in listPriority)
|
|
{
|
|
combinedList.Add(new ProcessingItem()
|
|
{
|
|
AccountNo = processingItem.AccountNo,
|
|
AccountId = processingItem.AccountId,
|
|
CaseId = processingItem.CaseId,
|
|
QueuedBy = "RepoManager-Retry"
|
|
});
|
|
}
|
|
}
|
|
|
|
if (listSecondary == null || !listSecondary.Any()) return combinedList;
|
|
|
|
foreach (var processingItem in listSecondary)
|
|
{
|
|
if (combinedList.Any(x => x.AccountNo == processingItem.AccountNo)) continue;
|
|
|
|
processingItem.QueuedBy = "RepoManager";
|
|
combinedList.Add(processingItem);
|
|
}
|
|
|
|
return combinedList;
|
|
}
|
|
|
|
public async Task<IReadOnlyCollection<ProcessingItem>> RetrieveRepoAccountsOfInterest()
|
|
{
|
|
var repoAccountNos = new List<ProcessingItem>();
|
|
try
|
|
{
|
|
repoAccountNos.AddRange(await _repoAccountService.GetItemsForProcessing());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Failed to Retrieve Repo Accounts For Polling in RepoQueueProducerService");
|
|
}
|
|
|
|
return repoAccountNos;
|
|
}
|
|
|
|
public void SendRequestToQueue(ProcessingItem queueRequest)
|
|
{
|
|
try
|
|
{
|
|
_queueService.Enqueue(queueRequest, _consumerConfig);
|
|
Log.Debug($"Queued The Following Account {queueRequest.AccountNo}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failed to Add AccountNo To Queue {queueRequest.AccountNo}");
|
|
}
|
|
}
|
|
}
|
|
} |