public async Task <ActionResult> SelectLegalEntity(string hashedAccountId, string transferConnectionCode, string cohortRef = "") { if (!await IsUserRoleAuthorized(hashedAccountId, Role.Owner, Role.Transactor)) { return(View("AccessDenied")); } var response = await Orchestrator.GetLegalEntities(hashedAccountId, transferConnectionCode, cohortRef, OwinWrapper.GetClaimValue(@"sub")); if (response.Data.LegalEntities == null || !response.Data.LegalEntities.Any()) { throw new InvalidStateException($"No legal entities associated with account {hashedAccountId}"); } if (response.Data.LegalEntities.Count() > 1) { return(View(response)); } var autoSelectLegalEntity = response.Data.LegalEntities.First(); var hasSigned = EmployerCommitmentsOrchestrator.HasSignedAgreement( autoSelectLegalEntity, !string.IsNullOrWhiteSpace(transferConnectionCode)); if (hasSigned) { return(RedirectToAction("SearchProvider", new SelectLegalEntityViewModel { TransferConnectionCode = transferConnectionCode, CohortRef = response.Data.CohortRef, LegalEntityCode = autoSelectLegalEntity.Code, // no need to store LegalEntities, as the property is only read in the SelectLegalEntity view, which we're now skipping })); } return(RedirectToAction("AgreementNotSigned", new { HashedAccountId = hashedAccountId, LegalEntityCode = autoSelectLegalEntity.Code, TransferConnectionCode = transferConnectionCode, CohortRef = response.Data.CohortRef, HasSignedAgreement = false, LegalEntityName = autoSelectLegalEntity.Name ?? string.Empty })); }