<commit_before>var r = require('./../helpers/response');

module.exports = function(server, model, config) {

    /**
     * POST: /api/push/:widgetToken
     * post body:
     *  - data {json object}
     */
    server.post('/api/push/:token', function(req, res, next) {

        function extractBodyData(rawData) {
            try {
                return JSON.parse(rawData);
            }
            catch (e) {
                r.fail(res, {message: "JSON data is not valid"}, 400);
                next();
            }
        }

        function pushData(widget, rawData) {
            var data = extractBodyData(rawData);

            r.ok(res);

            // TODO: validation stuff
            // TODO: add check that data was saved
            widget.data = data;
            widget.last_update_date = new Date();
            widget.save();

            return next();
        }

        var query  = model.Widget.where({ key: req.params.token });
        query.findOne(function (err, widget) {
            if (widget) pushData(widget, req.body.toString());
            else {
                if (err) r.fail(res, err);
                else r.fail(res);

                return next();
            }
        });
    });
};<commit_msg>push fix for empty req.body<commit_after>var r = require('./../helpers/response');

module.exports = function(server, model, config) {

    /**
     * POST: /api/push/:widgetToken
     * post body:
     *  - data {json object}
     */
    server.post('/api/push/:token', function(req, res, next) {

        function extractBodyData(rawData) {
            try {
                return JSON.parse(rawData);
            }
            catch (e) {
                r.fail(res, {message: "JSON data is not valid"}, 400);
                next();
            }
        }

        function pushData(widget, rawData) {
            var data = extractBodyData(rawData);

            r.ok(res);

            // TODO: validation stuff
            // TODO: add check that data was saved
            widget.data = data;
            widget.last_update_date = new Date();
            widget.save();

            return next();
        }

        var query  = model.Widget.where({ key: req.params.token });
        query.findOne(function (err, widget) {
            if (typeof req.body === 'undefined') {
                r.fail(res, {message: "No data to push"}, 400);
                return next();
            }
            else if (widget) pushData(widget, req.body.toString());
            else {
                if (err) r.fail(res, err);
                else r.fail(res);

                return next();
            }
        });
    });
};
{'fraction_non_alphanumeric': 0.1335578002244669, 'fraction_numerical': 0.003367003367003367, 'mean_word_length': 1.3811219946571682, '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': '4360585', 'n_tokens_mistral': 753, 'n_tokens_neox': 717, 'n_words': 178}