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);
        }
예제 #2
0
        public async Task <IEnumerable <EmployerInfo> > GetLegalEntitiesForProviderAsync(long ukprn)
        {
            ProviderPermissions providerPermissions = null;

            using (var httpClient = CreateHttpClient(_configuration))
            {
                var queryData = new { Ukprn = ukprn, Operation = "Recruitment" };
                var uri       = new Uri(AddQueryString("accountproviderlegalentities", queryData), UriKind.RelativeOrAbsolute);

                try
                {
                    var response = await httpClient.GetAsync(uri);

                    if (!response.IsSuccessStatusCode)
                    {
                        _logger.LogError("An invalid response received when trying to get provider relationships");
                        return(new EmployerInfo[] {});
                    }
                    var content = await response.Content.ReadAsStringAsync();

                    providerPermissions = JsonConvert.DeserializeObject <ProviderPermissions>(content);
                }
                catch (HttpRequestException ex)
                {
                    _logger.LogError(ex, "Error trying to retrieve legal entities.", null);
                }
                catch (JsonReaderException ex)
                {
                    _logger.LogError(ex, $"Couldn't deserialise {nameof(ProviderPermissions)}.", null);
                }

                return(await GetEmployerInfosAsync(providerPermissions));
            }
        }