<commit_before>
/*
 * GET users listing.
 */

var db = require('../mongodb');

exports.list = function(req, res){
  res.send("respond with a resource");
};

exports.register = function(req, res)
{
	var regid = req.body.regid;
	
	if (!regid || regid.length == 0)
	{
		res.status(400).send("No registration ID!");
		return;
	}
	
	// TODO: check if it exists already, otherwise this will give an error!
	db.devices.insert({"_id": regid}, function(err, saved) {
		if (err || !saved)
		{
			res.status(500).send("An error occurred: " + err);
		}
		else
		{
			res.send(saved);
		}
	});
};

exports.deregister = function(req, res)
{
	var regid = req.body.regid;
	
	if (!regid || regid.length == 0)
	{
		res.status(400).send("No registration ID!");
		return ;
	}
	
	// Remove device regid from mongo:
	db.devices.remove({"_id": regid}, function(err) {<commit_msg>Fix bug if same ID re-registers (appears to happen if app version num changes)<commit_after>
/*
 * GET users listing.
 */

var db = require('../mongodb');

exports.list = function(req, res){
  res.send("respond with a resource");
};

exports.register = function(req, res)
{
	var regid = req.body.regid;
	
	if (!regid || regid.length == 0)
	{
		res.status(400).send("No registration ID!");
		return;
	}
	
	// check if it exists already, otherwise this will give an error!
	db.devices.find({'_id': regid}, function(err, list){
		if (list.length == 0) {
			db.devices.insert({"_id": regid}, function(err, saved) {
				if (err || !saved)
				{
					res.status(500).send("An error occurred: " + err);
				}
				else
				{
					res.send(saved);
				}
			});
		}
		else
		{
			res.send(list[0]);
		}
	})
};

exports.deregister = function(req, res)
{
	var regid = req.body.regid;
	
	if (!regid || regid.length == 0)
	{
		res.status(400).send("No registration ID!");
		return ;
	}
	
	// Remove device regid from mongo:
	db.devices.remove({"_id": regid}, function(err) {
{'fraction_non_alphanumeric': 0.17954070981210857, 'fraction_numerical': 0.012526096033402923, 'mean_word_length': 3.2039473684210527, 'pattern_counts': {'":': 4, '<': 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': '5725150', 'n_tokens_mistral': 788, 'n_tokens_neox': 690, 'n_words': 171}