Skip to content

Commit

Permalink
Merge pull request #2206 from SkillsFundingAgency/TM-86_options_to_ap…
Browse files Browse the repository at this point in the history
…ply_for_transfers

Tm 86 options to apply for transfers
  • Loading branch information
ConorTill authored Sep 28, 2021
2 parents 6112146 + 40a948e commit 50feb8c
Show file tree
Hide file tree
Showing 20 changed files with 326 additions and 99 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,15 +29,15 @@ 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);
Assert.AreEqual(expectedResult, actualResult.PledgesCount);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using SFA.DAS.Authorization.EmployerFeatures.Models;
using SFA.DAS.Authorization.EmployerUserRoles.Options;
using SFA.DAS.Authorization.Features.Services;
using SFA.DAS.Authorization.Services;
using SFA.DAS.EAS.Account.Api.Client;
using SFA.DAS.EAS.Account.Api.Types;
using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses.Transfers;
using SFA.DAS.EmployerFinance.Interfaces.OuterApi;
using SFA.DAS.EmployerFinance.Services;
using SFA.DAS.EmployerFinance.Web.Orchestrators;
using SFA.DAS.HashingService;
using SFA.DAS.OidcMiddleware.Builders;

namespace SFA.DAS.EmployerFinance.Web.UnitTests.Orchestrators
{
[TestFixture]
public class WhenGettingTransfers
{
private TransfersOrchestrator _orchestrator;
private Mock<IAuthorizationService> _authorisationService;
private Mock<IHashingService> _hashingService;
private Mock<IManageApprenticeshipsService> _maService;
private Mock<IAccountApiClient> _accountApiClient;
private Mock<IFeatureTogglesService<EmployerFeatureToggle>> _featureTogglesService;

private const string HashedAccountId = "123ABC";
private const long AccountId = 1234;

[SetUp]
public void Setup()
{
_authorisationService = new Mock<IAuthorizationService>();
_hashingService = new Mock<IHashingService>();
_maService = new Mock<IManageApprenticeshipsService>();
_accountApiClient = new Mock<IAccountApiClient>();
_featureTogglesService = new Mock<IFeatureTogglesService<EmployerFeatureToggle>>();

_hashingService.Setup(h => h.DecodeValue(HashedAccountId)).Returns(AccountId);
_featureTogglesService.Setup(x => x.GetFeatureToggle(It.IsAny<string>())).Returns(new EmployerFeatureToggle { IsEnabled = true });

_orchestrator = new TransfersOrchestrator(_authorisationService.Object, _hashingService.Object, _maService.Object, _accountApiClient.Object, _featureTogglesService.Object);
}

[Test]
public async Task AndTheEmployerIsATransferReceiverThenReturnsIndexViewModelWithCanViewPledgesSectionSetToFalse()
{
_maService.Setup(o => o.GetIndex(AccountId)).ReturnsAsync(new GetIndexResponse
{
IsTransferReceiver = true
});

SetupTheAccountApiClient();

var actual = await _orchestrator.GetIndexViewModel(HashedAccountId);

Assert.IsFalse(actual.Data.CanViewPledgesSection);
}

[Test]
public async Task AndTheEmployerIsNotATransferReceiverButItIsNonLevyThenReturnsIndexViewModelWithCanViewPledgesSectionSetToFalse()
{
_maService.Setup(o => o.GetIndex(AccountId)).ReturnsAsync(new GetIndexResponse
{
IsTransferReceiver = false
});

SetupTheAccountApiClient();

var actual = await _orchestrator.GetIndexViewModel(HashedAccountId);

Assert.IsFalse(actual.Data.CanViewPledgesSection);
}

[Test]
public async Task AndTheEmployerIsNotATransferReceiverButItIsLevyThenReturnsIndexViewModelWithCanViewPledgesSectionSetToTrue()
{
_maService.Setup(o => o.GetIndex(AccountId)).ReturnsAsync(new GetIndexResponse
{
IsTransferReceiver = false
});

SetupTheAccountApiClient(true);

var actual = await _orchestrator.GetIndexViewModel(HashedAccountId);

Assert.IsTrue(actual.Data.CanViewPledgesSection);
}

[Test]
public async Task AndTheEmployerIsNonLevyThenCanViewApplySectionIsTrue()
{
_maService.Setup(o => o.GetIndex(AccountId)).ReturnsAsync(new GetIndexResponse
{
IsTransferSender = false
});

SetupTheAccountApiClient(false);

var actual = await _orchestrator.GetIndexViewModel(HashedAccountId);

Assert.IsTrue(actual.Data.CanViewApplySection);
}

[Test]
public async Task AndTheEmployerIsLevyAndNotSendingFundsThenCanViewApplySectionIsTrue()
{
_maService.Setup(o => o.GetIndex(AccountId)).ReturnsAsync(new GetIndexResponse
{
IsTransferSender = false
});

SetupTheAccountApiClient(true);

var actual = await _orchestrator.GetIndexViewModel(HashedAccountId);

Assert.IsTrue(actual.Data.CanViewApplySection);
}

[Test]
public async Task AndTheEmployerIsLevyAndSendingFundsThenCanViewApplySectionIsFalse()
{
_maService.Setup(o => o.GetIndex(AccountId)).ReturnsAsync(new GetIndexResponse
{
IsTransferSender = true
});

SetupTheAccountApiClient(true);

var actual = await _orchestrator.GetIndexViewModel(HashedAccountId);

Assert.IsFalse(actual.Data.CanViewApplySection);
}

[TestCase(true, true)]
[TestCase(false, false)]
public async Task ThenChecksTheUserIsAuthorisedToCreateTransfers(bool isAuthorised, bool expected)
{
_maService.Setup(o => o.GetIndex(AccountId)).ReturnsAsync(new GetIndexResponse
{
IsTransferReceiver = false
});

SetupTheAccountApiClient(true);

_authorisationService.Setup(o => o.IsAuthorizedAsync(EmployerUserRole.OwnerOrTransactor)).ReturnsAsync(isAuthorised);

var actual = await _orchestrator.GetIndexViewModel(HashedAccountId);

Assert.AreEqual(expected, actual.Data.RenderCreateTransfersPledgeButton);
}

private void SetupTheAccountApiClient(bool isLevy = false)
{
var modelToReturn = new AccountDetailViewModel
{
ApprenticeshipEmployerType = isLevy ? "Levy" : "NonLevy"
};

_accountApiClient.Setup(o => o.GetAccount(HashedAccountId)).ReturnsAsync(modelToReturn);
}
}
}
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
Expand Up @@ -29,14 +29,22 @@ public static string EmployerCommitmentsAction(this UrlHelper helper, string pat
return AccountAction(helper, baseUrl, path);
}

