Skip to content

Commit

Permalink
138587: Get Establishment information from last trust record via gias…
Browse files Browse the repository at this point in the history
… group table.
  • Loading branch information
blakebyron-nimble committed Sep 4, 2023
1 parent 4fcb1ef commit eaf5ea5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
66 changes: 66 additions & 0 deletions TramsDataApi.Test/Integration/TrustsV3IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,72 @@ public async Task ShouldReturnEstablishmentData_WhenTrustHasAnEstablishment()
establishment.Ukprn.Should().Be(establishmentData.Ukprn);
}

/// <summary>
/// Test covers data scenario where we have two records in the group table with nearly identical information. Assumption made that primary key of Open Trusts is higher than closed record.
/// </summary>
/// <returns></returns>
[Fact]
public async Task ShouldReturnEstablishmentDataAgainstOpenTrust_WhenTrustHasAnEstablishmentAndHasBeenOpenedAndClosedWithSameUKPRN()
{
//Arrange
string groupID = "TR02545";
string TrustName = "Trust A";
string TrustUKPRN = "123456789";

var closedTrustGroup = _fixture.Build<Group>()
.With(f => f.GroupUid, "1")
.With(f => f.GroupId, groupID)
.With(f=> f.GroupName, TrustName)
.With(f => f.Ukprn, TrustUKPRN)
.With(f => f.GroupStatus, "Closed")
.With(f => f.GroupStatusCode, "CLOSED")
.With(f => f.GroupType, "Single-academy trust")
.Without(p => p.CompaniesHouseNumber)
.Create();

var openTrustGroup = _fixture.Build<Group>()
.With(f => f.GroupUid, "2")
.With(f => f.GroupId, groupID)
.With(f => f.GroupName, TrustName)
.With(f => f.Ukprn, TrustUKPRN)
.With(f => f.GroupStatus, "Open")
.With(f => f.GroupStatusCode, "OPEN")
.With(f => f.GroupType, "Multi-academy trust")
.Create();

_legacyDbContext.Group.AddRange(closedTrustGroup, openTrustGroup);


var trustMasterData = BuildMasterTrustData(openTrustGroup);
_legacyDbContext.TrustMasterData.Add(trustMasterData);

var establishmentData = _fixture.Create<Establishment>();
establishmentData.TrustsCode = openTrustGroup.GroupUid;
_legacyDbContext.Establishment.Add(establishmentData);

_legacyDbContext.SaveChanges();

//Act
var httpRequestMessage = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri($"{_apiUrlPrefix}/trust/{trustMasterData.UKPRN}"),
};

var response = await _client.SendAsync(httpRequestMessage);
var jsonString = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<ApiSingleResponseV2<MasterTrustResponse>>(jsonString);

//Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
result.Data.TrustData.NumberInTrust.Should().Be(trustMasterData.NumberInTrust.ToString());
result.Data.GiasData.Ukprn.Should().Be(openTrustGroup.Ukprn);

result.Data.Establishments.Should().HaveCount(1);
var establishment = result.Data.Establishments[0];
establishment.Ukprn.Should().Be(establishmentData.Ukprn);
}

[Fact]
public async Task ShouldReturnAllTrusts_WhenSearchingTrusts_WithNoQueryParametersAndPagination()
{
Expand Down
6 changes: 6 additions & 0 deletions TramsDataApi/Gateways/ITrustGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ namespace TramsDataApi.Gateways
public interface ITrustGateway
{
Group GetGroupByUkPrn(string ukPrn);
/// <summary>
/// Returns the most recent group record should there be two records with the same UKPRN
/// </summary>
/// <param name="ukPrn"></param>
/// <returns></returns>
Group GetLatestGroupByUkPrn(string ukPrn);
Trust GetIfdTrustByGroupId(string groupId);
Trust GetIfdTrustByRID(string RID);
IList<Trust> GetIfdTrustsByTrustRef(string[] trustRefs);
Expand Down
9 changes: 8 additions & 1 deletion TramsDataApi/Gateways/TrustGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public Group GetGroupByUkPrn(string ukPrn)
return _dbContext.Group.FirstOrDefault(g => g.Ukprn == ukPrn);
}

public Trust GetIfdTrustByGroupId(string groupId)
public Group GetLatestGroupByUkPrn(string ukPrn)
{
return _dbContext.Group.OrderByDescending(f=> f.GroupUid).FirstOrDefault(g => g.Ukprn == ukPrn);
}

public Trust GetIfdTrustByGroupId(string groupId)
{
return _dbContext.Trust.FirstOrDefault(t => t.TrustRef == groupId);
}
Expand Down Expand Up @@ -84,5 +89,7 @@ public TrustMasterData GetMstrTrustByGroupId(string groupId)
{
return _dbContext.TrustMasterData.FirstOrDefault(t => t.GroupID == groupId);
}


}
}
9 changes: 7 additions & 2 deletions TramsDataApi/UseCases/GetMstrTrustByUkprn.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using TramsDataApi.Factories;
using TramsDataApi.Gateways;
using TramsDataApi.ResponseModels;
Expand All @@ -8,20 +9,24 @@ public class GetMstrTrustByUkprn : IGetMstrTrustByUkprn
{
private readonly ITrustGateway _trustGateway;
private readonly IGetEstablishmentsByTrustUid _getEstablishmentsByTrustUid;
private readonly ILogger<GetMstrTrustByUkprn> _logger;

public GetMstrTrustByUkprn(ITrustGateway trustGateway, IGetEstablishmentsByTrustUid getEstablishmentsByTrustUid)
public GetMstrTrustByUkprn(ITrustGateway trustGateway, IGetEstablishmentsByTrustUid getEstablishmentsByTrustUid, ILogger<GetMstrTrustByUkprn> logger)
{
_trustGateway = trustGateway;
_getEstablishmentsByTrustUid = getEstablishmentsByTrustUid;
_logger = logger;
}

public MasterTrustResponse Execute(string ukprn)
{
var group = _trustGateway.GetGroupByUkPrn(ukprn);

var group = _trustGateway.GetLatestGroupByUkPrn(ukprn);
if (group == null)
{
return null;
}
_logger.LogInformation("GetMstrTrustByUkprn: Found group with id {GroupUid}", group.GroupUid);

var trust = _trustGateway.GetMstrTrustByGroupId(group.GroupId);
var establishments = _getEstablishmentsByTrustUid.Execute(group.GroupUid);
Expand Down

0 comments on commit eaf5ea5

Please sign in to comment.