Example #1
0
        public async Task Handle(ImportPaymentsCommand message, IMessageHandlerContext context)
        {
            _logger.Info($"Calling Payments API");

            if (_configuration.PaymentsDisabled)
            {
                _logger.Info("Payment processing disabled");
                return;
            }

            var periodEnds = await _paymentsEventsApiClient.GetPeriodEnds();

            var result = await _mediator.SendAsync(new GetPeriodEndsRequest());

            var periodsToProcess = periodEnds.Where(pe => !result.CurrentPeriodEnds.Any(existing => existing.PeriodEndId == pe.Id));

            if (!periodsToProcess.Any())
            {
                _logger.Info("No Period Ends to Process");
                return;
            }

            foreach (var paymentsPeriodEnd in periodsToProcess)
            {
                await ProcessPaymentPeriod(paymentsPeriodEnd, context);
            }
        }
Example #2
0
        public async Task <ICollection <PeriodEnd> > GetUnprocessedPeriodEnds <T>()
        {
            var lastProcessedPeriodId = await _eventRepository.GetLastProcessedEventId <string>(GetFeedName <T>());

            var periodEnds = await _eventsApi.GetPeriodEnds();

            if (!HaveAnyPeriodsBeenProcessedPreviously(lastProcessedPeriodId))
            {
                return(periodEnds);
            }

            return(GetUnprocessedPeriods(periodEnds, lastProcessedPeriodId));
        }
        public async Task RunUpdate()
        {
            _logger.Info($"Calling Payments API");

            if (_configuration.PaymentsDisabled)
            {
                _logger.Info("Payment processing disabled");
                return;
            }

            var periodEnds = await _paymentsEventsApiClient.GetPeriodEnds();

            var result = await _mediator.SendAsync(new GetCurrentPeriodEndRequest());//order by completion date

            var periodFound      = result.CurrentPeriodEnd?.Id == null;
            var periodsToProcess = new List <PeriodEnd>();

            if (!periodFound)
            {
                var lastPeriodId = result.CurrentPeriodEnd.Id;

                foreach (var periodEnd in periodEnds)
                {
                    if (periodFound)
                    {
                        periodsToProcess.Add(periodEnd);
                    }
                    else if (periodEnd.Id.Equals(lastPeriodId))
                    {
                        periodFound = true;
                    }
                }
            }
            else
            {
                periodsToProcess.AddRange(periodEnds);
            }

            if (!periodsToProcess.Any())
            {
                _logger.Info("No Period Ends to Process");
                return;
            }

            var response = await _mediator.SendAsync(new GetAccountsQuery());

            foreach (var paymentsPeriodEnd in periodsToProcess)
            {
                var periodEnd = new EmployerPayments.Domain.Models.Payments.PeriodEnd
                {
                    Id = paymentsPeriodEnd.Id,
                    CalendarPeriodMonth   = paymentsPeriodEnd.CalendarPeriod?.Month ?? 0,
                    CalendarPeriodYear    = paymentsPeriodEnd.CalendarPeriod?.Year ?? 0,
                    CompletionDateTime    = paymentsPeriodEnd.CompletionDateTime,
                    AccountDataValidAt    = paymentsPeriodEnd.ReferenceData?.AccountDataValidAt,
                    CommitmentDataValidAt = paymentsPeriodEnd.ReferenceData?.CommitmentDataValidAt,
                    PaymentsForPeriod     = paymentsPeriodEnd.Links?.PaymentsForPeriod ?? string.Empty
                };

                _logger.Info($"Creating period end {periodEnd.Id}");
                await _mediator.SendAsync(new CreateNewPeriodEndCommand { NewPeriodEnd = periodEnd });

                if (!periodEnd.AccountDataValidAt.HasValue || !periodEnd.CommitmentDataValidAt.HasValue)
                {
                    continue;
                }

                foreach (var account in response.AccountIds)
                {
                    _logger.Info($"Createing payment queue message for accountId:{account} periodEndId:{periodEnd.Id}");

                    await _publisher.PublishAsync(new PaymentProcessorQueueMessage
                    {
                        AccountPaymentUrl = $"{periodEnd.PaymentsForPeriod}&employeraccountid={account}",
                        AccountId         = account,
                        PeriodEndId       = periodEnd.Id
                    });
                }
            }
        }