<commit_before>var ClaimModel = require('../../models/claim-model');
var validator = require('validator');

function claimFormatter(claim){
    let returnClaim = {
        text: claim.text,
        probability: claim.probability,
        creationDate: claim.creationDate,
        _id: claim._id,
        _key: claim._key,
        arguments: claim.arguments
    }
    return returnClaim;
}
module.exports = function create(req, res){
    //console.log("TODO: CLAIMS.CREATE escape post data: ", JSON.stringify(req.body));

    let errors = [];

    if (!req.body.hasOwnProperty('text') || req.body.text == '') {
        errors.push({title:'Text is required'});
    } else if (!validator.isAlphanumeric(req.body.username + '')) {
        errors.push({title:'Claim can only have alphanumeric characters'});
    }

    if (!req.body.hasOwnProperty('probability') || req.body.probability == '') {
        errors.push({title:'Probability is required'});
    } else if (!validator.isInt(req.body.probability + '', {min: 0, max: 100})) {
        //if this fails, just set it to 50
        req.body.probability = 50;
    }

    if (errors.length > 0) {
        res.status(400);
        res.json({ errors: errors });
        return;
    }

    var text = validator.escape(req.body.text);
    var probability = Number(req.body.probability);

    ClaimModel.getByText(text).then((data) => {
        if (data.length > 0) {
            let returnClaim = claimFormatter(data[0]);
            res.status(200);<commit_msg>default probability to 50 for new claims<commit_after>var ClaimModel = require('../../models/claim-model');
var validator = require('validator');

function claimFormatter(claim){
    let returnClaim = {
        text: claim.text,
        probability: claim.probability,
        creationDate: claim.creationDate,
        _id: claim._id,
        _key: claim._key,
        arguments: claim.arguments
    }
    return returnClaim;
}
module.exports = function create(req, res){
    //console.log("TODO: CLAIMS.CREATE escape post data: ", JSON.stringify(req.body));

    let errors = [];

    if (!req.body.hasOwnProperty('text') || req.body.text == '') {
        errors.push({title:'Text is required'});
    } else if (!validator.isAlphanumeric(req.body.username + '')) {
        errors.push({title:'Claim can only have alphanumeric characters'});
    }

    if (!validator.isInt(req.body.probability + '', {min: 0, max: 100})) {
        //if this fails, just set it to 50
        req.body.probability = 50;
    }

    if (errors.length > 0) {
        res.status(400);
        res.json({ errors: errors });
        return;
    }

    var text = validator.escape(req.body.text);
    var probability = Number(req.body.probability);

    ClaimModel.getByText(text).then((data) => {
        if (data.length > 0) {
            let returnClaim = claimFormatter(data[0]);
            res.status(200);
{'fraction_non_alphanumeric': 0.1521964718090626, 'fraction_numerical': 0.012452438602559669, 'mean_word_length': 2.6793893129770994, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4110681', 'n_tokens_mistral': 898, 'n_tokens_neox': 859, 'n_words': 190}