-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/dp-875-Informal-consortium-add-addit…
…ional-organisations
- Loading branch information
Showing
9 changed files
with
277 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
Services/CO.CDP.Organisation.WebApi.Tests/UseCase/AddOrganisationPartyUseCaseTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
using CO.CDP.Organisation.WebApi.Model; | ||
using CO.CDP.Organisation.WebApi.UseCase; | ||
using CO.CDP.OrganisationInformation.Persistence; | ||
using FluentAssertions; | ||
using Moq; | ||
using Persistence = CO.CDP.OrganisationInformation.Persistence; | ||
|
||
namespace CO.CDP.Organisation.WebApi.Tests.UseCase; | ||
|
||
public class AddOrganisationPartyUseCaseTests | ||
{ | ||
private readonly Mock<IOrganisationRepository> _orgRepoMock = new(); | ||
private readonly Mock<IShareCodeRepository> _shareCodeRepoMock = new(); | ||
private readonly Mock<IOrganisationPartiesRepository> _orgPartiesRepoMock = new(); | ||
private AddOrganisationPartyUseCase UseCase => new(_orgRepoMock.Object, _shareCodeRepoMock.Object, _orgPartiesRepoMock.Object); | ||
|
||
[Fact] | ||
public async Task Execute_ShouldThrowException_WhenParentOrganisationNotFound() | ||
{ | ||
var organisationId = Guid.NewGuid(); | ||
var addParty = new AddOrganisationParty | ||
{ | ||
OrganisationPartyId = Guid.NewGuid(), | ||
ShareCode = null, | ||
OrganisationRelationship = Model.OrganisationRelationship.Consortium, | ||
}; | ||
|
||
_orgRepoMock.Setup(repo => repo.Find(organisationId)).ReturnsAsync((Persistence.Organisation?)null); | ||
|
||
Func<Task> action = async () => await UseCase.Execute((organisationId, addParty)); | ||
|
||
await action.Should().ThrowAsync<UnknownOrganisationException>() | ||
.WithMessage($"Unknown organisation {organisationId}."); | ||
} | ||
|
||
[Fact] | ||
public async Task Execute_ShouldThrowException_WhenChildOrganisationNotFound() | ||
{ | ||
var organisationId = Guid.NewGuid(); | ||
var parentOrg = GivenOrganisation(organisationId); | ||
var childOrganisationId = Guid.NewGuid(); | ||
var addParty = new AddOrganisationParty | ||
{ | ||
OrganisationPartyId = childOrganisationId, | ||
ShareCode = null, | ||
OrganisationRelationship = Model.OrganisationRelationship.Consortium, | ||
}; | ||
|
||
_orgRepoMock.Setup(repo => repo.Find(organisationId)).ReturnsAsync(parentOrg); | ||
_orgRepoMock.Setup(repo => repo.Find(childOrganisationId)).ReturnsAsync((Persistence.Organisation?)null); | ||
|
||
Func<Task> action = async () => await UseCase.Execute((organisationId, addParty)); | ||
|
||
await action.Should().ThrowAsync<UnknownOrganisationException>() | ||
.WithMessage($"Unknown organisation {childOrganisationId}."); | ||
} | ||
|
||
[Fact] | ||
public async Task Execute_ShouldThrowException_WhenShareCodeIsInvalid() | ||
{ | ||
var organisationId = Guid.NewGuid(); | ||
var parentOrg = GivenOrganisation(organisationId); | ||
var childOrganisationId = Guid.NewGuid(); | ||
var childOrg = GivenOrganisation(childOrganisationId); | ||
var shareCode = "InvalidCode"; | ||
var addParty = new AddOrganisationParty | ||
{ | ||
OrganisationPartyId = childOrganisationId, | ||
ShareCode = shareCode, | ||
OrganisationRelationship = Model.OrganisationRelationship.Consortium, | ||
}; | ||
|
||
_orgRepoMock.Setup(repo => repo.Find(organisationId)).ReturnsAsync(parentOrg); | ||
_orgRepoMock.Setup(repo => repo.Find(childOrganisationId)).ReturnsAsync(childOrg); | ||
_shareCodeRepoMock.Setup(repo => repo.GetShareCodesAsync(childOrganisationId)).ReturnsAsync([]); | ||
|
||
Func<Task> action = async () => await UseCase.Execute((organisationId, addParty)); | ||
|
||
await action.Should().ThrowAsync<OrganisationShareCodeInvalid>() | ||
.WithMessage($"Invalid organisation share code: {shareCode}"); | ||
} | ||
|
||
[Fact] | ||
public async Task Execute_ShouldSaveOrganisationParty_WhenValidInputProvided() | ||
{ | ||
var organisationId = Guid.NewGuid(); | ||
var parentOrg = GivenOrganisation(organisationId); | ||
var childOrganisationId = Guid.NewGuid(); | ||
var childOrg = GivenOrganisation(childOrganisationId); | ||
var shareCode = "ValidCode"; | ||
var sharedConsent = new Persistence.Forms.SharedConsent | ||
{ | ||
Id = 1, | ||
Guid = Guid.NewGuid(), | ||
OrganisationId = childOrg.Id, | ||
Organisation = childOrg, | ||
FormId = 1, | ||
Form = null!, | ||
AnswerSets = [], | ||
SubmissionState = Persistence.Forms.SubmissionState.Submitted, | ||
SubmittedAt = null, | ||
FormVersionId = string.Empty, | ||
ShareCode = shareCode ?? "valid-sharecode", | ||
CreatedOn = DateTimeOffset.UtcNow, | ||
UpdatedOn = DateTimeOffset.UtcNow, | ||
}; | ||
|
||
_orgRepoMock.Setup(repo => repo.Find(organisationId)).ReturnsAsync(parentOrg); | ||
_orgRepoMock.Setup(repo => repo.Find(childOrganisationId)).ReturnsAsync(childOrg); | ||
_shareCodeRepoMock.Setup(repo => repo.GetShareCodesAsync(childOrganisationId)).ReturnsAsync([sharedConsent]); | ||
|
||
var result = await UseCase.Execute((organisationId, new AddOrganisationParty | ||
{ | ||
OrganisationPartyId = childOrganisationId, | ||
ShareCode = shareCode, | ||
OrganisationRelationship = Model.OrganisationRelationship.Consortium, | ||
})); | ||
|
||
result.Should().BeTrue(); | ||
|
||
_orgPartiesRepoMock.Verify(repo => repo.Save(It.Is<Persistence.OrganisationParty>(party => | ||
party.ParentOrganisationId == parentOrg.Id && | ||
party.ChildOrganisationId == childOrg.Id && | ||
party.SharedConsentId == sharedConsent.Id && | ||
party.OrganisationRelationship == Persistence.OrganisationRelationship.Consortium)), Times.Once); | ||
} | ||
|
||
private static Persistence.Organisation GivenOrganisation(Guid guid) => | ||
new() | ||
{ | ||
Guid = guid, | ||
Name = "Test", | ||
Type = OrganisationInformation.OrganisationType.Organisation, | ||
Tenant = It.IsAny<Tenant>(), | ||
ContactPoints = [new Persistence.Organisation.ContactPoint { Email = "test@test.com" }], | ||
SupplierInfo = new Persistence.Organisation.SupplierInformation() | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
Services/CO.CDP.Organisation.WebApi/UseCase/AddOrganisationPartyUseCase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using CO.CDP.Organisation.WebApi.Model; | ||
using CO.CDP.OrganisationInformation.Persistence; | ||
|
||
namespace CO.CDP.Organisation.WebApi.UseCase; | ||
|
||
public class AddOrganisationPartyUseCase( | ||
IOrganisationRepository orgRepo, | ||
IShareCodeRepository shareCodeRepo, | ||
IOrganisationPartiesRepository orgPartiesRepo | ||
) : IUseCase<(Guid, AddOrganisationParty), bool> | ||
{ | ||
public async Task<bool> Execute((Guid, AddOrganisationParty) command) | ||
{ | ||
(Guid organisationId, AddOrganisationParty addParty) = command; | ||
|
||
var parentOrganisation = await orgRepo.Find(organisationId) | ||
?? throw new UnknownOrganisationException($"Unknown organisation {organisationId}."); | ||
|
||
var childOrganisation = await orgRepo.Find(addParty.OrganisationPartyId) | ||
?? throw new UnknownOrganisationException($"Unknown organisation {addParty.OrganisationPartyId}."); | ||
|
||
int? sharedConsentId = null; | ||
|
||
if (!string.IsNullOrWhiteSpace(addParty.ShareCode)) | ||
{ | ||
var sharedConsents = await shareCodeRepo.GetShareCodesAsync(addParty.OrganisationPartyId); | ||
|
||
var sharedConsent = sharedConsents.FirstOrDefault(s => | ||
string.Equals(s.ShareCode, addParty.ShareCode, StringComparison.InvariantCultureIgnoreCase)); | ||
|
||
if (sharedConsent == null | ||
|| sharedConsent.SubmissionState != OrganisationInformation.Persistence.Forms.SubmissionState.Submitted | ||
|| sharedConsent.OrganisationId != childOrganisation.Id) | ||
{ | ||
throw new OrganisationShareCodeInvalid(addParty.ShareCode); | ||
} | ||
|
||
sharedConsentId = sharedConsent.Id; | ||
} | ||
|
||
await orgPartiesRepo.Save( | ||
new OrganisationInformation.Persistence.OrganisationParty | ||
{ | ||
ParentOrganisationId = parentOrganisation.Id, | ||
ChildOrganisationId = childOrganisation.Id, | ||
OrganisationRelationship = (OrganisationInformation.Persistence.OrganisationRelationship)addParty.OrganisationRelationship, | ||
SharedConsentId = sharedConsentId, | ||
}); | ||
|
||
return true; | ||
} | ||
} |