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

FAI-552 - Update UpdatedPermissionEventHandler #1170

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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 @@ -29,32 +29,30 @@ public class UpdatedPermissionsExternalSystemEventHandlerTests
private const string UserLastName = "Smith";
private const long EmployerAccountId = 12345;
private const string EmployerAccountIdEncoded = "ABC123";
private const string EmployerAccountLegalEntityIdPublicEncoded = "ZXY987";
private const long AccountLegalEntityId = 1231;
private const long AccountProviderId = 321;
private const long AccountProviderLegalEntityId = 1223;
private const long Ukprn = 11111111;
private const long RecruitLegalEntityId = 5678;
private const int NoOfDummyLegalEntitiesToCreate = 10;
private readonly Fixture _autoFixture = new Fixture();
private readonly IEnumerable<AccountLegalEntity> _dummyLegalEntities;
private readonly RecruitWebJobsSystemConfiguration _jobsConfig;
private readonly Mock<IRecruitQueueService> _mockRecruitQueueService;
private readonly Mock<IEmployerAccountProvider> _mockEmployerAccountProvider;
private readonly Mock<IEncodingService> _mockEncoder;
private readonly Mock<IMessaging> _mockMessaging;
private readonly UpdatedPermissionsExternalSystemEventsHandler _sut;

public UpdatedPermissionsExternalSystemEventHandlerTests()
{
_dummyLegalEntities = _autoFixture.CreateMany<AccountLegalEntity>(NoOfDummyLegalEntitiesToCreate);
_jobsConfig = new RecruitWebJobsSystemConfiguration { DisabledJobs = new List<string>() };
_mockRecruitQueueService = new Mock<IRecruitQueueService>();
_mockEmployerAccountProvider = new Mock<IEmployerAccountProvider>();
_mockEncoder = new Mock<IEncodingService>();
_mockMessaging = new Mock<IMessaging>();

_mockEncoder.Setup(x => x.Encode(EmployerAccountId, EncodingType.AccountId)).Returns(EmployerAccountIdEncoded);
_mockEncoder.Setup(x => x.Encode(AccountLegalEntityId, EncodingType.PublicAccountLegalEntityId)).Returns(EmployerAccountLegalEntityIdPublicEncoded);

_sut = new UpdatedPermissionsExternalSystemEventsHandler(Mock.Of<ILogger<UpdatedPermissionsExternalSystemEventsHandler>>(), _jobsConfig,
_mockRecruitQueueService.Object, _mockEmployerAccountProvider.Object,
_mockRecruitQueueService.Object,
_mockEncoder.Object, _mockMessaging.Object);
}

Expand All @@ -67,7 +65,6 @@ public async Task GivenJobsConfigWithEventHandlerJobDisabled_ThenVerifyNoDepende

_mockRecruitQueueService.VerifyNoOtherCalls();
_mockMessaging.VerifyNoOtherCalls();
_mockEmployerAccountProvider.VerifyNoOtherCalls();
_mockEncoder.VerifyNoOtherCalls();
}

Expand All @@ -80,8 +77,6 @@ public async Task GivenEventWithDefaultGuidUserRef_ThenVerifyNoDependenciesAreCa
await _sut.Handle(new UpdatedPermissionsEvent(EmployerAccountId, AccountLegalEntityId, AccountProviderId, AccountProviderLegalEntityId, Ukprn, Guid.Empty, string.Empty, string.Empty, string.Empty, grantedOperations, previousOperations, DateTime.UtcNow), null);

_mockRecruitQueueService.VerifyNoOtherCalls();
_mockEmployerAccountProvider.VerifyNoOtherCalls();
_mockEncoder.VerifyNoOtherCalls();
_mockMessaging.Verify(x => x.SendCommandAsync(It.Is<SetupProviderCommand>(c => c.Ukprn == Ukprn)), Times.Once);
}

