/// <summary>
        /// The get content.
        /// </summary>
        /// <param name="emailCargo">
        /// The email Job.
        /// </param>
        /// <returns>
        /// The <see cref="EmailContent"/>.
        /// </returns>
        /// <exception cref="TemplateRenderException">
        /// error while rendering the template
        /// </exception>
        public EmailData GetContent(object emailCargo)
        {
            EmailData          emailData          = null;
            CampaignEmailCargo campaignEmailCargo = emailCargo as CampaignEmailCargo;

            if (campaignEmailCargo != null)
            {
                string locationId = campaignEmailCargo.LocationId;
                CampaignTemplateData campaignTemplateData = new CampaignTemplateData
                {
                    EmailAddress         = campaignEmailCargo.EmailAddress,
                    UnsubscribeUrl       = campaignEmailCargo.UnsubscribeUrl,
                    LocationId           = locationId,
                    Content              = campaignEmailCargo.Content,
                    IncludeBusinessNames = campaignEmailCargo.IncludeBusinessNames
                };

                var model = this._templateModelCreator.GenerateModel(campaignTemplateData);
                if (model != null)
                {
                    EmailRenderingClient <CampaignDataContract> emailRenderingClient = new EmailRenderingClient
                                                                                       <CampaignDataContract>
                    {
                        EmailRenderingServiceUrl = campaignEmailCargo.EmailRenderingServiceAddress
                    };
                    emailData = new EmailData
                    {
                        Subject  = campaignEmailCargo.Subject,
                        HtmlBody = emailRenderingClient.RenderHtml(model)
                    };
                }
            }

            return(emailData);
        }
Exemple #2
0
        /// <summary>
        /// Creates the email content for deals email
        /// </summary>
        /// <param name="dealsEmailCargo">Deals Email cargo</param>
        /// <returns>EmailContent for deals email</returns>
        private EmailContent PrepareEmail(DealsEmailCargo dealsEmailCargo)
        {
            EmailData    emailData    = _emailContentCreator.GetContent(dealsEmailCargo);
            bool         isTest       = (dealsEmailCargo.Hints != null) && dealsEmailCargo.Hints.IsTest;
            EmailContent emailContent = new EmailContent
            {
                From        = isTest ? this._emailFromAddressTestAccount : this._emailFromAddress,
                FromDisplay = this._emailFromDisplay,
                Subject     = emailData.Subject,
                HtmlBody    = emailData.HtmlBody,
                TextBody    = emailData.TextBody,
                Category    = dealsEmailCargo.Campaign
            };

            if (!dealsEmailCargo.Hints.IsTestEmail)
            {
                // Saving the email data in the history table
                UserEmailEntity emailToSend = new UserEmailEntity(dealsEmailCargo.UserId, dealsEmailCargo.LocationId, DateTime.UtcNow, EmailType.WeeklyDeal);
                emailToSend.SetSerializedPayload(new UserEmailPayload {
                    DealIds = emailData.DealIds
                });
                this._userHistoryStorage.SaveUserEmailEntity(emailToSend);
            }

            return(emailContent);
        }
Exemple #3
0
        /// <summary>
        /// Returns the emaildata for merchant transaction report
        /// </summary>
        /// <param name="merchantReportEmailCargo">The Merchant report email cargo</param>
        /// <param name="analyticsReponse">Response from analytics for merchant transaction call</param>
        /// <returns>Emaildata for merchant transaction report</returns>
        private EmailData GetEmailData(MerchantReportEmailCargo merchantReportEmailCargo, string analyticsReponse)
        {
            MerchantReportContract merchantReportContract;
            Dictionary <string, List <RedemptionContract> > redemptionsByMerchant = new Dictionary <string, List <RedemptionContract> >();

            if (!string.IsNullOrEmpty(analyticsReponse))
            {
                JArray jresponse = JArray.Parse(analyticsReponse);
                if (jresponse.Count > 0)
                {
                    RedemptionContract[] redemptions = new RedemptionContract[jresponse.Count];
                    for (int i = 0; i < jresponse.Count; i++)
                    {
                        var redemption = jresponse[i].ToObject <RedemptionContract>();
                        if (redemption != null)
                        {
                            redemptions[i] = redemption;
                        }
                    }
                    var groupedItems = redemptions.GroupBy(redemption => redemption.MerchantId);
                    foreach (var groupItem in groupedItems)
                    {
                        redemptionsByMerchant[groupItem.Key.ToString()] = groupItem.OrderBy(redemption => redemption.AuthorizationDateTimeLocal).ToList();
                    }
                }
            }

            MerchantTemplateData merchantTemplateData = new MerchantTemplateData
            {
                EmailAddress          = merchantReportEmailCargo.EmailAddress,
                FromDate              = merchantReportEmailCargo.FromDate,
                ToDate                = merchantReportEmailCargo.ToDate,
                ScheduleType          = merchantReportEmailCargo.ScheduleType,
                RedemptionsByMerchant = redemptionsByMerchant,
                MerchantPortalUrl     = _merchantPortalUrl,
                UnsubscribeUrl        = merchantReportEmailCargo.UnsubscribeUrl
            };

            Log.Verbose("Calling model creator to create the data contract for the job : {0}", merchantReportEmailCargo.ToString());
            merchantReportContract = _templateModelCreator.GenerateModel(merchantTemplateData);
            EmailRenderingClient <MerchantReportContract> emailRenderingClient = new EmailRenderingClient <MerchantReportContract>
            {
                EmailRenderingServiceUrl = _merchantTemplateUrl
            };

            EmailData emailData = new EmailData
            {
                Subject  = merchantReportEmailCargo.Subject,
                HtmlBody = emailRenderingClient.RenderHtml(merchantReportContract),
                TextBody = string.Empty
            };

            Log.Verbose("Ready to send the email for the job : {0}", merchantReportEmailCargo.ToString());

            return(emailData);
        }
