public static void Create(OffersEmailSchedule offersEmailSchedule)
        {
            var settings = new JsonSerializerSettings {
                TypeNameHandling = TypeNameHandling.Objects
            };
            var dataContract = JsonConvert.DeserializeObject <BaseContract>(offersEmailSchedule.MetaData, settings);

            DealsContract dealsContract = dataContract as DealsContract;

            if (dealsContract != null && !string.IsNullOrEmpty(offersEmailSchedule.TemplateUrl))
            {
                if (offersEmailSchedule.IsTestEmail)
                {
                    CreateTestEmailCargo(dealsContract, new Tuple <string, string>(offersEmailSchedule.TemplateUrl, offersEmailSchedule.CampaignName));
                }
                else
                {
                    CreateEmailCargo(dealsContract, new Tuple <string, string>(offersEmailSchedule.TemplateUrl, offersEmailSchedule.CampaignName));
                }
            }
        }
        private static void CreateTestEmailCargo(DealsContract dealsContract, Tuple <string, string> tuple)
        {
            Log.Verbose("Creating test email cargo");
            List <Guid> dealGuids = null;

            if (dealsContract.Deals != null && dealsContract.Deals.Any())
            {
                dealGuids = dealsContract.Deals.Select(dealId => new Guid(dealId)).ToList();
            }

            User user = _usersDal.GetUserByUserId(new Guid(_testUserId));

            if (!dealsContract.Location.StartsWith("us:postal:"))
            {
                dealsContract.Location = string.Format("{0}:{1}", "us:postal", dealsContract.Location);
            }
            foreach (string email in _lstTestEmailAddress)
            {
                DealsEmailCargo dealsEmailCargo = new DealsEmailCargo
                {
                    Id           = user.Id,
                    UserId       = user.Id,
                    EmailAddress = email,
                    Campaign     = tuple.Item2,
                    Hints        = new EmailJobHints {
                        IsTest = false, IncludeDeals = true, IsTestEmail = true
                    },
                    LocationId     = dealsContract.Location,
                    UnsubscribeUrl = _usersDal.GetUnsubscribeUrlInfo(user.Id).UnsubscribeUrl,
                    DealIds        = dealGuids,
                    IsCloDeal      = true,
                    Subject        = dealsContract.Subject
                };
                dealsEmailCargo.EmailRenderingServiceAddress = string.Format(tuple.Item1, dealsEmailCargo.Campaign);
                _emailJobsQueue.Enqueue(dealsEmailCargo);
                Log.Verbose("Enqueued Deals test email cargo : {0} ", dealsEmailCargo.ToString());
            }
        }
        private static void CreateEmailCargo(DealsContract dealsContract, Tuple <string, string> tuple)
        {
            Log.Verbose("Creating clo email cargo");
            string accessToken = GetAnalyticsAccessToken();

            Log.Verbose("got analytics access token");
            object      continuationContext = null;
            bool        hasMore             = true;
            List <Guid> dealGuids           = null;

            if (dealsContract.Deals != null && dealsContract.Deals.Any())
            {
                dealGuids = dealsContract.Deals.Select(dealId => new Guid(dealId)).ToList();
            }

            while (hasMore)
            {
                try
                {
                    EmailsSubscriptionsBatchResponse response = _usersDal.GetNextEmailSubscriptionsBatch(10000, true, continuationContext, SubscriptionType.WeeklyDeals);
                    {
                        if (response.EmailSubscriptions != null)
                        {
                            foreach (EmailSubscription emailSubscription in response.EmailSubscriptions)
                            {
                                if (emailSubscription.LocationId.Contains("us:postal:"))
                                {
                                    Tuple <bool, string> cloRegionInfo = CloHelper.IsCloRegion(emailSubscription.LocationId);
                                    if (cloRegionInfo.Item1)
                                    {
                                        User user = _usersDal.GetUserByUserId(emailSubscription.UserId);
                                        if (!string.IsNullOrEmpty(user.MsId))
                                        {
                                            Tuple <DateTime?, DateTime?> cloInfo = GetCloInfo(user.Id, accessToken);
                                            if (cloInfo.Item1 != null)
                                            {
                                                DealsEmailCargo dealsEmailCargo = new DealsEmailCargo
                                                {
                                                    Id           = user.Id,
                                                    UserId       = user.Id,
                                                    EmailAddress = user.Email,
                                                    Campaign     = tuple.Item2,
                                                    Hints        = new EmailJobHints {
                                                        IsTest = false, IncludeDeals = true
                                                    },
                                                    LocationId     = emailSubscription.LocationId,
                                                    UnsubscribeUrl = _usersDal.GetUnsubscribeUrlInfo(user.Id).UnsubscribeUrl,
                                                    DealIds        = dealGuids,
                                                    IsCloDeal      = true,
                                                    Subject        = dealsContract.Subject
                                                };
                                                dealsEmailCargo.EmailRenderingServiceAddress = string.Format(tuple.Item1, dealsEmailCargo.Campaign);
                                                _emailJobsQueue.Enqueue(dealsEmailCargo);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    hasMore             = response.HasMore;
                    continuationContext = response.ContinuationContext;
                }
                catch (Exception exception)
                {
                    Log.Error(exception.Message);
                }
            }
        }