I have created one Background service that execute after every 5 minutes and this service used for sending email after 6 hour,24 hour and 48 hour for user booking order is not confirmed.
for that i have created below method and enums.
public enum Enum_AbandonedCartEmailNotifyHour
{
Hour06 = 6,
Hour24 = 24,
Hour48 = 48
}
public enum Enum_AbandonedCartEmailTemplateSlug
{
Abandonedcart6hr,
Abandonedcart24hr,
Abandonedcart48hr
}
and in service timer every time i have called below method.
public void ProcessOrders()
{
var abandoned6hrsOrders = Get6hrsOrders(); //get orders
if(abandoned6hrsOrders.Count > 0)
this.ProcessEmail(abandoned6hrsOrders, Enum_AbandonedCartEmailNotifyHour.Hour06, Enum_AbandonedCartEmailTemplateSlug.Abandonedcart6hr, Enum_AbandonedCartEmailNotifyStatus.Hour06);
var abandoned24hrsOrders = Get24hrsOrders();//get orders
if (abandoned24hrsOrders.Count > 0)
this.ProcessEmail(abandoned24hrsOrders, Enum_AbandonedCartEmailNotifyHour.Hour24, Enum_AbandonedCartEmailTemplateSlug.Abandonedcart24hr, Enum_AbandonedCartEmailNotifyStatus.Hour24);
var abandoned48hrsOrders = Get48hrsOrders();//get orders
if (abandoned48hrsOrders.Count > 0)
this.ProcessEmail(abandoned48hrsOrders, Enum_AbandonedCartEmailNotifyHour.Hour48, Enum_AbandonedCartEmailTemplateSlug.Abandonedcart48hr, Enum_AbandonedCartEmailNotifyStatus.Hour48);
}
in above method i have checked validation and send email if valid and change notification status in database table.
private void ProcessEmail(List<Order> orders, Enum_AbandonedCartEmailNotifyHour hours,
Enum_AbandonedCartEmailTemplateSlug abandonedCartEmailSlug, Enum_AbandonedCartEmailNotifyStatus notifyStatus)
{
foreach (var item in orders)
{
bool isValid = false;
isValid = CheckValidOrder(item);
Logger.Debug($"check valid order : {item.ID}");
if (isValid)
{
SendEmail(item, abandonedCartEmailSlug);
item.NotifyStatus= notifyStatus;
}
else
{
item.NotifyStatus= Enum_AbandonedCartEmailNotifyStatus.InValid;
}
item.Modified = DateTime.Now;
_orderService.Update(item);
_unitOfWorkAsync.SaveChanges();
}
}
here is method for sendemail for order
public void SendEmail(Order order, Enum_AbandonedCartEmailTemplateSlug abandonedCartEmailSlug)
{
if (order != null)
{
var domain = this.GetDomainForBackground();
var emailSlug = abandonedCartEmailSlug.ToString().ToLower();
var emailTemplateQuery = _emailTemplateService.Query(x => x.Slug.ToLower() == emailSlug).Select();
var emailTemplate = emailTemplateQuery.FirstOrDefault();
if (emailTemplate != null)
{
dynamic email = new Postal.Email("Email");
email.To = order.AspNetUserReceiver.Email;
email.From = CacheHelper.Settings.EmailAddress;
email.Subject = emailTemplate.Subject;
email.Body = emailTemplate.Body;
email.SpaceTitle = order.Listing.Title;
email.Description = order.Listing.Description;
if (order.Listing.Description.Length > 100)
{
email.Description = order.Listing.Description.Substring(0,100) + $"... <a href='{GetListingUrl(order.ListingID)}'>Show more</a>";
}
email.CallbackUrl = GetCallBackUrlForBooking(domain, order.ID, order.IsHourlyBooking);
email.FirstName = order.AspNetUserReceiver.FirstName;
email.Category = order.Listing.Category.Name;
var suburb = GetSuburbName(order.Listing.Latitude, order.Listing.Longitude);
email.Suburbname = suburb;
if (string.IsNullOrEmpty(suburb))
{
suburb = GetSuburbName(order.Listing.Location);
}
email.LocationSearchUrl = GetUrlForLocationSearch(domain, suburb, order.Listing.CategoryID);
if (order.IsHourlyBooking)
{
var str = order.Select(x => $"</br>Number Of Space:{x.Quantity} </br> Time: {x.GetBookedDateString()}, {x.GetBookedTimeString()}").ToList();
var orderInformation = string.Join("</br>", str);
email.OrderInformation = orderInformation;
}
else
{
var orderInformation = $@"
Number of spaces : {order.Quantity}<br />
Start Date: {order.FromDate.Value.ToLongDateTimeStringFormat() }<br />
End Date: {order.ToDate.Value.ToLongDateTimeStringFormat()}<br />
";
email.OrderInformation = orderInformation;
}
this.SendEmailNotLoggedInternal(email);
}
}
}
now my question or doubt is what if in future required any changes or adding send email after 72hours or after 3 hours.
please suggest me to best way to design this code. i have no idea for this or i have written code is good or right or any required changes.