{"size": 1045, "ext": "js", "max_stars_repo_path": "api.js", "max_stars_repo_name": "SkewerSpot/thankyou-gen", "max_stars_repo_stars_event_min_datetime": "2021-03-10T12:55:44.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-10T12:55:44.000Z", "max_issues_repo_path": "api.js", "max_issues_repo_name": "SkewerSpot/thankyou-gen", "max_issues_repo_issues_event_min_datetime": "2020-01-15T09:17:15.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T05:34:52.000Z", "max_forks_repo_path": "api.js", "max_forks_repo_name": "SkewerSpot/thankyou-gen", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 3, "max_forks_count": null, "avg_line_length": 25.487804878, "max_line_length": 77, "alphanum_fraction": 0.6497607656}
const express = require('express');
const router = express.Router();
const DbLib = require('./db/db-lib');
const Util = require('./util');

/**
 * Returns an array of `count` unique codes.
 *
 * The API endpoint optionally espects two query string params:
 * - count: The desired number of unique codes to generate (bet. 1-1000).
 * - dummy: If 'true', return dummy codes. Otherwise, generate from database.
 *
 * Sample usage:
 * - /unique-codes?count=100
 * - /unique-codes?count=100&dummy=true
 */
router.get('/unique-codes', async (req, res) => {
  let count = 0;
  let dummyMode = false;
  let codes = [];

  count = parseInt(req.query.count);
  if (isNaN(count) || count < 0 || count > 1000) {
    return res.status(400).json({
      error: '`count` query param must be an integer between 0-1000'
    });
  }

  dummyMode = req.query.dummy === 'true';

  if (dummyMode) {
    codes = Util.generateDummyUniqueCodes(count);
  } else {
    codes = await DbLib.generateUniqueCodes(count);
  }

  res.json(codes);
});

module.exports = router;