Expand All @@ -94,8 +89,6 @@ public async Task GivenEventWithGrantedRecruitmentPermissions_ThenVerifyNoDepend
await _sut.Handle(new UpdatedPermissionsEvent(EmployerAccountId, AccountLegalEntityId, AccountProviderId, AccountProviderLegalEntityId, Ukprn, Guid.NewGuid(), UserEmailAddress, UserFirstName, UserLastName, grantedOperations, previousOperations, DateTime.UtcNow), null);

_mockRecruitQueueService.VerifyNoOtherCalls();
_mockEmployerAccountProvider.VerifyNoOtherCalls();
_mockEncoder.VerifyNoOtherCalls();
_mockMessaging.Verify(x => x.SendCommandAsync(It.Is<SetupProviderCommand>(c => c.Ukprn == Ukprn)), Times.Once);
}

Expand All @@ -108,91 +101,15 @@ public async Task GivenEventWithGrantedRecruitmentPermissionsAndMore_ThenVerifyN
await _sut.Handle(new UpdatedPermissionsEvent(EmployerAccountId, AccountLegalEntityId, AccountProviderId, AccountProviderLegalEntityId, Ukprn, Guid.NewGuid(), UserEmailAddress, UserFirstName, UserLastName, grantedOperations, previousOperations, DateTime.UtcNow), null);

_mockRecruitQueueService.VerifyNoOtherCalls();
_mockEmployerAccountProvider.VerifyNoOtherCalls();
_mockEncoder.VerifyNoOtherCalls();
_mockMessaging.Verify(x => x.SendCommandAsync(It.Is<SetupProviderCommand>(c => c.Ukprn == Ukprn)), Times.Once);
}

[Fact]
public async Task GivenNotExistingLegalEntityId_ThenExceptionIsThrown()
{
var grantedOperations = new HashSet<Operation> { Operation.CreateCohort };
var previousOperations = new HashSet<Operation>();

_mockEncoder.Setup(x => x.Encode(EmployerAccountId, EncodingType.AccountId)).Returns(EmployerAccountIdEncoded);
_mockEmployerAccountProvider.Setup(x => x.GetLegalEntitiesConnectedToAccountAsync(EmployerAccountIdEncoded))
.ReturnsAsync(_dummyLegalEntities);

var exception = await Assert.ThrowsAsync<Exception>(() => _sut.Handle(new UpdatedPermissionsEvent(EmployerAccountId, AccountLegalEntityId, AccountProviderId, AccountProviderLegalEntityId, Ukprn, Guid.NewGuid(), UserEmailAddress, UserFirstName, UserLastName, grantedOperations, previousOperations, DateTime.UtcNow), null));

exception.Message.Should().Be($"Could not find matching Account Legal Entity Id {AccountLegalEntityId} for Employer Account {EmployerAccountId}");
_mockEncoder.Verify(x => x.Encode(EmployerAccountId, EncodingType.AccountId), Times.Once);
_mockEmployerAccountProvider.Verify(x => x.GetLegalEntitiesConnectedToAccountAsync(EmployerAccountIdEncoded), Times.Once);
}

[Fact]
public async Task GivenMatchingExistingLegalEntityId_ThenVerifyTransferProcessIsQueued()
{
var matchingLegalEntityViewModel = _autoFixture.Build<AccountLegalEntity>()
.With(l => l.DasAccountId, EmployerAccountIdEncoded)
.With(l => l.AccountLegalEntityId, AccountLegalEntityId)
.With(l => l.LegalEntityId, RecruitLegalEntityId)
.Create();

var grantedOperations = new HashSet<Operation> { Operation.CreateCohort };
var previousOperations = new HashSet<Operation>();

_mockEncoder.Setup(x => x.Encode(EmployerAccountId, EncodingType.AccountId)).Returns(EmployerAccountIdEncoded);
_mockEmployerAccountProvider.Setup(x => x.GetLegalEntitiesConnectedToAccountAsync(EmployerAccountIdEncoded))
.ReturnsAsync(_dummyLegalEntities.Append(matchingLegalEntityViewModel));

await _sut.Handle(new UpdatedPermissionsEvent(EmployerAccountId, AccountLegalEntityId, AccountProviderId, AccountProviderLegalEntityId, Ukprn, Guid.NewGuid(), UserEmailAddress, UserFirstName, UserLastName, grantedOperations, previousOperations, DateTime.UtcNow), null);

_mockEncoder.Verify(x => x.Encode(EmployerAccountId, EncodingType.AccountId), Times.Once);
_mockEmployerAccountProvider.Verify(x => x.GetLegalEntitiesConnectedToAccountAsync(EmployerAccountIdEncoded), Times.Once);
_mockRecruitQueueService.Verify(x => x.AddMessageAsync(It.IsAny<TransferVacanciesFromProviderQueueMessage>()), Times.Once);
_mockMessaging.Verify(x => x.SendCommandAsync(It.Is<SetupProviderCommand>(c => c.Ukprn == Ukprn)), Times.Once);
}

