From f8c1f2339a9fb1b29591583cbb7963a5b17a9a90 Mon Sep 17 00:00:00 2001 From: Conor Till Date: Wed, 15 Sep 2021 14:33:04 +0100 Subject: [PATCH] Refactoring GetPledges into GetIndex --- .../WhenBuildingGetPledgesRequest.cs | 8 +-- .../WhenIGetAPledgesCount.cs | 16 ++--- .../Controllers/TransfersController.cs | 2 +- .../Orchestrators/TransfersOrchestrator.cs | 59 ++++--------------- .../SFA.DAS.EmployerFinance.Web.csproj | 2 +- .../ViewModels/Transfers/IndexViewModel.cs | 9 +++ .../ViewModels/TransfersIndexViewModel.cs | 9 --- .../Views/Transfers/Index.cshtml | 4 +- .../GetIndexRequest.cs} | 8 +-- .../OuterApiResponses/GetPledgesResponse.cs | 7 --- .../Transfers/GetIndexResponse.cs | 8 +++ .../Services/ILevyTransferMatchingService.cs | 9 --- .../Services/IManageApprenticeshipsService.cs | 10 ++++ .../Services/LevyTransferMatchingService.cs | 25 -------- .../Services/ManageApprenticeshipsService.cs | 23 ++++++++ 15 files changed, 83 insertions(+), 116 deletions(-) create mode 100644 src/SFA.DAS.EmployerFinance.Web/ViewModels/Transfers/IndexViewModel.cs delete mode 100644 src/SFA.DAS.EmployerFinance.Web/ViewModels/TransfersIndexViewModel.cs rename src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/{GetPledgesRequest.cs => Transfers/GetIndexRequest.cs} (57%) delete mode 100644 src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetPledgesResponse.cs create mode 100644 src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/Transfers/GetIndexResponse.cs delete mode 100644 src/SFA.DAS.EmployerFinance/Services/ILevyTransferMatchingService.cs create mode 100644 src/SFA.DAS.EmployerFinance/Services/IManageApprenticeshipsService.cs delete mode 100644 src/SFA.DAS.EmployerFinance/Services/LevyTransferMatchingService.cs create mode 100644 src/SFA.DAS.EmployerFinance/Services/ManageApprenticeshipsService.cs diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetPledgesRequest.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetPledgesRequest.cs index 20a734e3f8..d0ff4e9906 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetPledgesRequest.cs +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Infrastructure/OuterApiRequests/WhenBuildingGetPledgesRequest.cs @@ -1,6 +1,6 @@ using FluentAssertions; using NUnit.Framework; -using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests.Transfers; namespace SFA.DAS.EmployerFinance.UnitTests.Infrastructure.OuterApiRequests { @@ -9,11 +9,11 @@ public class WhenBuildingGetPledgesRequest [Test] public void Then_The_Url_Is_Correctly_Constructed() { - var accountId = 123; + long accountId = 123; - var actual = new GetPledgesRequest(accountId); + var actual = new GetIndexRequest(accountId); - actual.GetUrl.Should().Be($"Pledges?accountId={accountId}"); + actual.GetUrl.Should().Be($"Transfers/{accountId}"); } } } \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance.UnitTests/Services/LevyTransferMatchingServiceTests/WhenIGetAPledgesCount.cs b/src/SFA.DAS.EmployerFinance.UnitTests/Services/LevyTransferMatchingServiceTests/WhenIGetAPledgesCount.cs index f6138eaefe..4578a573d8 100644 --- a/src/SFA.DAS.EmployerFinance.UnitTests/Services/LevyTransferMatchingServiceTests/WhenIGetAPledgesCount.cs +++ b/src/SFA.DAS.EmployerFinance.UnitTests/Services/LevyTransferMatchingServiceTests/WhenIGetAPledgesCount.cs @@ -1,8 +1,8 @@ using System.Threading.Tasks; using Moq; using NUnit.Framework; -using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; -using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests.Transfers; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses.Transfers; using SFA.DAS.EmployerFinance.Interfaces.OuterApi; using SFA.DAS.EmployerFinance.Services; @@ -11,14 +11,14 @@ namespace SFA.DAS.EmployerFinance.UnitTests.Services.LevyTransferMatchingService public class WhenIGetAPledgesCount { private Mock _mockApiClient; - private LevyTransferMatchingService _levyTransferMatchingService; + private ManageApprenticeshipsService _levyTransferMatchingService; [SetUp] public void Arrange() { _mockApiClient = new Mock(); - _levyTransferMatchingService = new LevyTransferMatchingService(_mockApiClient.Object); + _levyTransferMatchingService = new ManageApprenticeshipsService(_mockApiClient.Object); } [Test] @@ -29,13 +29,13 @@ public async Task ThenTheOuterApiIsCalledAndTotalPledgesForAccountReturned() var accountId = 123; _mockApiClient - .Setup(x => x.Get(It.Is(y => y.GetUrl.EndsWith(accountId.ToString())))) - .ReturnsAsync(new GetPledgesResponse() + .Setup(x => x.Get(It.Is(y => y.GetUrl.EndsWith(accountId.ToString())))) + .ReturnsAsync(new GetIndexResponse() { - TotalPledges = expectedResult, + PledgesCount = expectedResult, }); - var actualResult = await _levyTransferMatchingService.GetPledgesCount(accountId); + var actualResult = await _levyTransferMatchingService.GetIndex(accountId); Assert.AreEqual(expectedResult, actualResult); } diff --git a/src/SFA.DAS.EmployerFinance.Web/Controllers/TransfersController.cs b/src/SFA.DAS.EmployerFinance.Web/Controllers/TransfersController.cs index 0de8112db1..029ac09cb4 100644 --- a/src/SFA.DAS.EmployerFinance.Web/Controllers/TransfersController.cs +++ b/src/SFA.DAS.EmployerFinance.Web/Controllers/TransfersController.cs @@ -19,7 +19,7 @@ public TransfersController(TransfersOrchestrator transfersOrchestrator) [Route("transfers")] public async Task Index(string hashedAccountId) { - var viewModel = await _transfersOrchestrator.Index(hashedAccountId); + var viewModel = await _transfersOrchestrator.GetIndexViewModel(hashedAccountId); return View(viewModel); } diff --git a/src/SFA.DAS.EmployerFinance.Web/Orchestrators/TransfersOrchestrator.cs b/src/SFA.DAS.EmployerFinance.Web/Orchestrators/TransfersOrchestrator.cs index 2c081a2fa3..60f1f9e491 100644 --- a/src/SFA.DAS.EmployerFinance.Web/Orchestrators/TransfersOrchestrator.cs +++ b/src/SFA.DAS.EmployerFinance.Web/Orchestrators/TransfersOrchestrator.cs @@ -1,12 +1,10 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; using SFA.DAS.Authorization.EmployerUserRoles.Options; using SFA.DAS.Authorization.Services; using SFA.DAS.EAS.Account.Api.Client; using SFA.DAS.EmployerFinance.Services; -using SFA.DAS.EmployerFinance.Web.ViewModels; using SFA.DAS.HashingService; -using SFA.DAS.Common.Domain.Types; +using SFA.DAS.EmployerFinance.Web.ViewModels.Transfers; namespace SFA.DAS.EmployerFinance.Web.Orchestrators { @@ -14,9 +12,7 @@ public class TransfersOrchestrator { private readonly IAuthorizationService _authorizationService; private readonly IHashingService _hashingService; - private readonly ILevyTransferMatchingService _levyTransferMatchingService; - private readonly IAccountApiClient _accountApiClient; - private readonly ICohortsService _cohortsService; + private readonly IManageApprenticeshipsService _manageApprenticeshipsService; protected TransfersOrchestrator() { @@ -26,58 +22,29 @@ protected TransfersOrchestrator() public TransfersOrchestrator( IAuthorizationService authorizationService, IHashingService hashingService, - ILevyTransferMatchingService levyTransferMatchingService, - IAccountApiClient accountApiClient, - ICohortsService cohortsService) + IManageApprenticeshipsService manageApprenticeshipsService) { _authorizationService = authorizationService; _hashingService = hashingService; - _levyTransferMatchingService = levyTransferMatchingService; - _accountApiClient = accountApiClient; - _cohortsService = cohortsService; + _manageApprenticeshipsService = manageApprenticeshipsService; } - public async Task> Index(string hashedAccountId) + public async Task> GetIndexViewModel(string hashedAccountId) { - var accountDetail = await _accountApiClient.GetAccount(hashedAccountId).ConfigureAwait(false); - var cohortsCount = await _cohortsService.GetCohortsCount(accountDetail.AccountId).ConfigureAwait(false); - - Enum.TryParse(accountDetail.ApprenticeshipEmployerType, true, out ApprenticeshipEmployerType employerType); - - if (employerType != ApprenticeshipEmployerType.Levy || cohortsCount > 0) - { - return new OrchestratorResponse() - { - Data = new TransfersIndexViewModel() - { - CanViewPledgesSection = false - } - }; - } - - return await GenerateTransfersViewIndexModelForLevyAccounts(hashedAccountId); - } - - private async Task> GenerateTransfersViewIndexModelForLevyAccounts(string hashedAccountId) - { - bool renderCreateTransfersPledgeButton = - await _authorizationService.IsAuthorizedAsync(EmployerUserRole.OwnerOrTransactor); - var accountId = _hashingService.DecodeValue(hashedAccountId); + var indexTask = _manageApprenticeshipsService.GetIndex(accountId); + var renderCreateTransfersPledgeButtonTask = _authorizationService.IsAuthorizedAsync(EmployerUserRole.OwnerOrTransactor); - var pledgesCount = await _levyTransferMatchingService.GetPledgesCount(accountId); + await Task.WhenAll(indexTask, renderCreateTransfersPledgeButtonTask); - var viewModel = new OrchestratorResponse() + return new OrchestratorResponse { - Data = new TransfersIndexViewModel() + Data = new IndexViewModel { - RenderCreateTransfersPledgeButton = renderCreateTransfersPledgeButton, - PledgesCount = pledgesCount, - CanViewPledgesSection = true + PledgesCount = indexTask.Result.PledgesCount, + IsTransferReceiver = indexTask.Result.IsTransferReceiver } }; - - return viewModel; } } } \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance.Web/SFA.DAS.EmployerFinance.Web.csproj b/src/SFA.DAS.EmployerFinance.Web/SFA.DAS.EmployerFinance.Web.csproj index 86cbcdcbd5..aaa9ee5a25 100644 --- a/src/SFA.DAS.EmployerFinance.Web/SFA.DAS.EmployerFinance.Web.csproj +++ b/src/SFA.DAS.EmployerFinance.Web/SFA.DAS.EmployerFinance.Web.csproj @@ -211,7 +211,7 @@ - + diff --git a/src/SFA.DAS.EmployerFinance.Web/ViewModels/Transfers/IndexViewModel.cs b/src/SFA.DAS.EmployerFinance.Web/ViewModels/Transfers/IndexViewModel.cs new file mode 100644 index 0000000000..06672f99b3 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance.Web/ViewModels/Transfers/IndexViewModel.cs @@ -0,0 +1,9 @@ +namespace SFA.DAS.EmployerFinance.Web.ViewModels.Transfers +{ + public class IndexViewModel + { + public bool IsTransferReceiver { get; set; } + public bool RenderCreateTransfersPledgeButton { get; set; } + public int PledgesCount { get; set; } + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance.Web/ViewModels/TransfersIndexViewModel.cs b/src/SFA.DAS.EmployerFinance.Web/ViewModels/TransfersIndexViewModel.cs deleted file mode 100644 index 179e71c063..0000000000 --- a/src/SFA.DAS.EmployerFinance.Web/ViewModels/TransfersIndexViewModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace SFA.DAS.EmployerFinance.Web.ViewModels -{ - public class TransfersIndexViewModel - { - public bool CanViewPledgesSection { get; set; } - public bool RenderCreateTransfersPledgeButton { get; set; } - public int PledgesCount { get; set; } - } -} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance.Web/Views/Transfers/Index.cshtml b/src/SFA.DAS.EmployerFinance.Web/Views/Transfers/Index.cshtml index b3e3e1e3a8..f346662918 100644 --- a/src/SFA.DAS.EmployerFinance.Web/Views/Transfers/Index.cshtml +++ b/src/SFA.DAS.EmployerFinance.Web/Views/Transfers/Index.cshtml @@ -1,4 +1,4 @@ -@model OrchestratorResponse +@model OrchestratorResponse @{ ViewBag.PageID = "transfers"; @@ -10,7 +10,7 @@

Transfers

-@if (Model.Data.CanViewPledgesSection) +@if (Model.Data.IsTransferReceiver) {
diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetPledgesRequest.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/Transfers/GetIndexRequest.cs similarity index 57% rename from src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetPledgesRequest.cs rename to src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/Transfers/GetIndexRequest.cs index 8bc150db18..5f9c2840a8 100644 --- a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/GetPledgesRequest.cs +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiRequests/Transfers/GetIndexRequest.cs @@ -1,16 +1,16 @@ using SFA.DAS.EmployerFinance.Interfaces.OuterApi; -namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests +namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests.Transfers { - public class GetPledgesRequest : IGetApiRequest + public class GetIndexRequest : IGetApiRequest { private readonly long _accountId; - public GetPledgesRequest(long accountId) + public GetIndexRequest(long accountId) { _accountId = accountId; } - public string GetUrl => $"Pledges?accountId={_accountId}"; + public string GetUrl => $"Transfers/{_accountId}"; } } \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetPledgesResponse.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetPledgesResponse.cs deleted file mode 100644 index d8bb058cdd..0000000000 --- a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/GetPledgesResponse.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses -{ - public class GetPledgesResponse - { - public int TotalPledges { get; set; } - } -} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/Transfers/GetIndexResponse.cs b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/Transfers/GetIndexResponse.cs new file mode 100644 index 0000000000..0c8b0049ab --- /dev/null +++ b/src/SFA.DAS.EmployerFinance/Infrastructure/OuterApiResponses/Transfers/GetIndexResponse.cs @@ -0,0 +1,8 @@ +namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses.Transfers +{ + public class GetIndexResponse + { + public int PledgesCount { get; set; } + public bool IsTransferReceiver { get; set; } + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Services/ILevyTransferMatchingService.cs b/src/SFA.DAS.EmployerFinance/Services/ILevyTransferMatchingService.cs deleted file mode 100644 index c918657b6d..0000000000 --- a/src/SFA.DAS.EmployerFinance/Services/ILevyTransferMatchingService.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Threading.Tasks; - -namespace SFA.DAS.EmployerFinance.Services -{ - public interface ILevyTransferMatchingService - { - Task GetPledgesCount(long accountId); - } -} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Services/IManageApprenticeshipsService.cs b/src/SFA.DAS.EmployerFinance/Services/IManageApprenticeshipsService.cs new file mode 100644 index 0000000000..26d29daae6 --- /dev/null +++ b/src/SFA.DAS.EmployerFinance/Services/IManageApprenticeshipsService.cs @@ -0,0 +1,10 @@ +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses.Transfers; +using System.Threading.Tasks; + +namespace SFA.DAS.EmployerFinance.Services +{ + public interface IManageApprenticeshipsService + { + Task GetIndex(long accountId); + } +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Services/LevyTransferMatchingService.cs b/src/SFA.DAS.EmployerFinance/Services/LevyTransferMatchingService.cs deleted file mode 100644 index 05a8f2d905..0000000000 --- a/src/SFA.DAS.EmployerFinance/Services/LevyTransferMatchingService.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace SFA.DAS.EmployerFinance.Services -{ - using System.Threading.Tasks; - using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests; - using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses; - using SFA.DAS.EmployerFinance.Interfaces.OuterApi; - - public class LevyTransferMatchingService : ILevyTransferMatchingService - { - private readonly IApiClient _apiClient; - - public LevyTransferMatchingService( - IApiClient apiClient) - { - _apiClient = apiClient; - } - - public async Task GetPledgesCount(long accountId) - { - var getPledgesResponse = await _apiClient.Get(new GetPledgesRequest(accountId)); - - return getPledgesResponse.TotalPledges; - } - } -} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerFinance/Services/ManageApprenticeshipsService.cs b/src/SFA.DAS.EmployerFinance/Services/ManageApprenticeshipsService.cs new file mode 100644 index 0000000000..ba7b753c6a --- /dev/null +++ b/src/SFA.DAS.EmployerFinance/Services/ManageApprenticeshipsService.cs @@ -0,0 +1,23 @@ +using System.Threading.Tasks; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests.Transfers; +using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses.Transfers; +using SFA.DAS.EmployerFinance.Interfaces.OuterApi; + +namespace SFA.DAS.EmployerFinance.Services +{ + public class ManageApprenticeshipsService : IManageApprenticeshipsService + { + private readonly IApiClient _apiClient; + + public ManageApprenticeshipsService( + IApiClient apiClient) + { + _apiClient = apiClient; + } + + public async Task GetIndex(long accountId) + { + return await _apiClient.Get(new GetIndexRequest(accountId)); + } + } +} \ No newline at end of file