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.Entities; namespace LCU.API.Internal.Operations.IncentiveApi.Services.NightlyDecisionServices; public class ReferralActivityDecisionService : NightlyIncentiveProcessesService, INightlyIncentiveProcessesService { private readonly IServiceScopeFactory _serviceScopeFactory; private readonly ProcessResult _processResult; public ReferralActivityDecisionService(ServicesFactoryConfiguration servicesFactoryConfiguration, IServiceScopeFactory serviceScopeFactory) : base(servicesFactoryConfiguration) { _serviceScopeFactory = serviceScopeFactory; _processResult = new ProcessResult(); } public async Task RunProductDecisionProcess(NightlyIncentiveProcessRequest request) { try { var result = await UpdateAndDecideActivities(); var message = $"{result.Count()} Referrals were updated"; Log.Information(message); _processResult.MessageStack.Add(message); } catch (Exception exception) { var message = $"There was an issue with the Referral Update process. Exception Message: {exception.Message}."; _processResult.MessageStack.Add(message); _processResult.Success = false; } return _processResult; } public async Task> UpdateAndDecideActivities() { using var scope = _serviceScopeFactory.CreateScope(); var changes = await Task.Run(async () => await scope.ServiceProvider.GetRequiredService().MatchOrExpireReferrals()); return changes; } }