Esempio n. 1
0
        /// <summary>
        /// Sends the nightly/monthly emails for users that have requested it.
        /// </summary>
        private void SendNightlyEmails()
        {
            // Find all users who qualify for expiring currency notifications - they may trigger an early email.
            List <EarnedGratuity> lstUsersWithExpiringCurrencies = EarnedGratuity.GratuitiesForUser(null, Gratuity.GratuityTypes.CurrencyAlerts);

            if (!String.IsNullOrEmpty(UserRestriction))
            {
                lstUsersWithExpiringCurrencies.RemoveAll(eg => eg.Username.CompareCurrentCultureIgnoreCase(UserRestriction) != 0);
            }

            // And the list of people who have a subscription to expiring currencies
            List <Profile> lstUsersSubscribedForExpiration = new List <Profile>(Profile.UsersWithSubscriptions(EmailSubscription.FlagForType(SubscriptionType.Expiration), DateTime.Now.AddDays(-7)));

            // get the list of people who have a subscription OTHER than simple monthly or expiration
            List <Profile> lstUsersToSend = new List <Profile>(Profile.UsersWithSubscriptions(~(EmailSubscription.FlagForType(SubscriptionType.MonthlyTotals) | EmailSubscription.FlagForType(SubscriptionType.Expiration)), DateTime.Now.AddDays(-7)));

            if (!String.IsNullOrEmpty(UserRestriction))
            {
                lstUsersToSend.RemoveAll(pf => pf.UserName.CompareOrdinalIgnoreCase(UserRestriction) != 0);
            }

            // See who has expiring currencies that require notification.
            foreach (EarnedGratuity eg in lstUsersWithExpiringCurrencies)
            {
                // Skip over anybody that's not also subscribed for expiration notices
                Profile pf = lstUsersSubscribedForExpiration.Find(profile => profile.UserName.CompareCurrentCultureIgnoreCase(eg.Username) == 0);
                if (pf == null)
                {
                    continue;
                }

                IEnumerable <CurrencyStatusItem> expiringCurrencies = CurrencyStatusItem.CheckForExpiringCurrencies(eg.State, eg.Username, out string newState);
                if (newState.CompareOrdinal(eg.State) != 0)
                {
                    eg.State = newState;
                    eg.Commit();
                }
                if (expiringCurrencies.Any())
                {
                    if (SendMailForUser(pf, Resources.Profile.EmailCurrencyExpiringMailSubject, string.Empty))
                    {
                        // Don't send the weekly mail, since we just pre-empted it.
                        lstUsersToSend.RemoveAll(p => pf.UserName.CompareCurrentCultureIgnoreCase(eg.Username) == 0);
                        pf.LastEmailDate = DateTime.Now;
                        pf.FCommit();
                    }
                }
            }

            foreach (Profile pf in lstUsersToSend)
            {
                if (SendMailForUser(pf, Resources.Profile.EmailWeeklyMailSubject, String.Empty))
                {
                    pf.LastEmailDate = DateTime.Now;
                    pf.FCommit();
                }
            }

            // Now do the monthly/annual emails
            if (DateTime.Now.Day == 1)
            {
                lstUsersToSend = new List <Profile>(Profile.UsersWithSubscriptions(EmailSubscription.FlagForType(SubscriptionType.MonthlyTotals), DateTime.Now.AddDays(1)));

                // We don't update the last-email sent on this because this email is asynchronous - i.e., not dependent on any other mail that was sent.
                foreach (Profile pf in lstUsersToSend)
                {
                    SendMailForUser(pf, String.Format(CultureInfo.CurrentCulture, Resources.Profile.EmailMonthlySubject, DateTime.Now.AddMonths(-1).ToString("MMMM", CultureInfo.InvariantCulture)), "monthly");
                }
            }

            // Do a pending flights reminder - every week or so
            if (DateTime.Now.DayOfYear % 7 == 6)
            {
                IDictionary <string, int> usersWithPendingFlights = PendingFlight.UsersWithLotsOfPendingFlights(30);
                foreach (string szUser in usersWithPendingFlights.Keys)
                {
                    Profile pf = Profile.GetUser(szUser);
                    util.NotifyUser(Branding.ReBrand(Resources.Profile.PendingFlightsReminderSubject),
                                    Branding.ReBrand(String.Format(CultureInfo.CurrentCulture, Resources.Profile.PendingFlightsReminder, pf.UserFullName, usersWithPendingFlights[szUser])),
                                    new System.Net.Mail.MailAddress(pf.Email, pf.UserFullName), false, true);
                }
            }
        }