diff --git a/uSync.BackOffice/Extensions/uSyncActionExtensions.cs b/uSync.BackOffice/Extensions/uSyncActionExtensions.cs index 9aba66b7..a13cb9d6 100644 --- a/uSync.BackOffice/Extensions/uSyncActionExtensions.cs +++ b/uSync.BackOffice/Extensions/uSyncActionExtensions.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using Umbraco.Extensions; @@ -29,35 +30,44 @@ public static int CountChanges(this IEnumerable actions) /// public static bool IsValidAction(this HandlerActions requestedAction, IEnumerable actions) => requestedAction == HandlerActions.None || - actions.Count() == 0 || + actions.Count() == 0 || actions.InvariantContains("all") || actions.InvariantContains(requestedAction.ToString()); /// /// Convert a list of actions into a summary list of actions, uses less cpu when people sync massive amounts of content. /// - public static IEnumerable ConvertToSummary(this IEnumerable actions, bool strict) + public static IEnumerable ConvertToSummary(this IEnumerable actions, bool strict) { var summary = new List(); - foreach(var items in actions.GroupBy(x => x.HandlerAlias)) + foreach (var items in actions.GroupBy(x => x.HandlerAlias)) { var fails = items.Where(x => !x.Success).ToList(); summary.Add(uSyncAction.SetAction( - success: true, - name: items.Key, - type: items.Key, + success: true, + name: items.Key, + type: items.Key, change: Core.ChangeType.Information, message: $"({items.CountChanges()}/{items.Count()} Changes) ({fails.Count} failures)") - ); + ); if (!strict) summary.AddRange(fails); - + } return summary; } + /// + /// try to find an action in the list based on key, and handler alias + /// + public static bool TryFindAction(this IEnumerable actions, Guid key, string handlerAlias, out uSyncAction action) + { + action = actions.FirstOrDefault(x => $"{x.key}_{x.HandlerAlias}" == $"{key}_{handlerAlias}", new uSyncAction { key = Guid.Empty }); + return action.key != Guid.Empty; + } + } } diff --git a/uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs b/uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs index f7961cfc..4a146e56 100644 --- a/uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs +++ b/uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs @@ -573,15 +573,14 @@ private void PerformSecondPassImports(List> importedItems, // if the second attempt has a message on it, add it to the first attempt. if (!string.IsNullOrWhiteSpace(attempt.Message) || attempt.Details?.Any() == true) { - uSyncAction action = actions.FirstOrDefault(x => $"{x.key}_{x.HandlerAlias}" == $"{itemKey}_{this.Alias}", new uSyncAction { key = Guid.Empty }); - if (action.key != Guid.Empty) + if (actions.TryFindAction(itemKey, this.Alias, out var action)) { actions.Remove(action); action.Message += attempt.Message ?? ""; if (attempt.Details?.Any() == true) { - var details = action.Details.ToList(); + var details = action.Details?.ToList() ?? []; details.AddRange(attempt.Details); action.Details = details; } @@ -595,8 +594,7 @@ private void PerformSecondPassImports(List> importedItems, } else { - uSyncAction action = actions.FirstOrDefault(x => $"{x.key}_{x.HandlerAlias}" == $"{itemKey}_{this.Alias}", new uSyncAction { key = Guid.Empty }); - if (action.key != Guid.Empty) + if (actions.TryFindAction(itemKey, this.Alias, out var action)) { actions.Remove(action); action.Success = attempt.Success;