Skip to content
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -18,37 +17,68 @@ namespace VirtoCommerce.OrdersModule.Data.Handlers
{
public class IndexCustomerOrderChangedEventHandler : IEventHandler<OrderChangedEvent>
{
private readonly ISettingsManager _settingsManager;
private readonly IConfiguration _configuration;
private readonly IIndexingJobService _indexingJobService;
private readonly IEnumerable<IndexDocumentConfiguration> _indexingConfigurations;
protected ISettingsManager SettingsManager { get; }

protected IConfiguration Configuration { get; }

protected IIndexingJobService IndexingJobService { get; }

protected IEnumerable<IndexDocumentConfiguration> IndexingConfigurations { get; }

public IndexCustomerOrderChangedEventHandler(
ISettingsManager settingsManager,
IConfiguration configuration,
IIndexingJobService indexingJobService,
IEnumerable<IndexDocumentConfiguration> indexingConfigurations)
{
_settingsManager = settingsManager;
_configuration = configuration;
_indexingJobService = indexingJobService;
_indexingConfigurations = indexingConfigurations;
SettingsManager = settingsManager;
Configuration = configuration;
IndexingJobService = indexingJobService;
IndexingConfigurations = indexingConfigurations;
}

public async Task Handle(OrderChangedEvent message)
public virtual async Task Handle(OrderChangedEvent message)
{
if (!_configuration.IsOrderFullTextSearchEnabled() ||
!await _settingsManager.GetValueAsync<bool>(ModuleConstants.Settings.General.EventBasedIndexation))
if (!await ShouldIndexAsync())
{
return;
}

var indexEntries = message?.ChangedEntries
.Select(x => new IndexEntry { Id = x.OldEntry.Id, EntryState = x.EntryState, Type = ModuleConstants.OrderIndexDocumentType })
.ToArray() ?? Array.Empty<IndexEntry>();
await IndexOrdersAsync(message);
}

protected virtual async Task<bool> ShouldIndexAsync()
{
return Configuration.IsOrderFullTextSearchEnabled() &&
await SettingsManager.GetValueAsync<bool>(ModuleConstants.Settings.General.EventBasedIndexation);
}

protected virtual Task IndexOrdersAsync(OrderChangedEvent message)
{
var indexEntries = GetOrderIndexEntries(message);

if (indexEntries.Length > 0)
{
var documentBuilders = IndexingConfigurations
.GetDocumentBuilders(ModuleConstants.OrderIndexDocumentType, typeof(CustomerOrderChangesProvider))
.ToList();

IndexingJobService.EnqueueIndexAndDeleteDocuments(indexEntries, JobPriority.Normal, documentBuilders);
}

return Task.CompletedTask;
}

_indexingJobService.EnqueueIndexAndDeleteDocuments(indexEntries,
JobPriority.Normal, _indexingConfigurations.GetDocumentBuilders(ModuleConstants.OrderIndexDocumentType, typeof(CustomerOrderChangesProvider)).ToList());
protected virtual IndexEntry[] GetOrderIndexEntries(OrderChangedEvent message)
{
return message?.ChangedEntries
.Select(x => new IndexEntry
{
Id = x.OldEntry.Id,
EntryState = x.EntryState,
Type = ModuleConstants.OrderIndexDocumentType,
})
.ToArray() ?? [];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VirtoCommerce.CatalogModule.Core.Model;
using VirtoCommerce.CustomerModule.Core.Model;
using VirtoCommerce.OrdersModule.Core.Model;
using VirtoCommerce.OrdersModule.Data.Repositories;
using VirtoCommerce.Platform.Core.ChangeLog;
Expand All @@ -17,83 +15,84 @@ public class CustomerOrderChangesProvider : IIndexDocumentChangesProvider
{
public const string ChangeLogObjectType = nameof(CustomerOrder);

private readonly Func<IOrderRepository> _orderRepositoryFactory;
private readonly IChangeLogSearchService _changeLogSearchService;
protected Func<IOrderRepository> OrderRepositoryFactory { get; }

protected IChangeLogSearchService ChangeLogSearchService { get; }

public CustomerOrderChangesProvider(Func<IOrderRepository> orderRepositoryFactory, IChangeLogSearchService changeLogSearchService)
{
_orderRepositoryFactory = orderRepositoryFactory;
_changeLogSearchService = changeLogSearchService;
OrderRepositoryFactory = orderRepositoryFactory;
ChangeLogSearchService = changeLogSearchService;
}

public async Task<IList<IndexDocumentChange>> GetChangesAsync(DateTime? startDate, DateTime? endDate, long skip, long take)
public virtual async Task<IList<IndexDocumentChange>> GetChangesAsync(DateTime? startDate, DateTime? endDate, long skip, long take)
{
if (startDate == null && endDate == null)
{
return GetChangesFromRepository(skip, take);
}

return await GetChangesFromOperaionLog(startDate, endDate, skip, take);
return await GetChangesFromOperationLog(startDate, endDate, skip, take);
}

public async Task<long> GetTotalChangesCountAsync(DateTime? startDate, DateTime? endDate)
public virtual async Task<long> GetTotalChangesCountAsync(DateTime? startDate, DateTime? endDate)
{
if (startDate == null && endDate == null)
{
// Get total products count
using (var repository = _orderRepositoryFactory())
{
return repository.CustomerOrders.Count();
}
using var repository = OrderRepositoryFactory();

return repository.CustomerOrders.Count();
}

var criteria = GetChangeLogSearchCriteria(startDate, endDate, 0, 0);

// Get changes count from operation log
return (await _changeLogSearchService.SearchAsync(criteria)).TotalCount;
return (await ChangeLogSearchService.SearchAsync(criteria)).TotalCount;
}

/// <summary>
/// Get documents from repository and return them as changes
/// </summary>
private IList<IndexDocumentChange> GetChangesFromRepository(long skip, long take)
protected virtual IList<IndexDocumentChange> GetChangesFromRepository(long skip, long take)
{
using (var repository = _orderRepositoryFactory())
{
var productIds = repository.CustomerOrders
.OrderBy(i => i.CreatedDate)
.Select(i => i.Id)
.Skip((int)skip)
.Take((int)take)
.ToArray();

return productIds.Select(id =>
using var repository = OrderRepositoryFactory();

var productIds = repository.CustomerOrders
.OrderBy(x => x.CreatedDate)
.Select(x => new { x.Id, ModifiedDate = x.ModifiedDate ?? x.CreatedDate })
.Skip((int)skip)
.Take((int)take)
.ToArray();

return productIds
.Select(x =>
new IndexDocumentChange
{
DocumentId = id,
DocumentId = x.Id,
ChangeType = IndexDocumentChangeType.Modified,
ChangeDate = DateTime.UtcNow
}
).ToArray();
}
ChangeDate = x.ModifiedDate,
})
.ToArray();
}

/// <summary>
/// Get changes from operation log
/// </summary>
private async Task<IList<IndexDocumentChange>> GetChangesFromOperaionLog(DateTime? startDate, DateTime? endDate, long skip, long take)
protected virtual async Task<IList<IndexDocumentChange>> GetChangesFromOperationLog(DateTime? startDate, DateTime? endDate, long skip, long take)
{
var criteria = GetChangeLogSearchCriteria(startDate, endDate, skip, take);
var operations = (await _changeLogSearchService.SearchAsync(criteria)).Results;

return operations.Select(o =>
new IndexDocumentChange
{
DocumentId = o.ObjectId,
ChangeType = o.OperationType == EntryState.Deleted ? IndexDocumentChangeType.Deleted : IndexDocumentChangeType.Modified,
ChangeDate = o.ModifiedDate ?? o.CreatedDate,
}
).ToArray();
var operations = (await ChangeLogSearchService.SearchAsync(criteria)).Results;

return operations
.Select(x =>
new IndexDocumentChange
{
DocumentId = x.ObjectId,
ChangeType = x.OperationType == EntryState.Deleted ? IndexDocumentChangeType.Deleted : IndexDocumentChangeType.Modified,
ChangeDate = x.ModifiedDate ?? x.CreatedDate,
})
.ToArray();
}

protected virtual ChangeLogSearchCriteria GetChangeLogSearchCriteria(DateTime? startDate, DateTime? endDate, long skip, long take)
Expand All @@ -104,12 +103,12 @@ protected virtual ChangeLogSearchCriteria GetChangeLogSearchCriteria(DateTime? s

if (types.Count != 0)
{
types.Add(nameof(CustomerOrder));
types.Add(ChangeLogObjectType);
criteria.ObjectTypes = types;
}
else
{
criteria.ObjectType = nameof(CustomerOrder);
criteria.ObjectType = ChangeLogObjectType;
}

criteria.StartDate = startDate;
Expand Down
Loading