-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Delete all Summary 2.0 components that has a reference to a del…
…eted layout
- Loading branch information
Showing
4 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
backend/src/Designer/EventHandlers/LayoutPageDeleted/LayoutPageDeleterHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Altinn.Studio.Designer.Events; | ||
using Altinn.Studio.Designer.Hubs.SyncHub; | ||
using Altinn.Studio.Designer.Infrastructure.GitRepository; | ||
using Altinn.Studio.Designer.Models; | ||
using Altinn.Studio.Designer.Services.Interfaces; | ||
using MediatR; | ||
using System.Text.Json.Nodes; | ||
using Altinn.App.Core.Helpers; | ||
|
||
namespace Altinn.Studio.Designer.EventHandlers.LayoutPageDeleted; | ||
|
||
public class LayoutPageDeletedHandler(IAltinnGitRepositoryFactory altinnGitRepositoryFactory, | ||
IFileSyncHandlerExecutor fileSyncHandlerExecutor) : INotificationHandler<LayoutPageDeletedEvent> | ||
{ | ||
public async Task Handle(LayoutPageDeletedEvent notification, CancellationToken cancellationToken) | ||
{ | ||
AltinnAppGitRepository altinnAppGitRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository( | ||
notification.EditingContext.Org, | ||
notification.EditingContext.Repo, | ||
notification.EditingContext.Developer); | ||
|
||
LayoutSets layoutSets = await altinnAppGitRepository.GetLayoutSetsFile(cancellationToken); | ||
|
||
await fileSyncHandlerExecutor.ExecuteWithExceptionHandlingAndConditionalNotification( | ||
notification.EditingContext, | ||
SyncErrorCodes.LayoutPageDeletedSyncError, | ||
"layouts", | ||
async () => | ||
{ | ||
bool hasChanges = false; | ||
foreach (LayoutSetConfig layoutSet in layoutSets.Sets) | ||
{ | ||
Dictionary<string, JsonNode> formLayouts = await altinnAppGitRepository.GetFormLayouts(layoutSet.Id, cancellationToken); | ||
foreach (var formLayout in formLayouts) | ||
{ | ||
hasChanges |= await RemoveComponentsReferencingLayout( | ||
notification, | ||
altinnAppGitRepository, | ||
layoutSets.Sets, | ||
layoutSet.Id, | ||
formLayout, | ||
cancellationToken); | ||
} | ||
} | ||
return hasChanges; | ||
}); | ||
} | ||
|
||
private static async Task<bool> RemoveComponentsReferencingLayout(LayoutPageDeletedEvent notification, AltinnAppGitRepository altinnAppGitRepository, List<LayoutSetConfig> layoutSets, string layoutSetName, KeyValuePair<string, JsonNode> formLayout, CancellationToken cancellationToken) | ||
{ | ||
if (formLayout.Value["data"] is not JsonObject data || data["layout"] is not JsonArray layoutArray) | ||
{ | ||
return false; | ||
} | ||
|
||
bool hasChanges = false; | ||
layoutArray.RemoveAll(jsonNode => | ||
{ | ||
if (jsonNode["type"]?.GetValue<string>() == "Summary2" && jsonNode["target"] is JsonObject targetObject) | ||
{ | ||
string summaryType = targetObject["type"]?.GetValue<string>(); | ||
string taskId = targetObject["taskId"]?.GetValue<string>(); | ||
string layouSetId = string.IsNullOrEmpty(taskId) ? layoutSetName : layoutSets?.FirstOrDefault(item => item.Tasks?.Contains(taskId) ?? false)?.Id; | ||
bool hasLayoutSet = summaryType == "layout" && layouSetId == notification.LayoutSetName; | ||
if (hasLayoutSet) | ||
{ | ||
hasChanges = true; | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
|
||
if (hasChanges) | ||
{ | ||
await altinnAppGitRepository.SaveLayout(layoutSetName, $"{formLayout.Key}.json", formLayout.Value, cancellationToken); | ||
} | ||
return hasChanges; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Altinn.Studio.Designer.Models; | ||
using MediatR; | ||
|
||
namespace Altinn.Studio.Designer.Events; | ||
|
||
public class LayoutPageDeletedEvent : INotification | ||
{ | ||
public AltinnRepoEditingContext EditingContext { get; set; } | ||
public string LayoutSetName { get; set; } | ||
public string LayoutName { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters