Webiant Logo Webiant Logo
  1. No results found.

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

CampaignModelFactory.cs

using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Messages;
using Nop.Services.Helpers;
using Nop.Services.Messages;
using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;
using Nop.Web.Areas.Admin.Models.Messages;
using Nop.Web.Framework.Extensions;
using Nop.Web.Framework.Models.Extensions;

namespace Nop.Web.Areas.Admin.Factories;

/// 
/// Represents the campaign model factory implementation
/// 
public partial class CampaignModelFactory : ICampaignModelFactory
{
    #region Fields

    protected readonly CatalogSettings _catalogSettings;
    protected readonly EmailAccountSettings _emailAccountSettings;
    protected readonly IBaseAdminModelFactory _baseAdminModelFactory;
    protected readonly ICampaignService _campaignService;
    protected readonly IDateTimeHelper _dateTimeHelper;
    protected readonly IMessageTokenProvider _messageTokenProvider;

    #endregion

    #region Ctor

    public CampaignModelFactory(CatalogSettings catalogSettings,
        EmailAccountSettings emailAccountSettings,
        IBaseAdminModelFactory baseAdminModelFactory,
        ICampaignService campaignService,
        IDateTimeHelper dateTimeHelper,
        IMessageTokenProvider messageTokenProvider)
    {
        _catalogSettings = catalogSettings;
        _emailAccountSettings = emailAccountSettings;
        _baseAdminModelFactory = baseAdminModelFactory;
        _campaignService = campaignService;
        _dateTimeHelper = dateTimeHelper;
        _messageTokenProvider = messageTokenProvider;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare campaign search model
    /// 
    /// Campaign search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the campaign search model
    /// 
    public virtual async Task PrepareCampaignSearchModelAsync(CampaignSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare available stores
        await _baseAdminModelFactory.PrepareStoresAsync(searchModel.AvailableStores);

        searchModel.HideStoresList = _catalogSettings.IgnoreStoreLimitations || searchModel.AvailableStores.SelectionIsNotPossible();

        //prepare page parameters
        searchModel.SetGridPageSize();

        return searchModel;
    }

    /// 
    /// Prepare paged campaign list model
    /// 
    /// Campaign search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the campaign list model
    /// 
    public virtual async Task PrepareCampaignListModelAsync(CampaignSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get campaigns
        var campaigns = (await _campaignService.GetAllCampaignsAsync(searchModel.StoreId)).ToPagedList(searchModel);

        //prepare grid model
        var model = await new CampaignListModel().PrepareToGridAsync(searchModel, campaigns, () =>
        {
            return campaigns.SelectAwait(async campaign =>
            {
                //fill in model values from the entity
                var campaignModel = campaign.ToModel();

                //convert dates to the user time
                campaignModel.CreatedOn = await _dateTimeHelper.ConvertToUserTimeAsync(campaign.CreatedOnUtc, DateTimeKind.Utc);
                if (campaign.DontSendBeforeDateUtc.HasValue)
                {
                    campaignModel.DontSendBeforeDate = await _dateTimeHelper
                        .ConvertToUserTimeAsync(campaign.DontSendBeforeDateUtc.Value, DateTimeKind.Utc);
                }

                return campaignModel;
            });
        });

        return model;
    }

    /// 
    /// Prepare campaign model
    /// 
    /// Campaign model
    /// Campaign
    /// Whether to exclude populating of some properties of model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the campaign model
    /// 
    public virtual async Task PrepareCampaignModelAsync(CampaignModel model, Campaign campaign, bool excludeProperties = false)
    {
        //fill in model values from the entity
        if (campaign != null)
        {
            model ??= campaign.ToModel();
            if (campaign.DontSendBeforeDateUtc.HasValue)
                model.DontSendBeforeDate = await _dateTimeHelper.ConvertToUserTimeAsync(campaign.DontSendBeforeDateUtc.Value, DateTimeKind.Utc);
        }

        model.AllowedTokens = string.Join(", ", await _messageTokenProvider.GetListOfCampaignAllowedTokensAsync());

        //whether to fill in some of properties
        if (!excludeProperties)
            model.EmailAccountId = _emailAccountSettings.DefaultEmailAccountId;

        //prepare available stores
        await _baseAdminModelFactory.PrepareStoresAsync(model.AvailableStores);

        //prepare available customer roles
        await _baseAdminModelFactory.PrepareCustomerRolesAsync(model.AvailableCustomerRoles);

        //prepare available email accounts
        await _baseAdminModelFactory.PrepareEmailAccountsAsync(model.AvailableEmailAccounts, false);

        return model;
    }

    #endregion
}