Webiant Logo Webiant Logo
  1. No results found.

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

SaveLastActivityAttribute.cs

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Core.Http.Extensions;
using Nop.Data;

namespace Nop.Web.Framework.Mvc.Filters;

/// 
/// Represents filter attribute that saves last customer activity date
/// 
public sealed class SaveLastActivityAttribute : TypeFilterAttribute
{
    #region Ctor

    /// 
    /// Create instance of the filter attribute
    /// 
    public SaveLastActivityAttribute() : base(typeof(SaveLastActivityFilter))
    {
    }

    #endregion

    #region Nested filter

    /// 
    /// Represents a filter that saves last customer activity date
    /// 
    private class SaveLastActivityFilter : IAsyncActionFilter
    {
        #region Fields

        protected readonly CustomerSettings _customerSettings;
        protected readonly IRepository _customerRepository;
        protected readonly IWorkContext _workContext;

        #endregion

        #region Ctor

        public SaveLastActivityFilter(CustomerSettings customerSettings,
            IRepository customerRepository,
            IWorkContext workContext)
        {
            _customerSettings = customerSettings;
            _customerRepository = customerRepository;
            _workContext = workContext;
        }

        #endregion

        #region Utilities

        /// 
        /// Called asynchronously before the action, after model binding is complete.
        /// 
        /// A context for action filters
        /// A task that represents the asynchronous operation
        private async Task SaveLastActivityAsync(ActionExecutingContext context)
        {
            ArgumentNullException.ThrowIfNull(context);

            //only in GET requests
            if (!context.HttpContext.Request.IsGetRequest())
                return;

            if (!DataSettingsManager.IsDatabaseInstalled())
                return;

            //update last activity date
            var customer = await _workContext.GetCurrentCustomerAsync();
            if (customer.LastActivityDateUtc.AddMinutes(_customerSettings.LastActivityMinutes) < DateTime.UtcNow)
            {
                customer.LastActivityDateUtc = DateTime.UtcNow;

                //update customer without event notification
                await _customerRepository.UpdateAsync(customer, false);
            }
        }

        #endregion

        #region Methods

        /// 
        /// Called asynchronously before the action, after model binding is complete.
        /// 
        /// A context for action filters
        /// A delegate invoked to execute the next action filter or the action itself
        /// A task that represents the asynchronous operation
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            await SaveLastActivityAsync(context);
            if (context.Result == null)
                await next();
        }

        #endregion
    }

    #endregion
}