From 904cf9662e2d101b67b90ba70b1d921f88a72b7a Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Mon, 4 Jul 2022 11:12:04 +0100 Subject: [PATCH] FAI-552 - Update UpdatedPermissionEventHandler Updated the handler to no longer call the accounts database to get the public encoded account legal entity id. Instead just gets it from encoding the known internal id. This is to stop the issue of the dashboard not being created when the ALE has been deleted and provider permissions updated --- ...missionsExternalSystemEventHandlerTests.cs | 112 ++---------------- ...dPermissionsExternalSystemEventsHandler.cs | 37 +----- 2 files changed, 17 insertions(+), 132 deletions(-) diff --git a/src/Jobs/Recruit.Vacancies.Jobs.UnitTests/ExternalSystemEventHandlers/UpdatedPermissionsExternalSystemEventHandlerTests.cs b/src/Jobs/Recruit.Vacancies.Jobs.UnitTests/ExternalSystemEventHandlers/UpdatedPermissionsExternalSystemEventHandlerTests.cs index 52fa39632a..8506d24019 100644 --- a/src/Jobs/Recruit.Vacancies.Jobs.UnitTests/ExternalSystemEventHandlers/UpdatedPermissionsExternalSystemEventHandlerTests.cs +++ b/src/Jobs/Recruit.Vacancies.Jobs.UnitTests/ExternalSystemEventHandlers/UpdatedPermissionsExternalSystemEventHandlerTests.cs @@ -30,32 +30,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 _dummyLegalEntities; private readonly RecruitWebJobsSystemConfiguration _jobsConfig; private readonly Mock _mockRecruitQueueService; - private readonly Mock _mockEmployerAccountProvider; private readonly Mock _mockEncoder; private readonly Mock _mockMessaging; private readonly UpdatedPermissionsExternalSystemEventsHandler _sut; public UpdatedPermissionsExternalSystemEventHandlerTests() { - _dummyLegalEntities = _autoFixture.CreateMany(NoOfDummyLegalEntitiesToCreate); _jobsConfig = new RecruitWebJobsSystemConfiguration { DisabledJobs = new List() }; _mockRecruitQueueService = new Mock(); - _mockEmployerAccountProvider = new Mock(); _mockEncoder = new Mock(); _mockMessaging = new Mock(); + _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>(), _jobsConfig, - _mockRecruitQueueService.Object, _mockEmployerAccountProvider.Object, + _mockRecruitQueueService.Object, _mockEncoder.Object, _mockMessaging.Object); } @@ -68,7 +66,6 @@ public async Task GivenJobsConfigWithEventHandlerJobDisabled_ThenVerifyNoDepende _mockRecruitQueueService.VerifyNoOtherCalls(); _mockMessaging.VerifyNoOtherCalls(); - _mockEmployerAccountProvider.VerifyNoOtherCalls(); _mockEncoder.VerifyNoOtherCalls(); } @@ -81,8 +78,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(c => c.Ukprn == Ukprn)), Times.Once); } @@ -95,8 +90,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(c => c.Ukprn == Ukprn)), Times.Once); } @@ -109,91 +102,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(c => c.Ukprn == Ukprn)), Times.Once); - } - - [Fact] - public async Task GivenNotExistingLegalEntityId_ThenExceptionIsThrown() - { - var grantedOperations = new HashSet { Operation.CreateCohort }; - var previousOperations = new HashSet(); - - _mockEncoder.Setup(x => x.Encode(EmployerAccountId, EncodingType.AccountId)).Returns(EmployerAccountIdEncoded); - _mockEmployerAccountProvider.Setup(x => x.GetLegalEntitiesConnectedToAccountAsync(EmployerAccountIdEncoded)) - .ReturnsAsync(_dummyLegalEntities); - - var exception = await Assert.ThrowsAsync(() => _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() - .With(l => l.DasAccountId, EmployerAccountIdEncoded) - .With(l => l.AccountLegalEntityId, AccountLegalEntityId) - .With(l => l.LegalEntityId, RecruitLegalEntityId) - .Create(); - - var grantedOperations = new HashSet { Operation.CreateCohort }; - var previousOperations = new HashSet(); - - _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()), Times.Once); - _mockMessaging.Verify(x => x.SendCommandAsync(It.Is(c => c.Ukprn == Ukprn)), Times.Once); - } - - [Fact] - public async Task GivenMatchingExistingLegalEntityId_ThenVerifyTransferReviewProcessIsQueued() - { - var matchingLegalEntityViewModel = _autoFixture.Build() - .With(l => l.DasAccountId, EmployerAccountIdEncoded) - .With(l => l.AccountLegalEntityId, AccountLegalEntityId) - .With(l => l.LegalEntityId, RecruitLegalEntityId) - .Create(); - - var grantedOperations = new HashSet { Operation.Recruitment, Operation.CreateCohort }; - var previousOperations = new HashSet(); - - _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()), Times.Once); _mockMessaging.Verify(x => x.SendCommandAsync(It.Is(c => c.Ukprn == Ukprn)), Times.Once); } [Fact] public async Task GivenMatchingExistingLegalEntityId_ThenTransferQueuedMessageIsMappedCorrectly() { - var matchingLegalEntityViewModel = _autoFixture.Build() - .With(l => l.DasAccountId, EmployerAccountIdEncoded) - .With(l => l.AccountLegalEntityId, AccountLegalEntityId) - .With(l => l.LegalEntityId, RecruitLegalEntityId) - .Create(); - var grantedOperations = new HashSet { Operation.CreateCohort }; var previousOperations = new HashSet(); - _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; @@ -207,28 +124,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(c => c.Ukprn == Ukprn)), Times.Once); } [Fact] public async Task GivenMatchingExistingLegalEntityId_ThenReviewQueuedMessageIsMappedCorrectly() { - var matchingLegalEntityViewModel = _autoFixture.Build() - .With(l => l.DasAccountId, EmployerAccountIdEncoded) - .With(l => l.AccountLegalEntityId, AccountLegalEntityId) - .With(l => l.LegalEntityId, RecruitLegalEntityId) - .Create(); - var grantedOperations = new HashSet { Operation.Recruitment, Operation.CreateCohort }; var previousOperations = new HashSet(); - _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; @@ -241,10 +150,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(c => c.Ukprn == Ukprn)), Times.Once); } } } \ No newline at end of file diff --git a/src/Jobs/Recruit.Vacancies.Jobs/ExternalSystemEventHandlers/UpdatedPermissionsExternalSystemEventsHandler.cs b/src/Jobs/Recruit.Vacancies.Jobs/ExternalSystemEventHandlers/UpdatedPermissionsExternalSystemEventsHandler.cs index cdfeeb470f..4b100f4527 100644 --- a/src/Jobs/Recruit.Vacancies.Jobs/ExternalSystemEventHandlers/UpdatedPermissionsExternalSystemEventsHandler.cs +++ b/src/Jobs/Recruit.Vacancies.Jobs/ExternalSystemEventHandlers/UpdatedPermissionsExternalSystemEventsHandler.cs @@ -21,7 +21,6 @@ public class UpdatedPermissionsExternalSystemEventsHandler : IHandleMessages _logger; private readonly IRecruitQueueService _recruitQueueService; - private readonly IEmployerAccountProvider _employerAccountProvider; private readonly IEncodingService _encoder; private readonly RecruitWebJobsSystemConfiguration _jobsConfig; private readonly IMessaging _messaging; @@ -29,14 +28,12 @@ public class UpdatedPermissionsExternalSystemEventsHandler : IHandleMessages GetType().Name; public UpdatedPermissionsExternalSystemEventsHandler(ILogger 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; } @@ -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}", @@ -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}" @@ -106,12 +88,5 @@ await _recruitQueueService.AddMessageAsync(new TransferVacanciesFromEmployerRevi await _messaging.SendCommandAsync(new SetupProviderCommand(message.Ukprn)); } - - private async Task 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); - } } } \ No newline at end of file