{"size": 1309, "ext": "js", "max_stars_repo_path": "api/controllers/pr/findPrs/gitlab/getNextPage.js", "max_stars_repo_name": "Riya792/hacktoberfest-checker", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "api/controllers/pr/findPrs/gitlab/getNextPage.js", "max_issues_repo_name": "Riya792/hacktoberfest-checker", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/controllers/pr/findPrs/gitlab/getNextPage.js", "max_forks_repo_name": "Riya792/hacktoberfest-checker", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.18, "max_line_length": 76, "alphanum_fraction": 0.5821237586}
'use strict';

const hasNextPage = require('./hasNextPage');

const getNextPage = (
  pagination,
  gitlab,
  username,
  searchYear,
  pullRequestData
) =>
  new Promise((resolve, reject) => {
    gitlab.MergeRequests.all({
      scope: 'all',
      author_username: username,
      //created_after: `${searchYear}-08-30T00:00:00-12:00`,
      //created_before: `${searchYear}-10-31T23:59:59-12:00`,
      // 30 is the default but this makes it clearer/allows it to be tweaked
      per_page: pagination.perPage,
      page: pagination.next,
      showExpanded: true,
    })
      .then((res) => {
        const newPullRequestData = pullRequestData.concat(res.data);
        const pagination = res.paginationInfo;

        if (hasNextPage(pagination)) {
          getNextPage(
            pagination,
            gitlab,
            username,
            searchYear,
            newPullRequestData
          ).then((pullRequestData) => resolve(pullRequestData));
          return;
        }

        if (process.env.NODE_ENV !== 'production') {
          console.log(`Found ${pullRequestData.length} pull requests.`);
        }
        resolve(newPullRequestData);
      })
      .catch((err) => {
        console.log('Error: ' + err);
        return reject();
      });
  });

module.exports = getNextPage;
