-
Notifications
You must be signed in to change notification settings - Fork 94
chore: global settings endpoints v9 #18893
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
Open
lassopicasso
wants to merge
9
commits into
main
Choose a base branch
from
feat/global-settings-endpoints-v9
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d109822
new endpoints for validation on navigation
lassopicasso 5b2d647
create endpoints for TaskNavigation
lassopicasso 316ce9f
create layoutsetscontroller.cs and move global settings into that file
lassopicasso f25a06a
update ILayoutsetsService
lassopicasso 8f133d0
finalize layoutsetsController, LayoutsetsService, and its interface
lassopicasso 06a146f
remove v9 logic from appdevelopment controller, service and interface
lassopicasso df9b286
Merge branch 'main' into feat/global-settings-endpoints-v9
lassopicasso faf1763
add new service in dependency injection
lassopicasso 5d63cbb
coderabbit feedback, return null if global settings.json file does no…
lassopicasso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
src/Designer/backend/src/Designer/Controllers/LayoutsetsController.cs
This file contains hidden or 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,121 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Altinn.Studio.Designer.Filters; | ||
| using Altinn.Studio.Designer.Helpers; | ||
| using Altinn.Studio.Designer.Models; | ||
| using Altinn.Studio.Designer.Models.Dto; | ||
| using Altinn.Studio.Designer.Services.Interfaces; | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace Altinn.Studio.Designer.Controllers; | ||
|
|
||
| /// <summary> | ||
| /// Controller for handling layout sets related operations for v9 and newer, such as fetching and saving global settings for validation on navigation and task navigation. | ||
| /// </summary> | ||
| [ApiController] | ||
| [Authorize] | ||
| [AutoValidateAntiforgeryToken] | ||
| [Route("designer/api/{org}/{app:regex(^(?!datamodels$)[[a-z]][[a-z0-9-]]{{1,28}}[[a-z0-9]]$)}/layoutsets")] | ||
| public class LayoutsetsController : Controller | ||
| { | ||
| private readonly ILayoutsetsService _layoutsetsService; | ||
|
|
||
| public LayoutsetsController(ILayoutsetsService layoutsetsService) | ||
| { | ||
| _layoutsetsService = layoutsetsService; | ||
| } | ||
|
|
||
| private AltinnRepoEditingContext CreateContext(string org, string app) | ||
| { | ||
| string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext); | ||
| return AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer); | ||
| } | ||
|
|
||
| [HttpGet("layout-sets/settings/validation-on-navigation")] | ||
| [UseSystemTextJson] | ||
| public async Task<IActionResult> GetGlobalValidationOnNavigationSettings( | ||
| string org, | ||
| string app, | ||
| CancellationToken cancellationToken | ||
| ) | ||
| { | ||
| AltinnRepoEditingContext editingContext = CreateContext(org, app); | ||
| ValidationOnNavigation? config = await _layoutsetsService.GetGlobalValidationOnNavigationSettings( | ||
| editingContext, | ||
| cancellationToken | ||
| ); | ||
| return Ok(config); | ||
| } | ||
|
|
||
| [HttpPost("layout-sets/settings/validation-on-navigation")] | ||
| [UseSystemTextJson] | ||
| public async Task<IActionResult> SaveGlobalValidationOnNavigationSettings( | ||
| string org, | ||
| string app, | ||
| [FromBody] ValidationOnNavigation config, | ||
| CancellationToken cancellationToken | ||
| ) | ||
| { | ||
| AltinnRepoEditingContext editingContext = CreateContext(org, app); | ||
| await _layoutsetsService.SaveGlobalValidationOnNavigationSettings(editingContext, config, cancellationToken); | ||
| return Ok(); | ||
| } | ||
|
|
||
| [HttpDelete("layout-sets/settings/validation-on-navigation")] | ||
| public async Task<IActionResult> DeleteGlobalValidationOnNavigationSettings( | ||
| string org, | ||
| string app, | ||
| CancellationToken cancellationToken | ||
| ) | ||
| { | ||
| AltinnRepoEditingContext editingContext = CreateContext(org, app); | ||
| await _layoutsetsService.SaveGlobalValidationOnNavigationSettings(editingContext, null, cancellationToken); | ||
| return Ok(); | ||
| } | ||
|
|
||
| [HttpGet("layout-sets/settings/task-navigation")] | ||
| [UseSystemTextJson] | ||
| public async Task<IActionResult> GetGlobalTaskNavigationSettings( | ||
| string org, | ||
| string app, | ||
| CancellationToken cancellationToken | ||
| ) | ||
| { | ||
| AltinnRepoEditingContext editingContext = CreateContext(org, app); | ||
| IEnumerable<TaskNavigationGroupDto> result = await _layoutsetsService.GetGlobalTaskNavigationSettingsDto( | ||
| editingContext, | ||
| cancellationToken | ||
| ); | ||
|
|
||
| return Ok(result); | ||
| } | ||
|
|
||
| [HttpPost("layout-sets/settings/task-navigation")] | ||
| [UseSystemTextJson] | ||
| public async Task<IActionResult> UpdateGlobalTaskNavigationSettings( | ||
| string org, | ||
| string app, | ||
| [FromBody] IEnumerable<TaskNavigationGroupDto> taskNavigationGroupDtoList, | ||
| CancellationToken cancellationToken | ||
| ) | ||
| { | ||
| try | ||
| { | ||
| AltinnRepoEditingContext editingContext = CreateContext(org, app); | ||
| await _layoutsetsService.UpdateGlobalTaskNavigationSettings( | ||
| editingContext, | ||
| taskNavigationGroupDtoList, | ||
| cancellationToken | ||
| ); | ||
|
|
||
| return NoContent(); | ||
| } | ||
| catch (ArgumentException exception) | ||
| { | ||
| return BadRequest(exception.Message); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
src/Designer/backend/src/Designer/Services/Interfaces/ILayoutsetsService.cs
This file contains hidden or 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,32 @@ | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Altinn.Studio.Designer.Models; | ||
| using Altinn.Studio.Designer.Models.Dto; | ||
|
|
||
| namespace Altinn.Studio.Designer.Services.Interfaces; | ||
|
|
||
| public interface ILayoutsetsService | ||
| { | ||
| public Task<ValidationOnNavigation?> GetGlobalValidationOnNavigationSettings( | ||
| AltinnRepoEditingContext context, | ||
| CancellationToken cancellationToken | ||
| ); | ||
|
|
||
| public Task SaveGlobalValidationOnNavigationSettings( | ||
| AltinnRepoEditingContext editingContext, | ||
| ValidationOnNavigation? validationOnNavigation, | ||
| CancellationToken cancellationToken | ||
| ); | ||
|
|
||
| public Task<IEnumerable<TaskNavigationGroupDto>> GetGlobalTaskNavigationSettingsDto( | ||
| AltinnRepoEditingContext editingContext, | ||
| CancellationToken cancellationToken | ||
| ); | ||
|
|
||
| public Task UpdateGlobalTaskNavigationSettings( | ||
| AltinnRepoEditingContext editingContext, | ||
| IEnumerable<TaskNavigationGroupDto> taskNavigationGroupDtoList, | ||
| CancellationToken cancellationToken | ||
| ); | ||
| } |
134 changes: 134 additions & 0 deletions
134
src/Designer/backend/src/Designer/Services/LayoutsetsService.cs
This file contains hidden or 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,134 @@ | ||
| #nullable enable | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Altinn.App.Core.Internal.Process.Elements; | ||
| using Altinn.Studio.Designer.Infrastructure.GitRepository; | ||
| using Altinn.Studio.Designer.Mappers; | ||
| using Altinn.Studio.Designer.Models; | ||
| using Altinn.Studio.Designer.Models.Dto; | ||
| using Altinn.Studio.Designer.Services.Interfaces; | ||
|
|
||
| public class LayoutsetsService : ILayoutsetsService | ||
| { | ||
| private readonly IAltinnGitRepositoryFactory _altinnGitRepositoryFactory; | ||
|
|
||
| public LayoutsetsService(IAltinnGitRepositoryFactory altinnGitRepositoryFactory) | ||
| { | ||
| _altinnGitRepositoryFactory = altinnGitRepositoryFactory; | ||
| } | ||
|
|
||
| public async Task<ValidationOnNavigation?> GetGlobalValidationOnNavigationSettings( | ||
| AltinnRepoEditingContext altinnRepoEditingContext, | ||
| CancellationToken cancellationToken | ||
| ) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| AltinnAppGitRepository altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository( | ||
| altinnRepoEditingContext.Org, | ||
| altinnRepoEditingContext.Repo, | ||
| altinnRepoEditingContext.Developer | ||
| ); | ||
|
|
||
| UiSettings globalSettingsFile = await altinnAppGitRepository.GetGlobalSettingsFile(cancellationToken); | ||
| return globalSettingsFile?.ValidationOnNavigation; | ||
| } | ||
|
|
||
| public async Task SaveGlobalValidationOnNavigationSettings( | ||
| AltinnRepoEditingContext altinnRepoEditingContext, | ||
| ValidationOnNavigation? config, | ||
| CancellationToken cancellationToken | ||
| ) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| AltinnAppGitRepository altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository( | ||
| altinnRepoEditingContext.Org, | ||
| altinnRepoEditingContext.Repo, | ||
| altinnRepoEditingContext.Developer | ||
| ); | ||
|
|
||
| UiSettings globalSettingsFile = await altinnAppGitRepository.GetGlobalSettingsFile(cancellationToken); | ||
| globalSettingsFile ??= new UiSettings(); | ||
| globalSettingsFile.ValidationOnNavigation = config; | ||
| await altinnAppGitRepository.SaveGlobalSettingsFile(globalSettingsFile); | ||
| } | ||
|
|
||
| public async Task<IEnumerable<TaskNavigationGroupDto>> GetGlobalTaskNavigationSettingsDto( | ||
| AltinnRepoEditingContext editingContext, | ||
| CancellationToken cancellationToken | ||
| ) | ||
| { | ||
| IEnumerable<TaskNavigationGroup> taskNavigationGroups = await GetGlobalTaskNavigationSettings( | ||
| editingContext, | ||
| cancellationToken | ||
| ); | ||
|
|
||
| IEnumerable<ProcessTask> tasks = GetTasks(editingContext, cancellationToken); | ||
|
|
||
| Dictionary<string, string?> taskTypesById = tasks.ToDictionary( | ||
| task => task.Id, | ||
| task => task.ExtensionElements?.TaskExtension?.TaskType | ||
| ); | ||
|
|
||
| return taskNavigationGroups.Select(group => group.ToDto(taskId => taskTypesById.GetValueOrDefault(taskId))); | ||
| } | ||
|
|
||
| public async Task<List<TaskNavigationGroup>> GetGlobalTaskNavigationSettings( | ||
| AltinnRepoEditingContext altinnRepoEditingContext, | ||
| CancellationToken cancellationToken | ||
| ) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| AltinnAppGitRepository altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository( | ||
| altinnRepoEditingContext.Org, | ||
| altinnRepoEditingContext.Repo, | ||
| altinnRepoEditingContext.Developer | ||
| ); | ||
|
|
||
| UiSettings globalSettingsFile = await altinnAppGitRepository.GetGlobalSettingsFile(cancellationToken); | ||
| return globalSettingsFile?.TaskNavigation?.ToList() ?? []; | ||
| } | ||
|
|
||
| public IEnumerable<ProcessTask> GetTasks( | ||
| AltinnRepoEditingContext altinnRepoEditingContext, | ||
| CancellationToken cancellationToken | ||
| ) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| AltinnAppGitRepository altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository( | ||
| altinnRepoEditingContext.Org, | ||
| altinnRepoEditingContext.Repo, | ||
| altinnRepoEditingContext.Developer | ||
| ); | ||
|
|
||
| Definitions definitions = altinnAppGitRepository.GetProcessDefinitions(); | ||
| return definitions.Process.Tasks; | ||
| } | ||
|
|
||
| public async Task UpdateGlobalTaskNavigationSettings( | ||
| AltinnRepoEditingContext altinnRepoEditingContext, | ||
| IEnumerable<TaskNavigationGroupDto> taskNavigationGroupDtoList, | ||
| CancellationToken cancellationToken | ||
| ) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| AltinnAppGitRepository altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository( | ||
| altinnRepoEditingContext.Org, | ||
| altinnRepoEditingContext.Repo, | ||
| altinnRepoEditingContext.Developer | ||
| ); | ||
|
|
||
| IEnumerable<TaskNavigationGroup> taskNavigationGroupList = taskNavigationGroupDtoList.Select(x => x.ToDomain()); | ||
|
|
||
| UiSettings globalSettingsFile = await altinnAppGitRepository.GetGlobalSettingsFile(cancellationToken); | ||
|
|
||
| globalSettingsFile ??= new UiSettings(); | ||
|
|
||
| globalSettingsFile.TaskNavigation = taskNavigationGroupList.Any() ? taskNavigationGroupList : null; | ||
|
|
||
| await altinnAppGitRepository.SaveGlobalSettingsFile(globalSettingsFile); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle missing global settings file before reading.
GetGlobalSettingsFilereadsSettings.jsonunconditionally. On first write scenario (file not created yet), this throws and breaks both save endpoints that expect default/empty settings to be possible.Proposed fix
public async Task<UiSettings> GetGlobalSettingsFile(CancellationToken cancellationToken = default) { string globalSettingsFilePath = GetPathToGlobalSettingsFile(); cancellationToken.ThrowIfCancellationRequested(); + if (!FileExistsByRelativePath(globalSettingsFilePath)) + { + return new UiSettings(); + } string fileContent = await ReadTextByRelativePathAsync(globalSettingsFilePath, cancellationToken); UiSettings globalSettingsFile = JsonSerializer.Deserialize<UiSettings>(fileContent, s_jsonOptions); return globalSettingsFile; }📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should i return null instead in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 6351
@lassopicassoYes, returningnullactually works equally well here — and arguably fits the existing caller patterns even better. Looking at all four call sites inLayoutsetsService:GetGlobalValidationOnNavigationSettingsglobalSettingsFile?.ValidationOnNavigation— null-safeSaveGlobalValidationOnNavigationSettingsglobalSettingsFile ??= new UiSettings();— explicit null fallbackGetGlobalTaskNavigationSettingsglobalSettingsFile?.TaskNavigation?.ToList() ?? []— null-safeUpdateGlobalTaskNavigationSettingsglobalSettingsFile ??= new UiSettings();— explicit null fallbackEvery caller already guards against
nullwith either the?.operator or??=, so returningnullwhen the file doesn't exist is semantically clean — it accurately signals "the file doesn't exist yet" and lets the service layer decide what the default should be. The proposed fix would then be:public async Task<UiSettings> GetGlobalSettingsFile(CancellationToken cancellationToken = default) { string globalSettingsFilePath = GetPathToGlobalSettingsFile(); cancellationToken.ThrowIfCancellationRequested(); + if (!FileExistsByRelativePath(globalSettingsFilePath)) + { + return null; + } string fileContent = await ReadTextByRelativePathAsync(globalSettingsFilePath, cancellationToken); UiSettings globalSettingsFile = JsonSerializer.Deserialize<UiSettings>(fileContent, s_jsonOptions); return globalSettingsFile; }Either approach (
nullornew UiSettings()) will work, butnullis the more idiomatic choice for a repository-layer method — it separates "no file" from "empty settings object" clearly.🧠 Learnings used