Try your search with a different keyword or use * as a wildcard.
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Logging;
using Nop.Data;
namespace Nop.Services.Logging;
///
/// Customer activity service
///
public partial class CustomerActivityService : ICustomerActivityService
{
#region Fields
protected readonly IRepository _activityLogRepository;
protected readonly IRepository _activityLogTypeRepository;
protected readonly IWebHelper _webHelper;
protected readonly IWorkContext _workContext;
#endregion
#region Ctor
public CustomerActivityService(IRepository activityLogRepository,
IRepository activityLogTypeRepository,
IWebHelper webHelper,
IWorkContext workContext)
{
_activityLogRepository = activityLogRepository;
_activityLogTypeRepository = activityLogTypeRepository;
_webHelper = webHelper;
_workContext = workContext;
}
#endregion
#region Methods
///
/// Updates an activity log type item
///
/// Activity log type item
/// A task that represents the asynchronous operation
public virtual async Task UpdateActivityTypeAsync(ActivityLogType activityLogType)
{
await _activityLogTypeRepository.UpdateAsync(activityLogType);
}
///
/// Gets all activity log type items
///
///
/// A task that represents the asynchronous operation
/// The task result contains the activity log type items
///
public virtual async Task> GetAllActivityTypesAsync()
{
var activityLogTypes = await _activityLogTypeRepository.GetAllAsync(query =>
{
return from alt in query
orderby alt.Name
select alt;
}, cache => default);
return activityLogTypes;
}
///
/// Gets an activity log type item
///
/// Activity log type identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the activity log type item
///
public virtual async Task GetActivityTypeByIdAsync(int activityLogTypeId)
{
return await _activityLogTypeRepository.GetByIdAsync(activityLogTypeId, cache => default);
}
///
/// Inserts an activity log item
///
/// System keyword
/// Comment
/// Entity
///
/// A task that represents the asynchronous operation
/// The task result contains the activity log item
///
public virtual async Task InsertActivityAsync(string systemKeyword, string comment, BaseEntity entity = null)
{
return await InsertActivityAsync(await _workContext.GetCurrentCustomerAsync(), systemKeyword, comment, entity);
}
///
/// Inserts an activity log item
///
/// Customer
/// System keyword
/// Comment
/// Entity
///
/// A task that represents the asynchronous operation
/// The task result contains the activity log item
///
public virtual async Task InsertActivityAsync(Customer customer, string systemKeyword, string comment, BaseEntity entity = null)
{
if (customer == null || customer.IsSearchEngineAccount())
return null;
//try to get activity log type by passed system keyword
var activityLogType = (await GetAllActivityTypesAsync()).FirstOrDefault(type => type.SystemKeyword.Equals(systemKeyword));
if (!activityLogType?.Enabled ?? true)
return null;
//insert log item
var logItem = new ActivityLog
{
ActivityLogTypeId = activityLogType.Id,
EntityId = entity?.Id,
EntityName = entity?.GetType().Name,
CustomerId = customer.Id,
Comment = CommonHelper.EnsureMaximumLength(comment ?? string.Empty, 4000),
CreatedOnUtc = DateTime.UtcNow,
IpAddress = _webHelper.GetCurrentIpAddress()
};
await _activityLogRepository.InsertAsync(logItem);
return logItem;
}
///
/// Deletes an activity log item
///
/// Activity log type
/// A task that represents the asynchronous operation
public virtual async Task DeleteActivityAsync(ActivityLog activityLog)
{
await _activityLogRepository.DeleteAsync(activityLog);
}
///
/// Gets all activity log items
///
/// Log item creation from; pass null to load all records
/// Log item creation to; pass null to load all records
/// Customer identifier; pass null to load all records
/// Activity log type identifier; pass null to load all records
/// IP address; pass null or empty to load all records
/// Entity name; pass null to load all records
/// Entity identifier; pass null to load all records
/// Page index
/// Page size
///
/// A task that represents the asynchronous operation
/// The task result contains the activity log items
///
public virtual async Task> GetAllActivitiesAsync(DateTime? createdOnFrom = null, DateTime? createdOnTo = null,
int? customerId = null, int? activityLogTypeId = null, string ipAddress = null, string entityName = null, int? entityId = null,
int pageIndex = 0, int pageSize = int.MaxValue)
{
return await _activityLogRepository.GetAllPagedAsync(query =>
{
//filter by IP
if (!string.IsNullOrEmpty(ipAddress))
query = query.Where(logItem => logItem.IpAddress.Contains(ipAddress));
//filter by creation date
if (createdOnFrom.HasValue)
query = query.Where(logItem => createdOnFrom.Value <= logItem.CreatedOnUtc);
if (createdOnTo.HasValue)
query = query.Where(logItem => createdOnTo.Value >= logItem.CreatedOnUtc);
//filter by log type
if (activityLogTypeId.HasValue && activityLogTypeId.Value > 0)
query = query.Where(logItem => activityLogTypeId == logItem.ActivityLogTypeId);
//filter by customer
if (customerId.HasValue && customerId.Value > 0)
query = query.Where(logItem => customerId.Value == logItem.CustomerId);
//filter by entity
if (!string.IsNullOrEmpty(entityName))
query = query.Where(logItem => logItem.EntityName.Equals(entityName));
if (entityId.HasValue && entityId.Value > 0)
query = query.Where(logItem => entityId.Value == logItem.EntityId);
query = query.OrderByDescending(logItem => logItem.CreatedOnUtc).ThenBy(logItem => logItem.Id);
return query;
}, pageIndex, pageSize);
}
///
/// Gets an activity log item
///
/// Activity log identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the activity log item
///
public virtual async Task GetActivityByIdAsync(int activityLogId)
{
return await _activityLogRepository.GetByIdAsync(activityLogId);
}
///
/// Clears activity log
///
/// A task that represents the asynchronous operation
public virtual async Task ClearAllActivitiesAsync()
{
await _activityLogRepository.TruncateAsync();
}
#endregion
}