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

feat: deleting subform layoutset #13680

Merged
merged 7 commits into from
Oct 9, 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
6 changes: 6 additions & 0 deletions backend/src/Designer/Controllers/AppDevelopmentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,12 @@ public async Task<ActionResult> DeleteLayoutSet(string org, string app, [FromRou
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer);
LayoutSets layoutSets = await _appDevelopmentService.DeleteLayoutSet(editingContext, layoutSetIdToUpdate, cancellationToken);

await _mediator.Publish(new LayoutSetDeletedEvent
{
EditingContext = editingContext,
LayoutSetId = layoutSetIdToUpdate
}, cancellationToken);
return Ok(layoutSets);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Collections.Generic;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using Altinn.App.Core.Helpers;
using Altinn.Studio.Designer.Events;
using Altinn.Studio.Designer.Hubs.SyncHub;
using Altinn.Studio.Designer.Infrastructure.GitRepository;
using Altinn.Studio.Designer.Services.Interfaces;
using MediatR;

namespace Altinn.Studio.Designer.EventHandlers.LayoutSetDeleted;

public class LayoutSetDeletedComponentRefHandler(IAltinnGitRepositoryFactory altinnGitRepositoryFactory, IFileSyncHandlerExecutor fileSyncHandlerExecutor) : INotificationHandler<LayoutSetDeletedEvent>
{
public async Task Handle(LayoutSetDeletedEvent notification, CancellationToken cancellationToken)
{
AltinnAppGitRepository altinnAppGitRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(
notification.EditingContext.Org,
notification.EditingContext.Repo,
notification.EditingContext.Developer);

string[] layoutSetNames = altinnAppGitRepository.GetLayoutSetNames();

await fileSyncHandlerExecutor.ExecuteWithExceptionHandlingAndConditionalNotification(
notification.EditingContext,
SyncErrorCodes.LayoutSetSubLayoutSyncError,
"layouts",
async () =>
{
bool hasChanges = false;
foreach (string layoutSetName in layoutSetNames)
{
Dictionary<string, JsonNode> formLayouts = await altinnAppGitRepository.GetFormLayouts(layoutSetName, cancellationToken);
foreach (var formLayout in formLayouts)
{
hasChanges |= await RemoveComponentsReferencingLayoutSet(
notification,
altinnAppGitRepository,
layoutSetName,
formLayout,
cancellationToken);
}
}
return hasChanges;
});
}

private static async Task<bool> RemoveComponentsReferencingLayoutSet(LayoutSetDeletedEvent notification, AltinnAppGitRepository altinnAppGitRepository, 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["layoutSet"]?.GetValue<string>() == notification.LayoutSetId)
{
hasChanges = true;
return true;
}
return false;
});

if (hasChanges)
{
await altinnAppGitRepository.SaveLayout(layoutSetName, $"{formLayout.Key}.json", formLayout.Value, cancellationToken);
}
return hasChanges;
}
}
10 changes: 10 additions & 0 deletions backend/src/Designer/Events/LayoutSetDeletedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Altinn.Studio.Designer.Models;
using MediatR;

namespace Altinn.Studio.Designer.Events;

public class LayoutSetDeletedEvent : INotification
{
public string LayoutSetId { get; set; }
public AltinnRepoEditingContext EditingContext { get; set; }
}
1 change: 1 addition & 0 deletions backend/src/Designer/Hubs/SyncHub/SyncErrorCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static class SyncErrorCodes
public const string ApplicationMetadataDataTypeSyncError = nameof(ApplicationMetadataDataTypeSyncError);
public const string LayoutSetsDataTypeSyncError = nameof(LayoutSetsDataTypeSyncError);
public const string LayoutSetComponentIdSyncError = nameof(LayoutSetComponentIdSyncError);
public const string LayoutSetSubLayoutSyncError = nameof(LayoutSetSubLayoutSyncError);
public const string SettingsComponentIdSyncError = nameof(SettingsComponentIdSyncError);
public const string LayoutPageAddSyncError = nameof(LayoutPageAddSyncError);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Altinn.Platform.Storage.Interface.Models;
using Altinn.Studio.Designer.Factories;
Expand Down Expand Up @@ -133,6 +134,32 @@ public async Task DeleteLayoutSet_AppWithoutLayoutSets_ReturnsNotFound(string or
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}

