Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DP-550 - Trust or Trustees returned by DataSharing API #738

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static List<ConnectedPersonInformation> CreateMockConnectedPersonInformat
"British",
DateTimeOffset.Now.AddYears(-30),
ConnectedPersonType.Individual,
ConnectedPersonCategory.PersonWithSignificantControl,
ConnectedEntityIndividualAndTrustCategoryType.PersonWithSignificantControlForIndividual,
"UK",
new List<ConnectedAddress>
{
Expand Down
32 changes: 29 additions & 3 deletions Services/CO.CDP.DataSharing.WebApi.Tests/EntityFactory.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using CO.CDP.DataSharing.WebApi.Model;
using CO.CDP.OrganisationInformation;
using CO.CDP.OrganisationInformation.Persistence;
using static CO.CDP.OrganisationInformation.Persistence.ConnectedEntity;
using static CO.CDP.OrganisationInformation.Persistence.Organisation;
using ConnectedEntityType = CO.CDP.OrganisationInformation.Persistence.ConnectedEntity.ConnectedEntityType;
using ConnectedOrganisationCategory = CO.CDP.OrganisationInformation.Persistence.ConnectedEntity.ConnectedOrganisationCategory;
using ConnectedPersonCategory = CO.CDP.OrganisationInformation.Persistence.ConnectedEntity.ConnectedPersonCategory;
using PersistenceForms = CO.CDP.OrganisationInformation.Persistence.Forms;

namespace CO.CDP.DataSharing.WebApi.Tests;
Expand Down Expand Up @@ -83,7 +83,7 @@ internal static PersistenceForms.SharedConsent GetSharedConsent(int organisation
return sharedConsent;
}

internal static List<ConnectedEntity> GetMockAssociatedPersons()
internal static List<ConnectedEntity> GetMockIndividuals()
{
var mockPersons = new List<ConnectedEntity>
{
Expand All @@ -96,7 +96,7 @@ internal static List<ConnectedEntity> GetMockAssociatedPersons()
Id = 1,
FirstName = "John",
LastName = "Doe",
Category = ConnectedPersonCategory.PersonWithSignificantControl,
Category = ConnectedEntityIndividualAndTrustCategoryType.PersonWithSignificantControlForIndividual,
CreatedOn = DateTimeOffset.UtcNow,
UpdatedOn = DateTimeOffset.UtcNow
},
Expand Down Expand Up @@ -134,6 +134,32 @@ internal static List<ConnectedEntity> GetMockAdditionalEntities()
return mockEntities;
}

internal static List<ConnectedEntity> GetMockTrustsOrTrustees()
{
var mockPersons = new List<ConnectedEntity>
{
new ConnectedEntity
{
Guid = Guid.NewGuid(),
EntityType = ConnectedEntityType.TrustOrTrustee,
IndividualOrTrust = new ConnectedEntity.ConnectedIndividualTrust
{
Id = 2,
FirstName = "John",
LastName = "Smith",
Category = ConnectedEntityIndividualAndTrustCategoryType.PersonWithSignificantControlForTrust,
CreatedOn = DateTimeOffset.UtcNow,
UpdatedOn = DateTimeOffset.UtcNow
},
SupplierOrganisation = GivenOrganisation(),
CreatedOn = DateTimeOffset.UtcNow,
UpdatedOn = DateTimeOffset.UtcNow
}
};

return mockPersons;
}

internal static Organisation.LegalForm GetLegalForm()
{
var mockLegalForm = new Organisation.LegalForm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,21 @@ public async Task ItReturnsMappedSupplierInformationWhenSharedConsentIsFound()

_shareCodeRepository.Setup(repo => repo.GetByShareCode(shareCode)).ReturnsAsync(sharedConsent);

var mockAssociatedPersons = EntityFactory.GetMockAssociatedPersons();
var mockIndividuals = EntityFactory.GetMockIndividuals();
var mockAdditionalEntities = EntityFactory.GetMockAdditionalEntities();
var mockTrustOrTrustees = EntityFactory.GetMockTrustsOrTrustees();
var mockLegalForm = EntityFactory.GetLegalForm();
var mockOperationTypes = EntityFactory.GetOperationTypes();

_organisationRepository.Setup(repo => repo.GetConnectedIndividualTrusts(organisationId))
.ReturnsAsync(mockAssociatedPersons);
.ReturnsAsync(mockIndividuals);

_organisationRepository.Setup(repo => repo.GetConnectedOrganisations(organisationId))
.ReturnsAsync(mockAdditionalEntities);

_organisationRepository.Setup(repo => repo.GetConnectedTrustsOrTrustees(organisationId))
.ReturnsAsync(mockTrustOrTrustees);

_organisationRepository.Setup(repo => repo.GetLegalForm(organisationId))
.ReturnsAsync(mockLegalForm);

Expand Down Expand Up @@ -127,8 +131,9 @@ private void AssertAddress(Address? address)
private void AssertAssociatedPersons(IEnumerable<AssociatedPerson>? associatedPersons)
{
associatedPersons.Should().NotBeNull();
associatedPersons.Should().HaveCount(1);
associatedPersons.Should().HaveCount(2);
associatedPersons?.First().Name.Should().Be("John Doe");
associatedPersons?.Should().Contain(x => x.Name == "John Smith");
}

private void AssertAdditionalEntities(IEnumerable<OrganisationReference>? additionalEntities)
Expand Down
18 changes: 14 additions & 4 deletions Services/CO.CDP.DataSharing.WebApi/DataService/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,23 @@ public static List<ConnectedPersonInformation> MapToConnectedPersonInformation(I
{
if (entity != null)
{
var connectedPersonType = entity.IndividualOrTrust?.ConnectedType != null ?
entity.IndividualOrTrust.ConnectedType :
ConnectedPersonType.Individual;

var connectedEntityIndividualAndTrustCategoryType = entity.IndividualOrTrust?.Category != null ?
entity.IndividualOrTrust.Category :
(connectedPersonType == ConnectedPersonType.Individual ?
ConnectedEntityIndividualAndTrustCategoryType.PersonWithSignificantControlForIndividual :
ConnectedEntityIndividualAndTrustCategoryType.PersonWithSignificantControlForTrust);

var individualTrust = entity.IndividualOrTrust != null ? new ConnectedIndividualTrust(
entity.IndividualOrTrust.FirstName,
entity.IndividualOrTrust.LastName,
entity.IndividualOrTrust.DateOfBirth,
entity.IndividualOrTrust.Nationality,
entity.IndividualOrTrust?.Category != null ? entity.IndividualOrTrust.Category : ConnectedPersonCategory.PersonWithSignificantControl,
entity.IndividualOrTrust?.ConnectedType != null ? entity.IndividualOrTrust.ConnectedType : ConnectedPersonType.Individual,
connectedEntityIndividualAndTrustCategoryType,
connectedPersonType,
entity.IndividualOrTrust?.ControlCondition.Select(c => c.ToString()).ToList() ?? new List<string>(),
entity.IndividualOrTrust?.ResidentCountry
) : null;
Expand Down Expand Up @@ -206,8 +216,8 @@ public static List<ConnectedPersonInformation> MapToConnectedPersonInformation(I
entity.IndividualOrTrust?.LastName ?? string.Empty,
entity.IndividualOrTrust?.Nationality,
entity.IndividualOrTrust?.DateOfBirth,
entity.IndividualOrTrust?.ConnectedType != null ? entity.IndividualOrTrust.ConnectedType : ConnectedPersonType.Individual,
entity.IndividualOrTrust?.Category != null ? entity.IndividualOrTrust.Category : ConnectedPersonCategory.PersonWithSignificantControl,
connectedPersonType,
connectedEntityIndividualAndTrustCategoryType,
entity.IndividualOrTrust?.ResidentCountry,
addresses,
entity.IndividualOrTrust?.ControlCondition.Select(c => c.ToString()).ToList() ?? new List<string>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record ConnectedPersonInformation
string? Nationality,
DateTimeOffset? DateOfBirth,
ConnectedPersonType PersonType,
ConnectedPersonCategory Category,
ConnectedEntityIndividualAndTrustCategoryType Category,
string? ResidentCountry,
List<ConnectedAddress> Addresses,
List<string> ControlConditions,
Expand All @@ -26,7 +26,7 @@ public record ConnectedIndividualTrust
string LastName,
DateTimeOffset? DateOfBirth,
string? Nationality,
ConnectedPersonCategory Category,
ConnectedEntityIndividualAndTrustCategoryType Category,
ConnectedPersonType PersonType,
List<string> ControlConditions,
string? ResidentCountry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,23 @@ private async Task<Details> GetDetails(SharedConsent sharedConsent)

private async Task<ICollection<AssociatedPerson>> AssociatedPersons(SharedConsent sharedConsent)
{
var associatedPersons = await organisationRepository.GetConnectedIndividualTrusts(sharedConsent.OrganisationId);
return associatedPersons.Select(x => new AssociatedPerson
var individuals = await organisationRepository.GetConnectedIndividualTrusts(sharedConsent.OrganisationId);
var trustsOrTrustees = await organisationRepository.GetConnectedTrustsOrTrustees(sharedConsent.OrganisationId);

return individuals.Union(trustsOrTrustees).Select(x => new AssociatedPerson
{
Id = x.Guid,
Name = string.Format($"{x.IndividualOrTrust?.FirstName} {x.IndividualOrTrust?.LastName}"),
Relationship = x.IndividualOrTrust?.Category.ToString() ?? string.Empty,
Uri = null,
Roles = []
Roles = x.SupplierOrganisation.Roles
}).ToList();
}

private async Task<ICollection<OrganisationReference>> AdditionalEntities(SharedConsent sharedConsent)
{
var additionalEntities = await organisationRepository.GetConnectedOrganisations(sharedConsent.OrganisationId);

return additionalEntities.Select(x => new OrganisationReference
{
Id = x.Guid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public async Task GetSummary_ShouldReturnConnectedEntityLookups_WhenEntitiesExis
{
FirstName = "First Name",
LastName = "Last Name",
Category = ConnectedEntity.ConnectedPersonCategory.DirectorOrIndividualWithTheSameResponsibilities,
Category = ConnectedEntity.ConnectedEntityIndividualAndTrustCategoryType.DirectorOrIndividualWithTheSameResponsibilitiesForIndividual,
ConnectedType = ConnectedEntity.ConnectedPersonType.Individual
},
SupplierOrganisation = organisation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,50 @@ public async Task GetConnectedOrganisations_WhenNoConnectedEntitiesExist_Returns
result.Should().BeEmpty();
}

[Fact]
public async Task GetConnectedTrustsOrTrustees_WhenConnectedEntitiesExist_ReturnsConnectedEntities()
{
using var repository = OrganisationRepository();

var supplierOrganisation = GivenOrganisation();
var connectedEntity = GivenConnectedTrustsOrTrustees(supplierOrganisation);

using var context = postgreSql.OrganisationInformationContext();
await context.Organisations.AddAsync(supplierOrganisation);
await context.ConnectedEntities.AddAsync(connectedEntity);
await context.SaveChangesAsync();

var result = await repository.GetConnectedTrustsOrTrustees(supplierOrganisation.Id);

result.Should().NotBeEmpty();
result.Should().HaveCount(1);
result.Should().Contain(x => x.EntityType == ConnectedEntity.ConnectedEntityType.TrustOrTrustee);

var organisation = result.First().Organisation;
organisation.Should().BeEquivalentTo(connectedEntity.Organisation, options =>
options
.Using<DateTimeOffset>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, TimeSpan.FromMilliseconds(100)))
.WhenTypeIs<DateTimeOffset>()
);
}

[Fact]
public async Task GetConnectedTrustsOrTrustees_WhenNoConnectedEntitiesExist_ReturnsEmptyList()
{
using var repository = OrganisationRepository();

var organisationId = 1;
var organisation = GivenOrganisation();

using var context = postgreSql.OrganisationInformationContext();
await context.Organisations.AddAsync(organisation);
await context.SaveChangesAsync();

var result = await repository.GetConnectedTrustsOrTrustees(organisationId);

result.Should().BeEmpty();
}

[Fact]
public async Task GetLegalForm_WhenNoLegalFormExists_ReturnNull()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ public static ConnectedEntity GivenConnectedOrganisation(
Guid? organisationId = null
)
{
var connectedOrganisation = new ConnectedEntity.ConnectedOrganisation
var connectedOrganisation = new ConnectedOrganisation
{
Id = 1,
Category = (ConnectedEntity.ConnectedOrganisationCategory)category,
Category = category,
Name = name,
OrganisationId = organisationId,
CreatedOn = DateTimeOffset.UtcNow,
Expand All @@ -265,15 +265,16 @@ public static ConnectedEntity GivenConnectedIndividualTrust(
Organisation supplierOrganisation,
string firstName = "John",
string lastName = "Doe",
ConnectedPersonCategory category = ConnectedPersonCategory.PersonWithSignificantControl
ConnectedEntityIndividualAndTrustCategoryType category = ConnectedEntityIndividualAndTrustCategoryType.PersonWithSignificantControlForIndividual
)
{
var individualTrust = new ConnectedEntity.ConnectedIndividualTrust
var individualTrust = new ConnectedIndividualTrust
{
Id = 1,
FirstName = firstName,
LastName = lastName,
Category = (ConnectedEntity.ConnectedPersonCategory)category,
Category = category,
ConnectedType = ConnectedPersonType.Individual,
CreatedOn = DateTimeOffset.UtcNow,
UpdatedOn = DateTimeOffset.UtcNow
};
Expand All @@ -289,5 +290,32 @@ public static ConnectedEntity GivenConnectedIndividualTrust(
};
}

public static ConnectedEntity GivenConnectedTrustsOrTrustees(
Organisation supplierOrganisation,
string firstName = "John",
string lastName = "Doe",
ConnectedEntityIndividualAndTrustCategoryType category = ConnectedEntityIndividualAndTrustCategoryType.PersonWithSignificantControlForTrust
)
{
var trustOrTrustee = new ConnectedIndividualTrust
{
Id = 1,
FirstName = firstName,
LastName = lastName,
Category = category,
ConnectedType = ConnectedPersonType.TrustOrTrustee,
CreatedOn = DateTimeOffset.UtcNow,
UpdatedOn = DateTimeOffset.UtcNow
};

return new ConnectedEntity
{
Guid = Guid.NewGuid(),
EntityType = (ConnectedEntity.ConnectedEntityType)ConnectedEntityType.TrustOrTrustee,
IndividualOrTrust = trustOrTrustee,
SupplierOrganisation = supplierOrganisation,
CreatedOn = DateTimeOffset.UtcNow,
UpdatedOn = DateTimeOffset.UtcNow
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public record ConnectedEntityAddress
public record ConnectedIndividualTrust : IEntityDate
{
public int Id { get; set; }
public required ConnectedPersonCategory Category { get; set; }
public required ConnectedEntityIndividualAndTrustCategoryType Category { get; set; }
public required string FirstName { get; set; }
public required string LastName { get; set; }
public DateTimeOffset? DateOfBirth { get; set; }
Expand Down Expand Up @@ -91,11 +91,14 @@ public enum ConnectedPersonType
TrustOrTrustee
}

public enum ConnectedPersonCategory
public enum ConnectedEntityIndividualAndTrustCategoryType
{
PersonWithSignificantControl = 1,
DirectorOrIndividualWithTheSameResponsibilities,
AnyOtherIndividualWithSignificantInfluenceOrControl
PersonWithSignificantControlForIndividual = 1,
DirectorOrIndividualWithTheSameResponsibilitiesForIndividual,
AnyOtherIndividualWithSignificantInfluenceOrControlForIndividual,
PersonWithSignificantControlForTrust,
DirectorOrIndividualWithTheSameResponsibilitiesForTrust,
AnyOtherIndividualWithSignificantInfluenceOrControlForTrust
}
dpatel017 marked this conversation as resolved.
Show resolved Hide resolved

public enum ConnectedOrganisationCategory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ public async Task<IList<ConnectedEntity>> GetConnectedOrganisations(int organisa
return await result.ToListAsync();
}

public async Task<IList<ConnectedEntity>> GetConnectedTrustsOrTrustees(int organisationId)
{
var result = context.ConnectedEntities
.Include(x => x.IndividualOrTrust)
.Where(x => x.IndividualOrTrust != null && x.EntityType == ConnectedEntity.ConnectedEntityType.TrustOrTrustee)
.Where(x => x.SupplierOrganisation != null && x.SupplierOrganisation.Id == organisationId);

return await result.ToListAsync();
}

public async Task<Organisation.LegalForm?> GetLegalForm(int organisationId)
{
var organisation = await context.Organisations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class DuplicateOrganisationException(string message, Exception? cause = n

public Task<IList<ConnectedEntity>> GetConnectedOrganisations(int organisationId);

public Task<IList<ConnectedEntity>> GetConnectedTrustsOrTrustees(int organisationId);

public Task<Organisation.LegalForm?> GetLegalForm(int organisationId);

public Task<IList<OperationType>> GetOperationTypes(int organisationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.HasPostgresEnum<ControlCondition>();
modelBuilder.HasPostgresEnum<ConnectedEntityType>();
modelBuilder.HasPostgresEnum<ConnectedPersonType>();
modelBuilder.HasPostgresEnum<ConnectedPersonCategory>();
modelBuilder.HasPostgresEnum<ConnectedEntityIndividualAndTrustCategoryType>();
modelBuilder.HasPostgresEnum<ConnectedOrganisationCategory>();

modelBuilder.Entity<ConnectedEntity>(entity =>
Expand Down