[Fact]
public async Task GivenMatchingExistingLegalEntityId_ThenVerifyTransferReviewProcessIsQueued()
{
var matchingLegalEntityViewModel = _autoFixture.Build<AccountLegalEntity>()
.With(l => l.DasAccountId, EmployerAccountIdEncoded)
.With(l => l.AccountLegalEntityId, AccountLegalEntityId)
.With(l => l.LegalEntityId, RecruitLegalEntityId)
.Create();

var grantedOperations = new HashSet<Operation> { Operation.Recruitment, Operation.CreateCohort };
var previousOperations = new HashSet<Operation>();

_mockEncoder.Setup(x => x.Encode(EmployerAccountId, EncodingType.AccountId)).Returns(EmployerAccountIdEncoded);
_mockEmployerAccountProvider.Setup(x => x.GetLegalEntitiesConnectedToAccountAsync(EmployerAccountIdEncoded))
.ReturnsAsync(_dummyLegalEntities.Append(matchingLegalEntityViewModel));

await _sut.Handle(new UpdatedPermissionsEvent(EmployerAccountId, AccountLegalEntityId, AccountProviderId, AccountProviderLegalEntityId, Ukprn, Guid.NewGuid(), UserEmailAddress, UserFirstName, UserLastName, grantedOperations, previousOperations, DateTime.UtcNow), null);

_mockEncoder.Verify(x => x.Encode(EmployerAccountId, EncodingType.AccountId), Times.Once);
_mockEmployerAccountProvider.Verify(x => x.GetLegalEntitiesConnectedToAccountAsync(EmployerAccountIdEncoded), Times.Once);
_mockRecruitQueueService.Verify(x => x.AddMessageAsync(It.IsAny<TransferVacanciesFromEmployerReviewToQAReviewQueueMessage>()), Times.Once);
_mockMessaging.Verify(x => x.SendCommandAsync(It.Is<SetupProviderCommand>(c => c.Ukprn == Ukprn)), Times.Once);
}

