/// <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); }
/// <summary> /// The is send time window valid. /// </summary> /// <param name="lastEmailEntity"> /// The last email entity. /// </param> /// <param name="remainderEmailCargo"> /// The job. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> private bool IsSendTimeWindowValid(UserEmailEntity lastEmailEntity, PromotionalEmailCargo remainderEmailCargo) { bool isSendTimeWindowValid = true; if (lastEmailEntity != null && lastEmailEntity.EmailDate > (DateTime.UtcNow - this._windowsBetweenRemainderEmails)) { Log.Error( "Can't send remainder email due to time windows constraint. Job Details=[{0}]; Last Email Date={1}; Window Between Emails={2}", remainderEmailCargo.ToString(), lastEmailEntity.EmailDate, this._windowsBetweenRemainderEmails); isSendTimeWindowValid = false; } return(isSendTimeWindowValid); }
private static ApplicationDbContext GetInMemoryDbContext() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var context = new ApplicationDbContext(options); context.Database.EnsureDeleted(); context.Database.EnsureCreated(); UserEmailEntity userEmailEntity = UserEmailEntityData; context.Add <UserEmailEntity>(userEmailEntity); context.SaveChanges(); return(context); }
/// <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); } } } }