Skip to content

Commit

Permalink
Merge pull request #720 from SkillsFundingAgency/CV-713-get-cohorts-f…
Browse files Browse the repository at this point in the history
…or-providerid

Cv 713 get cohorts for providerid
  • Loading branch information
Najamuddin-Muhammad authored May 5, 2020
2 parents f0db2fd + 92623ff commit 471c0d8
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CohortController(IMediator mediator)
[HttpGet]
public async Task<IActionResult> GetCohorts([FromQuery] GetCohortsRequest request)
{
var query = new GetCohortsQuery(request.AccountId);
var query = new GetCohortsQuery(request.AccountId, request.ProviderId);
var result = await _mediator.Send(query);

return Ok(new GetCohortsResponse(result.Cohorts));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ public class GetCohortsHandlerTests
public async Task Handle_WithAccountId_ShouldReturnDraftUnapprovedCohortsForThatEmployer()
{
var f = new GetCohortsHandlerTestFixtures();
f.AddEmptyDraftCohortForEmployer(f.AccountId);
f.AddEmptyDraftCohortWithEmployer(f.AccountId);

var response = await f.GetResponse(new GetCohortsQuery(f.AccountId));
var response = await f.GetResponse(new GetCohortsQuery(f.AccountId, null));

Assert.IsNotNull(response);
Assert.AreEqual(response.Cohorts.Length, f.SeedCohorts.Count);
Assert.AreEqual(response.Cohorts[0].AccountId, f.AccountId);
Assert.AreEqual(response.Cohorts[0].LegalEntityName, f.SeedCohorts[0].LegalEntityName);
Assert.AreEqual(response.Cohorts[0].ProviderId, f.SeedCohorts[0].ProviderId);
Assert.AreEqual(response.Cohorts[0].ProviderName, f.SeedCohorts[0].ProviderName);
Assert.AreEqual(response.Cohorts[0].CohortId, f.SeedCohorts[0].Id);
Assert.AreEqual(response.Cohorts[0].CreatedOn, f.SeedCohorts[0].CreatedOn);
Assert.AreEqual(f.SeedCohorts.Count, response.Cohorts.Length);
Assert.AreEqual(f.AccountId, response.Cohorts[0].AccountId);
Assert.AreEqual(f.SeedCohorts[0].LegalEntityName, response.Cohorts[0].LegalEntityName);
Assert.AreEqual(f.SeedCohorts[0].ProviderId, response.Cohorts[0].ProviderId);
Assert.AreEqual(f.SeedCohorts[0].ProviderName, response.Cohorts[0].ProviderName);
Assert.AreEqual(f.SeedCohorts[0].Id, response.Cohorts[0].CohortId);
Assert.AreEqual(f.SeedCohorts[0].CreatedOn, response.Cohorts[0].CreatedOn);
}

[Test]
public async Task Handle_WithAccountId_ShouldReturnEmptyMessagesAsNothingSent()
{
var f = new GetCohortsHandlerTestFixtures();
f.AddEmptyDraftCohortForEmployer(f.AccountId);
f.AddEmptyDraftCohortWithEmployer(f.AccountId);

var response = await f.GetResponse(new GetCohortsQuery(f.AccountId));
var response = await f.GetResponse(new GetCohortsQuery(f.AccountId, null));

Assert.IsNull(response.Cohorts[0].LatestMessageFromEmployer);
Assert.IsNull(response.Cohorts[0].LatestMessageFromProvider);
Expand All @@ -52,12 +52,12 @@ public async Task Handle_WithAccountId_ShouldReturnEmptyMessagesAsNothingSent()
public async Task Handle_WithAccountIdWithNoCohorts_ShouldReturnEmptyList()
{
var f = new GetCohortsHandlerTestFixtures();
f.AddEmptyDraftCohortForEmployer(f.AccountId);
f.AddEmptyDraftCohortWithEmployer(f.AccountId);

var response = await f.GetResponse(new GetCohortsQuery(f.NonMatchingAccountId));
var response = await f.GetResponse(new GetCohortsQuery(f.NonMatchingId, null));

Assert.IsNotNull(response);
Assert.AreEqual(response.Cohorts.Length, 0);
Assert.AreEqual(0, response.Cohorts.Length);
}

[Test]
Expand All @@ -66,10 +66,10 @@ public async Task Handle_WithAccountIdWithApprovedCohort_ShouldReturnEmptyList()
var f = new GetCohortsHandlerTestFixtures();
f.AddCohortForEmployerApprovedByBoth(f.AccountId);

var response = await f.GetResponse(new GetCohortsQuery(f.AccountId));
var response = await f.GetResponse(new GetCohortsQuery(f.AccountId, null));

Assert.IsNotNull(response);
Assert.AreEqual(response.Cohorts.Length, 0);
Assert.AreEqual(0, response.Cohorts.Length);
}

[Test]
Expand All @@ -78,10 +78,10 @@ public async Task Handle_WithAccountIdWithTransferApprovedCohort_ShouldReturnEmp
var f = new GetCohortsHandlerTestFixtures();
f.AddTransferCohortForEmployerAndApprovedByAll(f.AccountId);

var response = await f.GetResponse(new GetCohortsQuery(f.AccountId));
var response = await f.GetResponse(new GetCohortsQuery(f.AccountId, null));

Assert.IsNotNull(response);
Assert.AreEqual(response.Cohorts.Length, 0);
Assert.AreEqual(0, response.Cohorts.Length);
}

