Webiant Logo Webiant Logo
  1. No results found.

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

AffiliateService.cs

using Nop.Core;
using Nop.Core.Domain.Affiliates;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Seo;
using Nop.Data;
using Nop.Services.Common;
using Nop.Services.Seo;

namespace Nop.Services.Affiliates;

/// 
/// Affiliate service
/// 
public partial class AffiliateService : IAffiliateService
{
    #region Fields

    protected readonly IAddressService _addressService;
    protected readonly IRepository
_addressRepository; protected readonly IRepository _affiliateRepository; protected readonly IRepository _orderRepository; protected readonly IUrlRecordService _urlRecordService; protected readonly IWebHelper _webHelper; protected readonly SeoSettings _seoSettings; #endregion #region Ctor public AffiliateService(IAddressService addressService, IRepository
addressRepository, IRepository affiliateRepository, IRepository orderRepository, IUrlRecordService urlRecordService, IWebHelper webHelper, SeoSettings seoSettings) { _addressService = addressService; _addressRepository = addressRepository; _affiliateRepository = affiliateRepository; _orderRepository = orderRepository; _urlRecordService = urlRecordService; _webHelper = webHelper; _seoSettings = seoSettings; } #endregion #region Methods /// /// Gets an affiliate by affiliate identifier /// /// Affiliate identifier /// /// A task that represents the asynchronous operation /// The task result contains the affiliate /// public virtual async Task GetAffiliateByIdAsync(int affiliateId) { return await _affiliateRepository.GetByIdAsync(affiliateId, cache => default, useShortTermCache: true); } /// /// Gets an affiliate by friendly URL name /// /// Friendly URL name /// /// A task that represents the asynchronous operation /// The task result contains the affiliate /// public virtual async Task GetAffiliateByFriendlyUrlNameAsync(string friendlyUrlName) { if (string.IsNullOrWhiteSpace(friendlyUrlName)) return null; var query = from a in _affiliateRepository.Table orderby a.Id where a.FriendlyUrlName == friendlyUrlName select a; var affiliate = await query.FirstOrDefaultAsync(); return affiliate; } /// /// Marks affiliate as deleted /// /// Affiliate /// A task that represents the asynchronous operation public virtual async Task DeleteAffiliateAsync(Affiliate affiliate) { await _affiliateRepository.DeleteAsync(affiliate); } /// /// Gets all affiliates /// /// Friendly URL name; null to load all records /// First name; null to load all records /// Last name; null to load all records /// Value indicating whether to load affiliates only with orders placed (by affiliated customers) /// Orders created date from (UTC); null to load all records. It's used only with "loadOnlyWithOrders" parameter st to "true". /// Orders created date to (UTC); null to load all records. It's used only with "loadOnlyWithOrders" parameter st to "true". /// Page index /// Page size /// A value indicating whether to show hidden records /// /// A task that represents the asynchronous operation /// The task result contains the affiliates /// public virtual async Task> GetAllAffiliatesAsync(string friendlyUrlName = null, string firstName = null, string lastName = null, bool loadOnlyWithOrders = false, DateTime? ordersCreatedFromUtc = null, DateTime? ordersCreatedToUtc = null, int pageIndex = 0, int pageSize = int.MaxValue, bool showHidden = false) { return await _affiliateRepository.GetAllPagedAsync(query => { if (!string.IsNullOrWhiteSpace(friendlyUrlName)) query = query.Where(a => a.FriendlyUrlName.Contains(friendlyUrlName)); if (!string.IsNullOrWhiteSpace(firstName)) query = from aff in query join addr in _addressRepository.Table on aff.AddressId equals addr.Id where addr.FirstName.Contains(firstName) select aff; if (!string.IsNullOrWhiteSpace(lastName)) query = from aff in query join addr in _addressRepository.Table on aff.AddressId equals addr.Id where addr.LastName.Contains(lastName) select aff; if (!showHidden) query = query.Where(a => a.Active); query = query.Where(a => !a.Deleted); if (loadOnlyWithOrders) { var ordersQuery = _orderRepository.Table; if (ordersCreatedFromUtc.HasValue) ordersQuery = ordersQuery.Where(o => ordersCreatedFromUtc.Value <= o.CreatedOnUtc); if (ordersCreatedToUtc.HasValue) ordersQuery = ordersQuery.Where(o => ordersCreatedToUtc.Value >= o.CreatedOnUtc); ordersQuery = ordersQuery.Where(o => !o.Deleted); query = from a in query join o in ordersQuery on a.Id equals o.AffiliateId select a; } query = query.Distinct().OrderByDescending(a => a.Id); return query; }, pageIndex, pageSize); } /// /// Inserts an affiliate /// /// Affiliate /// A task that represents the asynchronous operation public virtual async Task InsertAffiliateAsync(Affiliate affiliate) { await _affiliateRepository.InsertAsync(affiliate); } /// /// Updates the affiliate /// /// Affiliate /// A task that represents the asynchronous operation public virtual async Task UpdateAffiliateAsync(Affiliate affiliate) { await _affiliateRepository.UpdateAsync(affiliate); } /// /// Get full name /// /// Affiliate /// /// A task that represents the asynchronous operation /// The task result contains the affiliate full name /// public virtual async Task GetAffiliateFullNameAsync(Affiliate affiliate) { ArgumentNullException.ThrowIfNull(affiliate); var affiliateAddress = await _addressService.GetAddressByIdAsync(affiliate.AddressId); if (affiliateAddress == null) return string.Empty; var fullName = $"{affiliateAddress.FirstName} {affiliateAddress.LastName}"; return fullName; } /// /// Generate affiliate URL /// /// Affiliate /// /// A task that represents the asynchronous operation /// The task result contains the generated affiliate URL /// public virtual Task GenerateUrlAsync(Affiliate affiliate) { ArgumentNullException.ThrowIfNull(affiliate); var storeUrl = _webHelper.GetStoreLocation(); var url = !string.IsNullOrEmpty(affiliate.FriendlyUrlName) ? //use friendly URL _webHelper.ModifyQueryString(storeUrl, NopAffiliateDefaults.AffiliateQueryParameter, affiliate.FriendlyUrlName) : //use ID _webHelper.ModifyQueryString(storeUrl, NopAffiliateDefaults.AffiliateIdQueryParameter, affiliate.Id.ToString()); return Task.FromResult(url); } /// /// Validate friendly URL name /// /// Affiliate /// Friendly URL name /// /// A task that represents the asynchronous operation /// The task result contains the valid friendly name /// public virtual async Task ValidateFriendlyUrlNameAsync(Affiliate affiliate, string friendlyUrlName) { ArgumentNullException.ThrowIfNull(affiliate); //ensure we have only valid chars friendlyUrlName = await _urlRecordService.GetSeNameAsync(friendlyUrlName, _seoSettings.ConvertNonWesternChars, _seoSettings.AllowUnicodeCharsInUrls); //max length //(consider a store URL + probably added {0}-{1} below) friendlyUrlName = CommonHelper.EnsureMaximumLength(friendlyUrlName, NopAffiliateDefaults.FriendlyUrlNameLength); //ensure this name is not reserved yet //empty? nothing to check if (string.IsNullOrEmpty(friendlyUrlName)) return friendlyUrlName; //check whether such friendly URL name already exists (and that is not the current affiliate) var i = 2; var tempName = friendlyUrlName; while (true) { var affiliateByFriendlyUrlName = await GetAffiliateByFriendlyUrlNameAsync(tempName); var reserved = affiliateByFriendlyUrlName != null && affiliateByFriendlyUrlName.Id != affiliate.Id; if (!reserved) break; tempName = $"{friendlyUrlName}-{i}"; i++; } friendlyUrlName = tempName; return friendlyUrlName; } #endregion }