<commit_before>var express = require('express');
var userRouter = express.Router();
var queries = require('./userQueries.js');

/*set up id and parameter check for router. Broken out to keep this file short-ish*/
require('./transactionCheck.js')(userRouter);

/*set up routes*/
userRouter.post('/account', function(req, res){ //get user's account
    queries.getAccount(req.body.id, function(err, data){
        if(err){
            res.send(500, 'Databse read error: ', err);
        }else if (data.length){
            res.send(data[0]);
        }else{
            res.send(400, 'account id not found in database');
        }
    });
});

userRouter.post('/buy', function(req, res){
    queries.buy(req.body.currency, req.body.quantity, req.body.id, function(err, data){
        if(!err){
            if(!data){
                queries.getAccount(req.body.id, function(err, data){ //if success, send snapshot of account
                    if(!err){
                        res.send(200, data[0]);
                    }
                });
            }else{
                res.send(405, 'Insufficient funds: $' + data[0].usd);
            }
        }else{
            res.send(500, err);
        }
    });
});

userRouter.post('/sell', function(req, res){
    queries.sell(req.body.currency, req.body.quantity, req.body.id, function(err, data){
        if(!err){
            if(!data){
                queries.getAccount(req.body.id, function(err, data){ //if success, send snapshot of account
                    if(!err){
                        res.send(200, data[0]);
                    }
                });
            }else{
                res.send(405, 'Insufficient currency: $' + data[0][currency]);
            }
        }else{
            res.send(500, err);
        }
    });
});

userRouter.delete('/deleteaccount', function(req, res){
    queries.deleteUser(req.body.id, function(err, data){
        if(!err){
            res.send(200, 'User '+ req.body.id +' permanently deleted');
        }else{
            res.send(500, err);
        }
    });
});

userRouter.post('/ledger', function(req, res){ //get history of user transactions
    queries.getHistory(req.body.id, function(err, data){
        if(!err){
            if(!data.length){
                res.send(400, 'account id not found in database');
            }else{<commit_msg>fixes typo in error message<commit_after>var express = require('express');
var userRouter = express.Router();
var queries = require('./userQueries.js');

/*set up id and parameter check for router. Broken out to keep this file short-ish*/
require('./transactionCheck.js')(userRouter);

/*set up routes*/
userRouter.post('/account', function(req, res){ //get user's account
    queries.getAccount(req.body.id, function(err, data){
        if(err){
            res.send(500, 'Databse read error: ', err);
        }else if (data.length){
            res.send(data[0]);
        }else{
            res.send(400, 'account id not found in database');
        }
    });
});

userRouter.post('/buy', function(req, res){
    queries.buy(req.body.currency, req.body.quantity, req.body.id, function(err, data){
        if(!err){
            if(!data){
                queries.getAccount(req.body.id, function(err, data){ //if success, send snapshot of account
                    if(!err){
                        res.send(200, data[0]);
                    }
                });
            }else{
                res.send(405, 'Insufficient funds: $' + data[0].usd);
            }
        }else{
            res.send(500, err);
        }
    });
});

userRouter.post('/sell', function(req, res){
    queries.sell(req.body.currency, req.body.quantity, req.body.id, function(err, data){
        if(!err){
            if(!data){
                queries.getAccount(req.body.id, function(err, data){ //if success, send snapshot of account
                    if(!err){
                        res.send(200, data[0]);
                    }
                });
            }else{
                res.send(405, 'Insufficient currency: $' + data[0][req.body.currency]);
            }
        }else{
            res.send(500, err);
        }
    });
});

userRouter.delete('/deleteaccount', function(req, res){
    queries.deleteUser(req.body.id, function(err, data){
        if(!err){
            res.send(200, 'User '+ req.body.id +' permanently deleted');
        }else{
            res.send(500, err);
        }
    });
});

userRouter.post('/ledger', function(req, res){ //get history of user transactions
    queries.getHistory(req.body.id, function(err, data){
        if(!err){
            if(!data.length){
                res.send(400, 'account id not found in database');
            }else{
{'fraction_non_alphanumeric': 0.1582612365477949, 'fraction_numerical': 0.016037138636843216, 'mean_word_length': 1.8468468468468469, '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': '4513037', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1370, 'n_words': 305}