public static string LevyMatchingTransfersAction(this UrlHelper helper, string path)
public static string LevyTransfersMatchingAccountAction(this UrlHelper helper, string path)
{
var configuration = DependencyResolver.Current.GetService<EmployerFinanceConfiguration>();
var baseUrl = configuration.LevyTransferMatchingBaseUrl;

return AccountAction(helper, baseUrl, path);
}

public static string LevyTransfersMatchingAction(this UrlHelper helper, string path)
{
var configuration = DependencyResolver.Current.GetService<EmployerFinanceConfiguration>();
var baseUrl = configuration.LevyTransferMatchingBaseUrl;

return Action(baseUrl, path);
}

public static string ReservationsAction(this UrlHelper helper, string path)
{
var configuration = DependencyResolver.Current.GetService<EmployerFinanceConfiguration>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using SFA.DAS.Authorization.EmployerUserRoles.Options;
using SFA.DAS.Authorization.Services;
using SFA.DAS.EmployerFinance.Services;
using SFA.DAS.EmployerFinance.Web.ViewModels;
using SFA.DAS.HashingService;
using SFA.DAS.EmployerFinance.Web.ViewModels.Transfers;
using SFA.DAS.Common.Domain.Types;
using SFA.DAS.Authorization.Features.Services;
using SFA.DAS.Authorization.EmployerFeatures.Models;
using SFA.DAS.EAS.Account.Api.Client;

namespace SFA.DAS.EmployerFinance.Web.Orchestrators
{
public class TransfersOrchestrator
{
private readonly IAuthorizationService _authorizationService;
private readonly IHashingService _hashingService;
private readonly ILevyTransferMatchingService _levyTransferMatchingService;
private readonly IManageApprenticeshipsService _manageApprenticeshipsService;
private readonly IAccountApiClient _accountApiClient;
private readonly IFeatureTogglesService<EmployerFeatureToggle> _featureTogglesService;

protected TransfersOrchestrator()
{
Expand All @@ -21,31 +28,43 @@ protected TransfersOrchestrator()
public TransfersOrchestrator(
IAuthorizationService authorizationService,
IHashingService hashingService,
ILevyTransferMatchingService levyTransferMatchingService)
IManageApprenticeshipsService manageApprenticeshipsService,
IAccountApiClient accountApiClient,
IFeatureTogglesService<EmployerFeatureToggle> featureTogglesService)
{
_authorizationService = authorizationService;
_hashingService = hashingService;
_levyTransferMatchingService = levyTransferMatchingService;
_manageApprenticeshipsService = manageApprenticeshipsService;
_accountApiClient = accountApiClient;
_featureTogglesService = featureTogglesService;
}

public async Task<OrchestratorResponse<TransfersIndexViewModel>> Index(string hashedAccountId)
public async Task<OrchestratorResponse<IndexViewModel>> GetIndexViewModel(string hashedAccountId)
{
bool renderCreateTransfersPledgeButton = await _authorizationService.IsAuthorizedAsync(EmployerUserRole.OwnerOrTransactor);

var accountId = _hashingService.DecodeValue(hashedAccountId);
var indexTask = _manageApprenticeshipsService.GetIndex(accountId);
var accountDetail = _accountApiClient.GetAccount(hashedAccountId);

var renderCreateTransfersPledgeButtonTask = _authorizationService.IsAuthorizedAsync(EmployerUserRole.OwnerOrTransactor);
var renderApplicationListButton = _featureTogglesService.GetFeatureToggle("ApplicationList");

await Task.WhenAll(indexTask, renderCreateTransfersPledgeButtonTask, accountDetail);

var pledgesCount = await _levyTransferMatchingService.GetPledgesCount(accountId);
Enum.TryParse(accountDetail.Result.ApprenticeshipEmployerType, true, out ApprenticeshipEmployerType employerType);

var viewModel = new OrchestratorResponse<TransfersIndexViewModel>()

return new OrchestratorResponse<IndexViewModel>
{
Data = new TransfersIndexViewModel()
Data = new IndexViewModel
{
RenderCreateTransfersPledgeButton = renderCreateTransfersPledgeButton,
PledgesCount = pledgesCount,
CanViewPledgesSection = !(indexTask.Result.IsTransferReceiver || employerType == ApprenticeshipEmployerType.NonLevy),
CanViewApplySection = !(indexTask.Result.IsTransferSender && employerType == ApprenticeshipEmployerType.Levy),
PledgesCount = indexTask.Result.PledgesCount,
ApplicationsCount = indexTask.Result.ApplicationsCount,
RenderCreateTransfersPledgeButton = renderCreateTransfersPledgeButtonTask.Result,
RenderApplicationListButton = renderApplicationListButton.IsEnabled
}
};

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,12 @@
namespace SFA.DAS.EmployerFinance.Web.ViewModels.Transfers
{
public class IndexViewModel
{
public bool RenderCreateTransfersPledgeButton { get; set; }
public bool RenderApplicationListButton { get; set; }
public bool CanViewPledgesSection { get; set; }
public bool CanViewApplySection { get; set; }
public int PledgesCount { get; set; }
public int ApplicationsCount { get; set; }
}
}

This file was deleted.

Loading

0 comments on commit 50feb8c

Please sign in to comment.