Try your search with a different keyword or use * as a wildcard.
using Nop.Core;
using Nop.Core.Domain.Messages;
using Nop.Data;
using Nop.Services.Customers;
namespace Nop.Services.Messages;
///
/// Campaign service
///
public partial class CampaignService : ICampaignService
{
#region Fields
protected readonly ICustomerService _customerService;
protected readonly IEmailSender _emailSender;
protected readonly IMessageTokenProvider _messageTokenProvider;
protected readonly IQueuedEmailService _queuedEmailService;
protected readonly IRepository _campaignRepository;
protected readonly IStoreContext _storeContext;
protected readonly ITokenizer _tokenizer;
#endregion
#region Ctor
public CampaignService(ICustomerService customerService,
IEmailSender emailSender,
IMessageTokenProvider messageTokenProvider,
IQueuedEmailService queuedEmailService,
IRepository campaignRepository,
IStoreContext storeContext,
ITokenizer tokenizer)
{
_customerService = customerService;
_emailSender = emailSender;
_messageTokenProvider = messageTokenProvider;
_queuedEmailService = queuedEmailService;
_campaignRepository = campaignRepository;
_storeContext = storeContext;
_tokenizer = tokenizer;
}
#endregion
#region Methods
///
/// Inserts a campaign
///
/// Campaign
/// A task that represents the asynchronous operation
public virtual async Task InsertCampaignAsync(Campaign campaign)
{
await _campaignRepository.InsertAsync(campaign);
}
///
/// Updates a campaign
///
/// Campaign
/// A task that represents the asynchronous operation
public virtual async Task UpdateCampaignAsync(Campaign campaign)
{
await _campaignRepository.UpdateAsync(campaign);
}
///
/// Deleted a queued email
///
/// Campaign
/// A task that represents the asynchronous operation
public virtual async Task DeleteCampaignAsync(Campaign campaign)
{
await _campaignRepository.DeleteAsync(campaign);
}
///
/// Gets a campaign by identifier
///
/// Campaign identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the campaign
///
public virtual async Task GetCampaignByIdAsync(int campaignId)
{
return await _campaignRepository.GetByIdAsync(campaignId, cache => default);
}
///
/// Gets all campaigns
///
/// Store identifier; 0 to load all records
///
/// A task that represents the asynchronous operation
/// The task result contains the campaigns
///
public virtual async Task> GetAllCampaignsAsync(int storeId = 0)
{
var campaigns = await _campaignRepository.GetAllAsync(query =>
{
if (storeId > 0)
query = query.Where(c => c.StoreId == storeId);
query = query.OrderBy(c => c.CreatedOnUtc);
return query;
});
return campaigns;
}
///
/// Sends a campaign to specified emails
///
/// Campaign
/// Email account
/// Subscriptions
///
/// A task that represents the asynchronous operation
/// The task result contains the otal emails sent
///
public virtual async Task SendCampaignAsync(Campaign campaign, EmailAccount emailAccount,
IEnumerable subscriptions)
{
ArgumentNullException.ThrowIfNull(campaign);
ArgumentNullException.ThrowIfNull(emailAccount);
var totalEmailsSent = 0;
foreach (var subscription in subscriptions)
{
var customer = await _customerService.GetCustomerByEmailAsync(subscription.Email);
//ignore deleted or inactive customers when sending newsletter campaigns
if (customer != null && (!customer.Active || customer.Deleted))
continue;
var tokens = new List();
await _messageTokenProvider.AddStoreTokensAsync(tokens, await _storeContext.GetCurrentStoreAsync(), emailAccount);
await _messageTokenProvider.AddNewsLetterSubscriptionTokensAsync(tokens, subscription);
if (customer != null)
await _messageTokenProvider.AddCustomerTokensAsync(tokens, customer);
var subject = _tokenizer.Replace(campaign.Subject, tokens, false);
var body = _tokenizer.Replace(campaign.Body, tokens, true);
var email = new QueuedEmail
{
Priority = QueuedEmailPriority.Low,
From = emailAccount.Email,
FromName = emailAccount.DisplayName,
To = subscription.Email,
Subject = subject,
Body = body,
CreatedOnUtc = DateTime.UtcNow,
EmailAccountId = emailAccount.Id,
DontSendBeforeDateUtc = campaign.DontSendBeforeDateUtc
};
await _queuedEmailService.InsertQueuedEmailAsync(email);
totalEmailsSent++;
}
return totalEmailsSent;
}
///
/// Sends a campaign to specified email
///
/// Campaign
/// Email account
/// Email
/// A task that represents the asynchronous operation
public virtual async Task SendCampaignAsync(Campaign campaign, EmailAccount emailAccount, string email)
{
ArgumentNullException.ThrowIfNull(campaign);
ArgumentNullException.ThrowIfNull(emailAccount);
var tokens = new List();
await _messageTokenProvider.AddStoreTokensAsync(tokens, await _storeContext.GetCurrentStoreAsync(), emailAccount);
var customer = await _customerService.GetCustomerByEmailAsync(email);
if (customer != null)
await _messageTokenProvider.AddCustomerTokensAsync(tokens, customer);
var subject = _tokenizer.Replace(campaign.Subject, tokens, false);
var body = _tokenizer.Replace(campaign.Body, tokens, true);
await _emailSender.SendEmailAsync(emailAccount, subject, body, emailAccount.Email, emailAccount.DisplayName, email, null);
}
#endregion
}