Skip to content

Commit

Permalink
changes around TM-133 refactoring for the tech review
Browse files Browse the repository at this point in the history
  • Loading branch information
sscaife committed Sep 14, 2021
1 parent 9e03a98 commit 19160d5
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ namespace SFA.DAS.EmployerFinance.UnitTests.Services.CohortsServiceTests
public class WhenIGetACohortsCount
{
private Mock<IApiClient> _apiClient;
private CohortsService _service;
private ApprenticeshipService _service;

[SetUp]
public void Setup()
{
_apiClient = new Mock<IApiClient>();
_service = new CohortsService(_apiClient.Object);
_service = new ApprenticeshipService(_apiClient.Object);
}

[Test]
public async Task ThenTheApiIsCalledWithAValidAccountIdAndTheCohortsCountIsReturned()
{
SetupApiClient(1);

var actual = await _service.GetCohortsCount(1);
var actual = await _service.GetApprenticeshipsFor(1);

Assert.AreEqual(1, actual);
}
Expand All @@ -41,7 +41,7 @@ public async Task ThenTheApiIsCalledWithAnInvalidAccountIdAndTheCohortsCountIsZe
{
SetupApiClient(0, false);

var actual = await _service.GetCohortsCount(0);
var actual = await _service.GetApprenticeshipsFor(0);

Assert.AreEqual(0, actual);
}
Expand All @@ -62,11 +62,11 @@ private void SetupApiClient(long accountId, bool addItem = true)
}

_apiClient.Setup(o =>
o.Get<GetCohortsResponse>(
It.Is<GetCohortsRequest>(i => i.GetUrl.Equals($"api/cohorts?accountId={accountId}"))))
.ReturnsAsync(new GetCohortsResponse
o.Get<GetApplicationsResponse>(
It.Is<GetApplicationsRequest>(i => i.GetUrl.Equals($"api/cohorts?accountId={accountId}"))))
.ReturnsAsync(new GetApplicationsResponse
{
Cohorts = items
//Apprenticeships = items
});
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/SFA.DAS.EmployerFinance.Web/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.EmployerFinance.Models.Apprenticeships;

