<commit_before>var mongoose = require('../config/database.js');

var Schema = mongoose.Schema;

var userSchema = new Schema({
	fbId: {type:String, require:true, unique:true},
	modules: [{code: String}]
});

// User defined functions
userSchema.statics.addUser = function(Id, cb) {
	newUser = new this();
	newUser.fbId = Id;
	newUser.save(function (err,user) {
		cb(err, user);
	})
}

userSchema.statics.deleteUser = function(Id, cb) {
	this.findOne({fbId: Id}, function(err, user) {
		if (err) 
			cb(err);
		else
			user.remove(function(err) {
			cb(err);
		})
	})
}

userSchema.statics.addModule = function(Id, module, cb) {
	this.findOne({fbId: Id}, function(err, user) {
		if (err) return console.error(err);
		var ifExist = 0; 
		for (i=0; i<user.modules.length; i++) {
			if (user.modules[i].code == module) {
				ifExist = 1;
			}
		}
		if (!ifExist) {
			user.modules.push({code:module});
			user.save(function(err) {
				cb(err, user);
			});
		}
		else 
			err = new Error("Module already exists");
			cb(err, user);
	})
}

userSchema.statics.deleteModule = function(Id, module, cb) {
	this.findOne({fbId: Id}, function(err, user) {
		if (err) return console.error(err);
		var ifExist = 0; 
		for (i=0; i<user.modules.length; i++) {
			if (user.modules[i].code == module) {
				ifExist = 1;
				user.modules.splice(i, 1);
			}
		}
		if (ifExist) {
			user.save(function(err) {
				cb(err, user);
			});
		}
		else 
			err = new Error("Module does not exist");
			cb(err, user);
	})
}

// Make the schema a model to export
var User = mongoose.model('User', userSchema);

module.exports = User;<commit_msg>fix deleteuser bugs<commit_after>var mongoose = require('../config/database.js');

var Schema = mongoose.Schema;

var userSchema = new Schema({
	fbId: {type:String, require:true, unique:true},
	modules: [{code: String}]
});

// User defined functions
userSchema.statics.addUser = function(Id, cb) {
	newUser = new this();
	newUser.fbId = Id;
	newUser.save(function (err,user) {
		cb(err, user);
	})
}

userSchema.statics.deleteUser = function(Id, cb) {
	this.findOne({fbId: Id}, function(err, user) {
		if (err) 
			cb(err);
		else {
			var ifExist = 0; 
			for (i=0; i<user.modules.length; i++) {
				if (user.modules[i].code == module) {
					ifExist = 1;
				}
			}
			if (!ifExist) {
				user.remove(function(err) {
					cb(err);
				});
			}
			else {
				err = new Error("No such user");
				cb(err, user);
			}
		}

	})
}

userSchema.statics.addModule = function(Id, module, cb) {
	this.findOne({fbId: Id}, function(err, user) {
		if (err) return console.error(err);
		var ifExist = 0; 
		for (i=0; i<user.modules.length; i++) {
			if (user.modules[i].code == module) {
				ifExist = 1;
			}
		}
		if (!ifExist) {
			user.modules.push({code:module});
			user.save(function(err) {
				cb(err, user);
			});
		}
		else 
			err = new Error("Module already exists");
		cb(err, user);
	})
}

userSchema.statics.deleteModule = function(Id, module, cb) {
	this.findOne({fbId: Id}, function(err, user) {
		if (err) return console.error(err);
		var ifExist = 0; 
		for (i=0; i<user.modules.length; i++) {
			if (user.modules[i].code == module) {
				ifExist = 1;
				user.modules.splice(i, 1);
			}
		}
		if (ifExist) {
			user.save(function(err) {
				cb(err, user);
			});
		}
		else 
			err = new Error("Module does not exist");
		cb(err, user);
	})
}

// Make the schema a model to export
var User = mongoose.model('User', userSchema);

module.exports = User;
{'fraction_non_alphanumeric': 0.1789928057553957, 'fraction_numerical': 0.004892086330935252, 'mean_word_length': 3.2598039215686274, 'pattern_counts': {'":': 0, '<': 8, '<?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': '7610590', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1338, 'n_words': 295}