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: add context-types to put endpoint #702

Merged
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
5 changes: 5 additions & 0 deletions .changeset/pr-702-2020907790.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

---
"fusion-project-portal": patch
---
Context types can be added in OnboardedApp PUT endpoint
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Equinor.ProjectExecutionPortal.Application.Commands.OnboardedApps.Onbo

public class OnboardAppCommand : IRequest<Guid>
{
public OnboardAppCommand(string appKey, bool isLegacy, IList<string> contextTypes)
public OnboardAppCommand(string appKey, bool isLegacy, IList<string>? contextTypes)
{
AppKey = appKey;
IsLegacy = isLegacy;
Expand All @@ -19,7 +19,7 @@ public OnboardAppCommand(string appKey, bool isLegacy, IList<string> contextType

public string AppKey { get; }
public bool IsLegacy { get; }
public IList<string> ContextTypes { get; set; }
public IList<string>? ContextTypes { get; set; }

public class Handler : IRequestHandler<OnboardAppCommand, Guid>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Equinor.ProjectExecutionPortal.Domain.Common.Exceptions;
using Equinor.ProjectExecutionPortal.Application.Services.ContextTypeService;
using Equinor.ProjectExecutionPortal.Domain.Common.Exceptions;
using Equinor.ProjectExecutionPortal.Domain.Entities;
using Equinor.ProjectExecutionPortal.Infrastructure;
using MediatR;
Expand All @@ -8,27 +9,32 @@ namespace Equinor.ProjectExecutionPortal.Application.Commands.OnboardedApps.Upda

public class UpdateOnboardedAppCommand : IRequest<Guid>
{
public UpdateOnboardedAppCommand(string appKey, bool isLegacy)
public UpdateOnboardedAppCommand(string appKey, bool isLegacy, IList<string>? contextTypes)
{
AppKey = appKey;
IsLegacy = isLegacy;
ContextTypes = contextTypes;
}

public string AppKey { get; }
public bool IsLegacy { get; }
public IList<string>? ContextTypes { get; set; }

public class Handler : IRequestHandler<UpdateOnboardedAppCommand, Guid>
{
private readonly IReadWriteContext _readWriteContext;
private readonly IContextTypeService _contextTypeService;

public Handler(IReadWriteContext readWriteContext)
public Handler(IReadWriteContext readWriteContext, IContextTypeService contextTypeService)
{
_readWriteContext = readWriteContext;
_contextTypeService = contextTypeService;
}

public async Task<Guid> Handle(UpdateOnboardedAppCommand command, CancellationToken cancellationToken)
{
var entity = await _readWriteContext.Set<OnboardedApp>()
.Include(x => x.ContextTypes)
.FirstOrDefaultAsync(x => x.AppKey == command.AppKey, cancellationToken);

if (entity == null)
Expand All @@ -38,6 +44,15 @@ public async Task<Guid> Handle(UpdateOnboardedAppCommand command, CancellationTo

entity.Update(command.IsLegacy);

try
{
entity.AddContextTypes(await _contextTypeService.GetContextTypesByContextTypeKey(command.ContextTypes, cancellationToken));
}
catch (InvalidActionException ex)
{
throw new InvalidOperationException(ex.Message);
}

await _readWriteContext.SaveChangesAsync(cancellationToken);

return entity.Id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public async Task<ActionResult<Guid>> UpdateOnboardedApp([FromRoute] string appK
{
return FusionApiError.NotFound(appKey, ex.Message);
}
catch (InvalidOperationException ex)
{
return FusionApiError.InvalidOperation("400", ex.Message);
}
catch (Exception)
{
return FusionApiError.InvalidOperation("500", "An error occurred while updating the onboarded app");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using Equinor.ProjectExecutionPortal.Application.Commands.OnboardedApps.UpdateOnboardedApp;
using Equinor.ProjectExecutionPortal.Application.Queries.OnboardedContexts.GetOnboardedContext;
using FluentValidation;

namespace Equinor.ProjectExecutionPortal.WebApi.ViewModels.OnboardedApp
{
public class ApiUpdateOnboardedAppRequest
{
public bool IsLegacy { get; set; }
public IList<string>? ContextTypes { get; set; }

public UpdateOnboardedAppCommand ToCommand(string appKey)
{
return new UpdateOnboardedAppCommand(appKey, IsLegacy);
return new UpdateOnboardedAppCommand(appKey, IsLegacy, ContextTypes);
}

public class ApiUpdateOnboardedAppRequestValidator : AbstractValidator<ApiUpdateOnboardedAppRequest>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ public async Task Update_OnboardedApp_AsAdministratorUser_ShouldReturnOk()

var payload = new ApiUpdateOnboardedAppRequest
{
IsLegacy = false
IsLegacy = false,
ContextTypes = new List<string>() {}

};

// Act
Expand All @@ -200,6 +202,7 @@ public async Task Update_OnboardedApp_AsAdministratorUser_ShouldReturnOk()
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual(theOneToUpdate.AppKey, theOneAfterUpdate.AppKey);
Assert.AreEqual(theOneToUpdate.IsLegacy, theOneAfterUpdate.IsLegacy);
Assert.AreEqual(theOneToUpdate.Contexts.Count, theOneAfterUpdate.Contexts.Count);
}

[TestMethod]
Expand Down