namespace SFA.DAS.EmployerFinance.Web.Extensions
{
public static class ListExtensions
{
public static long GetActivelyFundedApprenticeCount(this IEnumerable<ApprenticeshipDetail> apprenticesDetails)
{
return apprenticesDetails.Count(o =>
o.ApprenticeshipStatus == ApprenticeshipStatus.WaitingToStart ||
o.ApprenticeshipStatus == ApprenticeshipStatus.Live ||
o.ApprenticeshipStatus == ApprenticeshipStatus.Paused);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using SFA.DAS.EmployerFinance.Web.ViewModels;
using SFA.DAS.HashingService;
using SFA.DAS.Common.Domain.Types;
using SFA.DAS.EmployerFinance.Web.Extensions;

namespace SFA.DAS.EmployerFinance.Web.Orchestrators
{
Expand All @@ -16,7 +17,8 @@ public class TransfersOrchestrator
private readonly IHashingService _hashingService;
private readonly ILevyTransferMatchingService _levyTransferMatchingService;
private readonly IAccountApiClient _accountApiClient;
private readonly ICohortsService _cohortsService;
private IApprenticeshipService _apprenticeshipService;


protected TransfersOrchestrator()
{
Expand All @@ -28,36 +30,48 @@ public TransfersOrchestrator(
IHashingService hashingService,
ILevyTransferMatchingService levyTransferMatchingService,
IAccountApiClient accountApiClient,
ICohortsService cohortsService)
IApprenticeshipService apprenticeshipService)
{
_authorizationService = authorizationService;
_hashingService = hashingService;
_levyTransferMatchingService = levyTransferMatchingService;
_accountApiClient = accountApiClient;
_cohortsService = cohortsService;
_apprenticeshipService = apprenticeshipService;
}

public async Task<OrchestratorResponse<TransfersIndexViewModel>> Index(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)
if (employerType != ApprenticeshipEmployerType.Levy)
{
return new OrchestratorResponse<TransfersIndexViewModel>()
{
Data = new TransfersIndexViewModel()
{
CanViewPledgesSection = false
}
};
return GenerateTransfersViewIndexModelForNonLevyOrCurrentlyFundedLevyEmployers();
}

var fundedApprentices = await _apprenticeshipService.GetApprenticeshipsFor(accountDetail.AccountId)
.ConfigureAwait(false);

if (fundedApprentices.GetActivelyFundedApprenticeCount() > 0)
{
return GenerateTransfersViewIndexModelForNonLevyOrCurrentlyFundedLevyEmployers();
}

return await GenerateTransfersViewIndexModelForLevyAccounts(hashedAccountId);
}

private static OrchestratorResponse<TransfersIndexViewModel> GenerateTransfersViewIndexModelForNonLevyOrCurrentlyFundedLevyEmployers()
{
return new OrchestratorResponse<TransfersIndexViewModel>()
{
Data = new TransfersIndexViewModel()
{
CanViewPledgesSection = false
}
};
}

private async Task<OrchestratorResponse<TransfersIndexViewModel>> GenerateTransfersViewIndexModelForLevyAccounts(string hashedAccountId)
{
bool renderCreateTransfersPledgeButton =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
<Compile Include="DependencyResolution\StructureMapDependencyScope.cs" />
<Compile Include="DependencyResolution\StructureMapScopeModule.cs" />
<Compile Include="Extensions\HtmlHelperExtensions.cs" />
<Compile Include="Extensions\ListExtensions.cs" />
<Compile Include="Extensions\TimeSpanExtensions.cs" />
<Compile Include="Extensions\UrlHelperExtensions.cs" />
<Compile Include="Filters\LevyEmployerTypeOnly.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public ManageApprenticeshipsOuterApiRegistry ()
For<ManageApprenticeshipsOuterApiConfiguration>().Use(c => c.GetInstance<EmployerFinanceConfiguration>().ManageApprenticeshipsOuterApiConfiguration).Singleton();
For<IApiClient>().Use<ManageApprenticeshipsApiClient>().Ctor<HttpClient>().Is(new HttpClient()).Singleton();
For<IApiClient>().Add<CommitmentsApiClient>().Named("CAPI").Ctor<HttpClient>().Is(new HttpClient()).Singleton();
For<ICohortsService>().Use<CohortsService>().Ctor<IApiClient>().IsNamedInstance("CAPI").Singleton();
For<IApprenticeshipService>().Use<ApprenticeshipService>().Ctor<IApiClient>().IsNamedInstance("CAPI").Singleton();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@

namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests
{
public class GetCohortsRequest : IGetApiRequest
public class GetApplicationsRequest : IGetApiRequest
{
private readonly long _accountId;
private readonly int _pageSize;

public GetCohortsRequest(long accountId)
public GetApplicationsRequest(long accountId, int pageSize)
{
_accountId = accountId;
_pageSize = pageSize;
}

public string GetUrl => $"api/cohorts?accountId={_accountId}";
public string GetUrl => $"api/apprenticeships?accountId={_accountId}&PageItemCount={_pageSize}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using SFA.DAS.EmployerFinance.Models.Apprenticeships;

namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses
{
public class GetApplicationsResponse
{
public IEnumerable<ApprenticeshipDetail> Apprenticeships { get; set; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using SFA.DAS.CommitmentsV2.Types;

namespace SFA.DAS.EmployerFinance.Models.Apprenticeships
{
public class ApprenticeshipDetail
{
public long Id { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public string Uln { get; set; }

public string EmployerName { get; set; }

public string ProviderName { get; set; }

public string CourseName { get; set; }

public DateTime StartDate { get; set; }

public DateTime EndDate { get; set; }

public PaymentStatus PaymentStatus { get; set; }

public ApprenticeshipStatus ApprenticeshipStatus { get; set; }

public IEnumerable<Alerts> Alerts { get; set; }
}
}
14 changes: 0 additions & 14 deletions src/SFA.DAS.EmployerFinance/Models/Cohort/CurrentCohorts.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/SFA.DAS.EmployerFinance/SFA.DAS.EmployerFinance.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PackageReference Include="SFA.DAS.Caches" Version="1.1.4" />
<PackageReference Include="SFA.DAS.Commitments.Api.Client" Version="4.1.355" />
<PackageReference Include="SFA.DAS.Commitments.Api.Types" Version="4.1.355" />
<PackageReference Include="SFA.DAS.CommitmentsV2.Api.Types" Version="4.1.1063" />
<PackageReference Include="SFA.DAS.CommitmentsV2.Types" Version="7.71.0" />
<PackageReference Include="SFA.DAS.Common.Domain" Version="1.4.283" />
<PackageReference Include="SFA.DAS.Configuration" Version="1.0.0.53229" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
using SFA.DAS.EmployerFinance.Infrastructure.OuterApiRequests;
using SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses;
using SFA.DAS.EmployerFinance.Interfaces.OuterApi;
using SFA.DAS.EmployerFinance.Models.Cohort;
using SFA.DAS.EmployerFinance.Models.Apprenticeships;

namespace SFA.DAS.EmployerFinance.Services
{
public class CohortsService : ICohortsService
public class ApprenticeshipService : IApprenticeshipService
{
private readonly IApiClient _apiClient;

public CohortsService(IApiClient apiClient)
public ApprenticeshipService(IApiClient apiClient)
{
_apiClient = apiClient;
}

public async Task<int> GetCohortsCount(long accountId)
public async Task<IEnumerable<ApprenticeshipDetail>> GetApprenticeshipsFor(long accountId)
{
var cohortsCountResponse = await _apiClient.Get<GetCohortsResponse>(new GetCohortsRequest(accountId)).ConfigureAwait(false);
var applicationsResponse = await _apiClient.Get<GetApplicationsResponse>(new GetApplicationsRequest(accountId, 500)).ConfigureAwait(false);

return cohortsCountResponse.Cohorts.Count;
return applicationsResponse.Apprenticeships;
}
}
}
11 changes: 11 additions & 0 deletions src/SFA.DAS.EmployerFinance/Services/IApprenticeshipService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using SFA.DAS.EmployerFinance.Models.Apprenticeships;

namespace SFA.DAS.EmployerFinance.Services
{
public interface IApprenticeshipService
{
Task<IEnumerable<ApprenticeshipDetail>> GetApprenticeshipsFor(long accountId);
}
}
9 changes: 0 additions & 9 deletions src/SFA.DAS.EmployerFinance/Services/ICohortsService.cs

This file was deleted.

0 comments on commit 19160d5

Please sign in to comment.