public ActionResult Create(string userId, string submitType)
        {
            try
            {
                Guid trackingId = Guid.NewGuid();
                Guid userGuid;
                Guid.TryParse(userId, out userGuid);
                User user = this.GetUsersDal().GetUserByUserId(userGuid);
                EmailSubscription emailSubscription = this.GetUsersDal().GetEmailSubscriptionsByUserId(userGuid, true, SubscriptionType.WeeklyDeals.ToString()).FirstOrDefault();
                var unsubscribeUrl = this.GetUsersDal().GetUnsubscribeUrlInfo(user.Id).UnsubscribeUrl;
                var emailJob       = new DealsEmailCargo {
                    Id = trackingId, UserId = user.Id, EmailAddress = user.Email, LocationId = emailSubscription.LocationId, UnsubscribeUrl = unsubscribeUrl, Hints = new EmailJobHints(), Categories = user.Info.Preferences.Categories
                };
                if (user.Info != null && user.Info.Preferences != null)
                {
                    emailJob.Categories = user.Info.Preferences.Categories;
                }

                bool isTest = submitType == TestType;
                emailJob.Hints.IsTest = isTest;

                this.GetEmailJobsQueue().Enqueue(emailJob);

                return(this.RedirectToAction("Index", "EmailSubmit", new { trackingId, userGuid, isTest }));
            }
            catch (Exception e)
            {
                Log.Warn("Submit Email Job Return Error. Details: {0}", e);
                return(this.View("Error"));
            }
        }
Exemple #2
0
        private static void DispatchEmailJob(EmailSubscription emailSubscription, UsersDal usersDal, JobsQueue <EmailCargo> emailJobsQueue, string campaign, string emailRenderingUrl,
                                             bool isCloDeal, string[] categories, bool includeDeals = true, bool useTestAccount = false, string subject = null, IEnumerable <Guid> dealIds = null)
        {
            try
            {
                User   user           = usersDal.GetUserByUserId(emailSubscription.UserId);
                string unsubscribeUrl = usersDal.GetUnsubscribeUrlInfo(user.Id).UnsubscribeUrl;
                if (string.IsNullOrEmpty(user.Email))
                {
                    Record(string.Format("can't dispatch email job for user without emil address. User Id={0}", user.Id));

                    return;
                }
                if (string.IsNullOrEmpty(unsubscribeUrl))
                {
                    Record(string.Format("can't dispatch email job for user without unsubscribe url. User Id={0}", user.Id));

                    return;
                }

                DealsEmailCargo dealsEmailCargo = new DealsEmailCargo
                {
                    Id           = Guid.NewGuid(),
                    EmailAddress = user.Email,
                    Campaign     = campaign,
                    Hints        = new EmailJobHints {
                        IsTest = useTestAccount, IncludeDeals = includeDeals
                    },
                    LocationId     = emailSubscription.LocationId,
                    UnsubscribeUrl = unsubscribeUrl,
                    UserId         = user.Id,
                    DealIds        = dealIds,
                    IsCloDeal      = isCloDeal,
                    EmailRenderingServiceAddress = emailRenderingUrl
                };

                if (categories != null && categories.Any())
                {
                    List <Guid> lstGuid = new List <Guid>();
                    foreach (string category in categories)
                    {
                        lstGuid.Add(GetCategoryGuid(category));
                    }
                    dealsEmailCargo.Categories = lstGuid;
                }
                else
                {
                    dealsEmailCargo.Categories = (user.Info != null && user.Info.Preferences != null) ? user.Info.Preferences.Categories : null;
                }

                if (dealsEmailCargo.Hints.IncludeDeals && !string.IsNullOrEmpty(user.MsId) && !user.MsId.StartsWith("fb"))
                {
                    dealsEmailCargo.Anid = AnidIdentityProvider.Instance.DeriveIdentity(user.MsId.ToUpper());
                }
                if (!string.IsNullOrEmpty(subject))
                {
                    dealsEmailCargo.Subject = subject;
                }
                emailJobsQueue.Enqueue(dealsEmailCargo);
                Record(string.Format("Email Job Enqueued. Id={0}; LocationId={1};UnsubscribeUrl={2};UserId={3};CategoriesCount={4}",
                                     dealsEmailCargo.Id,
                                     dealsEmailCargo.LocationId,
                                     dealsEmailCargo.UnsubscribeUrl,
                                     dealsEmailCargo.UserId,
                                     dealsEmailCargo.Categories == null ? 0 : dealsEmailCargo.Categories.Count()));
            }
            catch (Exception e)
            {
                Log.Error("Error while dispathing email job. User Id={0}; Location Id={1}; Error={2}", emailSubscription.UserId, emailSubscription.LocationId, e);
            }
        }