{"size": 1503, "ext": "js", "max_stars_repo_path": "src/routes/svcs.js", "max_stars_repo_name": "ferng/runlog-js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/routes/svcs.js", "max_issues_repo_name": "ferng/runlog-js", "max_issues_repo_issues_event_min_datetime": "2017-03-17T10:14:24.000Z", "max_issues_repo_issues_event_max_datetime": "2019-01-08T19:55:58.000Z", "max_forks_repo_path": "src/routes/svcs.js", "max_forks_repo_name": "ferng/runlog-js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 20, "max_forks_count": null, "avg_line_length": 25.05, "max_line_length": 128, "alphanum_fraction": 0.6347305389}
/**
 * Express router to deal with incoming service REST requests.
 * @module src/routes/svcs
 * @private
 */

const express = require('express');
const log = require('../utils/logger.js').getLogger();
const db = require('../utils/dbConn.js');

/**
 * Express router to mount general system related functions on.
 * @type {object}
 * @const
 * @namespace svcsExpressRoutes
 * @private
 */
const router = new express.Router();
router.use((req, res, next) => {
  next();
});


/**
 * GET: Route returning existing dropDown options from database.
 * @name GET/api/svcs/selectOpts
 * @function
 * @memberof module:src/routes/svcs~svcsExpressRoutes
 * @inner
 * @param {Request} req - Express request
 * @param {Result} res - Express response.
 * @return {HTTP_body} the result of the query as a json payload containing all dropDown option values (even if there are none).
 * @return {HTTP_response} HTTP-200 if OK, HTTP-500 if anything went wrong.
 * @private
 */
router.get('/selectOpts/*', (req, res) => {
  const optionsTypes = req.params[0].split(',');
  const selectOptions = [];

  Promise.all(optionsTypes.map(optionType =>
    db.get(optionType, {})
      .then((data) => {
        selectOptions.push({
          optType: optionType,
          options: data,
        });
      })))
    .then(() => {
      log.debug('Select options retrieved.');
      res.json(selectOptions);
    })
    .catch((err) => {
      log.error(err);
      res.status(500).send('');
    });
});


module.exports = router;
