using LCU.API.Internal.Operations.IncentiveApi.Exceptions; using LCU.API.Internal.Operations.IncentiveApi.Models.ExternalActivities; using LCU.API.Internal.Operations.IncentiveApi.Services.MicroApiServices; using LCU.Api.Internal.MaidBotLib.Models; using LCU.Library.Internal.Exceptions; using LCU.Library.Internal.IncentiveApiModels.Requests; using LCU.Library.Internal.IncentiveApiModels.Enums; using LCU.Library.Internal.IncentiveApiModels.Results; using LCU.Library.Internal.LCUServiceFactory.Configurations; using LCU.API.Internal.Operations.IncentiveApi.Services.IncentiveProcesses; using Microsoft.Extensions.DependencyInjection; namespace LCU.API.Internal.Operations.IncentiveApi.Services.DnaActivityServices; public class AtmCheckCardActivityService : IncentiveProcessesService, IIncentiveProcessesService { private readonly ICoreApiWrapperService _coreApiWrapperService; private readonly IServiceScopeFactory _serviceScopeFactory; private readonly ProcessResult _incentiveProcessResult; public AtmCheckCardActivityService(ServicesFactoryConfiguration servicesFactoryConfiguration, ICoreApiWrapperService coreApiWrapperService, IServiceScopeFactory serviceScopeFactory) : base(servicesFactoryConfiguration) { _coreApiWrapperService = coreApiWrapperService; _serviceScopeFactory = serviceScopeFactory; _incentiveProcessResult = new ProcessResult(); } public async Task SyncLatestActivitiesFromDna(IncentiveProcessRequest request) { try { var externalActivities = await ProcessLatestActivity(); var message = $"Added {externalActivities.Count()} Atm Check Card Sales from DNA"; Log.Information(message); _incentiveProcessResult.MessageStack.Add(message); } catch (Exception exception) { var message = $"There was an issue with Atm Check Card Sales from DNA process. Exception Message: {exception.Message}."; _incentiveProcessResult.MessageStack.Add(message); _incentiveProcessResult.Success = false; } return _incentiveProcessResult; } public async Task> ProcessLatestActivity() { try { using var scope = _serviceScopeFactory.CreateScope(); var lastUpdate = await Task.Run(async () => await scope.ServiceProvider.GetRequiredService().GetLastActivityProcessedData(Process.ImportAtmCheckCardSalesFromDna) ?? throw new MissingProcessorRunHistoryException(Process.ImportAtmCheckCardSalesFromDna)); var allActivities = (await _coreApiWrapperService.GetNewAtmCheckCardSalesActivity(lastUpdate.LastProcessedRecordTimestamp)).ToList(); var filteredActivities = await Task.Run(async () => await scope.ServiceProvider.GetRequiredService().GetFilteredExternalActivities(allActivities.Cast().ToList())); var mappedActivities = await Task.Run(async () => await scope.ServiceProvider.GetRequiredService().HandleMapExternalActivities(filteredActivities.ToList(), Product.CheckCard, ActivityType.Other)); if (mappedActivities.Count > 0) { foreach (var activity in mappedActivities) { await Task.Run(async () => await scope.ServiceProvider.GetRequiredService().HandleIncomingActivityDecision(activity, Product.CheckCard, ActivityType.Other)); } } if (allActivities.Count < 1) { await Task.Run(async () => await scope.ServiceProvider.GetRequiredService().UpdateProcessorHistory(Process.ImportAtmCheckCardSalesFromDna, lastUpdate.LastProcessedRecordId, lastUpdate.LastProcessedRecordTimestamp)); return filteredActivities; } var latestActivity = allActivities.Where(activity => activity.ActivityDateTime == allActivities.Select(ca => ca.ActivityDateTime).Max()).OrderByDescending(x => x.ActivityNbr).First(); await Task.Run(async () => await scope.ServiceProvider.GetRequiredService().UpdateProcessorHistory(Process.ImportAtmCheckCardSalesFromDna, latestActivity.ActivityNbr, latestActivity.ActivityDateTime)); return filteredActivities; } catch (ServiceException serviceException) { Log.ForContext(Enum.GetName(typeof(PropertyEnum), PropertyEnum.Group), Enum.GetName(typeof(GroupEnum), GroupEnum.ITDevelopment)) .Error(serviceException, $"Import Check and ATM Cards Failure. " + $"Failed to Process Latest Check and ATM Cards. Will try again from same starting position on next run."); throw; } catch (Exception exception) { Log.ForContext(Enum.GetName(typeof(PropertyEnum), PropertyEnum.Group), Enum.GetName(typeof(GroupEnum), GroupEnum.ITDevelopment)) .Fatal(exception, $"Import Check and ATM Cards Failure. " + $"Failed to Process Latest Check and ATM Cards. Will try again from same starting position on next run."); throw; } } }