using LCU.API.Internal.Operations.IncentiveApiLib.Repositories; using LCU.API.Internal.Operations.IncentiveApi.Services; using LCU.API.Internal.Operations.IncentiveApi.Services.Payout; using LCU.Library.Internal.IncentiveApiModels.Common; using LCU.Library.Internal.IncentiveApiModels.Requests; using LCU.Library.Internal.IncentiveApiModels.Results; using LCU.Library.Internal.ApiResponse.Services; namespace LCU.API.Internal.Operations.IncentiveApi.Services.Reporting; public class ReportingService : IReportingService { private readonly IUserRepository _userRepository; private readonly IPayoutService _payoutService; private readonly ProcessResult _reportingProcessResult; public ReportingService(IPayoutService payoutService, IUserRepository userRepository) { _payoutService = payoutService; _userRepository = userRepository; _reportingProcessResult = new ProcessResult(); } public async Task HandleMonthlyReporting(ReportingRequest request) { try { var period = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1); period = request.MonthsBack == 0 ? period.AddMonths(-1) : period.AddMonths(request.MonthsBack); var range = new MonthRange(period); _payoutService.CreateMonthlyAccountingPayouts(range).GetAwaiter().GetResult(); Log.Information($"Reporting Service processed"); _reportingProcessResult.MessageStack.Add($"Reporting Service Processed Month Starting {range.ToMonth}."); await SafeQueueReturnResponse.TryQueueResponse(request, _reportingProcessResult.Success, _reportingProcessResult.MessageStack, nameof(ReportingService)); } catch (Exception exception) { var message = $"There was an issue with handling the Reporting Service Process Exception Message: {exception.Message}."; _reportingProcessResult.MessageStack.Add(message); _reportingProcessResult.Success = false; await SafeQueueReturnResponse.TryQueueResponse(request, _reportingProcessResult.Success, _reportingProcessResult.MessageStack, nameof(ReportingService)); } } }