Beispiel #1
0
        /// <summary>
        /// Executes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Execute(IJobExecutionContext context)
        {
            context.Result = string.Empty;
            StringBuilder jobSummaryBuilder = new StringBuilder();

            jobSummaryBuilder.AppendLine("Summary:");
            jobSummaryBuilder.AppendLine(string.Empty);

            // Send emails.
            SendExpiredCreditCardNoticesResult sendExpiredCreditCardNoticesResult = SendExpiredCreditCardNotices(context);

            jobSummaryBuilder.AppendLine($"{sendExpiredCreditCardNoticesResult.ExaminedCount} scheduled credit card transaction(s) were examined.");

            // Report any email send successes.
            if (sendExpiredCreditCardNoticesResult.NoticesSentCount > 0 || !sendExpiredCreditCardNoticesResult.EmailSendExceptions.Any())
            {
                jobSummaryBuilder.AppendLine($"<i class='fa fa-circle text-success'></i> {sendExpiredCreditCardNoticesResult.NoticesSentCount } notice(s) sent.");
            }

            context.UpdateLastStatusMessage(jobSummaryBuilder.ToString());

            // Combine all Exceptions encoutered along the way; we'll roll them into an AggregateException below.
            var innerExceptions = new List <Exception>();

            // Report any email send failures.
            if (sendExpiredCreditCardNoticesResult.EmailSendExceptions.Any())
            {
                jobSummaryBuilder.AppendLine($"<i class='fa fa-circle text-danger'></i> {sendExpiredCreditCardNoticesResult.EmailSendExceptions.Count()} error(s) occurred when sending expired credit card notice(s). See exception log for details.");
                context.UpdateLastStatusMessage(jobSummaryBuilder.ToString());

                innerExceptions.AddRange(sendExpiredCreditCardNoticesResult.EmailSendExceptions);
            }

            // Remove expired, saved accounts.
            RemoveExpiredSavedAccountsResult removeExpiredSavedAccountsResult = RemoveExpiredSavedAccounts(context);

            // Report any account removal successes.
            if (removeExpiredSavedAccountsResult.AccountsDeletedCount > 0)
            {
                jobSummaryBuilder.AppendLine($"<i class='fa fa-circle text-success'></i> {removeExpiredSavedAccountsResult.AccountsDeletedCount} saved account(s) that expired before {removeExpiredSavedAccountsResult.DeleteIfExpiredBeforeDate.ToShortDateString()} removed.");
                context.UpdateLastStatusMessage(jobSummaryBuilder.ToString());
            }

            // Report any account removal failures.
            if (removeExpiredSavedAccountsResult.AccountRemovalExceptions.Any())
            {
                jobSummaryBuilder.AppendLine($"<i class='fa fa-circle text-danger'></i> {removeExpiredSavedAccountsResult.AccountRemovalExceptions.Count()} error(s) occurred when removing saved, expired account(s). See exception log for details.");
                context.UpdateLastStatusMessage(jobSummaryBuilder.ToString());

                innerExceptions.AddRange(removeExpiredSavedAccountsResult.AccountRemovalExceptions);
            }

            // If any errors encountered, throw an exception to mark the job as unsuccessful.
            if (innerExceptions.Any())
            {
                var aggregateException = new AggregateException(innerExceptions);

                throw new RockJobWarningException("Send Credit Card Expiration Notices completed with warnings.", aggregateException);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Removes the expired saved accounts.
        /// </summary>
        /// <param name="removedExpiredSavedAccountDays">The removed expired saved account days.</param>
        /// <returns></returns>
        internal RemoveExpiredSavedAccountsResult RemoveExpiredSavedAccounts(int removedExpiredSavedAccountDays)
        {
            var financialPersonSavedAccountQry = new FinancialPersonSavedAccountService(new RockContext()).Queryable()
                                                 .Where(a =>
                                                        a.FinancialPaymentDetail.CardExpirationDate != null &&
                                                        (a.PersonAliasId.HasValue || a.GroupId.HasValue) &&
                                                        a.FinancialPaymentDetailId.HasValue &&
                                                        a.IsSystem == false)
                                                 .OrderBy(a => a.Id);

            var savedAccountInfoList = financialPersonSavedAccountQry.Select(a => new
            {
                Id = a.Id,
                FinancialPaymentDetail = a.FinancialPaymentDetail
            }).ToList();

            DateTime now          = RockDateTime.Now;
            int      currentMonth = now.Month;
            int      currentYear  = now.Year;

            var result = new RemoveExpiredSavedAccountsResult()
            {
                // if today is 3/16/2020 and removedExpiredSavedAccountDays is 90, only delete card if it expired before 12/17/2019
                DeleteIfExpiredBeforeDate = RockDateTime.Today.AddDays(-removedExpiredSavedAccountDays)
            };

            foreach (var savedAccountInfo in savedAccountInfoList)
            {
                int?expirationMonth = savedAccountInfo.FinancialPaymentDetail.ExpirationMonth;
                int?expirationYear  = savedAccountInfo.FinancialPaymentDetail.ExpirationYear;
                if (!expirationMonth.HasValue || !expirationYear.HasValue)
                {
                    continue;
                }

                if (expirationMonth.Value < 1 || expirationMonth.Value > 12 || expirationYear <= DateTime.MinValue.Year || expirationYear >= DateTime.MaxValue.Year)
                {
                    // invalid month (or year)
                    continue;
                }

                // a credit card with an expiration of April 2020 would be expired on May 1st, 2020
                var cardExpirationDate = new DateTime(expirationYear.Value, expirationMonth.Value, 1).AddMonths(1);

                /* Example:
                 * Today's Date: 2020-3-16
                 * removedExpiredSavedAccountDays: 90
                 * Expired Before Date: 2019-12-17 (Today (2020-3-16) - removedExpiredSavedAccountDays)
                 * Cards that expired before 2019-12-17 should be deleted
                 * Delete 04/20 (Expires 05/01/2020) card? No
                 * Delete 05/20 (Expires 06/01/2020) card? No
                 * Delete 01/20 (Expires 03/01/2020) card? No
                 * Delete 12/19 (Expires 01/01/2020) card? No
                 * Delete 11/19 (Expires 12/01/2019) card? Yes
                 * Delete 10/19 (Expires 11/01/2019) card? Yes
                 *
                 * Today's Date: 2020-3-16
                 * removedExpiredSavedAccountDays: 0
                 * Expired Before Date: 2019-03-16 (Today (2020-3-16) - 0)
                 * Cards that expired before 2019-03-16 should be deleted
                 * Delete 04/20 (Expires 05/01/2020) card? No
                 * Delete 05/20 (Expires 06/01/2020) card? No
                 * Delete 01/20 (Expires 03/01/2020) card? Yes
                 * Delete 12/19 (Expires 01/01/2020) card? Yes
                 * Delete 11/19 (Expires 12/01/2019) card? Yes
                 * Delete 10/19 (Expires 11/01/2019) card? Yes
                 */

                if (cardExpirationDate >= result.DeleteIfExpiredBeforeDate)
                {
                    // We want to only delete cards that expired more than X days ago, so if this card expiration day is after that, skip
                    continue;
                }

                // Card expiration date is older than X day ago, so delete it.
                // Wrapping the following in a try/catch so a single deletion failure doesn't end the process for all deletion candidates.
                try
                {
                    using (var savedAccountRockContext = new RockContext())
                    {
                        var financialPersonSavedAccountService = new FinancialPersonSavedAccountService(savedAccountRockContext);
                        var financialPersonSavedAccount        = financialPersonSavedAccountService.Get(savedAccountInfo.Id);
                        if (financialPersonSavedAccount != null)
                        {
                            if (financialPersonSavedAccountService.CanDelete(financialPersonSavedAccount, out _))
                            {
                                financialPersonSavedAccountService.Delete(financialPersonSavedAccount);
                                savedAccountRockContext.SaveChanges();
                                result.AccountsDeletedCount++;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Provide better identifying context in case the caught exception is too vague.
                    var exception = new Exception($"Unable to delete FinancialPersonSavedAccount (ID = {savedAccountInfo.Id}).", ex);

                    result.AccountRemovalExceptions.Add(exception);
                }
            }

            return(result);
        }