<commit_before>var role = "nothing";
module.exports = function(app){
	var mysql = require('mysql');//it loads the mysql package
	var dbconfig = require('../../config/database');//it loads the configuration of the database
	var connConstant = require('../../config/ConnectConstant');//it loads a module that ensure continuous connection to the database
	var connection;//variable used for the connection to the database
	var ownerflag=false;//flag used to check if the user is an owner
	//============function to check if the user is owner of a specific project============
	function checkIfOwner(user,proj_name,callback){
		connection=connConstant.connection;
		//query to select all the projects a user owns
		var selectProjects = "SELECT `project_name` FROM " + dbconfig.projects_table +" JOIN " + dbconfig.owners_table + " ON "+ dbconfig.projects_table+
		".`project_id` = "+ dbconfig.owners_table + ".`project_id` "+" WHERE "+ dbconfig.owners_table+".`user_id` = '" + user.id + "'";
		//console.log(selectProjects)
		connection.query(selectProjects, function(err, rows){
			if (rows.length > 0) {
            	for(var i in rows){
        			if(rows[i].project_name==proj_name){
        				ownerflag=true;//if the user owns the project, the flag is set to true
        			}
            	}
	        }
	        callback(ownerflag);
		});
	}
	// =============================================================================
	// Add an collaborator in a project I own API===================================
	// =============================================================================
	app.get('/api/addCollabProjOwn', function(req, res) {
		res.setHeader('Content-Type', 'application/json');
		var scase_token= req.param('scase_token');//require your scase token in order to authenticate
		var scase_signature = req.param('scase_signature');//require your scase_signature in order to authenticate
		var proj_name= req.param('project_name');//require project name
		var github_name = req.param('github_name');//require the github name of the user you would like to add as a collaborator
		if(scase_token&&proj_name&&github_name){
			connection=connConstant.connection;//ensure that there is a connection to the DB
			//we select the user with the scase_token provided 
			var selectUsersQuery = "SELECT * FROM " + dbconfig.users_table + " WHERE scase_token = '" + scase_token + "'";//query to get all the info of the user with the provided scase token
			connection.query(selectUsersQuery, function(err, rows){
                if (rows.length > 0) {
                	var user = rows[0];
                	connection = connConstant.connection;
                	var ownerflag;
                	checkIfOwner(user,proj_name,function(ownerflag){//we gonna check if the user is owner of the project
                		//console.log(ownerflag);
						if(ownerflag==true){//if the user owns the project we proceed
							var checkIfUserExistsQuery = "SELECT id FROM " + dbconfig.users_table + " WHERE " +
								dbconfig.users_table +".github_name=" + "'" + github_name +"'";
							//we are goint to check if the user that we are asked to add as a collaborator exists
							connection=connConstant.connection;
							connection.query(checkIfUserExistsQuery, function(err,rows){
								if(rows.length>0){
									var user_id = rows[0].id
									//if the user exists, we get the project's id for the provided project name
									var getProjectId = "SELECT project_id FROM " + dbconfig.projects_table + " WHERE project_name="+ "'"
													+ proj_name +"'";
									connection=connConstant.connection;
									connection.query(getProjectId, function(err, rows){
										if(rows.length>0){//clause if the project exists
											//query to insert a collaborator
											var createCollabQuery = "INSERT INTO " +dbconfig.collaborators_table+ "(user_id,project_id)" +
												" VALUES (" + "'"+ user_id + "'"+ ",'"+rows[0].project_id+"')";//insert the user as an owner in the owner's table
											connection=connConstant.connection;
											connection.query(createCollabQuery, function(err, rows){
												//res.setHeader('Content-Type', 'application/json');
												var obj = '{'+ '"collaborator '+github_name + '": "added"}';
												if(err){
													var obj = '{'+ '"collaborator '+github_name + '": "not added"}';
												}
												var Jobj=JSON.parse(obj);
												res.send(Jobj);
											});
										}
										else{//the project does not exist we return a message
											//res.setHeader('Content-Type', 'application/json');
											var obj = '{'+ '"Project '+proj_name + '": "does not exist'+'"}';
											var Jobj=JSON.parse(obj);
											res.send(Jobj);
										}
									});
								}
								else{
									//res.setHeader('Content-Type', 'application/json');
									var obj = '{'+ '"user with '+github_name + '": "does not exist in S-Case"}';
									var Jobj=JSON.parse(obj);
									res.send(Jobj);
								}
							});
						}
						else if(ownerflag==false){
							//res.setHeader('Content-Type', 'application/json');
							var obj = '{'+ '"User with scase_token '+scase_token + '": "does not own project ' + proj_name +' or project does not exist"}';
							var Jobj=JSON.parse(obj);
							res.send(Jobj);
						}
					});
                }
                else {
                    res.setHeader('Content-Type', 'application/json');
					var obj = '{'+ '"User with scase_token  '+scase_token + '": "do not exist in S-Case"}';
					var Jobj=JSON.parse(obj);
					res.send(Jobj);
				}
            });
		}
		else{
			//res.setHeader('Content-Type', 'application/json');
			var obj = '{"message": "you miss some parameters"}';<commit_msg>added all<commit_after>var role = "nothing";
module.exports = function(app){
	var mysql = require('mysql');//it loads the mysql package
	var dbconfig = require('../../config/database');//it loads the configuration of the database
	var connConstant = require('../../config/ConnectConstant');//it loads a module that ensure continuous connection to the database
	var connection;//variable used for the connection to the database
	var ownerflag=false;//flag used to check if the user is an owner
	var jwt = require('jsonwebtoken');//
	//============function to check if the user is owner of a specific project============
	function checkIfOwner(user,proj_name,callback){
		connection=connConstant.connection;
		//query to select all the projects a user owns
		var selectProjects = "SELECT `project_name` FROM " + dbconfig.projects_table +" JOIN " + dbconfig.owners_table + " ON "+ dbconfig.projects_table+
		".`project_id` = "+ dbconfig.owners_table + ".`project_id` "+" WHERE "+ dbconfig.owners_table+".`user_id` = '" + user.id + "'";
		//console.log(selectProjects)
		connection.query(selectProjects, function(err, rows){
			if (rows.length > 0) {
            	for(var i in rows){
        			if(rows[i].project_name==proj_name){
        				ownerflag=true;//if the user owns the project, the flag is set to true
        			}
            	}
	        }
	        callback(ownerflag);
		});
	}
	// =============================================================================
	// Add an collaborator in a project I own API===================================
	// =============================================================================
	app.get('/api/addCollabProjOwn', function(req, res) {
		res.setHeader('Content-Type', 'application/json');
		var scase_token= req.param('scase_token');//require your scase token in order to authenticate
		var scase_signature = req.param('scase_signature');//require your scase_signature in order to authenticate
		var proj_name= req.param('project_name');//require project name
		var github_name = req.param('github_name');//require the github name of the user you would like to add as a collaborator
		if(scase_token&&proj_name&&github_name){
			connection=connConstant.connection;//ensure that there is a connection to the DB
			//we select the user with the scase_token provided 
			var selectUsersQuery = "SELECT * FROM " + dbconfig.users_table + " WHERE scase_token = '" + scase_token + "'";//query to get all the info of the user with the provided scase token
			connection.query(selectUsersQuery, function(err, rows){
                if (rows.length > 0) {
                	var produced_signature=jwt.sign({ scasetoken : scase_token},rows[0].scaseSecret);
                	if(scase_signature==produced_signature){//we check if the produced signature is the same with the one provided
                		var user = rows[0];
                		connection = connConstant.connection;
                		var ownerflag;
	                	checkIfOwner(user,proj_name,function(ownerflag){//we gonna check if the user is owner of the project
	                		//console.log(ownerflag);
							if(ownerflag==true){//if the user owns the project we proceed
								var checkIfUserExistsQuery = "SELECT id FROM " + dbconfig.users_table + " WHERE " +
									dbconfig.users_table +".github_name=" + "'" + github_name +"'";
								//we are goint to check if the user that we are asked to add as a collaborator exists
								connection=connConstant.connection;
								connection.query(checkIfUserExistsQuery, function(err,rows){
									if(rows.length>0){
										var user_id = rows[0].id
										//if the user exists, we get the project's id for the provided project name
										var getProjectId = "SELECT project_id FROM " + dbconfig.projects_table + " WHERE project_name="+ "'"
														+ proj_name +"'";
										connection=connConstant.connection;
										connection.query(getProjectId, function(err, rows){
											if(rows.length>0){//clause if the project exists
												//query to insert a collaborator
												var createCollabQuery = "INSERT INTO " +dbconfig.collaborators_table+ "(user_id,project_id)" +
													" VALUES (" + "'"+ user_id + "'"+ ",'"+rows[0].project_id+"')";//insert the user as an owner in the owner's table
												connection=connConstant.connection;
												connection.query(createCollabQuery, function(err, rows){
													//res.setHeader('Content-Type', 'application/json');
													var obj = '{'+ '"collaborator '+github_name + '": "added"}';
													if(err){
														var obj = '{'+ '"collaborator '+github_name + '": "not added"}';
													}
													var Jobj=JSON.parse(obj);
													res.send(Jobj);
												});
											}
											else{//the project does not exist we return a message
												//res.setHeader('Content-Type', 'application/json');
												var obj = '{'+ '"Project '+proj_name + '": "does not exist'+'"}';
												var Jobj=JSON.parse(obj);
												res.send(Jobj);
											}
										});
									}
									else{
										//res.setHeader('Content-Type', 'application/json');
										var obj = '{'+ '"user with '+github_name + '": "does not exist in S-Case"}';
										var Jobj=JSON.parse(obj);
										res.send(Jobj);
									}
								});
							}
							else if(ownerflag==false){
								//res.setHeader('Content-Type', 'application/json');
								var obj = '{'+ '"User with scase_token '+scase_token + '": "does not own project ' + proj_name +' or project does not exist"}';
								var Jobj=JSON.parse(obj);
								res.send(Jobj);
							}
						});
                	}
                	else{
                		 res.setHeader('Content-Type', 'application/json');
						var obj = '{'+ '"User with scase_signature: '+scase_signature + '": "does not exist in S-Case"}';
						var Jobj=JSON.parse(obj);
						res.send(Jobj);
                	}
                }
                else {
                    res.setHeader('Content-Type', 'application/json');
					var obj = '{'+ '"User with scase_token: '+scase_token + '": "does not exist in S-Case"}';
					var Jobj=JSON.parse(obj);
					res.send(Jobj);
				}
            });
		}
		else{
			//res.setHeader('Content-Type', 'application/json');
			var obj = '{"message": "you miss some parameters"}';
{'fraction_non_alphanumeric': 0.16211066931742876, 'fraction_numerical': 0.001242544731610338, 'mean_word_length': 2.6729540614542135, 'pattern_counts': {'":': 15, '<': 3, '<?xml version=': 0, '>': 11, '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': '5261225', 'n_tokens_mistral': 4417, 'n_tokens_neox': 3356, 'n_words': 929}