[Fact]
public async Task GivenMatchingExistingLegalEntityId_ThenTransferQueuedMessageIsMappedCorrectly()
{
var matchingLegalEntityViewModel = _autoFixture.Build<AccountLegalEntity>()
.With(l => l.DasAccountId, EmployerAccountIdEncoded)
.With(l => l.AccountLegalEntityId, AccountLegalEntityId)
.With(l => l.LegalEntityId, RecruitLegalEntityId)
.Create();

var grantedOperations = new HashSet<Operation> { Operation.CreateCohort };
var previousOperations = new HashSet<Operation>();
_mockEncoder.Setup(x => x.Encode(EmployerAccountId, EncodingType.AccountId)).Returns(EmployerAccountIdEncoded);
_mockEmployerAccountProvider.Setup(x => x.GetLegalEntitiesConnectedToAccountAsync(EmployerAccountIdEncoded))
.ReturnsAsync(_dummyLegalEntities.Append(matchingLegalEntityViewModel));


var userRef = Guid.NewGuid();

TransferVacanciesFromProviderQueueMessage queuedMessage = null;
Expand All @@ -206,28 +123,20 @@ public async Task GivenMatchingExistingLegalEntityId_ThenTransferQueuedMessageIs
Assert.NotNull(queuedMessage);
Assert.Equal(Ukprn, queuedMessage.Ukprn);
Assert.Equal(EmployerAccountIdEncoded, queuedMessage.EmployerAccountId);
Assert.Equal(matchingLegalEntityViewModel.AccountLegalEntityPublicHashedId, queuedMessage.AccountLegalEntityPublicHashedId);
Assert.Equal(EmployerAccountLegalEntityIdPublicEncoded, queuedMessage.AccountLegalEntityPublicHashedId);
Assert.Equal(userRef, queuedMessage.UserRef);
Assert.Equal(UserEmailAddress, queuedMessage.UserEmailAddress);
Assert.Equal($"{UserFirstName} {UserLastName}", queuedMessage.UserName);
Assert.Equal(TransferReason.EmployerRevokedPermission, queuedMessage.TransferReason);
_mockMessaging.Verify(x => x.SendCommandAsync(It.Is<SetupProviderCommand>(c => c.Ukprn == Ukprn)), Times.Once);
}