[Theory]
[InlineData("ttd", "app-with-layoutsets", "testUser", "layoutSet3",
"layoutSet2", "layoutFile1InSet2", "subform-component-id")]
public async Task DeleteLayoutSet_RemovesComponentsReferencingLayoutSet(string org, string app, string developer, string layoutSetToDeleteId,
string layoutSetWithRef, string layoutSetFile, string deletedComponentId)
{
string targetRepository = TestDataHelper.GenerateTestRepoName();
await CopyRepositoryForTest(org, app, developer, targetRepository);

string url = $"{VersionPrefix(org, targetRepository)}/layout-set/{layoutSetToDeleteId}";
using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Delete, url);

using var response = await HttpClient.SendAsync(httpRequestMessage);
response.StatusCode.Should().Be(HttpStatusCode.OK, await response.Content.ReadAsStringAsync());

JsonNode formLayout = (await GetFormLayouts(org, targetRepository, developer, layoutSetWithRef))[layoutSetFile];
JsonArray layout = formLayout["data"]?["layout"] as JsonArray;

layout.Should().NotBeNull();
layout
Fixed Show fixed Hide fixed
.Where(jsonNode => jsonNode["layoutSet"] != null)
Fixed Show fixed Hide fixed
.Should()
.NotContain(jsonNode => jsonNode["layoutSet"].GetValue<string>() == deletedComponentId,
$"No components should reference the deleted layout set {deletedComponentId}");
}

private async Task<LayoutSets> GetLayoutSetsFile(string org, string app, string developer)
{
AltinnGitRepositoryFactory altinnGitRepositoryFactory =
Expand All @@ -143,6 +170,16 @@ private async Task<LayoutSets> GetLayoutSetsFile(string org, string app, string
return await altinnAppGitRepository.GetLayoutSetsFile();
}

private async Task<Dictionary<string, JsonNode>> GetFormLayouts(string org, string app, string developer, string layoutSetName)
{
AltinnGitRepositoryFactory altinnGitRepositoryFactory =
new(TestDataHelper.GetTestDataRepositoriesRootDirectory());
AltinnAppGitRepository altinnAppGitRepository =
altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, app, developer);
Dictionary<string, JsonNode> formLayouts = await altinnAppGitRepository.GetFormLayouts(layoutSetName);
return formLayouts;
}

private async Task<Application> GetApplicationMetadataFile(string org, string app, string developer)
{
AltinnGitRepositoryFactory altinnGitRepositoryFactory =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
{
"schema": "https://altinncdn.no/schemas/json/layout/layout.schema.v1.json",
"data": {
"layout": [{
"id": "component-id",
"type": "Header",
"textResourceBindings": {
"title": "some-old-id",
"body": "another-key"
}
}]
}
"schema": "https://altinncdn.no/schemas/json/layout/layout.schema.v1.json",
"data": {
"layout": [
{
"id": "component-id",
"type": "Header",
"textResourceBindings": {
"title": "some-old-id",
"body": "another-key"
}
},
{
"id": "subform-component-id",
"layoutSet": "layoutSet3"
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('SyncSuccessQueriesInvalidator', () => {

await waitFor(() =>
expect(queryClientMock.invalidateQueries).toHaveBeenCalledWith({
queryKey: [QueryKey.FormLayouts, org, app, selectedLayoutSet],
queryKey: [QueryKey.FormLayouts, org, app],
}),
);
expect(queryClientMock.invalidateQueries).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class SyncSuccessQueriesInvalidator extends Queue {

// Maps folder names to their cache keys for invalidation upon sync success - can be extended to include more folders
private readonly folderNameCacheKeyMap: Record<string, Array<QueryKey | string>> = {
layouts: [QueryKey.FormLayouts, '[org]', '[app]', '[layoutSetName]'],
layouts: [QueryKey.FormLayouts, '[org]', '[app]'],
mlqn marked this conversation as resolved.
Show resolved Hide resolved
};

public set layoutSetName(layoutSetName: string) {
Expand Down
Loading