Skip to content

Commit

Permalink
TreeNode under Worklfow update fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kapustb committed Oct 19, 2023
1 parent 024e13f commit c58ef94
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/AlgoliaSearchModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using CMS.DataEngine;
using CMS.DocumentEngine;
using CMS.Helpers;

using DocumentFormat.OpenXml.Wordprocessing;
using Kentico.Xperience.Algolia.Extensions;
using Kentico.Xperience.Algolia.Services;

Expand Down Expand Up @@ -60,7 +60,8 @@ protected override void OnInit()

DocumentEvents.Delete.Before += HandleDocumentEvent;
DocumentEvents.Update.Before += HandleDocumentEvent;
DocumentEvents.Update.Before += ForceMoveUpdate;
DocumentEvents.Update.After += ForceMoveUpdate;
DocumentEvents.Delete.After += ForceMoveUpdate;
DocumentEvents.Insert.After += HandleDocumentEvent;
WorkflowEvents.Publish.After += HandleWorkflowEvent;
WorkflowEvents.Archive.After += HandleWorkflowEvent;
Expand Down Expand Up @@ -108,15 +109,15 @@ private void HandleDocumentEvent(object sender, DocumentEventArgs e)

private void ForceMoveUpdate(object sender, DocumentEventArgs e)
{
var columns = e.Node.ChangedColumns();
if (columns.Any(col => col.Equals(nameof(TreeNode.NodeParentID), System.StringComparison.OrdinalIgnoreCase)))
if (!EventShouldContinue(e.Node))
{
algoliaTaskLogger.HandleEvent(e.Node, WorkflowEvents.Publish.Name);
return;
}

foreach (var child in e.Node.AllChildren)
{
algoliaTaskLogger.HandleEvent(child, WorkflowEvents.Publish.Name);
}
var columns = e.Node.ChangedColumns();
if (columns.Any(x => x.Equals(nameof(TreeNode.NodeParentID), System.StringComparison.OrdinalIgnoreCase)))
{
algoliaTaskLogger.HandleEventAfter(e.Node, e.CurrentHandler.Name);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Services/IAlgoliaTaskLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public interface IAlgoliaTaskLogger
/// <param name="node">The <see cref="TreeNode"/> that triggered the event.</param>
/// <param name="eventName">The name of the Xperience event that was triggered.</param>
void HandleEvent(TreeNode node, string eventName);
void HandleEventAfter(TreeNode node, string eventName);
}
}
21 changes: 21 additions & 0 deletions src/Services/Implementations/DefaultAlgoliaTaskLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ public void HandleEvent(TreeNode node, string eventName)
}
}

public void HandleEventAfter(TreeNode node, string eventName)
{
foreach (var indexName in IndexStore.Instance.GetAll().Select(index => index.IndexName))
{
if (!node.IsIndexedByIndex(indexName))
{
continue;
}

try
{
var queueItem = new AlgoliaQueueItem(node, GetTaskType(node, eventName), indexName, node.ChangedColumns());
AlgoliaQueueWorker.Current.EnqueueAlgoliaQueueItem(queueItem);
}
catch (InvalidOperationException ex)
{
eventLogService.LogException(nameof(DefaultAlgoliaTaskLogger), nameof(HandleEvent), ex);
}
}
}

private AlgoliaTaskType GetTaskType(TreeNode node, string eventName)
{
if (eventName.Equals(DocumentEvents.Insert.Name, StringComparison.OrdinalIgnoreCase) ||
Expand Down

0 comments on commit c58ef94

Please sign in to comment.