[Test]
Expand All @@ -90,12 +90,12 @@ public async Task Handle_WithAccountId_CohortWithTransferSender_ShouldReturnTran
var f = new GetCohortsHandlerTestFixtures();
f.AddCohortWithTransferSender(f.AccountId);

var response = await f.GetResponse(new GetCohortsQuery(f.AccountId));
var response = await f.GetResponse(new GetCohortsQuery(f.AccountId, null));

Assert.IsNotNull(response);
Assert.AreEqual(response.Cohorts.Length, 1);
Assert.AreEqual(response.Cohorts[0].TransferSenderId, f.TransferSenderId);
Assert.AreEqual(response.Cohorts[0].TransferSenderName, "TransferSender");
Assert.AreEqual(1, response.Cohorts.Length);
Assert.AreEqual(f.TransferSenderId, response.Cohorts[0].TransferSenderId);
Assert.AreEqual("TransferSender", response.Cohorts[0].TransferSenderName);
}

[Test]
Expand All @@ -104,19 +104,82 @@ public async Task Handle_WithAccountIdWithMixedCohorts_ShouldReturn2CohortsAndEx
var f = new GetCohortsHandlerTestFixtures();
f.AddUnapprovedCohortForEmployerWithMessagesAnd2Apprentices(f.AccountId)
.AddUnapprovedCohortForEmployerWithMessagesAnd2Apprentices(f.AccountId)
.AddUnapprovedCohortForEmployerWithMessagesAnd2Apprentices(f.NonMatchingAccountId)
.AddUnapprovedCohortForEmployerWithMessagesAnd2Apprentices(f.NonMatchingId)
.AddCohortForEmployerApprovedByBoth(f.AccountId);

var response = await f.GetResponse(new GetCohortsQuery(f.AccountId));
var response = await f.GetResponse(new GetCohortsQuery(f.AccountId, null));

Assert.IsNotNull(response);
Assert.AreEqual(response.Cohorts.Length, 2);
Assert.AreEqual(response.Cohorts[0].LatestMessageFromEmployer.Text, "EmployerLast");
Assert.AreEqual(response.Cohorts[0].LatestMessageFromProvider.Text, "ProviderLast");
Assert.AreEqual(response.Cohorts[0].NumberOfDraftApprentices, 2);
Assert.AreEqual(response.Cohorts[1].LatestMessageFromEmployer.Text, "EmployerLast");
Assert.AreEqual(response.Cohorts[1].LatestMessageFromProvider.Text, "ProviderLast");
Assert.AreEqual(response.Cohorts[1].NumberOfDraftApprentices, 2);
Assert.AreEqual(2, response.Cohorts.Length);
Assert.AreEqual("EmployerLast", response.Cohorts[0].LatestMessageFromEmployer.Text);
Assert.AreEqual("ProviderLast", response.Cohorts[0].LatestMessageFromProvider.Text);
Assert.AreEqual(2, response.Cohorts[0].NumberOfDraftApprentices);
Assert.AreEqual("EmployerLast", response.Cohorts[1].LatestMessageFromEmployer.Text);
Assert.AreEqual("ProviderLast", response.Cohorts[1].LatestMessageFromProvider.Text);
Assert.AreEqual(2, response.Cohorts[1].NumberOfDraftApprentices);
}