[Fact]
public async Task GivenMatchingExistingLegalEntityId_ThenReviewQueuedMessageIsMappedCorrectly()
{
var matchingLegalEntityViewModel = _autoFixture.Build<AccountLegalEntity>()
.With(l => l.DasAccountId, EmployerAccountIdEncoded)
.With(l => l.AccountLegalEntityId, AccountLegalEntityId)
.With(l => l.LegalEntityId, RecruitLegalEntityId)
.Create();

var grantedOperations = new HashSet<Operation> { Operation.Recruitment, Operation.CreateCohort };
var previousOperations = new HashSet<Operation>();
_mockEncoder.Setup(x => x.Encode(EmployerAccountId, EncodingType.AccountId)).Returns(EmployerAccountIdEncoded);
_mockEmployerAccountProvider.Setup(x => x.GetLegalEntitiesConnectedToAccountAsync(EmployerAccountIdEncoded))
.ReturnsAsync(_dummyLegalEntities.Append(matchingLegalEntityViewModel));


var userRef = Guid.NewGuid();

TransferVacanciesFromEmployerReviewToQAReviewQueueMessage queuedMessage = null;
Expand All @@ -240,10 +149,11 @@ public async Task GivenMatchingExistingLegalEntityId_ThenReviewQueuedMessageIsMa

Assert.NotNull(queuedMessage);
Assert.Equal(Ukprn, queuedMessage.Ukprn);
Assert.Equal(matchingLegalEntityViewModel.AccountLegalEntityPublicHashedId, queuedMessage.AccountLegalEntityPublicHashedId);
Assert.Equal(EmployerAccountLegalEntityIdPublicEncoded, queuedMessage.AccountLegalEntityPublicHashedId);
Assert.Equal(userRef, queuedMessage.UserRef);
Assert.Equal(UserEmailAddress, queuedMessage.UserEmailAddress);
Assert.Equal($"{UserFirstName} {UserLastName}", queuedMessage.UserName);
_mockMessaging.Verify(x => x.SendCommandAsync(It.Is<SetupProviderCommand>(c => c.Ukprn == Ukprn)), Times.Once);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,19 @@ public class UpdatedPermissionsExternalSystemEventsHandler : IHandleMessages<Upd
{
private readonly ILogger<UpdatedPermissionsExternalSystemEventsHandler> _logger;
private readonly IRecruitQueueService _recruitQueueService;
private readonly IEmployerAccountProvider _employerAccountProvider;
private readonly IEncodingService _encoder;
private readonly RecruitWebJobsSystemConfiguration _jobsConfig;
private readonly IMessaging _messaging;

private string ExternalSystemEventHandlerName => GetType().Name;

public UpdatedPermissionsExternalSystemEventsHandler(ILogger<UpdatedPermissionsExternalSystemEventsHandler> logger, RecruitWebJobsSystemConfiguration jobsConfig,
IRecruitQueueService recruitQueueService,
IEmployerAccountProvider employerAccountProvider, IEncodingService encoder,
IRecruitQueueService recruitQueueService, IEncodingService encoder,
IMessaging messaging)
{
_logger = logger;
_jobsConfig = jobsConfig;
_recruitQueueService = recruitQueueService;
_employerAccountProvider = employerAccountProvider;
_encoder = encoder;
_messaging = messaging;
}
Expand All @@ -56,25 +53,19 @@ public async Task Handle(UpdatedPermissionsEvent message, IMessageHandlerContext
_logger.LogInformation($"Not handling Provider {nameof(Operation.Recruitment)} Permission being revoked as it is a consequence of Provider being blocked by QA on Recruit.");
return;
}

var employerAccountId = _encoder.Encode(message.AccountId, EncodingType.AccountId);
var employerAccountLegalEntityId = _encoder.Encode(message.AccountLegalEntityId, EncodingType.PublicAccountLegalEntityId);

if (message.GrantedOperations.Contains(Operation.Recruitment) == false)
{
_logger.LogInformation($"Transferring vacancies from Provider {message.Ukprn} to Employer {message.AccountId}");

var employerAccountId = _encoder.Encode(message.AccountId, EncodingType.AccountId);

var legalEntity = await GetAssociatedLegalEntityAsync(message, employerAccountId);

if (legalEntity == null)
{
throw new Exception($"Could not find matching Account Legal Entity Id {message.AccountLegalEntityId} for Employer Account {message.AccountId}");
}

await _recruitQueueService.AddMessageAsync(new TransferVacanciesFromProviderQueueMessage
{
Ukprn = message.Ukprn,
EmployerAccountId = employerAccountId,
AccountLegalEntityPublicHashedId = legalEntity.AccountLegalEntityPublicHashedId,
AccountLegalEntityPublicHashedId = employerAccountLegalEntityId,
UserRef = message.UserRef.Value,
UserEmailAddress = message.UserEmailAddress,
UserName = $"{message.UserFirstName} {message.UserLastName}",
Expand All @@ -85,19 +76,10 @@ await _recruitQueueService.AddMessageAsync(new TransferVacanciesFromProviderQueu
{
_logger.LogInformation($"Transferring vacancies from Employer Review to QA Review for Provider {message.Ukprn}");

var employerAccountId = _encoder.Encode(message.AccountId, EncodingType.AccountId);

var legalEntity = await GetAssociatedLegalEntityAsync(message, employerAccountId);

if (legalEntity == null)
{
throw new Exception($"Could not find matching Account Legal Entity Id {message.AccountLegalEntityId} for Employer Account {message.AccountId}");
}

await _recruitQueueService.AddMessageAsync(new TransferVacanciesFromEmployerReviewToQAReviewQueueMessage
{
Ukprn = message.Ukprn,
AccountLegalEntityPublicHashedId = legalEntity.AccountLegalEntityPublicHashedId,
AccountLegalEntityPublicHashedId = employerAccountLegalEntityId,
UserRef = message.UserRef.Value,
UserEmailAddress = message.UserEmailAddress,
UserName = $"{message.UserFirstName} {message.UserLastName}"
Expand All @@ -106,12 +88,5 @@ await _recruitQueueService.AddMessageAsync(new TransferVacanciesFromEmployerRevi

await _messaging.SendCommandAsync(new SetupProviderCommand(message.Ukprn));
}

private async Task<LegalEntity> GetAssociatedLegalEntityAsync(UpdatedPermissionsEvent message, string employerAccountId)
{
var legalEntities = await _employerAccountProvider.GetLegalEntitiesConnectedToAccountAsync(employerAccountId);
var legalEntity = legalEntities.FirstOrDefault(le => le.AccountLegalEntityId == message.AccountLegalEntityId);
return legalEntity == null ? null : LegalEntityMapper.MapFromAccountApiLegalEntity(legalEntity);
}
}
}
Loading