<commit_before>var request = require('request');
var qs = require('querystring');
var models = require('../../../models');


function updateUserHasVoted(user_id, callback) {
    models.User.findOne({_id: user_id}, function(err, user) {
        if (err) {
            return callback(err);
        }
        user.has_voted = true;
        user.save(function (err) {
            if (err) {
                console.error(err);
                callback(err);
            } else {
                callback(null, user.id);
            }
        });
    });
}


module.exports = function(req, res) {
    if (req.session.user.has_voted)
        res.json(403 , "has_voted");

    // Two lines of google voodoo
    req.body.backupCache = '';
    req.body.submit = 'Submit';
    // Record the user's IP
    req.body['entry.39.single'] = ('x-forwarded-for' in req.headers) ? req.headers['x-forwarded-for'] : req.connection.remoteAddress;
    // e-mail
    req.body['entry.40.single'] = req.session.user.email;

    var vote_data = qs.stringify(req.body);
	var post_opts = {
        uri: 'https://docs.google.com/spreadsheet/formResponse?formkey=dDI5dUg1TFMzUENKc09lSXp5aTh2alE6MQ&ifq',
        headers: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        body: vote_data
    };
    request.post(post_opts, function(err, resp, body) {
        if (err) {
            console.error(err);
            res.json("ok");
        }
        updateUserHasVoted(req.session.user._id, function(err) {
            if(err) {
                console.error(err);
            }
            res.json("ok");
        });
    });
};
<commit_msg>fix headers sent more than once that crashes the staging<commit_after>var request = require('request');
var qs = require('querystring');
var models = require('../../../models');


function updateUserHasVoted(user_id, callback) {
    models.User.findOne({_id: user_id}, function(err, user) {
        if (err) {
            return callback(err);
        }
        user.has_voted = true;
        user.save(function (err) {
            if (err) {
                console.error(err);
                callback(err);
            } else {
                callback(null, user.id);
            }
        });
    });
}


module.exports = function(req, res) {
    if (req.session.user.has_voted)
        res.json(403 , "has_voted");

    // Two lines of google voodoo
    req.body.backupCache = '';
    req.body.submit = 'Submit';
    // Record the user's IP
    req.body['entry.39.single'] = ('x-forwarded-for' in req.headers) ? req.headers['x-forwarded-for'] : req.connection.remoteAddress;
    // e-mail
    req.body['entry.40.single'] = req.session.user.email;

    var vote_data = qs.stringify(req.body);
	var post_opts = {
        uri: 'https://docs.google.com/spreadsheet/formResponse?formkey=dDI5dUg1TFMzUENKc09lSXp5aTh2alE6MQ&ifq',
        headers: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        body: vote_data
    };
    request.post(post_opts, function(err, resp, body) {
        if (err) {
            console.error(err);
            res.json("ok");
        }
        else
            updateUserHasVoted(req.session.user._id, function(err) {
                if(err) {
                    console.error(err);
                }
                res.json("ok");
            });
    });
};

{'fraction_non_alphanumeric': 0.15046838407494145, 'fraction_numerical': 0.00819672131147541, 'mean_word_length': 1.9080851063829787, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5302490', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1084, 'n_words': 182}