Example #1
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);
        }
        /// <summary>
        /// Creates the template contract for merchant transaction report email
        /// </summary>
        /// <param name="modelData">Merchant Email template data</param>
        /// <returns>The MerchantReportContract</returns>
        public MerchantReportContract GenerateModel(EmailTemplateData modelData)
        {
            MerchantReportContract merchantReportContract = null;

            if (modelData is MerchantTemplateData)
            {
                MerchantTemplateData merchantTemplateData = modelData as MerchantTemplateData;
                merchantReportContract = new MerchantReportContract
                {
                    FromDate                  = merchantTemplateData.FromDate.ToString("MM/dd/yyyy"),
                    ToDate                    = merchantTemplateData.ToDate.ToString("MM/dd/yyyy"),
                    ScheduleType              = merchantTemplateData.ScheduleType,
                    MerchantPortalUrl         = merchantTemplateData.MerchantPortalUrl,
                    MerchantStoreTransactions = new MerchantStoreTransactionContract[merchantTemplateData.RedemptionsByMerchant.Count]
                };

                Dictionary <string, List <RedemptionContract> > redemptionsByMerhantId = merchantTemplateData.RedemptionsByMerchant;

                List <MerchantStoreTransactionContract> storeTransactions = new List <MerchantStoreTransactionContract>();
                foreach (KeyValuePair <string, List <RedemptionContract> > kvp in redemptionsByMerhantId)
                {
                    MerchantStoreTransactionContract   storeTransactionContract = new MerchantStoreTransactionContract();
                    List <MerchantTransactionContract> transactionContracts     = new List <MerchantTransactionContract>();

                    foreach (var merchantStore in kvp.Value)
                    {
                        if (string.IsNullOrEmpty(storeTransactionContract.MerchantName))
                        {
                            storeTransactionContract.MerchantName = merchantStore.MerchantName;
                        }

                        if (storeTransactionContract.StoreLocation == null)
                        {
                            storeTransactionContract.StoreLocation = new MerchantLocationContract
                            {
                                Address = string.Format("{0} {1}", merchantStore.MerchantLocation.Address1, merchantStore.MerchantLocation.Address2),
                                City    = merchantStore.MerchantLocation.City,
                                State   = merchantStore.MerchantLocation.State,
                                Postal  = merchantStore.MerchantLocation.Postal
                            };
                        }
                        List <Guid> dealsGuid = new List <Guid> {
                            merchantStore.DealId
                        };
                        Task <IEnumerable <Deal> > dealsByGuidTask = _dealsClient.GetDealsById(dealsGuid);
                        IEnumerable <Deal>         deals           = dealsByGuidTask.Result.ToList();

                        MerchantTransactionContract transactionContract = new MerchantTransactionContract
                        {
                            RedemptionTime = merchantStore.AuthorizationDateTimeLocal != null
                                                     ? merchantStore.AuthorizationDateTimeLocal.ToString()
                                                     : string.Empty,
                            DealTitle          = !deals.Any() ? string.Empty : deals.First().Title,
                            SettlementDate     = merchantStore.EventDateTimeUtc.ToString("MM/dd/yyyy"),
                            SettlementAmount   = merchantStore.Amount.ToString(),
                            Discount           = merchantStore.DiscountAmount.ToString(),
                            CardLastFourDigits = merchantStore.CardLastFourDigits
                        };

                        int dollarValue = merchantStore.Amount / 100;
                        int cents       = merchantStore.Amount % 100;
                        transactionContract.SettlementAmount = string.Format("${0}.{1}", dollarValue, cents.ToString("00"));

                        dollarValue = merchantStore.DiscountAmount / 100;
                        cents       = merchantStore.DiscountAmount % 100;
                        transactionContract.Discount = string.Format("${0}.{1}", dollarValue, cents.ToString("00"));

                        transactionContracts.Add(transactionContract);
                    }
                    storeTransactionContract.Transactions = transactionContracts.ToArray();
                    storeTransactions.Add(storeTransactionContract);
                }

                merchantReportContract.MerchantStoreTransactions = storeTransactions.ToArray();
            }

            return(merchantReportContract);
        }