Webiant Logo Webiant Logo
  1. No results found.

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

BackInStockSubscriptionService.cs

using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Customers;
using Nop.Data;
using Nop.Services.Messages;

namespace Nop.Services.Catalog;

/// 
/// Back in stock subscription service
/// 
public partial class BackInStockSubscriptionService : IBackInStockSubscriptionService
{
    #region Fields

    protected readonly IRepository _backInStockSubscriptionRepository;
    protected readonly IRepository _customerRepository;
    protected readonly IRepository _productRepository;
    protected readonly IWorkflowMessageService _workflowMessageService;

    #endregion

    #region Ctor

    public BackInStockSubscriptionService(IRepository backInStockSubscriptionRepository,
        IRepository customerRepository,
        IRepository productRepository,
        IWorkflowMessageService workflowMessageService)
    {
        _backInStockSubscriptionRepository = backInStockSubscriptionRepository;
        _customerRepository = customerRepository;
        _productRepository = productRepository;
        _workflowMessageService = workflowMessageService;
    }

    #endregion

    #region Methods

    /// 
    /// Delete a back in stock subscription
    /// 
    /// Subscription
    /// A task that represents the asynchronous operation
    public virtual async Task DeleteSubscriptionAsync(BackInStockSubscription subscription)
    {
        await _backInStockSubscriptionRepository.DeleteAsync(subscription);
    }

    /// 
    /// Gets all subscriptions
    /// 
    /// Customer identifier
    /// Store identifier; pass 0 to load all records
    /// Page index
    /// Page size
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the subscriptions
    /// 
    public virtual async Task> GetAllSubscriptionsByCustomerIdAsync(int customerId,
        int storeId = 0, int pageIndex = 0, int pageSize = int.MaxValue)
    {
        return await _backInStockSubscriptionRepository.GetAllPagedAsync(query =>
        {
            //customer
            query = query.Where(biss => biss.CustomerId == customerId);

            //store
            if (storeId > 0)
                query = query.Where(biss => biss.StoreId == storeId);

            //product
            query = from q in query
                join p in _productRepository.Table on q.ProductId equals p.Id
                where !p.Deleted
                select q;

            query = query.OrderByDescending(biss => biss.CreatedOnUtc);

            return query;
        }, pageIndex, pageSize);
    }

    /// 
    /// Gets all subscriptions
    /// 
    /// Customer id
    /// Product identifier
    /// Store identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the subscriptions
    /// 
    public virtual async Task FindSubscriptionAsync(int customerId, int productId, int storeId)
    {
        var query = from biss in _backInStockSubscriptionRepository.Table
            orderby biss.CreatedOnUtc descending
            where biss.CustomerId == customerId &&
                  biss.ProductId == productId &&
                  biss.StoreId == storeId
            select biss;

        var subscription = await query.FirstOrDefaultAsync();

        return subscription;
    }

    /// 
    /// Gets a subscription
    /// 
    /// Subscription identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the subscription
    /// 
    public virtual async Task GetSubscriptionByIdAsync(int subscriptionId)
    {
        return await _backInStockSubscriptionRepository.GetByIdAsync(subscriptionId, cache => default, useShortTermCache: true);
    }

    /// 
    /// Inserts subscription
    /// 
    /// Subscription
    /// A task that represents the asynchronous operation
    public virtual async Task InsertSubscriptionAsync(BackInStockSubscription subscription)
    {
        await _backInStockSubscriptionRepository.InsertAsync(subscription);
    }

    /// 
    /// Send notification to subscribers
    /// 
    /// Product
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the number of sent email
    /// 
    public virtual async Task SendNotificationsToSubscribersAsync(Product product)
    {
        ArgumentNullException.ThrowIfNull(product);

        var result = 0;
        var subscriptions = await GetAllSubscriptionsByProductIdAsync(product.Id);
        foreach (var subscription in subscriptions)
        {
            var customer = await _customerRepository.GetByIdAsync(subscription.CustomerId);
            result += (await _workflowMessageService.SendBackInStockNotificationAsync(subscription, customer?.LanguageId ?? 0)).Count;
        }

        for (var i = 0; i <= subscriptions.Count - 1; i++)
            await DeleteSubscriptionAsync(subscriptions[i]);

        return result;
    }

    /// 
    /// Gets all subscriptions
    /// 
    /// Product identifier
    /// Store identifier; pass 0 to load all records
    /// Page index
    /// Page size
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the subscriptions
    /// 
    public virtual async Task> GetAllSubscriptionsByProductIdAsync(int productId,
        int storeId = 0, int pageIndex = 0, int pageSize = int.MaxValue)
    {
        return await _backInStockSubscriptionRepository.GetAllPagedAsync(query =>
        {
            //product
            query = query.Where(biss => biss.ProductId == productId);
            //store
            if (storeId > 0)
                query = query.Where(biss => biss.StoreId == storeId);
            //customer
            query = from biss in query
                join c in _customerRepository.Table on biss.CustomerId equals c.Id
                where c.Active && !c.Deleted
                select biss;

            query = query.OrderByDescending(biss => biss.CreatedOnUtc);

            return query;
        }, pageIndex, pageSize);
    }

    #endregion
}