{"size": 1409, "ext": "js", "max_stars_repo_path": "lib/frontend/get-github-subscriptions.js", "max_stars_repo_name": "cognoa/github-for-jira", "max_stars_repo_stars_event_min_datetime": "2022-01-31T04:32:35.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-15T09:28:51.000Z", "max_issues_repo_path": "lib/frontend/get-github-subscriptions.js", "max_issues_repo_name": "cognoa/github-for-jira", "max_issues_repo_issues_event_min_datetime": "2020-09-14T10:55:44.000Z", "max_issues_repo_issues_event_max_datetime": "2021-07-28T10:46:43.000Z", "max_forks_repo_path": "lib/frontend/get-github-subscriptions.js", "max_forks_repo_name": "cognoa/github-for-jira", "max_forks_repo_forks_event_min_datetime": "2022-03-15T09:28:55.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-15T09:28:55.000Z"}
{"max_stars_count": 3, "max_issues_count": 136, "max_forks_count": 1, "avg_line_length": 36.1282051282, "max_line_length": 109, "alphanum_fraction": 0.6579134138}
const { Subscription } = require('../models');

module.exports = async (req, res, next) => {
  if (!req.session.githubToken) {
    return next(new Error('Unauthorized'));
  }

  const { github, client, isAdmin } = res.locals;

  const { installationId } = req.params;
  try {
    const { data: { login } } = await github.users.getAuthenticated();
    // get the installation to see if the user is an admin of it
    const { data: installation } = await client.apps.getInstallation({ installation_id: installationId });
    // get all subscriptions from the database for this installation ID
    const subscriptions = await Subscription.getAllForInstallation(installationId);
    // Only show the page if the logged in user is an admin of this installation
    if (await isAdmin({
      org: installation.account.login,
      username: login,
      type: installation.target_type,
    })) {
      const { data: info } = await client.apps.getAuthenticated();
      return res.render('github-subscriptions.hbs', {
        csrfToken: req.csrfToken(),
        installation,
        info,
        subscriptions,
        hasSubscriptions: subscriptions.length > 0,
      });
    } else {
      return next(new Error('Unauthorized'));
    }
  } catch (err) {
    console.log(`Unable to show subscription page. error=${err}, installation=${req.params.installationId}`);
    return next(new Error('Not Found'));
  }
};
