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: Delete all Summary 2.0 components that has a reference to a deleted component #14126

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 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
58 changes: 48 additions & 10 deletions backend/src/Designer/Controllers/AppDevelopmentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,28 @@

if (formLayoutPayload.ComponentIdsChange is not null && !string.IsNullOrEmpty(layoutSetName))
{
foreach (var componentIdChange in formLayoutPayload.ComponentIdsChange)
{
await _mediator.Publish(new ComponentIdChangedEvent
if (componentIdChange.OldComponentId != componentIdChange.NewComponentId)
{
OldComponentId = componentIdChange.OldComponentId,
NewComponentId = componentIdChange.NewComponentId,
LayoutSetName = layoutSetName,
EditingContext = editingContext
}, cancellationToken);
if (componentIdChange.NewComponentId == null)
{
await _mediator.Publish(new ComponentDeletedEvent
{
ComponentId = componentIdChange.OldComponentId,
LayoutSetName = layoutSetName,
EditingContext = editingContext
}, cancellationToken);
}
await _mediator.Publish(new ComponentIdChangedEvent
{
OldComponentId = componentIdChange.OldComponentId,
NewComponentId = componentIdChange.NewComponentId,
LayoutSetName = layoutSetName,
EditingContext = editingContext
}, cancellationToken);
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.
}
if (!formLayouts.ContainsKey(layoutName))
{
Expand All @@ -159,16 +171,26 @@
/// <param name="app">Application identifier which is unique within an organisation.</param>
/// <param name="layoutSetName">The name of the layout set the specific layout belongs to</param>
/// <param name="layoutName">The form layout to be deleted</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that observes if operation is cancelled.</param>
/// <returns>A success message if the save was successful</returns>
[HttpDelete]
[Route("form-layout/{layoutName}")]
public ActionResult DeleteFormLayout(string org, string app, [FromQuery] string layoutSetName, [FromRoute] string layoutName)
public async Task<ActionResult> DeleteFormLayout(string org, string app, [FromQuery] string layoutSetName, [FromRoute] string layoutName, CancellationToken cancellationToken)
{
try
{
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer);

await _mediator.Publish(new LayoutPageDeletedEvent
{
EditingContext = editingContext,
LayoutSetName = layoutSetName,
LayoutName = layoutName,
}, cancellationToken);

_appDevelopmentService.DeleteFormLayout(editingContext, layoutSetName, layoutName);

return Ok();
}
catch (FileNotFoundException exception)
Expand All @@ -185,16 +207,24 @@
/// <param name="app">Application identifier which is unique within an organisation.</param>
/// <param name="layoutSetName">Name of the layout set the specific layout belongs to</param>
/// <param name="layoutName">The current name of the form layout</param>
/// <param name="cancellationToken">An <see cref="CancellationToken"/> that observes if operation is cancelled.</param>
/// <returns>A success message if the save was successful</returns>
[HttpPost]
[Route("form-layout-name/{layoutName}")]
public ActionResult UpdateFormLayoutName(string org, string app, [FromQuery] string layoutSetName, [FromRoute] string layoutName, [FromBody] string newName)
public async Task<ActionResult> UpdateFormLayoutName(string org, string app, [FromQuery] string layoutSetName, [FromRoute] string layoutName, [FromBody] string newName, CancellationToken cancellationToken)
{
try
{
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer);
_appDevelopmentService.UpdateFormLayoutName(editingContext, layoutSetName, layoutName, newName);
await _mediator.Publish(new LayoutPageIdChangedEvent
{
EditingContext = editingContext,
LayoutSetName = layoutSetName,
LayoutName = layoutName,
NewLayoutName = newName,
}, cancellationToken);
return Ok();
}
catch (FileNotFoundException exception)
Expand Down Expand Up @@ -385,6 +415,12 @@
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer);
LayoutSets layoutSets = await _appDevelopmentService.UpdateLayoutSetName(editingContext, layoutSetIdToUpdate, newLayoutSetName, cancellationToken);
await _mediator.Publish(new LayoutSetIdChangedEvent
{
EditingContext = editingContext,
LayoutSetName = layoutSetIdToUpdate,
NewLayoutSetName = newLayoutSetName,
}, cancellationToken);
return Ok(layoutSets);
}