[Test]
public async Task Handle_WithProviderId_ShouldReturnDraftUnapprovedCohortsForThatProvider()
{
var f = new GetCohortsHandlerTestFixtures();
f.AddEmptyDraftCohortWithEmployer(f.AccountId);

var response = await f.GetResponse(new GetCohortsQuery(null, f.ProviderId));

Assert.IsNotNull(response);
Assert.AreEqual(f.SeedCohorts.Count, response.Cohorts.Length);
Assert.AreEqual(f.AccountId, response.Cohorts[0].AccountId);
Assert.AreEqual(f.SeedCohorts[0].LegalEntityName, response.Cohorts[0].LegalEntityName);
Assert.AreEqual(f.SeedCohorts[0].ProviderId, response.Cohorts[0].ProviderId);
Assert.AreEqual(f.SeedCohorts[0].ProviderName, response.Cohorts[0].ProviderName);
Assert.AreEqual(f.SeedCohorts[0].Id, response.Cohorts[0].CohortId);
Assert.AreEqual(f.SeedCohorts[0].CreatedOn, response.Cohorts[0].CreatedOn);
}

[Test]
public async Task Handle_WithProviderIdWithNoCohorts_ShouldReturnEmptyList()
{
var f = new GetCohortsHandlerTestFixtures();
f.AddEmptyDraftCohortWithEmployer(f.AccountId);

var response = await f.GetResponse(new GetCohortsQuery(null, f.NonMatchingId));

Assert.IsNotNull(response);
Assert.AreEqual(0, response.Cohorts.Length);
}

[Test]
public async Task Handle_WithProviderIdWithApprovedCohort_ShouldReturnEmptyList()
{
var f = new GetCohortsHandlerTestFixtures();
f.AddCohortForEmployerApprovedByBoth(f.AccountId);

var response = await f.GetResponse(new GetCohortsQuery(null, f.ProviderId));

Assert.IsNotNull(response);
Assert.AreEqual(0, response.Cohorts.Length);
}

[Test]
public async Task Handle_WithProviderIdWithMixedCohorts_ShouldReturn3CohortsForProviderIdAndExcludeApprovedCohorts()
{
var f = new GetCohortsHandlerTestFixtures();
f.AddUnapprovedCohortForEmployerWithMessagesAnd2Apprentices(f.AccountId)
.AddUnapprovedCohortForEmployerWithMessagesAnd2Apprentices(f.AccountId)
.AddUnapprovedCohortForEmployerWithMessagesAnd2Apprentices(f.NonMatchingId)
.AddCohortForEmployerApprovedByBoth(f.AccountId);

var response = await f.GetResponse(new GetCohortsQuery(f.AccountId, null));

Assert.IsNotNull(response);
Assert.AreEqual(2, response.Cohorts.Length);
Assert.AreEqual("EmployerLast", response.Cohorts[0].LatestMessageFromEmployer.Text);
Assert.AreEqual("ProviderLast", response.Cohorts[0].LatestMessageFromProvider.Text);
Assert.AreEqual(2, response.Cohorts[0].NumberOfDraftApprentices);
Assert.AreEqual("EmployerLast", response.Cohorts[1].LatestMessageFromEmployer.Text);
Assert.AreEqual("ProviderLast", response.Cohorts[1].LatestMessageFromProvider.Text);
Assert.AreEqual(2, response.Cohorts[1].NumberOfDraftApprentices);
}
}

