using Nop.Core.Domain.Messages; using Nop.Services.Helpers; using Nop.Services.Localization; using Nop.Services.Messages; using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions; using Nop.Web.Areas.Admin.Models.Messages; using Nop.Web.Framework.Models.Extensions; namespace Nop.Web.Areas.Admin.Factories; /// /// Represents the queued email model factory implementation /// public partial class QueuedEmailModelFactory : IQueuedEmailModelFactory { #region Fields protected readonly IDateTimeHelper _dateTimeHelper; protected readonly IEmailAccountService _emailAccountService; protected readonly ILocalizationService _localizationService; protected readonly IQueuedEmailService _queuedEmailService; #endregion #region Ctor public QueuedEmailModelFactory(IDateTimeHelper dateTimeHelper, IEmailAccountService emailAccountService, ILocalizationService localizationService, IQueuedEmailService queuedEmailService) { _dateTimeHelper = dateTimeHelper; _emailAccountService = emailAccountService; _localizationService = localizationService; _queuedEmailService = queuedEmailService; } #endregion #region Utilities /// /// Gets a friendly email account name /// protected virtual string GetEmailAccountName(EmailAccount emailAccount) { if (emailAccount == null) return string.Empty; if (!string.IsNullOrWhiteSpace(emailAccount.DisplayName)) return emailAccount.Email + " (" + emailAccount.DisplayName + ")"; return emailAccount.Email; } #endregion #region Methods /// /// Prepare queued email search model /// /// Queued email search model /// /// A task that represents the asynchronous operation /// The task result contains the queued email search model /// public virtual Task PrepareQueuedEmailSearchModelAsync(QueuedEmailSearchModel searchModel) { ArgumentNullException.ThrowIfNull(searchModel); //prepare default search values searchModel.SearchMaxSentTries = 10; //prepare page parameters searchModel.SetGridPageSize(); return Task.FromResult(searchModel); } /// /// Prepare paged queued email list model /// /// Queued email search model /// /// A task that represents the asynchronous operation /// The task result contains the queued email list model /// public virtual async Task PrepareQueuedEmailListModelAsync(QueuedEmailSearchModel searchModel) { ArgumentNullException.ThrowIfNull(searchModel); //get parameters to filter emails var startDateValue = !searchModel.SearchStartDate.HasValue ? null : (DateTime?)_dateTimeHelper.ConvertToUtcTime(searchModel.SearchStartDate.Value, await _dateTimeHelper.GetCurrentTimeZoneAsync()); var endDateValue = !searchModel.SearchEndDate.HasValue ? null : (DateTime?)_dateTimeHelper.ConvertToUtcTime(searchModel.SearchEndDate.Value, await _dateTimeHelper.GetCurrentTimeZoneAsync()).AddDays(1); //get queued emails var queuedEmails = await _queuedEmailService.SearchEmailsAsync(fromEmail: searchModel.SearchFromEmail, toEmail: searchModel.SearchToEmail, createdFromUtc: startDateValue, createdToUtc: endDateValue, loadNotSentItemsOnly: searchModel.SearchLoadNotSent, loadOnlyItemsToBeSent: false, maxSendTries: searchModel.SearchMaxSentTries, loadNewest: true, pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize); //prepare list model var model = await new QueuedEmailListModel().PrepareToGridAsync(searchModel, queuedEmails, () => { return queuedEmails.SelectAwait(async queuedEmail => { //fill in model values from the entity var queuedEmailModel = queuedEmail.ToModel(); //little performance optimization: ensure that "Body" is not returned queuedEmailModel.Body = string.Empty; //convert dates to the user time queuedEmailModel.CreatedOn = await _dateTimeHelper.ConvertToUserTimeAsync(queuedEmail.CreatedOnUtc, DateTimeKind.Utc); //fill in additional values (not existing in the entity) var emailAccount = await _emailAccountService.GetEmailAccountByIdAsync(queuedEmail.EmailAccountId); queuedEmailModel.EmailAccountName = GetEmailAccountName(emailAccount); queuedEmailModel.PriorityName = await _localizationService.GetLocalizedEnumAsync(queuedEmail.Priority); if (queuedEmail.DontSendBeforeDateUtc.HasValue) { queuedEmailModel.DontSendBeforeDate = await _dateTimeHelper .ConvertToUserTimeAsync(queuedEmail.DontSendBeforeDateUtc.Value, DateTimeKind.Utc); } if (queuedEmail.SentOnUtc.HasValue) queuedEmailModel.SentOn = await _dateTimeHelper.ConvertToUserTimeAsync(queuedEmail.SentOnUtc.Value, DateTimeKind.Utc); return queuedEmailModel; }); }); return model; } /// /// Prepare queued email model /// /// Queued email model /// Queued email /// Whether to exclude populating of some properties of model /// /// A task that represents the asynchronous operation /// The task result contains the queued email model /// public virtual async Task PrepareQueuedEmailModelAsync(QueuedEmailModel model, QueuedEmail queuedEmail, bool excludeProperties = false) { if (queuedEmail == null) return model; //fill in model values from the entity model ??= queuedEmail.ToModel(); model.EmailAccountName = GetEmailAccountName(await _emailAccountService.GetEmailAccountByIdAsync(queuedEmail.EmailAccountId)); model.PriorityName = await _localizationService.GetLocalizedEnumAsync(queuedEmail.Priority); model.CreatedOn = await _dateTimeHelper.ConvertToUserTimeAsync(queuedEmail.CreatedOnUtc, DateTimeKind.Utc); if (queuedEmail.SentOnUtc.HasValue) model.SentOn = await _dateTimeHelper.ConvertToUserTimeAsync(queuedEmail.SentOnUtc.Value, DateTimeKind.Utc); if (queuedEmail.DontSendBeforeDateUtc.HasValue) model.DontSendBeforeDate = await _dateTimeHelper.ConvertToUserTimeAsync(queuedEmail.DontSendBeforeDateUtc.Value, DateTimeKind.Utc); else model.SendImmediately = true; return model; } #endregion }