Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexed node updated on any parent movement in the tree #34

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/AlgoliaSearchModule.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Configuration;

using System.Linq;
using Algolia.Search.Clients;

using CMS.Base;
using CMS.Core;
using CMS.DataEngine;
using CMS.DocumentEngine;
using CMS.Helpers;

using Kentico.Xperience.Algolia.Extensions;
using Kentico.Xperience.Algolia.Services;

Expand Down Expand Up @@ -59,7 +58,7 @@ protected override void OnInit()
conversionService = Service.Resolve<IConversionService>();

DocumentEvents.Delete.Before += HandleDocumentEvent;
DocumentEvents.Update.Before += HandleDocumentEvent;
DocumentEvents.Update.Before += HandleDocumentUpdate;
DocumentEvents.Insert.After += HandleDocumentEvent;
WorkflowEvents.Publish.After += HandleWorkflowEvent;
WorkflowEvents.Archive.After += HandleWorkflowEvent;
Expand Down Expand Up @@ -104,5 +103,23 @@ private void HandleDocumentEvent(object sender, DocumentEventArgs e)

algoliaTaskLogger.HandleEvent(e.Node, e.CurrentHandler.Name);
}

private void HandleDocumentUpdate(object sender, DocumentEventArgs e)
{
if (!EventShouldContinue(e.Node))
{
return;
}

algoliaTaskLogger.HandleEvent(e.Node, e.CurrentHandler.Name);

var columns = e.Node.ChangedColumns();
if (columns.Any(x => x.Equals(nameof(TreeNode.NodeParentID), System.StringComparison.OrdinalIgnoreCase)))
{
var treeProvider = new TreeProvider();
var node = treeProvider.SelectSingleDocument(e.Node.DocumentID, coupledData: true);
algoliaTaskLogger.ForceHandleEvent(node, e.CurrentHandler.Name);
}
}
}
}
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 ForceHandleEvent(TreeNode node, string eventName);
}
}
22 changes: 18 additions & 4 deletions src/Services/Implementations/DefaultAlgoliaTaskLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal class DefaultAlgoliaTaskLogger : IAlgoliaTaskLogger
{
private readonly IEventLogService eventLogService;


public DefaultAlgoliaTaskLogger(IEventLogService eventLogService) {
this.eventLogService = eventLogService;
}
Expand All @@ -32,6 +31,22 @@ public void HandleEvent(TreeNode node, string eventName)
return;
}

LogAlgoliaTask(node, eventName, nameof(HandleEvent));
}

/// <summary>
/// Loops through all registered Algolia indexes and logs a task if the passed
/// <paramref name="node"/> is indexed. Does not check for worklfow of the node and indexes the node even if under a workflow.
/// </summary>
/// <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>
public void ForceHandleEvent(TreeNode node, string eventName)
{
LogAlgoliaTask(node, eventName, nameof(ForceHandleEvent));
}

private void LogAlgoliaTask(TreeNode node, string eventName, string exceptionLogEventCode)
{
foreach (var indexName in IndexStore.Instance.GetAll().Select(index => index.IndexName))
bkapustik marked this conversation as resolved.
Show resolved Hide resolved
{
if (!node.IsIndexedByIndex(indexName))
Expand All @@ -46,16 +61,15 @@ public void HandleEvent(TreeNode node, string eventName)
}
catch (InvalidOperationException ex)
{
eventLogService.LogException(nameof(DefaultAlgoliaTaskLogger), nameof(HandleEvent), ex);
eventLogService.LogException(nameof(DefaultAlgoliaTaskLogger), exceptionLogEventCode, ex);
}
}
}


private AlgoliaTaskType GetTaskType(TreeNode node, string eventName)
{
if (eventName.Equals(DocumentEvents.Insert.Name, StringComparison.OrdinalIgnoreCase) ||
(eventName.Equals(WorkflowEvents.Publish.Name, StringComparison.OrdinalIgnoreCase) && node.WorkflowHistory.Count == 0))
(eventName.Equals(WorkflowEvents.Publish.Name, StringComparison.OrdinalIgnoreCase) && (node.WorkflowHistory == null || node.WorkflowHistory.Count == 0)))
{
return AlgoliaTaskType.CREATE;
}
Expand Down
Loading