Expand All @@ -130,17 +193,19 @@ public GetCohortsHandlerTestFixtures()
SeedAccounts = new List<Account>();
_autoFixture = new Fixture();
AccountId = _autoFixture.Create<long>();
ProviderId = _autoFixture.Create<long>();
TransferSenderId = AccountId + 1;

TransferSender = new Account(TransferSenderId, "", "", "TransferSender", DateTime.UtcNow);
Account = new Account(AccountId,"","","TEST", DateTime.UtcNow);
AccountLegalEntity = new AccountLegalEntity(Account, 1,1, "", "","TEST", OrganisationType.Charities, "", DateTime.UtcNow);
Provider = new Provider(1, "TEST PROVIDER", DateTime.UtcNow, DateTime.UtcNow);
Provider = new Provider(ProviderId, "TEST PROVIDER", DateTime.UtcNow, DateTime.UtcNow);
}

public long AccountId { get; }
public long ProviderId { get; }
public long TransferSenderId { get; set; }
public long NonMatchingAccountId => AccountId + 100;
public long NonMatchingId => AccountId + 100;
public List<Cohort> SeedCohorts { get; }
public List<Account> SeedAccounts { get; }
public Account Account { get; }
Expand All @@ -160,7 +225,7 @@ public Task<GetCohortsResult> GetResponse(GetCohortsQuery query)
});
}