Expand All @@ -402,13 +438,15 @@
{
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
LayoutSetName = layoutSetIdToUpdate
}, cancellationToken);

LayoutSets layoutSets = await _appDevelopmentService.DeleteLayoutSet(editingContext, layoutSetIdToUpdate, cancellationToken);

return Ok(layoutSets);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Events;
using Altinn.Studio.Designer.Hubs.SyncHub;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.Services.Interfaces;
using MediatR;

namespace Altinn.Studio.Designer.EventHandlers.ComponentDeleted;

public class ComponentDeletedLayoutsHandler(IFileSyncHandlerExecutor fileSyncHandlerExecutor, IAppDevelopmentService appDevelopmentService) : INotificationHandler<ComponentDeletedEvent>
{
public async Task Handle(ComponentDeletedEvent notification, CancellationToken cancellationToken)
{
await fileSyncHandlerExecutor.ExecuteWithExceptionHandlingAndConditionalNotification(
notification.EditingContext,
SyncErrorCodes.ComponentDeletedLayoutsSyncError,
"layouts",
async () =>
{
List<Reference> referencesToDelete = [new Reference("component", notification.LayoutSetName, notification.ComponentId)];
return await appDevelopmentService.UpdateLayoutReferences(notification.EditingContext, referencesToDelete, cancellationToken);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Events;
using Altinn.Studio.Designer.Hubs.SyncHub;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.Services.Interfaces;
using MediatR;

Expand All @@ -13,12 +15,15 @@ public class ComponentIdChangedLayoutsHandler : INotificationHandler<ComponentId
{
private readonly IAltinnGitRepositoryFactory _altinnGitRepositoryFactory;
private readonly IFileSyncHandlerExecutor _fileSyncHandlerExecutor;
private readonly IAppDevelopmentService _appDevelopmentService;

public ComponentIdChangedLayoutsHandler(IAltinnGitRepositoryFactory altinnGitRepositoryFactory,
IFileSyncHandlerExecutor fileSyncHandlerExecutor)
IFileSyncHandlerExecutor fileSyncHandlerExecutor,
IAppDevelopmentService appDevelopmentService)
{
_altinnGitRepositoryFactory = altinnGitRepositoryFactory;
_fileSyncHandlerExecutor = fileSyncHandlerExecutor;
_appDevelopmentService = appDevelopmentService;
}

public async Task Handle(ComponentIdChangedEvent notification, CancellationToken cancellationToken)
Expand All @@ -45,6 +50,10 @@ await _fileSyncHandlerExecutor.ExecuteWithExceptionHandlingAndConditionalNotific
hasChanges = true;
}
}

List<Reference> referencesToUpdate = [new Reference("component", notification.LayoutSetName, notification.OldComponentId, notification.NewComponentId)];
hasChanges = await _appDevelopmentService.UpdateLayoutReferences(notification.EditingContext, referencesToUpdate, cancellationToken);

return hasChanges;
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Events;
using Altinn.Studio.Designer.Hubs.SyncHub;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.Services.Interfaces;
using MediatR;

namespace Altinn.Studio.Designer.EventHandlers.LayoutPageDeleted;

public class LayoutPageDeletedLayoutsHandler(IFileSyncHandlerExecutor fileSyncHandlerExecutor, IAppDevelopmentService appDevelopmentService) : INotificationHandler<LayoutPageDeletedEvent>
{
public async Task Handle(LayoutPageDeletedEvent notification, CancellationToken cancellationToken)
{
await fileSyncHandlerExecutor.ExecuteWithExceptionHandlingAndConditionalNotification(
notification.EditingContext,
SyncErrorCodes.LayoutPageDeletedLayoutsSyncError,
"layouts",
async () =>
{
List<Reference> referencesToDelete = [new Reference("page", notification.LayoutSetName, notification.LayoutName)];
return await appDevelopmentService.UpdateLayoutReferences(notification.EditingContext, referencesToDelete, cancellationToken);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Events;
using Altinn.Studio.Designer.Hubs.SyncHub;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.Services.Interfaces;
using MediatR;

namespace Altinn.Studio.Designer.EventHandlers.LayoutPageDeleted;

public class LayoutPageIdChangedLayoutsHandler(IFileSyncHandlerExecutor fileSyncHandlerExecutor, IAppDevelopmentService appDevelopmentService) : INotificationHandler<LayoutPageIdChangedEvent>
{
public async Task Handle(LayoutPageIdChangedEvent notification, CancellationToken cancellationToken)
{
await fileSyncHandlerExecutor.ExecuteWithExceptionHandlingAndConditionalNotification(
notification.EditingContext,
SyncErrorCodes.LayoutPageIdChangedLayoutsSyncError,
"layouts",
async () =>
{
List<Reference> referencesToUpdate = [new Reference("page", notification.LayoutSetName, notification.LayoutName, notification.NewLayoutName)];
return await appDevelopmentService.UpdateLayoutReferences(notification.EditingContext, referencesToUpdate, cancellationToken);
});
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Events;
using Altinn.Studio.Designer.Hubs.SyncHub;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.Services.Interfaces;
using MediatR;

namespace Altinn.Studio.Designer.EventHandlers.LayoutSetDeleted;

public class LayoutSetDeletedLayoutsHandler(IFileSyncHandlerExecutor fileSyncHandlerExecutor, IAppDevelopmentService appDevelopmentService) : INotificationHandler<LayoutSetDeletedEvent>
{
public async Task Handle(LayoutSetDeletedEvent notification, CancellationToken cancellationToken)
{
await fileSyncHandlerExecutor.ExecuteWithExceptionHandlingAndConditionalNotification(
notification.EditingContext,
SyncErrorCodes.LayoutSetDeletedLayoutsSyncError,
"layouts",
async () =>
{
List<Reference> referencesToDelete = [new Reference("layoutSet", notification.LayoutSetName, notification.LayoutSetName)];
return await appDevelopmentService.UpdateLayoutReferences(notification.EditingContext, referencesToDelete, cancellationToken);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Altinn.Studio.Designer.Events;
using Altinn.Studio.Designer.Hubs.SyncHub;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.Services.Interfaces;
using MediatR;

namespace Altinn.Studio.Designer.EventHandlers.LayoutSetDeleted;

public class LayoutSetIdChangedLayoutsHandler(IFileSyncHandlerExecutor fileSyncHandlerExecutor, IAppDevelopmentService appDevelopmentService) : INotificationHandler<LayoutSetIdChangedEvent>
{
public async Task Handle(LayoutSetIdChangedEvent notification, CancellationToken cancellationToken)
{
await fileSyncHandlerExecutor.ExecuteWithExceptionHandlingAndConditionalNotification(
notification.EditingContext,
SyncErrorCodes.LayoutSetIdChangedLayoutsSyncError,
"layouts",
async () =>
{
List<Reference> referencesToUpdate = [new Reference("layoutSet", notification.LayoutSetName, notification.LayoutSetName, notification.NewLayoutSetName)];
return await appDevelopmentService.UpdateLayoutReferences(notification.EditingContext, referencesToUpdate, cancellationToken);
});
}
}
11 changes: 11 additions & 0 deletions backend/src/Designer/Events/ComponentDeletedEvent.cs
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 ComponentDeletedEvent : INotification
{
public AltinnRepoEditingContext EditingContext { get; set; }
public string LayoutSetName { get; set; }
public string ComponentId { get; set; }
}
11 changes: 11 additions & 0 deletions backend/src/Designer/Events/LayoutPageDeletedEvent.cs
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; }
}
Loading
Loading