Exemple #4
0
        /// <summary>
        /// Returns the email content for add card remainder email
        /// </summary>
        /// <param name="remainderEmailCargo">Remainder email cargo</param>
        /// <returns>EmailData for add card remainder email</returns>
        private EmailData GetContentForCompleteSignup(PromotionalEmailCargo remainderEmailCargo)
        {
            EmailData emailData;
            EmailRenderingClient <object> emailRenderingClient = new EmailRenderingClient <object>
            {
                EmailRenderingServiceUrl = remainderEmailCargo.EmailRenderingServiceAddress
            };

            emailData = new EmailData
            {
                Subject  = remainderEmailCargo.Subject,
                HtmlBody = emailRenderingClient.RenderHtml(),
                TextBody = string.Empty
            };

            return(emailData);
        }
Exemple #5
0
        /// <summary>
        /// Handles the Remainder email job
        /// </summary>
        /// <param name="emailCargo">RemainderEmail cargo</param>
        public void Handle(EmailCargo emailCargo)
        {
            PromotionalEmailCargo remainderEmailCargo = emailCargo as PromotionalEmailCargo;

            if (remainderEmailCargo != null)
            {
                var emailHistoryEntities = this._userHistoryStorage.GetUserEmailEntities(remainderEmailCargo.UserId, _mailHistoryLookback).ToList();
                if (this.IsSendTimeWindowValid(emailHistoryEntities.FirstOrDefault(elem => string.Compare(elem.EmailType, EmailType.CompleteSignup.ToString(), StringComparison.CurrentCultureIgnoreCase) == 0), remainderEmailCargo))
                {
                    EmailData    emailData    = _emailContentCreator.GetContent(remainderEmailCargo);
                    bool         isTest       = (remainderEmailCargo.Hints != null) && remainderEmailCargo.Hints.IsTest;
                    EmailContent emailContent = new EmailContent
                    {
                        From        = isTest ? this._emailFromAddressTestAccount : this._emailFromAddress,
                        FromDisplay = this._emailFromDisplay,
                        Subject     = emailData.Subject,
                        HtmlBody    = emailData.HtmlBody,
                        TextBody    = emailData.TextBody,
                        Category    = remainderEmailCargo.Campaign
                    };
                    SendEmailRequest request = new SendEmailRequest
                    {
                        Content = emailContent,
                        ToList  = new List <string> {
                            remainderEmailCargo.EmailAddress
                        },
                        IsTest = isTest
                    };

                    // Send the email
                    this._userServicesClient.SendEmail(remainderEmailCargo.Id, request, null);

                    PromotionalEmailType remainderType;
                    if (Enum.TryParse(remainderEmailCargo.PromotionalEmailType, true, out remainderType))
                    {
                        // Saving the email data in the history table
                        UserEmailEntity emailToSend = new UserEmailEntity(remainderEmailCargo.UserId, string.Empty, DateTime.UtcNow, EmailType.CompleteSignup);
                        this._userHistoryStorage.SaveUserEmailEntity(emailToSend);
                    }
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Returns the email content for remainder email
        /// </summary>
        /// <param name="emailCargo">Remainder Email cargo</param>
        /// <returns>EmailData for the remainder email</returns>
        public EmailData GetContent(object emailCargo)
        {
            PromotionalEmailCargo remainderEmailCargo = emailCargo as PromotionalEmailCargo;
            EmailData             emailData           = null;

            if (remainderEmailCargo != null)
            {
                PromotionalEmailType remainderType;
                if (Enum.TryParse(remainderEmailCargo.PromotionalEmailType, true, out remainderType))
                {
                    switch (remainderType)
                    {
                    case PromotionalEmailType.CompleteSignup:
                        emailData = GetContentForCompleteSignup(remainderEmailCargo);
                        break;
                    }
                }
            }
            return(emailData);
        }
Exemple #7
0
        /// <summary>
        /// Creates the email content for deals email
        /// </summary>
        /// <param name="campaignEmailCargo">Deals Email cargo</param>
        /// <returns>EmailContent for deals email</returns>
        private EmailContent PrepareEmail(CampaignEmailCargo campaignEmailCargo)
        {
            EmailContent emailContent = null;
            EmailData    emailData    = _emailContentCreator.GetContent(campaignEmailCargo);

            if (emailData != null)
            {
                bool isTest = (campaignEmailCargo.Hints != null) && campaignEmailCargo.Hints.IsTest;
                emailContent = new EmailContent
                {
                    From        = isTest ? this._emailFromAddressTestAccount : this._emailFromAddress,
                    FromDisplay = this._emailFromDisplay,
                    Subject     = emailData.Subject,
                    HtmlBody    = emailData.HtmlBody,
                    TextBody    = emailData.TextBody,
                    Category    = campaignEmailCargo.Campaign
                };
            }

            return(emailContent);
        }
        /// <summary>
        /// Handles the Merchant Email job
        /// </summary>
        /// <param name="emailCargo">Merchant Email cargo</param>
        public void Handle(EmailCargo emailCargo)
        {
            MerchantReportEmailCargo merchantReportEmailCargo = emailCargo as MerchantReportEmailCargo;

            if (merchantReportEmailCargo != null)
            {
                EmailData emailData = _emailContentCreator.GetContent(merchantReportEmailCargo);
                if (emailData != null)
                {
                    bool         isTest       = (merchantReportEmailCargo.Hints != null) && merchantReportEmailCargo.Hints.IsTest;
                    EmailContent emailContent = new EmailContent
                    {
                        From        = isTest ? this._emailFromAddressTestAccount : this._merchantEmailFromAddress,
                        FromDisplay = this._emailFromDisplay,
                        Subject     = emailData.Subject,
                        HtmlBody    = emailData.HtmlBody,
                        TextBody    = emailData.TextBody,
                        Category    = merchantReportEmailCargo.Campaign
                    };
                    SendEmailRequest request = new SendEmailRequest
                    {
                        Content = emailContent,
                        ToList  = new List <string> {
                            merchantReportEmailCargo.EmailAddress
                        },
                        IsTest = isTest
                    };

                    // Send the email
                    Log.Verbose("Sending email for the email job : {0}", emailCargo.ToString());
                    this._userServicesClient.SendEmail(merchantReportEmailCargo.Id, request, null);
                }
                JobFetcher.CompleteJob(merchantReportEmailCargo.Id);
            }
            else
            {
                Log.Error("Invalid handler for the email job : {0}", emailCargo.ToString());
            }
        }
Exemple #9
0
        /// <summary>
        /// Creates the content for merchant report
        /// </summary>
        /// <param name="emailCargo"></param>
        /// <returns></returns>
        public EmailData GetContent(object emailCargo)
        {
            EmailData emailData   = null;
            string    accessToken = GetAccessToken();

            Log.Verbose("Successfully got the access token from analytics");

            if (!string.IsNullOrEmpty(accessToken) && emailCargo is MerchantReportEmailCargo)
            {
                MerchantReportEmailCargo merchantReportEmailCargo = emailCargo as MerchantReportEmailCargo;
                var client = new HttpClient();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AnalyticsAuthorizationScheme, accessToken);
                string relativeUri    = string.Format("api/redemptions/getbymerchantuser?fromDateTime={0}&toDateTime={1}&userid={2}", merchantReportEmailCargo.FromDate, merchantReportEmailCargo.ToDate, merchantReportEmailCargo.UserId);
                var    redemptionsUri = new Uri(_analyticsApiEndpoint, relativeUri);

                Log.Verbose("Getting transaction data from analytics for the job : {0}", emailCargo.ToString());
                HttpResponseMessage response = client.GetAsync(redemptionsUri).Result;
                string analyticsReponse      = response.Content.ReadAsStringAsync().Result;

                if (!response.IsSuccessStatusCode)
                {
                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        Log.Warn("Unbale to generate report for the merchant user [Job Id : {0}, User Id : {1}]. Reason : {2}", merchantReportEmailCargo.Id, merchantReportEmailCargo.UserId, response.ReasonPhrase);
                    }
                    else
                    {
                        throw new Exception(response.ReasonPhrase);
                    }
                }
                else
                {
                    Log.Verbose("Got transaction data from analytics for the job : {0}", emailCargo.ToString());
                    emailData = GetEmailData(merchantReportEmailCargo, analyticsReponse);
                }
            }

            return(emailData);
        }
        /// <summary>
        /// The get content.
        /// </summary>
        /// <param name="emailCargo">
        /// The email Job.
        /// </param>
        /// <returns>
        /// The <see cref="EmailContent"/>.
        /// </returns>
        /// <exception cref="TemplateRenderException">
        /// error while rendering the template
        /// </exception>
        public EmailData GetContent(object emailCargo)
        {
            EmailData       emailData       = null;
            DealsEmailCargo dealsEmailCargo = emailCargo as DealsEmailCargo;

            if (dealsEmailCargo != null)
            {
                string locationId            = dealsEmailCargo.LocationId;
                bool   isSendTimeWindowValid = true;
                IEnumerable <UserEmailEntity> emailHistoryEntities = null;

                //If this is not a test email, check the history to make sure we are not sending the email to the same user within the sendtime window.
                if (!dealsEmailCargo.Hints.IsTestEmail)
                {
                    emailHistoryEntities  = this.userHistoryStorage.GetUserEmailEntities(dealsEmailCargo.UserId, mailHistoryLookback).ToList();
                    isSendTimeWindowValid = this.IsSendTimeWindowValid(emailHistoryEntities.FirstOrDefault(elem => elem.LocationId == locationId), dealsEmailCargo);
                }
                if (isSendTimeWindowValid)
                {
                    IEnumerable <Guid> dealsToExclude = null;
                    //if dealids are not included the cargo, we have to select random deals. Need to check in the history to make sure we are excluding deals that have
                    //already been sent in the past few weeks (based on the mail history lookback settings)
                    if (emailHistoryEntities != null && dealsEmailCargo.DealIds == null)
                    {
                        dealsToExclude = this.GetDealsToExclude(emailHistoryEntities);
                    }

                    EmailRenderingClient <DailyDealsContract> emailRenderingClient = new EmailRenderingClient <DailyDealsContract>
                    {
                        EmailRenderingServiceUrl = dealsEmailCargo.EmailRenderingServiceAddress
                    };
                    IEnumerable <Deal> deals = null;
                    if (dealsEmailCargo.Hints != null && dealsEmailCargo.Hints.IncludeDeals)
                    {
                        deals = this.dealsSelector.GetDeals(emailCargo as DealsEmailCargo, dealsToExclude).ToList();
                    }
                    if (deals != null && deals.Any())
                    {
                        DealsTemplateData dailyDealsTemplateData = new DealsTemplateData
                        {
                            EmailAddress   = dealsEmailCargo.EmailAddress,
                            UnsubscribeUrl = dealsEmailCargo.UnsubscribeUrl,
                            LocationId     = locationId,
                            Deals          = deals,
                            DealEmailType  =
                                dealsEmailCargo.DealIds != null && dealsEmailCargo.DealIds.Any()
                                        ? DealEmailType.TrendingDeal
                                        : DealEmailType.WeeklyDeal
                        };

                        var model = this.templateModelCreator.GenerateModel(dailyDealsTemplateData);
                        emailData = new EmailData
                        {
                            Subject = !string.IsNullOrEmpty(dealsEmailCargo.Subject)
                                              ? dealsEmailCargo.Subject
                                              : this.RenderSubject(model),
                            HtmlBody = emailRenderingClient.RenderHtml(model),
                            TextBody = string.Empty,
                            DealIds  = deals.Select(elem => new Guid(elem.Id)).ToList()
                        };
                    }
                    else
                    {
                        int dealsCount = deals != null?deals.Count() : 0;

                        throw new ModelContentException(string.Format("Number of deals is: {0}. This is insufficient for email model creation", dealsCount));
                    }
                }
            }

            return(emailData);
        }