{"size": 1382, "ext": "js", "max_stars_repo_path": "src/controllers/records.controller.js", "max_stars_repo_name": "ankurpata/ankurpatankar-gtr", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/controllers/records.controller.js", "max_issues_repo_name": "ankurpata/ankurpatankar-gtr", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/controllers/records.controller.js", "max_forks_repo_name": "ankurpata/ankurpatankar-gtr", "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": 17.4936708861, "max_line_length": 75, "alphanum_fraction": 0.5426917511}
const httpStatus = require('http-status');
const pick = require('../utils/pick');
const ApiError = require('../utils/ApiError');
const catchAsync = require('../utils/catchAsync');
const {recordsService} = require('../services');

/**
 * Main entry controller Fn to process request and fetch
 * records from the Getir Records Service
 * @type {function(*=, *=, *=): void}
 */
const getRecords = catchAsync(async (req, res) => {

  const {
    startDate,
    endDate,
    minCount,
    maxCount
  } = req.body;

  let selector = {};
  if (startDate) {
    selector = {
      ...selector,
      "createdAt": {
        $gte: new Date(startDate)
      }
    };
  }
  if (endDate) {
    selector = {
      ...selector,
      "createdAt": {
        ...selector["createdAt"],
        $lte: new Date(endDate)
      }
    };
  }

  if (minCount) {
    selector = {
      ...selector,
      "totalCount": {
        $gte: minCount
      }
    }
  }

  if (maxCount) {
    selector = {
      ...selector,
      "totalCount": {
        ...selector["totalCount"],
        $lte: maxCount
      }
    }
  }

  const project = {
    totalCount: {$sum: "$counts"},
    createdAt: 1,
    key: 1,
    _id: 0
  }

  const result = await recordsService.queryGetirRecords(selector, project);
  res.send({
    code: 0,
    msg: "Success",
    records: result
  });

});


module.exports = {
  getRecords
};
