diff --git a/TramsDataApi.Test/Integration/TrustsV3IntegrationTests.cs b/TramsDataApi.Test/Integration/TrustsV3IntegrationTests.cs
index 7cb824178..3cdd570bd 100644
--- a/TramsDataApi.Test/Integration/TrustsV3IntegrationTests.cs
+++ b/TramsDataApi.Test/Integration/TrustsV3IntegrationTests.cs
@@ -184,6 +184,72 @@ public async Task ShouldReturnEstablishmentData_WhenTrustHasAnEstablishment()
establishment.Ukprn.Should().Be(establishmentData.Ukprn);
}
+ ///
+ /// 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.
+ ///
+ ///
+ [Fact]
+ public async Task ShouldReturnEstablishmentDataAgainstOpenTrust_WhenTrustHasAnEstablishmentAndHasBeenOpenedAndClosedWithSameUKPRN()
+ {
+ //Arrange
+ string groupID = "TR02545";
+ string TrustName = "Trust A";
+ string TrustUKPRN = "123456789";
+
+ var closedTrustGroup = _fixture.Build()
+ .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()
+ .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();
+ 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>(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()
{
diff --git a/TramsDataApi/Gateways/ITrustGateway.cs b/TramsDataApi/Gateways/ITrustGateway.cs
index a58c46406..cfa161ca5 100644
--- a/TramsDataApi/Gateways/ITrustGateway.cs
+++ b/TramsDataApi/Gateways/ITrustGateway.cs
@@ -7,6 +7,12 @@ namespace TramsDataApi.Gateways
public interface ITrustGateway
{
Group GetGroupByUkPrn(string ukPrn);
+ ///
+ /// Returns the most recent group record should there be two records with the same UKPRN
+ ///
+ ///
+ ///
+ Group GetLatestGroupByUkPrn(string ukPrn);
Trust GetIfdTrustByGroupId(string groupId);
Trust GetIfdTrustByRID(string RID);
IList GetIfdTrustsByTrustRef(string[] trustRefs);
diff --git a/TramsDataApi/Gateways/TrustGateway.cs b/TramsDataApi/Gateways/TrustGateway.cs
index 9baf80b89..72a567285 100644
--- a/TramsDataApi/Gateways/TrustGateway.cs
+++ b/TramsDataApi/Gateways/TrustGateway.cs
@@ -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);
}
@@ -84,5 +89,7 @@ public TrustMasterData GetMstrTrustByGroupId(string groupId)
{
return _dbContext.TrustMasterData.FirstOrDefault(t => t.GroupID == groupId);
}
+
+
}
}
diff --git a/TramsDataApi/UseCases/GetMstrTrustByUkprn.cs b/TramsDataApi/UseCases/GetMstrTrustByUkprn.cs
index c268861c1..69232b7cd 100644
--- a/TramsDataApi/UseCases/GetMstrTrustByUkprn.cs
+++ b/TramsDataApi/UseCases/GetMstrTrustByUkprn.cs
@@ -1,3 +1,4 @@
+using Microsoft.Extensions.Logging;
using TramsDataApi.Factories;
using TramsDataApi.Gateways;
using TramsDataApi.ResponseModels;
@@ -8,20 +9,24 @@ public class GetMstrTrustByUkprn : IGetMstrTrustByUkprn
{
private readonly ITrustGateway _trustGateway;
private readonly IGetEstablishmentsByTrustUid _getEstablishmentsByTrustUid;
+ private readonly ILogger _logger;
- public GetMstrTrustByUkprn(ITrustGateway trustGateway, IGetEstablishmentsByTrustUid getEstablishmentsByTrustUid)
+ public GetMstrTrustByUkprn(ITrustGateway trustGateway, IGetEstablishmentsByTrustUid getEstablishmentsByTrustUid, ILogger 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);