public GetCohortsHandlerTestFixtures AddEmptyDraftCohortForEmployer(long accountId)
public GetCohortsHandlerTestFixtures AddEmptyDraftCohortWithEmployer(long? accountId)
{
var cohort = _autoFixture.Build<Cohort>().With(o=>o.EmployerAccountId, accountId)
.With(o => o.EditStatus, EditStatus.Neither)
Expand All @@ -171,8 +236,6 @@ public GetCohortsHandlerTestFixtures AddEmptyDraftCohortForEmployer(long account
.Without(o => o.TransferSender)
.Without(o => o.TransferRequests)
.Without(o => o.Messages)
.Without(o => o.AccountLegalEntity)
.Without(o => o.Provider)
.Create();

SeedCohorts.Add(cohort);
Expand All @@ -193,8 +256,6 @@ public GetCohortsHandlerTestFixtures AddCohortForEmployerApprovedByBoth(long acc
.Without(o => o.Apprenticeships)
.Without(o => o.TransferRequests)
.Without(o => o.Messages)
.Without(o => o.AccountLegalEntity)
.Without(o => o.Provider)
.Create();

SeedCohorts.Add(cohort);
Expand All @@ -214,8 +275,6 @@ public GetCohortsHandlerTestFixtures AddTransferCohortForEmployerAndApprovedByAl
.Without(o => o.Apprenticeships)
.Without(o => o.TransferRequests)
.Without(o => o.Messages)
.Without(o => o.AccountLegalEntity)
.Without(o => o.Provider)
.Create();

SeedCohorts.Add(cohort);
Expand Down Expand Up @@ -255,8 +314,6 @@ public GetCohortsHandlerTestFixtures AddUnapprovedCohortForEmployerWithMessagesA
.Without(o => o.TransferRequests)
.Without(o => o.TransferSender)
.Without(o => o.Messages)
.Without(o => o.AccountLegalEntity)
.Without(o => o.Provider)
.Create();

cohort.Apprenticeships.Add(new DraftApprenticeship());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ namespace SFA.DAS.CommitmentsV2.UnitTests.Application.Queries.GetCohorts
[TestFixture]
public class GetCohortsQueryValidationTests
{
[TestCase(1, true)]
[TestCase(null, false)]
public void Validate_WithAccountId_ShouldSetIsValidCorrectly(long? id, bool expectedIsValid)
[TestCase(1, null, true)]
[TestCase(null, null, false)]
[TestCase(null, 1, true)]
[TestCase(1, 1, true)]
public void Validate_WithAccountIdAndProviderId_ShouldSetIsValidCorrectly(long? accountId, long? providerId, bool expectedIsValid)
{
// arrange
var validator = new GetCohortsQueryValidator();
var validationResults = validator.Validate(new GetCohortsQuery(id));
var validationResults = validator.Validate(new GetCohortsQuery(accountId, providerId));

// act
var actualIsValid = validationResults.IsValid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Linq.Expressions;
using FluentValidation.TestHelper;
using FluentValidation.TestHelper;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Api.Types.Requests;
using SFA.DAS.CommitmentsV2.Validators;
Expand All @@ -10,25 +8,17 @@ namespace SFA.DAS.CommitmentsV2.UnitTests.Validators
[TestFixture]
public class GetCohortsRequestTests
{
[TestCase(1, true)]
[TestCase(null, false)]
public void Validate_AccountId_ShouldBeValidated(long? value, bool expectedValid)
{
AssertValidationResult(request => request.AccountId, value, expectedValid);
}

private void AssertValidationResult<T>(Expression<Func<GetCohortsRequest, T>> property, T value, bool expectedValid)
[TestCase(1, null, true)]
[TestCase(null, null, false)]
[TestCase(null, 1, true)]
[TestCase(1, 1, true)]
public void Validate_AccountIdAndProviderId_ShouldBeValidated(long? accountId, long? providerId, bool expectedValid)
{
var request = new GetCohortsRequest { AccountId = accountId, ProviderId = providerId };
var validator = new GetCohortsRequestValidator();

if (expectedValid)
{
validator.ShouldNotHaveValidationErrorFor(property, value);
}
else
{
validator.ShouldHaveValidationErrorFor(property, value);
}
var result = validator.TestValidate(request);
Assert.AreEqual(expectedValid, result.Result.IsValid);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
Expand Down Expand Up @@ -30,9 +29,9 @@ public async Task<GetCohortsResult> Handle(GetCohortsQuery command, Cancellation
var cohortFiltered = await (from c in _db.Value.Cohorts
join a in _db.Value.Accounts on c.TransferSenderId equals a.Id into account
from transferSender in account.DefaultIfEmpty()
where c.EmployerAccountId == command.AccountId &&
(c.EditStatus != EditStatus.Both ||
(c.TransferSenderId != null && c.TransferApprovalStatus != TransferApprovalStatus.Approved))
where c.EmployerAccountId == (command.AccountId ?? c.EmployerAccountId) && c.ProviderId == (command.ProviderId ?? c.ProviderId) &&
(c.EditStatus != EditStatus.Both ||
(c.TransferSenderId != null && c.TransferApprovalStatus != TransferApprovalStatus.Approved))
select new CohortSummary
{
AccountId = c.EmployerAccountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ namespace SFA.DAS.CommitmentsV2.Application.Queries.GetCohorts
public class GetCohortsQuery : IRequest<GetCohortsResult>
{
public long? AccountId { get; }
public long? ProviderId { get; }

public GetCohortsQuery(long? accountId)
public GetCohortsQuery(long? accountId, long? providerId)
{
AccountId = accountId;
ProviderId = providerId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class GetCohortsQueryValidator : AbstractValidator<GetCohortsQuery>
{
public GetCohortsQueryValidator()
{
RuleFor(model => model.AccountId).NotNull().WithMessage("The Account Id must be supplied");
RuleFor(model => model).Must(m=>m.ProviderId != null || m.AccountId != null).WithMessage("The Account Id or Provider Id must be supplied");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class GetCohortsRequestValidator : AbstractValidator<GetCohortsRequest>
{
public GetCohortsRequestValidator()
{
RuleFor(r => r.AccountId).NotNull();
RuleFor(request => request).Must(r => r.ProviderId != null || r.AccountId != null).WithMessage("The Account Id or Provider Id must be supplied");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,3 @@ Post-Deployment Script Template

EXEC sp_refreshview [dbo.CommitmentSummaryWithMessages]


-- CV-684: set all accounts to levy initially, so that a manual script can set nonlevy back again
-- This must be removed immediately after release
update [Accounts] set [LevyStatus]=1

0 comments on commit 471c0d8

Please sign in to comment.