Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

QueuedEmailService.cs

using Nop.Core;
using Nop.Core.Domain.Messages;
using Nop.Data;

namespace Nop.Services.Messages;

/// 
/// Queued email service
/// 
public partial class QueuedEmailService : IQueuedEmailService
{
    #region Fields

    protected readonly IRepository _queuedEmailRepository;

    #endregion

    #region Ctor

    public QueuedEmailService(IRepository queuedEmailRepository)
    {
        _queuedEmailRepository = queuedEmailRepository;
    }

    #endregion

    #region Methods

    /// 
    /// Inserts a queued email
    /// 
    /// Queued email        
    /// A task that represents the asynchronous operation
    public virtual async Task InsertQueuedEmailAsync(QueuedEmail queuedEmail)
    {
        await _queuedEmailRepository.InsertAsync(queuedEmail);
    }

    /// 
    /// Updates a queued email
    /// 
    /// Queued email
    /// A task that represents the asynchronous operation
    public virtual async Task UpdateQueuedEmailAsync(QueuedEmail queuedEmail)
    {
        await _queuedEmailRepository.UpdateAsync(queuedEmail);
    }

    /// 
    /// Deleted a queued email
    /// 
    /// Queued email
    /// A task that represents the asynchronous operation
    public virtual async Task DeleteQueuedEmailAsync(QueuedEmail queuedEmail)
    {
        await _queuedEmailRepository.DeleteAsync(queuedEmail);
    }

    /// 
    /// Deleted a queued emails
    /// 
    /// Queued emails
    /// A task that represents the asynchronous operation
    public virtual async Task DeleteQueuedEmailsAsync(IList queuedEmails)
    {
        await _queuedEmailRepository.DeleteAsync(queuedEmails);
    }

    /// 
    /// Gets a queued email by identifier
    /// 
    /// Queued email identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the queued email
    /// 
    public virtual async Task GetQueuedEmailByIdAsync(int queuedEmailId)
    {
        return await _queuedEmailRepository.GetByIdAsync(queuedEmailId, cache => default, useShortTermCache: true);
    }

    /// 
    /// Get queued emails by identifiers
    /// 
    /// queued email identifiers
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the queued emails
    /// 
    public virtual async Task> GetQueuedEmailsByIdsAsync(int[] queuedEmailIds)
    {
        return await _queuedEmailRepository.GetByIdsAsync(queuedEmailIds);
    }

    /// 
    /// Gets all queued emails
    /// 
    /// From Email
    /// To Email
    /// Created date from (UTC); null to load all records
    /// Created date to (UTC); null to load all records
    /// A value indicating whether to load only not sent emails
    /// A value indicating whether to load only emails for ready to be sent
    /// Maximum send tries
    /// A value indicating whether we should sort queued email descending; otherwise, ascending.
    /// Page index
    /// Page size
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the email item list
    /// 
    public virtual async Task> SearchEmailsAsync(string fromEmail,
        string toEmail, DateTime? createdFromUtc, DateTime? createdToUtc,
        bool loadNotSentItemsOnly, bool loadOnlyItemsToBeSent, int maxSendTries,
        bool loadNewest, int pageIndex = 0, int pageSize = int.MaxValue)
    {
        fromEmail = (fromEmail ?? string.Empty).Trim();
        toEmail = (toEmail ?? string.Empty).Trim();

        var query = _queuedEmailRepository.Table;
        if (!string.IsNullOrEmpty(fromEmail))
            query = query.Where(qe => qe.From.Contains(fromEmail));
        if (!string.IsNullOrEmpty(toEmail))
            query = query.Where(qe => qe.To.Contains(toEmail));
        if (createdFromUtc.HasValue)
            query = query.Where(qe => qe.CreatedOnUtc >= createdFromUtc);
        if (createdToUtc.HasValue)
            query = query.Where(qe => qe.CreatedOnUtc <= createdToUtc);
        if (loadNotSentItemsOnly)
            query = query.Where(qe => !qe.SentOnUtc.HasValue);
        if (loadOnlyItemsToBeSent)
        {
            var nowUtc = DateTime.UtcNow;
            query = query.Where(qe => !qe.DontSendBeforeDateUtc.HasValue || qe.DontSendBeforeDateUtc.Value <= nowUtc);
        }

        query = query.Where(qe => qe.SentTries < maxSendTries);
        query = loadNewest ?
            //load the newest records
            query.OrderByDescending(qe => qe.CreatedOnUtc) :
            //load by priority
            query.OrderByDescending(qe => qe.PriorityId).ThenBy(qe => qe.CreatedOnUtc);

        var queuedEmails = await query.ToPagedListAsync(pageIndex, pageSize);

        return queuedEmails;
    }

    /// 
    /// Deletes already sent emails
    /// 
    /// Created date from (UTC); null to load all records
    /// Created date to (UTC); null to load all records
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the number of deleted emails
    /// 
    public virtual async Task DeleteAlreadySentEmailsAsync(DateTime? createdFromUtc, DateTime? createdToUtc)
    {
        var query = _queuedEmailRepository.Table;

        // only sent emails
        query = query.Where(qe => qe.SentOnUtc.HasValue);

        if (createdFromUtc.HasValue)
            query = query.Where(qe => qe.CreatedOnUtc >= createdFromUtc);
        if (createdToUtc.HasValue)
            query = query.Where(qe => qe.CreatedOnUtc <= createdToUtc);

        var emails = query.ToArray();

        await DeleteQueuedEmailsAsync(emails);

        return emails.Length;
    }

    /// 
    /// Delete all queued emails
    /// 
    /// A task that represents the asynchronous operation
    public virtual async Task DeleteAllEmailsAsync()
    {
        await _queuedEmailRepository.TruncateAsync();
    }

    #endregion
}