Exemple #1
0
        protected override async Task HandleCore(RefreshPaymentDataCommand message)
        {
            var validationResult = _validator.Validate(message);

            if (!validationResult.IsValid())
            {
                throw new InvalidRequestException(validationResult.ValidationDictionary);
            }

            ICollection <PaymentDetails> payments = null;

            try
            {
                payments = await _paymentService.GetAccountPayments(message.PeriodEnd, message.AccountId);
            }
            catch (WebException ex)
            {
                _logger.Error(ex, $"Unable to get payment information for {message.PeriodEnd} accountid {message.AccountId}");
            }

            if (payments == null || !payments.Any())
            {
                return;
            }

            var existingPaymentIds = await _paymentsRepository.GetAccountPaymentIds(message.AccountId);

            var newPayments = payments.Where(p => !existingPaymentIds.Any(x => x.ToString().Equals(p.Id))).ToArray();

            if (!newPayments.Any())
            {
                return;
            }

            await _paymentsRepository.CreatePaymentData(newPayments);

            await _mediator.PublishAsync(new ProcessPaymentEvent { AccountId = message.AccountId });
        }