/// <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); }
/// <summary> /// Fetches the job and queues it up /// </summary> private static void GetJob() { Log.Verbose("Fetching merchant email jobs"); object continuationContext = null; bool hasMore = true; while (hasMore) { Log.Verbose("Asking for {0} email jobs", MerchantEmailJobsToFetch.ToString()); EmailJobBatchResponse response = _usersDal.GetEmailJobs(MerchantEmailJobsToFetch, continuationContext); Log.Verbose("Total jobs fetched {0}", response.EmailJobs.Count()); foreach (var job in response.EmailJobs) { if (job.SubscriptionType == SubscriptionType.TransactionReport) { User user = _usersDal.GetUserByUserId(job.UserId); var userSubscription = _usersDal.GetMerchantSubscriptionsByUserId(job.UserId).FirstOrDefault(subscription => subscription.SubscriptionType == SubscriptionType.TransactionReport); if (user != null && userSubscription != null) { Tuple <DateTime, DateTime> merchantReportDate = GetMerchantReportDate(userSubscription.EmailReportInterval); MerchantReportEmailCargo emailCargo = new MerchantReportEmailCargo() { Id = job.JobId, UserId = job.UserId, FromDate = merchantReportDate.Item1, ToDate = merchantReportDate.Item2, ScheduleType = userSubscription.EmailReportInterval.ToString(), EmailAddress = user.Email, Subject = _transactionReportSubject, Campaign = _transactionReportCampaign }; _emailJobsQueue.Enqueue(emailCargo); } } } hasMore = response.HasMore; continuationContext = response.ContinuationContext; } }
/// <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()); } }
/// <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); }