public async Task ExecuteAsync(UserId subscriberId, DateTime endTimeExclusive, IKeepAliveHandler keepAliveHandler, List <PaymentProcessingException> errors)
        {
            keepAliveHandler.AssertNotNull("keepAliveHandler");
            subscriberId.AssertNotNull("subscriberId");
            errors.AssertNotNull("errors");

            var creators = await this.getCreatorsAndFirstSubscribedDates.ExecuteAsync(subscriberId);

            var committedAccountBalanceValue = await this.getCommittedAccountBalanceDbStatement.ExecuteAsync(subscriberId);

            if (committedAccountBalanceValue < 0)
            {
                errors.Add(new PaymentProcessingException(string.Format("Committed account balance was {0} for user {1}.", committedAccountBalanceValue, subscriberId), subscriberId, null));
                committedAccountBalanceValue = 0m;
            }

            var committedAccountBalance = new CommittedAccountBalance(committedAccountBalanceValue);

            foreach (var creator in creators)
            {
                try
                {
                    await keepAliveHandler.KeepAliveAsync();

                    var latestCommittedLedgerDate = await this.getLatestCommittedLedgerDate.ExecuteAsync(subscriberId, creator.CreatorId);

                    var startTimeInclusive = latestCommittedLedgerDate ?? PaymentProcessingUtilities.GetPaymentProcessingStartDate(creator.FirstSubscribedDate);

                    if ((endTimeExclusive - startTimeInclusive) <= MinimumProcessingPeriod)
                    {
                        continue;
                    }

                    committedAccountBalance = await this.processPaymentsBetweenSubscriberAndCreator.ExecuteAsync(
                        subscriberId,
                        creator.CreatorId,
                        startTimeInclusive,
                        endTimeExclusive,
                        committedAccountBalance);
                }
                catch (Exception t)
                {
                    errors.Add(new PaymentProcessingException(t, subscriberId, creator.CreatorId));
                }
            }
        }
Example #2
0
        public async Task <PaymentProcessingData> ExecuteAsync(UserId subscriberId, UserId creatorId, DateTime startTimeInclusive, DateTime endTimeExclusive, CommittedAccountBalance committedAccountBalance)
        {
            using (PaymentsPerformanceLogger.Instance.Log(typeof(GetPaymentProcessingData)))
            {
                subscriberId.AssertNotNull("subscriberId");
                creatorId.AssertNotNull("creatorId");
                committedAccountBalance.AssertNotNull("committedAccountBalance");

                IReadOnlyList <SubscriberChannelsSnapshot>       subscriberChannelsSnapshots;
                IReadOnlyList <SubscriberSnapshot>               subscriberSnapshots;
                IReadOnlyList <CalculatedAccountBalanceSnapshot> calculatedAccountBalances;
                IReadOnlyList <CreatorChannelsSnapshot>          creatorChannelsSnapshots;
                IReadOnlyList <CreatorFreeAccessUsersSnapshot>   creatorFreeAccessUsersSnapshots;
                IReadOnlyList <CreatorPost>   creatorPosts;
                CreatorPercentageOverrideData creatorPercentageOverride;

                bool updateCache = false;
                if (this.cachedData != null &&
                    subscriberId.Equals(this.cachedData.SubscriberId) &&
                    startTimeInclusive >= this.cachedData.StartTimeInclusive &&
                    endTimeExclusive <= this.cachedData.EndTimeExclusive)
                {
                    subscriberChannelsSnapshots = this.cachedData.SubscriberChannelsSnapshots;
                    subscriberSnapshots         = this.cachedData.SubscriberSnapshots;
                    calculatedAccountBalances   = this.cachedData.CalculatedAccountBalanceSnapshots;
                }
                else
                {
                    updateCache = true;
                    subscriberChannelsSnapshots = await this.getSubscriberChannelsSnapshots.ExecuteAsync(subscriberId, startTimeInclusive, endTimeExclusive);

                    subscriberSnapshots = await this.getSubscriberSnapshots.ExecuteAsync(subscriberId, startTimeInclusive, endTimeExclusive);

                    calculatedAccountBalances = await this.getCalculatedAccountBalances.ExecuteAsync(subscriberId, LedgerAccountType.FifthweekCredit, startTimeInclusive, endTimeExclusive);
                }

                if (this.cachedData != null &&
                    creatorId.Equals(this.cachedData.CreatorId) &&
                    startTimeInclusive >= this.cachedData.StartTimeInclusive &&
                    endTimeExclusive <= this.cachedData.EndTimeExclusive)
                {
                    creatorChannelsSnapshots        = this.cachedData.CreatorChannelsSnapshots;
                    creatorFreeAccessUsersSnapshots = this.cachedData.CreatorFreeAccessUsersSnapshots;
                    creatorPosts = this.cachedData.CreatorPosts;
                    creatorPercentageOverride = this.cachedData.CreatorPercentageOverride;
                }
                else
                {
                    updateCache = true;
                    creatorChannelsSnapshots = await this.getCreatorChannelsSnapshots.ExecuteAsync(creatorId, startTimeInclusive, endTimeExclusive);

                    creatorFreeAccessUsersSnapshots = await this.getCreatorFreeAccessUsersSnapshots.ExecuteAsync(creatorId, startTimeInclusive, endTimeExclusive);

                    creatorPosts = await this.GetCreatorPosts(creatorChannelsSnapshots, startTimeInclusive, endTimeExclusive);

                    creatorPercentageOverride = await this.getCreatorPercentageOverride.ExecuteAsync(creatorId, startTimeInclusive);
                }

                if (updateCache)
                {
                    this.cachedData = new CachedData(
                        subscriberId,
                        creatorId,
                        startTimeInclusive,
                        endTimeExclusive,
                        subscriberChannelsSnapshots,
                        subscriberSnapshots,
                        calculatedAccountBalances,
                        creatorChannelsSnapshots,
                        creatorFreeAccessUsersSnapshots,
                        creatorPosts,
                        creatorPercentageOverride);
                }

                return(new PaymentProcessingData(
                           subscriberId,
                           creatorId,
                           startTimeInclusive,
                           endTimeExclusive,
                           committedAccountBalance,
                           subscriberChannelsSnapshots,
                           subscriberSnapshots,
                           calculatedAccountBalances,
                           creatorChannelsSnapshots,
                           creatorFreeAccessUsersSnapshots,
                           creatorPosts,
                           creatorPercentageOverride));
            }
        }
        public async Task <CommittedAccountBalance> ExecuteAsync(UserId subscriberId, UserId creatorId, DateTime startTimeInclusive, DateTime endTimeExclusive, CommittedAccountBalance committedAccountBalance)
        {
            subscriberId.AssertNotNull("subscriberId");
            creatorId.AssertNotNull("creatorId");
            committedAccountBalance.AssertNotNull("committedAccountBalance");

            var paymentProcessingData = await this.getPaymentProcessingData.ExecuteAsync(
                subscriberId,
                creatorId,
                startTimeInclusive,
                endTimeExclusive,
                committedAccountBalance);

            var results = await this.processPaymentProcessingData.ExecuteAsync(paymentProcessingData);

            await this.persistPaymentProcessingResults.ExecuteAsync(paymentProcessingData, results);

            return(results.CommittedAccountBalance);
        }