private async Task <List <EmployerInfo> > GetEmployerInfosAsync(ProviderPermissions providerPermissions) { var employerInfos = new List <EmployerInfo>(); var permittedEmployerAccounts = providerPermissions.AccountProviderLegalEntities.GroupBy(p => p.AccountId); foreach (var permittedEmployer in permittedEmployerAccounts) { var accountId = await _employerAccountProvider.GetEmployerAccountPublicHashedIdAsync(permittedEmployer.Key); var employerInfo = new EmployerInfo() { EmployerAccountId = accountId, Name = permittedEmployer.First().AccountName, //should be same in all the items hence read from first LegalEntities = new List <LegalEntity>() }; var legalEntityViewModels = await _employerAccountProvider.GetLegalEntitiesConnectedToAccountAsync(accountId); foreach (var permittedLegalEntity in permittedEmployer) { var matchingLegalEntity = legalEntityViewModels.FirstOrDefault(e => e.AccountLegalEntityPublicHashedId == permittedLegalEntity.AccountLegalEntityPublicHashedId); if (matchingLegalEntity != null) { var legalEntity = LegalEntityMapper.MapFromAccountApiLegalEntity(matchingLegalEntity); legalEntity.AccountLegalEntityPublicHashedId = permittedLegalEntity.AccountLegalEntityPublicHashedId; employerInfo.LegalEntities.Add(legalEntity); } } employerInfos.Add(employerInfo); } return(employerInfos); }
public async Task <bool> HasProviderGotEmployersPermissionAsync(long ukprn, string accountHashedId, string accountLegalEntityPublicHashedId, OperationType operation) { var accountDetails = await _accountApiClient.GetAccount(accountHashedId); var permittedLegalEntities = await GetProviderPermissionsforEmployer(ukprn, accountDetails.PublicHashedAccountId, operation); if (permittedLegalEntities.Any() == false) { return(false); } var accountId = await _employerAccountProvider.GetEmployerAccountPublicHashedIdAsync(permittedLegalEntities.First().AccountId); var allLegalEntities = (await _employerAccountProvider.GetLegalEntitiesConnectedToAccountAsync(accountId)).ToList(); var hasPermission = permittedLegalEntities.Join(allLegalEntities, ple => ple.AccountLegalEntityPublicHashedId, ale => ale.AccountLegalEntityPublicHashedId, (ple, ale) => ale) .Any(l => l.AccountLegalEntityPublicHashedId == accountLegalEntityPublicHashedId); return(hasPermission); }