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

Appman 1408 user overview #1947

Open
wants to merge 7 commits into
base: APPMAN-1409-account-details
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ public async Task Then_Gets_Account_Details_From_Mediator(
It.Is<GetEmployerAccountDetailsQuery>(x =>
x.AccountId == accountId
&& x.SelectedField == AccountFieldSelection.EmployerAccount),
It.IsAny<CancellationToken>()));
It.IsAny<CancellationToken>())).ReturnsAsync(getDetailsResult);

var controllerResult = await controller.GetAccountDetails(accountId, AccountFieldSelection.EmployerAccount) as ObjectResult;

controllerResult.Should().NotBeNull();
var model = controllerResult.Value as GetEmployerAccountDetailsResponse;

model.Account.Should().NotBeNull();
model.Account.Should().BeEquivalentTo(getDetailsResult);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using AutoFixture.NUnit3;
using FluentAssertions;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using SFA.DAS.Testing.AutoFixture;
using SFA.DAS.ToolsSupport.Api.Controllers;
using SFA.DAS.ToolsSupport.Application.Queries.GetUserOverview;

namespace SFA.DAS.ToolsSupport.Api.UnitTests.Controllers.UsersQuery;

public class WhenGettingUserOverview
{
[Test, MoqAutoData]
public async Task Then_Gets_Account_Details_From_Mediator(
Guid userId,
GetUserOverviewQueryResult getOverviewResult,
[Frozen] Mock<IMediator> mockMediator,
[Greedy] UsersQueryController controller)
{
mockMediator
.Setup(mediator => mediator.Send(
It.Is<GetUserOverviewQuery>(x =>
x.UserId == userId),
It.IsAny<CancellationToken>()))
.ReturnsAsync(getOverviewResult);

var controllerResult = await controller.GetUserOverview(userId) as ObjectResult;

controllerResult.Should().NotBeNull();
var model = controllerResult.Value as GetUserOverviewQueryResult;

model.Should().NotBeNull();
model.Id.Should().Be(getOverviewResult.Id);
model.FirstName.Should().Be(getOverviewResult.FirstName);
model.LastName.Should().Be(getOverviewResult.LastName);
model.Email.Should().Be(getOverviewResult.Email);
model.IsActive.Should().Be(getOverviewResult.IsActive);
model.IsLocked.Should().Be(getOverviewResult.IsLocked);
model.IsSuspended.Should().Be(getOverviewResult.IsSuspended);
model.AccountSummaries.Should().BeEquivalentTo(getOverviewResult.AccountSummaries);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using SFA.DAS.ToolsSupport.Api.Controllers;
using SFA.DAS.ToolsSupport.Application.Queries;

namespace SFA.DAS.ToolsSupport.Api.UnitTests.Controllers;
namespace SFA.DAS.ToolsSupport.Api.UnitTests.Controllers.UsersQuery;

[TestFixture]
public class WhenGettingUsersByEmail
Expand All @@ -22,7 +22,7 @@ public async Task Then_UsersQueryResponse_Returned_From_Mediator(
[Frozen] Mock<IMediator> mockMediator,
[Greedy] UsersQueryController sut)
{
mockMediator.Setup(x => x.Send(It.Is<GetUsersByEmailQuery>(p=>p.Email == email), It.IsAny<CancellationToken>())).ReturnsAsync(mockQueryResult);
mockMediator.Setup(x => x.Send(It.Is<GetUsersByEmailQuery>(p => p.Email == email), It.IsAny<CancellationToken>())).ReturnsAsync(mockQueryResult);

var actual = await sut.Get(email) as ObjectResult;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ public static void AddConfigurationOptions(this IServiceCollection services, ICo

services.Configure<EmployerProfilesApiConfiguration>(configuration.GetSection("EmployerProfilesInnerApi"));
services.AddSingleton(cfg => cfg.GetService<IOptions<EmployerProfilesApiConfiguration>>().Value);

services.Configure<EmployerUsersApiConfiguration>(configuration.GetSection("EmployerUsersInnerApi"));
services.AddSingleton(cfg => cfg.GetService<IOptions<EmployerUsersApiConfiguration>>().Value);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using SFA.DAS.ToolsSupport.Application.Queries;
using SFA.DAS.ToolsSupport.Application.Queries.GetUserOverview;
using System.Net;

namespace SFA.DAS.ToolsSupport.Api.Controllers;
Expand Down Expand Up @@ -33,5 +34,26 @@ public async Task<IActionResult> Get([FromQuery] string email)
return StatusCode((int) HttpStatusCode.InternalServerError);
}
}

[HttpGet]
[Route("user-overview")]
public async Task<IActionResult> GetUserOverview([FromQuery] Guid userId)
{
try
{
var response = await _mediator.Send(
new GetUserOverviewQuery
{
UserId = userId
});

return Ok(response);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't return mediatr responses from the apim layer

}
catch (Exception ex)
{
_logger.LogError(ex, "Error attempting to get user summary");
return StatusCode((int) HttpStatusCode.InternalServerError);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using SFA.DAS.ToolsSupport.Interfaces;
using SFA.DAS.ToolsSupport.Strategies;

namespace SFA.DAS.ToolsSupport.UnitTests.Application.Queries.EmployerAccount.GetEmployerAccountDetails;
namespace SFA.DAS.ToolsSupport.UnitTests.Application.Queries;

public class GetEmployerAccountDetailsQueryHandlerTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using AutoFixture.NUnit3;
using FluentAssertions;
using Moq;
using SFA.DAS.SharedOuterApi.Configuration;
using SFA.DAS.SharedOuterApi.Interfaces;
using SFA.DAS.Testing.AutoFixture;
using SFA.DAS.ToolsSupport.Application.Queries.GetUserOverview;
using SFA.DAS.ToolsSupport.InnerApi.Requests;
using SFA.DAS.ToolsSupport.InnerApi.Responses;
using SFA.DAS.ToolsSupport.Interfaces;

namespace SFA.DAS.ToolsSupport.UnitTests.Application.Queries;

public class GetUserOverviewQueryHandlerTests
{
[Test, MoqAutoData]
public async Task Then_GetLatestDetails_from_Reference_Api(
GetUserOverviewQuery query,
GetUserOverviewResponse userApiResponse,
List<Account> userAccounts,
[Frozen] Mock<IAccountsService> accountsService,
[Frozen] Mock<IInternalApiClient<EmployerProfilesApiConfiguration>> employerProfilesApi,
GetUserOverviewQueryHandler handler)
{
accountsService.Setup(x => x.GetUserAccounts(query.UserId)).ReturnsAsync(userAccounts);

var expectedUrl = $"api/users/{query.UserId}";

employerProfilesApi.Setup(x => x.Get<GetUserOverviewResponse>(It.Is<GetUserByIdRequest>(r => r.GetUrl == expectedUrl)))
.ReturnsAsync(userApiResponse);

var result = await handler.Handle(query, CancellationToken.None);

result.Should().NotBeNull();
result.Id.Should().Be(userApiResponse.Id);
result.FirstName.Should().Be(userApiResponse.FirstName);
result.LastName.Should().Be(userApiResponse.LastName);
result.Email.Should().Be(userApiResponse.Email);
result.IsActive.Should().Be(userApiResponse.IsActive);
result.IsLocked.Should().Be(userApiResponse.IsLocked);
result.IsSuspended.Should().Be(userApiResponse.IsSuspended);

result.AccountSummaries.Should().NotBeNull();
result.AccountSummaries.First().DasAccountName.Should().Be(userAccounts.First().DasAccountName);
result.AccountSummaries.First().HashedAccountId.Should().Be(userAccounts.First().HashedAccountId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using MediatR;

namespace SFA.DAS.ToolsSupport.Application.Queries.GetUserOverview;

public class GetUserOverviewQuery : IRequest<GetUserOverviewQueryResult>
{
public Guid UserId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using MediatR;
using SFA.DAS.SharedOuterApi.Configuration;
using SFA.DAS.SharedOuterApi.Interfaces;
using SFA.DAS.ToolsSupport.InnerApi.Requests;
using SFA.DAS.ToolsSupport.InnerApi.Responses;
using SFA.DAS.ToolsSupport.Interfaces;

namespace SFA.DAS.ToolsSupport.Application.Queries.GetUserOverview;

public class GetUserOverviewQueryHandler(
IInternalApiClient<EmployerProfilesApiConfiguration> client,
IAccountsService accountsService
) : IRequestHandler<GetUserOverviewQuery, GetUserOverviewQueryResult>
{
public async Task<GetUserOverviewQueryResult> Handle(GetUserOverviewQuery query, CancellationToken cancellationToken)
{
var accountsTask = accountsService.GetUserAccounts(query.UserId);

var userResponseTask = client.Get<GetUserOverviewResponse>(new GetUserByIdRequest(query.UserId));

await Task.WhenAll(accountsTask, userResponseTask);

var accounts = await accountsTask;
var userResponse = await userResponseTask;
if (userResponse == null)
{
return new GetUserOverviewQueryResult();
}

return new GetUserOverviewQueryResult
{
Id = userResponse.Id,
FirstName = userResponse.FirstName,
LastName = userResponse.LastName,
Email = userResponse.Email,
IsActive = userResponse.IsActive,
IsLocked = userResponse.IsLocked,
IsSuspended = userResponse.IsSuspended,
AccountSummaries = accounts != null ? accounts.Select(x =>
new AccountSummary
{
DasAccountName = x.DasAccountName,
HashedAccountId = x.HashedAccountId
}).ToList() : [],
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using SFA.DAS.ToolsSupport.InnerApi.Responses;

namespace SFA.DAS.ToolsSupport.Application.Queries.GetUserOverview;

public class GetUserOverviewQueryResult
{
public string Id { get; set; } = "";
public string FirstName { get; set; } = "";
public string LastName { get; set; } = "";
public string Email { get; set; } = "";
public bool IsActive { get; set; }
public bool IsLocked { get; set; }
public bool IsSuspended { get; set; }
public List<AccountSummary> AccountSummaries { get; set; } = [];

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using SFA.DAS.SharedOuterApi.Configuration;
using SFA.DAS.SharedOuterApi.InnerApi.Requests;
using SFA.DAS.SharedOuterApi.Interfaces;
using SFA.DAS.ToolsSupport.InnerApi.Requests.Account;
using SFA.DAS.ToolsSupport.InnerApi.Requests;
using SFA.DAS.ToolsSupport.InnerApi.Responses;
using SFA.DAS.ToolsSupport.Interfaces;

Expand All @@ -11,7 +10,7 @@ public class AccountsService(IAccountsApiClient<AccountsConfiguration> client) :
{
public async Task<Account> GetAccount(long accountId)
{
var response = await client.Get<Account>(new GetAccountRequest(accountId));
var response = await client.Get<Account>(new GetEmployerAccountByIdRequest(accountId));

return response;
}
Expand All @@ -22,4 +21,11 @@ public async Task<LegalEntity> GetEmployerAccountLegalEntity(string url)

return response;
}

public async Task<List<Account>> GetUserAccounts(Guid userId)
{
var response = await client.Get<List<Account>>(new GetUserAccountsRequest(userId));

return response;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using SFA.DAS.SharedOuterApi.Interfaces;

namespace SFA.DAS.ToolsSupport.InnerApi.Requests;

public class GetEmployerAccountLegalEntityRequest(string href) : IGetApiRequest
{
public string GetUrl => href;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using SFA.DAS.SharedOuterApi.Interfaces;

namespace SFA.DAS.ToolsSupport.InnerApi.Requests;

internal class GetUserAccountsRequest(Guid userId) : IGetApiRequest
{
public string GetUrl => $"api/user/{userId}/accounts";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using SFA.DAS.SharedOuterApi.Interfaces;

namespace SFA.DAS.ToolsSupport.InnerApi.Requests;

public class GetUserByIdRequest(Guid userId) : IGetApiRequest
{
public string GetUrl => $"api/users/{userId}";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SFA.DAS.ToolsSupport.InnerApi.Responses;
public class AccountSummary
{
public string DasAccountName { get; set; } = "";
public string HashedAccountId { get; set; } = "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace SFA.DAS.ToolsSupport.InnerApi.Responses;

public class GetUserOverviewResponse
{
public string Id { get; set; } = "";
public string FirstName { get; set; } = "";
public string LastName { get; set; } = "";
public string Email { get; set; } = "";
public bool IsActive { get; set; }
public bool IsLocked { get; set; }
public bool IsSuspended { get; set; }
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public interface IAccountsService
{
Task<Account> GetAccount(long accountId);
Task<LegalEntity> GetEmployerAccountLegalEntity(string url);
Task<List<Account>> GetUserAccounts(Guid userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Application\Commands\**" />
<Compile Remove="Application\Models\**" />
<EmbeddedResource Remove="Application\Commands\**" />
<EmbeddedResource Remove="Application\Models\**" />
<None Remove="Application\Commands\**" />
<None Remove="Application\Models\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MediatR" />
</ItemGroup>
Expand All @@ -14,9 +23,4 @@
<ProjectReference Include="..\..\Shared\SFA.DAS.SharedOuterApi\SFA.DAS.SharedOuterApi.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Application\Commands\" />
<Folder Include="Application\Models\" />
</ItemGroup>

</Project>