/// <summary>
        /// Generate Email Model method
        /// </summary>
        /// <returns> The <see cref="CampaignDataContract"/>. </returns>
        public CampaignDataContract GenerateModel(EmailTemplateData modelData)
        {
            CampaignDataContract campaignDataContract = null;
            CampaignTemplateData campaignTemplateData = modelData as CampaignTemplateData;

            if (campaignTemplateData != null)
            {
                var          location    = Users.Dal.DataModel.Location.Parse(campaignTemplateData.LocationId);
                const string locationStr = "Bellevue"; //Having a constant value here and commenting the below code since
                                                       //this code doesn't make sense anymore in Earn program. Also sending of emails
                                                       //will now be moved to Epiphany service and this code won't be used anymore
                //if (location.Type == Users.Dal.DataModel.LocationType.Postal || location.Type == Users.Dal.DataModel.LocationType.City)
                //{
                //    Log.Info("Getting Location for user: {0}, locationId: {1}", campaignTemplateData.EmailAddress, campaignTemplateData.LocationId);
                //    var geoCodePoint = GeoSpatial.GetGeoData(WebUtility.HtmlEncode(string.Format("{0} {1} {2}", location.CountryCode, location.AdminDistrict, location.Value)), GeoSpatial.GeoSource.VirtualEarth);
                //    if (geoCodePoint != null && geoCodePoint.Location != null)
                //    {
                //        locationStr = geoCodePoint.Location.Locality;
                //        Log.Info("Retrieved Location info : {0} for user: {1}, locationId: {2}", locationStr, campaignTemplateData.EmailAddress, campaignTemplateData.LocationId);
                //    }
                //    else
                //    {
                //        Log.Warn("Couldn't fetch location data for user: {0}, locationId: {1}", campaignTemplateData.EmailAddress, location);
                //    }
                //}


                var refinements = new Refinements
                {
                    ResultsPerBusiness = 1,
                    Market             = string.Format("en-{0}", location.CountryCode),
                    Flights            = string.Format("{0},{1}", CloFlight, campaignTemplateData.Campaign)
                };

                if (campaignTemplateData.IncludeBusinessNames)
                {
                    string regionCode = string.Format("{0} {1} {2}", location.CountryCode, location.AdminDistrict, location.Value);
                    Task <IEnumerable <Deal> > getDealsTask = this._dealsClient.GetDealsByRegion(regionCode, null, null, DealsCount, refinements);
                    var           returnedDeals             = getDealsTask.Result.ToList();
                    List <string> businesses = new List <string>();
                    foreach (var deal in returnedDeals)
                    {
                        if (!businesses.Contains(deal.Business.Name) && businesses.Count < 20)
                        {
                            businesses.Add(deal.Business.Name);
                        }
                    }
                    campaignDataContract = new CampaignDataContract {
                        UnsubscribeUrl = campaignTemplateData.UnsubscribeUrl, Content = campaignTemplateData.Content, Location = locationStr, PostalCode = location.Value, Businesses = businesses
                    };
                }
                else
                {
                    campaignDataContract = new CampaignDataContract {
                        UnsubscribeUrl = campaignTemplateData.UnsubscribeUrl, Content = campaignTemplateData.Content, Location = locationStr, PostalCode = location.Value
                    };
                }
            }

            return(campaignDataContract);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The get deals.
        /// </summary>
        /// <param name="emailJob">
        /// The email job.
        /// </param>
        /// <param name="dealsToExclude">deals to exclude</param>
        /// <returns>
        /// the list of deals
        /// </returns>
        /// <exception cref="ApplicationException">location id is wrong </exception>
        public IEnumerable <Deal> GetDeals(DealsEmailCargo emailJob, IEnumerable <Guid> dealsToExclude)
        {
            var refinements = new Refinements();

            if (emailJob.Categories != null && emailJob.Categories.Any())
            {
                refinements.Categories = emailJob.Categories.Select(elem => string.Format("lomo:{0}", elem));
            }

            refinements.ResultsPerBusiness = 1;
            string locationId = emailJob.LocationId;

            Users.Dal.DataModel.Location location = Users.Dal.DataModel.Location.Parse(locationId);

            Task <IEnumerable <Deal> > getDealsTask;

            if (location.CountryCode != "us")
            {
                Log.Error(string.Format("Location Id isn't in us. Location Id={0}", locationId));
            }

            refinements.Market = string.Format("en-{0}", location.CountryCode);
            string dealFlight = emailJob.IsCloDeal ? CloFlight : NonCloFlight;

            //Construct the flight name of the form <<DealFlight (whether to get clo or prepaid only deals)>>,<<campaign name (for tracking in analytics)?>>
            if (!string.IsNullOrEmpty(emailJob.Campaign))
            {
                refinements.Flights = string.Format("{0},{1}", dealFlight, emailJob.Campaign);
            }
            else
            {
                refinements.Flights = dealFlight;
            }

            List <Deal> selectedDeals;

            if (emailJob.DealIds != null && emailJob.DealIds.Any())
            {
                getDealsTask = this.dealsClient.GetDealsById(emailJob.DealIds.ToList(), refinements: refinements);
                //If we are selecting specific deals to send in the email, do not do any further actions like excluding previously sent deals
                selectedDeals = getDealsTask.Result.ToList();
            }
            else
            {
                if (location.Type == LocationType.Postal || location.Type == LocationType.City)
                {
                    string regionCode = string.Format("{0} {1} {2}", location.CountryCode, location.AdminDistrict,
                                                      location.Value);
                    getDealsTask = this.dealsClient.GetDealsByRegion(regionCode, null, null, DealsCount, refinements,
                                                                     emailJob.Anid);
                }
                else if (location.Type == LocationType.National)
                {
                    getDealsTask = this.dealsClient.GetOnlineDeals(DealsCount, refinements);
                }
                else
                {
                    throw new ApplicationException(
                              string.Format(
                                  "Location Id not supported. Only postal, national or city location type are allowed in this phase. Location Id={0}",
                                  locationId));
                }

                var returnedDeals = getDealsTask.Result.ToList();
                HashSet <string> dealsToExcludeHash = new HashSet <string>();
                if (dealsToExclude != null && dealsToExclude.Any())
                {
                    foreach (var dealGuid in dealsToExclude)
                    {
                        dealsToExcludeHash.Add(dealGuid.ToString().ToLowerInvariant());
                    }
                }

                selectedDeals = this.SelectDeals(6, dealsToExcludeHash, returnedDeals);
            }

            return(selectedDeals);
        }