public virtual async Task <OrganisationViewModel> CreateOrganisationViewModelAsync( EnterCodesViewModel enterCodes, User currentUser) { var org = await _organisationBusinessLogic.GetOrganisationByEmployerReferenceAndSecurityCodeAsync( enterCodes.EmployerReference, enterCodes.SecurityToken); if (org == null) { return(null); } var model = new OrganisationViewModel(); // when SecurityToken is expired then model.SecurityCodeExpired should be true model.IsSecurityCodeExpired = org.HasSecurityCodeExpired(); model.Employers = new PagedResult <EmployerRecord>(); model.Employers.Results = new List <EmployerRecord> { _organisationBusinessLogic.CreateEmployerRecord(org) }; model.SelectedEmployerIndex = 0; //Mark the organisation as authorised model.SelectedAuthorised = true; model.IsFastTrackAuthorised = true; return(model); }
public async Task <PagedResult <EmployerRecord> > SearchAsync(string searchText, int page, int pageSize, bool test = false) { var result = new PagedResult <EmployerRecord>(); if (test) { var employers = new List <EmployerRecord>(); var min = await _DataRepository.CountAsync <Organisation>(); var id = Numeric.Rand(min, int.MaxValue - 1); var employer = new EmployerRecord { OrganisationName = _sharedOptions.TestPrefix + "_GovDept_" + id, CompanyNumber = ("_" + id).Left(10), Address1 = "Test Address 1", Address2 = "Test Address 2", City = "Test Address 3", Country = "Test Country", PostCode = "Test Post Code", EmailDomains = "*@*", PoBox = null, SicCodeIds = "1" }; employers.Add(employer); result.ActualRecordTotal = employers.Count; result.VirtualRecordTotal = employers.Count; result.CurrentPage = page; result.PageSize = pageSize; result.Results = employers; return(result); } var searchResults = await _DataRepository.ToListAsync <Organisation>(o => o.SectorType == SectorTypes.Public && o.Status == OrganisationStatuses.Active); var searchResultsList = searchResults.Where(o => o.OrganisationName.ContainsI(searchText)) .OrderBy(o => o.OrganisationName) .ThenBy(o => o.OrganisationName) .ToList(); result.ActualRecordTotal = searchResultsList.Count; result.VirtualRecordTotal = searchResultsList.Count; result.CurrentPage = page; result.PageSize = pageSize; result.Results = searchResultsList.Page(pageSize, page).Select(o => _organisationBusinessLogic.CreateEmployerRecord(o)).ToList(); return(result); }
public async Task<PagedResult<EmployerRecord>> SearchAsync(string searchText, int page, int pageSize, bool test = false) { if (searchText.IsNumber()) searchText = searchText.PadLeft(8, '0'); var remoteTotal = 0; var searchResults = test ? null : LoadSearch(searchText); if (searchResults == null) { var orgs = new List<Organisation>(); var localResults = new List<Organisation>(); if (!test) { orgs = _DataRepository.GetAll<Organisation>() .Where( o => o.SectorType == SectorTypes.Private && o.Status == OrganisationStatuses.Active && o.LatestAddress != null) .ToList(); if (searchText.IsCompanyNumber()) localResults = orgs.Where(o => o.CompanyNumber.EqualsI(searchText)) .OrderBy(o => o.OrganisationName).ToList(); else localResults = orgs.Where(o => o.OrganisationName.ContainsI(searchText)) .OrderBy(o => o.OrganisationName).ToList(); } try { searchResults = await _CompaniesHouseAPI.SearchEmployersAsync(searchText, 1, _CompaniesHouseAPI.MaxRecords, test); remoteTotal = searchResults.Results.Count; } catch (Exception ex) { remoteTotal = -1; if ((ex.InnerException ?? ex).Message.ContainsI( "502", "Bad Gateway", "the connected party did not properly respond after a period of time") && localResults.Count > 0) searchResults = new PagedResult<EmployerRecord>(); else throw; } if (!searchText.IsCompanyNumber() && remoteTotal > 0) { //Replace CoHo orgs with db orgs with same company number var companyNumbers = new SortedSet<string>( searchResults.Results.Select(s => s.CompanyNumber), StringComparer.OrdinalIgnoreCase); var existingResults = orgs.Where(o => companyNumbers.Contains(o.CompanyNumber)).ToList(); localResults = localResults.Union(existingResults).ToList(); } var localTotal = localResults.Count; //Remove any coho results found in DB if (localTotal > 0) { localTotal -= searchResults.Results.RemoveAll(r => localResults.Any(l => l.CompanyNumber == r.CompanyNumber)); if (localResults.Count > 0) { if (test) //Make sure test employer is first searchResults.Results.AddRange(localResults.Select(o => _organisationBusinessLogic.CreateEmployerRecord(o))); else searchResults.Results.InsertRange(0, localResults.Select(o => _organisationBusinessLogic.CreateEmployerRecord(o))); searchResults.ActualRecordTotal += localTotal; } } if (!test) SaveSearch(searchText, searchResults, remoteTotal); } var result = new PagedResult<EmployerRecord>(); result.VirtualRecordTotal = searchResults.ActualRecordTotal > _CompaniesHouseAPI.MaxRecords ? _CompaniesHouseAPI.MaxRecords : searchResults.ActualRecordTotal; result.ActualRecordTotal = searchResults.ActualRecordTotal; result.CurrentPage = page; result.PageSize = pageSize; result.Results = searchResults.Results.Page(pageSize, page).ToList(); return result; }