Skip to content

Commit

Permalink
Refactoring GetPledges into GetIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
ConorTill committed Sep 15, 2021
1 parent 9e03a98 commit f8c1f23
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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}");
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,14 +11,14 @@ namespace SFA.DAS.EmployerFinance.UnitTests.Services.LevyTransferMatchingService
public class WhenIGetAPledgesCount
{
private Mock<IApiClient> _mockApiClient;
private LevyTransferMatchingService _levyTransferMatchingService;
private ManageApprenticeshipsService _levyTransferMatchingService;

[SetUp]
public void Arrange()
{
_mockApiClient = new Mock<IApiClient>();

_levyTransferMatchingService = new LevyTransferMatchingService(_mockApiClient.Object);
_levyTransferMatchingService = new ManageApprenticeshipsService(_mockApiClient.Object);
}

[Test]
Expand All @@ -29,13 +29,13 @@ public async Task ThenTheOuterApiIsCalledAndTotalPledgesForAccountReturned()
var accountId = 123;

_mockApiClient
.Setup(x => x.Get<GetPledgesResponse>(It.Is<GetPledgesRequest>(y => y.GetUrl.EndsWith(accountId.ToString()))))
.ReturnsAsync(new GetPledgesResponse()
.Setup(x => x.Get<GetIndexResponse>(It.Is<GetIndexRequest>(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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public TransfersController(TransfersOrchestrator transfersOrchestrator)
[Route("transfers")]
public async Task<ActionResult> Index(string hashedAccountId)
{
var viewModel = await _transfersOrchestrator.Index(hashedAccountId);
var viewModel = await _transfersOrchestrator.GetIndexViewModel(hashedAccountId);

return View(viewModel);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
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
{
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()
{
Expand All @@ -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<OrchestratorResponse<TransfersIndexViewModel>> Index(string hashedAccountId)
public async Task<OrchestratorResponse<IndexViewModel>> 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<TransfersIndexViewModel>()
{
Data = new TransfersIndexViewModel()
{
CanViewPledgesSection = false
}
};
}

return await GenerateTransfersViewIndexModelForLevyAccounts(hashedAccountId);
}

private async Task<OrchestratorResponse<TransfersIndexViewModel>> 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<TransfersIndexViewModel>()
return new OrchestratorResponse<IndexViewModel>
{
Data = new TransfersIndexViewModel()
Data = new IndexViewModel
{
RenderCreateTransfersPledgeButton = renderCreateTransfersPledgeButton,
PledgesCount = pledgesCount,
CanViewPledgesSection = true
PledgesCount = indexTask.Result.PledgesCount,
IsTransferReceiver = indexTask.Result.IsTransferReceiver
}
};

return viewModel;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
<Compile Include="ViewModels\TransactionLineViewModel.cs" />
<Compile Include="ViewModels\TransactionViewModel.cs" />
<Compile Include="ViewModels\TransactionViewResultViewModel.cs" />
<Compile Include="ViewModels\TransfersIndexViewModel.cs" />
<Compile Include="ViewModels\Transfers\IndexViewModel.cs" />
<Compile Include="ViewModels\TransferTransactionDetailsViewModel.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions src/SFA.DAS.EmployerFinance.Web/Views/Transfers/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model OrchestratorResponse<TransfersIndexViewModel>
@model OrchestratorResponse<SFA.DAS.EmployerFinance.Web.ViewModels.Transfers.IndexViewModel>

@{
ViewBag.PageID = "transfers";
Expand All @@ -10,7 +10,7 @@

<h1 class="heading-xlarge">Transfers</h1>

@if (Model.Data.CanViewPledgesSection)
@if (Model.Data.IsTransferReceiver)
{

<div class="govuk-grid-row">
Expand Down
Original file line number Diff line number Diff line change
@@ -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}";
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses.Transfers
{
public class GetIndexResponse
{
public int PledgesCount { get; set; }
public bool IsTransferReceiver { get; set; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses.Transfers;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerFinance.Services
{
public interface IManageApprenticeshipsService
{
Task<GetIndexResponse> GetIndex(long accountId);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<GetIndexResponse> GetIndex(long accountId)
{
return await _apiClient.Get<GetIndexResponse>(new GetIndexRequest(accountId));
}
}
}

0 comments on commit f8c1f23

Please sign in to comment.