<commit_before>var utils = require('./libs/utils');
var dbSettings = require('./libs/mongodb_settings');

exports.save = function(req, res) {
    if(req.body) {
        var timelogCollection = dbSettings.timelogCollection();
        var batch = timelogCollection.initializeUnorderedBulkOp({useLegacyOps: true});
        
        var ids = [];
        var timelogs = req.body.timelog;
        timelogs.forEach(function(log) {
            if(log._id) {
                batch.find({_id: log._id}).upsert().replaceOne(log);
            } else {
                batch.insert(log);
            }
            ids.push(log._id);
        });

        batch.execute(function(err, result) {
            findAllByIds(ids, function(err, timelogs) {
                returnTimelogArray(err, res, timelogs);
            });
        });
    }
};

exports.getByDates = function(req, res) {
    var start = utils.getStartDate(req, res);
    var end = utils.getEndDate(req, res);
    var userId = utils.getUserId(req, res);

    if(start && end && userId) {
        var timelogCollection = dbSettings.timelogCollection();
        var query = {
            userId : userId,
            date : {$gte: start,
                    $lte: end}
        };

        timelogCollection.find(query, {'sort': 'date'}).toArray(function(err, timelogs){
            returnTimelogArray(err, res, timelogs);
        });
    }
};

//private part
function findAllByIds(ids, callback) {
    var timelogCollection = dbSettings.timelogCollection();
    timelogCollection.find({_id:{ $in: ids}}, {"sort": "date"}).toArray(function(err, timelogs) {
        callback(err, timelogs);
    });
}

function returnTimelogArray(err, res, timelogs) {
    if(err) {
        res.status(500).json(err);
        return;
    }
    res.json({timelog: timelogs});
}<commit_msg>Fix user ID parsing for timelog<commit_after>var utils = require('./libs/utils');
var dbSettings = require('./libs/mongodb_settings');
var ObjectID = require('mongodb').ObjectID;

exports.save = function(req, res) {
    if(req.body) {
        var timelogCollection = dbSettings.timelogCollection();
        var batch = timelogCollection.initializeUnorderedBulkOp({useLegacyOps: true});
        
        var ids = [];
        var timelogs = req.body.timelog;
        timelogs.forEach(function(log) {
            if(log._id) {
                batch.find({_id: log._id}).upsert().replaceOne(log);
            } else {
                batch.insert(log);
            }
            ids.push(log._id);
        });

        batch.execute(function(err, result) {
            findAllByIds(ids, function(err, timelogs) {
                returnTimelogArray(err, res, timelogs);
            });
        });
    }
};

exports.getByDates = function(req, res) {
    var start = utils.getStartDate(req, res);
    var end = utils.getEndDate(req, res);
    var userId = utils.getUserId(req, res);

    if(start && end && userId) {
        if(ObjectID.isValid(userId)) {
            var timelogCollection = dbSettings.timelogCollection();
            var query = {
                userId : new ObjectID(userId),
                date : {$gte: start,
                        $lte: end}
            };

            timelogCollection.find(query, {'sort': 'date'}).toArray(function(err, timelogs){
                returnTimelogArray(err, res, timelogs);
            });
        } else {
            res.status(500).json({error: 'Incorrect user id format!'});
        }
    }
};

//private part
function findAllByIds(ids, callback) {
    var timelogCollection = dbSettings.timelogCollection();
    timelogCollection.find({_id:{ $in: ids}}, {"sort": "date"}).toArray(function(err, timelogs) {
        callback(err, timelogs);
    });
}

function returnTimelogArray(err, res, timelogs) {
    if(err) {
        res.status(500).json(err);
        return;
    }
    res.json({timelog: timelogs});
}
{'fraction_non_alphanumeric': 0.1428939237899073, 'fraction_numerical': 0.002317198764160659, 'mean_word_length': 2.2160596026490067, 'pattern_counts': {'":': 2, '<': 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': '3053095', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1207, 'n_words': 214}