<commit_before>const path = require('path')
const hash = require('../lib/hash')
const async = require('async')
const safeExtend = require('../lib/safe-extend')
const Badges = require('../models/badge');
const makeBadgeClassUrl = require('./utils').makeBadgeClassUrl
const Milestones = require('../models/milestone');
const imageHelper = require('../lib/image-helper')
const errorHelper = require('../lib/error-helper')
const middleware = require('../lib/middleware')
const sendPaginated = require('../lib/send-paginated');

const putBadgeHelper = imageHelper.putModel(Badges)
const dbErrorHandler = errorHelper.makeDbHandler('badge')

exports = module.exports = function applyBadgeRoutes (server) {
  // allows routes to override the display of the returned badges
  var responseFormatter = Badges.toResponse
  var badgeListName = 'badges'

  server.get('/public/badges', [
    function(req, res, next) {
      badgeListName = "badgelist"
      responseFormatter = function(row, request) {
        return { location: req.resolvePath(makeBadgeClassUrl(row)) }
      }
      next()
    },
    showAllBadges
  ]);
  server.get('/systems/:systemSlug/badges', [
    middleware.findSystem(),
    showAllBadges,
  ]);
  server.get('/systems/:systemSlug/issuers/:issuerSlug/badges', [
    middleware.findSystem(),
    middleware.findIssuer({where: {systemId: ['system', 'id']}}),
    showAllBadges,
  ]);
  server.get('/systems/:systemSlug/issuers/:issuerSlug/programs/:programSlug/badges', [
    middleware.findSystem(),
    middleware.findIssuer({where: {systemId: ['system', 'id']}}),
    middleware.findProgram({where: {issuerId: ['issuer', 'id']}}),
    showAllBadges,
  ]);
  function showAllBadges (req, res, next) {
    var query;
    var options = {relationships: true};

    switch ('' + req.query.archived) {
      case 'true':
      case '1':
        query = {archived: true};
        break;

      case 'false':
      case '0':
      case 'undefined':
        query = {archived: false};
        break;

      case 'any':
      case '':
        query = {};
        break;

      default:
        return res.send(400, {
          code: 'InvalidParameter',
          parameter: 'archived',
          message: 'Invalid `archived` parameter. Expecting one of \'true\', \'false\' or \'any\'.',
        });
    }

    if (req.system) query.systemId = req.system.id
    if (req.issuer) query.issuerId = req.issuer.id
    if (req.program) query.programId = req.program.id

    if (req.pageData) {
      options.limit = req.pageData.count;
      options.page = req.pageData.page;
      options.includeTotal = true;
    }

    Badges.get(query, options, function foundRows (error, result) {
      if (error)
        return dbErrorHandler(error, null, res, next);

      var total = 0;
      var rows = result;
      if (req.pageData) {
        total = result.total;
        rows = result.rows;
      }

      var responseData = {}
      responseData[badgeListName] = rows.map(responseFormatter)

      sendPaginated(req, res, responseData, total);

      return next();<commit_msg>fixed based on @stenington's feedback.<commit_after>const path = require('path')
const hash = require('../lib/hash')
const async = require('async')
const safeExtend = require('../lib/safe-extend')
const Badges = require('../models/badge');
const makeBadgeClassUrl = require('./utils').makeBadgeClassUrl
const Milestones = require('../models/milestone');
const imageHelper = require('../lib/image-helper')
const errorHelper = require('../lib/error-helper')
const middleware = require('../lib/middleware')
const sendPaginated = require('../lib/send-paginated');

const putBadgeHelper = imageHelper.putModel(Badges)
const dbErrorHandler = errorHelper.makeDbHandler('badge')

exports = module.exports = function applyBadgeRoutes (server) {
  // allows routes to override the display of the returned badges
  var responseFormatter = Badges.toResponse

  server.get('/public/badges', [
    function(req, res, next) {
      req.badgeListName = "badgelist"
      responseFormatter = function(row, request) {
        return { location: req.resolvePath(makeBadgeClassUrl(row)) }
      }
      next()
    },
    showAllBadges
  ]);
  server.get('/systems/:systemSlug/badges', [
    middleware.findSystem(),
    showAllBadges,
  ]);
  server.get('/systems/:systemSlug/issuers/:issuerSlug/badges', [
    middleware.findSystem(),
    middleware.findIssuer({where: {systemId: ['system', 'id']}}),
    showAllBadges,
  ]);
  server.get('/systems/:systemSlug/issuers/:issuerSlug/programs/:programSlug/badges', [
    middleware.findSystem(),
    middleware.findIssuer({where: {systemId: ['system', 'id']}}),
    middleware.findProgram({where: {issuerId: ['issuer', 'id']}}),
    showAllBadges,
  ]);
  function showAllBadges (req, res, next) {
    var query;
    var options = {relationships: true};

    switch ('' + req.query.archived) {
      case 'true':
      case '1':
        query = {archived: true};
        break;

      case 'false':
      case '0':
      case 'undefined':
        query = {archived: false};
        break;

      case 'any':
      case '':
        query = {};
        break;

      default:
        return res.send(400, {
          code: 'InvalidParameter',
          parameter: 'archived',
          message: 'Invalid `archived` parameter. Expecting one of \'true\', \'false\' or \'any\'.',
        });
    }

    if (req.system) query.systemId = req.system.id
    if (req.issuer) query.issuerId = req.issuer.id
    if (req.program) query.programId = req.program.id

    if (req.pageData) {
      options.limit = req.pageData.count;
      options.page = req.pageData.page;
      options.includeTotal = true;
    }

    Badges.get(query, options, function foundRows (error, result) {
      if (error)
        return dbErrorHandler(error, null, res, next);

      var total = 0;
      var rows = result;
      if (req.pageData) {
        total = result.total;
        rows = result.rows;
      }

      var responseData = {}
      badgeListName = req.badgeListName || 'badges'
      responseData[badgeListName] = rows.map(responseFormatter)

      sendPaginated(req, res, responseData, total);

      return next();
{'fraction_non_alphanumeric': 0.1485483870967742, 'fraction_numerical': 0.001935483870967742, 'mean_word_length': 2.9724535554131966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '310957', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1913, 'n_words': 411}