async function loadInformation()

in routes/orgAdmin.ts [159:244]


async function loadInformation(providers: IProviders, query: IUserInformationQuery): Promise<IUserInformationQuery> {
  // Input: query type and value; pre-queried and set single link, if present
  const { operations } = providers;
  const corporateAadId = query.link ? query.link.corporateId : null;
  if (corporateAadId) {
    try {
      const info = await operations.validateCorporateAccountCanLink(corporateAadId);
      query.realtimeGraph = info.graphEntry;
    } catch (graphError) {
      query.realtimeGraphError = graphError;
    }
    try {
      query.managerInfo = await operations.getCachedEmployeeManagementInformation(corporateAadId);
    } catch (managerError) {
      console.dir(managerError);
    }
  }

  // Get user account information from GitHub
  let thirdPartyId = query.link ? query.link.thirdPartyId : query.noLinkButKnownThirdPartyId;
  if (query.gitHubUserInfo && query.gitHubUserInfo.id) {
    // In the scenario that they have renamed their account, this may come up...
    thirdPartyId = query.gitHubUserInfo.id;
  }
  let thirdPartyUsername: string = null;
  let account: Account = null;
  try {
    if (thirdPartyId) {
      account = await getGitHubAccountInformationById(operations, thirdPartyId);
      query.gitHubUserInfo = account;
      const login = account.login;
      if (query.link && login !== query.link.thirdPartyUsername) {
        query.renamedGitHubUserOutcome = new UserQueryOutcomeRenamedThirdPartyUsername(login, query.link.thirdPartyUsername);
      }
      thirdPartyUsername = login;

      const { queryCache } = operations.providers;
      if (queryCache && queryCache.supportsRepositoryCollaborators) {
        const result = await queryCache.userCollaboratorRepositories(thirdPartyId);
        const collaboratorRepositories = [];
        for (const { repository } of result) {
          try {
            await repository.getDetails();
            collaboratorRepositories.push(repository.full_name);
          } catch (ignoreError) {
            console.dir(ignoreError);
          }
        }
        query.collaboratorRepositories = collaboratorRepositories;
      }
    }
  } catch (ignoreGetAccountError) {
    if (ignoreGetAccountError && ignoreGetAccountError.status == /* loose compare */ '404') {
      thirdPartyUsername = query.link ? query.link.thirdPartyUsername : null;
      if (thirdPartyUsername) {
        let deletedAccountError = null;
        let moreInfo = null;
        try {
          moreInfo = await operations.getAccountByUsername(thirdPartyUsername);
        } catch (deletedAccountCatch) {
          if (deletedAccountCatch && deletedAccountCatch.status == /* loose compare */ '404') {
            deletedAccountError = deletedAccountCatch;
            query.deletedGitHubUserOutcome = `The GitHub account '${thirdPartyUsername}' (ID ${thirdPartyId}) has been deleted`;
          } else {
            throw deletedAccountError;
          }
        }
        query.gitHubUserInfo = moreInfo;
        if (moreInfo && moreInfo.id != /* loose compare */ thirdPartyId) {
          const newId = moreInfo.id;
          query.renamedGitHubUserOutcome = new UserQueryOutcomeRenamedThirdPartyUsername(thirdPartyUsername, thirdPartyUsername, `The original GitHub username this user linked with, ${thirdPartyUsername}, exists. However, the user ID is different now. It was ${thirdPartyId} and now the ID is ${newId}. They most likely deleted their old account or have two-factor problems.`);
        }
      }
    } else {
      console.warn(ignoreGetAccountError);
    }
  }

  // Learn about all the org memberships for the username
  if (thirdPartyUsername && account) {
    const loginMemberships = await account.getOperationalOrganizationMemberships();
    query.orgs = loginMemberships;
  }

  return query;
}