<commit_before>'use strict';
var express = require('express');
var router = new express.Router();

var extensionNumberCommand = require('./lib/slashCommands/extensionNumber/ExtensionNumber');
var prsCommand = require('./lib/slashCommands/prs/Prs');

var ciCommand = require('./lib/slashCommands/ci/CI');

router.use(function(req, res, next) {
	// log each request to the console
	console.log('\n===>', req.method, req.path);
    if (process.env.NODE_ENV === 'DEBUG' || process.env.NODE_ENV === 'DEV') {
        console.log('PARAMS:\n', req.params);
        console.log('QUERY:\n', req.query);
        console.log('BODY:\n', req.body);
    }

	next();
});

/*
 * Routes:
 */
router.all('/', function (req, res) {
    res.send('Set your hooks to point here.');
});

/* TODO: Use middleware to check for slack token's existance (avoid
 * URL triggering from 3rd parties...
 */

/* TODO: Refactor to all slash commands URLs point to the same URL
 *  Ex.: `/slashCommands` instead of `slashCommands/commandName`
 *  and then infer with router to follow based on `req.query.command`
 */

/* TODO: Extract the following kind of treatment:
 *  var command = req.query && req.query.command || undefined;
 *  var text = req.query.text || undefined;
 *  to an express middleware for the /slashCommands/ endpoint
 */
router.get('/slashCommands/ci', function (req, res) {
    var command = req.query && req.query.command || undefined;
    var text = req.query.text || undefined;

    if (command && command === '/ci') {
        var args = text && text.split && text.split(' ') || [];

        ciCommand.runTask.call(ciCommand, args, function(err, out) {
            if (err) {
                res.status(400).send(err.message);
            } else {
                res.status(200).send(out);
            }
        });
    }
});

router.get('/slashCommands/ext', function (req, res) {
    var command = req.query && req.query.command || undefined;
    var text = req.query.text || undefined;

    if (command && command === '/ramal') {
        var findExtension = text ?
            extensionNumberCommand.find.bind(extensionNumberCommand, text) :
            extensionNumberCommand.find.bind(extensionNumberCommand);

        findExtension(function(out) {
            res.send(out);
        });
    }
});

router.get('/slashCommands/prs', function (req, res) {
    var command = req.query && req.query.command || undefined;
    var text = req.query.text || undefined;

    if (!text) {
        res.status(400).send('At least one argument is necessary');
        return;
    }

    if (command && command === '/prs') {
        var args = text.split(' ');
        var repo = args[0].trim();
        var criteria = args[1] || undefined;
        prsCommand.listPrs(repo, criteria, function (out) {
            //TODO: Right now it's just sending back to slack. Let's make it post to a real channel
            res.send(out);
        });
    }
});

module.exports = router;<commit_msg>Add: proper erro handling on router<commit_after>'use strict';
var express = require('express');
var router = new express.Router();

var extensionNumberCommand = require('./lib/slashCommands/extensionNumber/ExtensionNumber');
var prsCommand = require('./lib/slashCommands/prs/Prs');

var ciCommand = require('./lib/slashCommands/ci/CI');

router.use(function(req, res, next) {
	// log each request to the console
	console.log('\n===>', req.method, req.path);
    if (process.env.NODE_ENV === 'DEBUG' || process.env.NODE_ENV === 'DEV') {
        console.log('PARAMS:\n', req.params);
        console.log('QUERY:\n', req.query);
        console.log('BODY:\n', req.body);
    }

	next();
});

/*
 * Routes:
 */
router.all('/', function (req, res) {
    res.send('Set your hooks to point here.');
});

/* TODO: Use middleware to check for slack token's existance (avoid
 * URL triggering from 3rd parties...
 */

/* TODO: Refactor to all slash commands URLs point to the same URL
 *  Ex.: `/slashCommands` instead of `slashCommands/commandName`
 *  and then infer with router to follow based on `req.query.command`
 */

/* TODO: Extract the following kind of treatment:
 *  var command = req.query && req.query.command || undefined;
 *  var text = req.query.text || undefined;
 *  to an express middleware for the /slashCommands/ endpoint
 */
router.get('/slashCommands/ci', function (req, res) {
    var command = req.query && req.query.command || undefined;
    var text = req.query.text || undefined;

    if (command && command === '/ci') {
        var args = text && text.split && text.split(' ') || [];

        ciCommand.runTask.call(ciCommand, args, function(err, out) {
            if (err) {
                res.status(400).send(err.message);
            } else {
                res.status(200).send(out);
            }
        });
    }
});

router.get('/slashCommands/ext', function (req, res) {
    var command = req.query && req.query.command || undefined;
    var text = req.query.text || undefined;

    if (command && command === '/ramal') {
        var findExtension = text ?
            extensionNumberCommand.find.bind(extensionNumberCommand, text) :
            extensionNumberCommand.find.bind(extensionNumberCommand);

        findExtension(function(out) {
            res.send(out);
        });
    }
});

router.get('/slashCommands/prs', function (req, res) {
    var command = req.query && req.query.command || undefined;
    var text = req.query.text || undefined;

    if (!text) {
        res.status(400).send('At least one argument is necessary');
        return;
    }

    if (command && command === '/prs') {
        var args = text.split(' ');
        var repo = args[0].trim();
        var criteria = args[1] || undefined;
        prsCommand.listPrs(repo, criteria, function (err, out) {
            if (err) { res.status(400).send(err); }
            //TODO: Right now it's just sending back to slack. Let's make it post to a real channel
            res.send(out);
        });
    }
});

module.exports = router;
{'fraction_non_alphanumeric': 0.15715948777648428, 'fraction_numerical': 0.004490271079328123, 'mean_word_length': 2.685049019607843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4567637', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1772, 'n_words': 478}