{"size": 1808, "ext": "js", "max_stars_repo_path": "src/models/user/UserValidator.js", "max_stars_repo_name": "mequieroir/app", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/models/user/UserValidator.js", "max_issues_repo_name": "mequieroir/app", "max_issues_repo_issues_event_min_datetime": "2016-11-28T21:09:17.000Z", "max_issues_repo_issues_event_max_datetime": "2016-11-29T23:49:03.000Z", "max_forks_repo_path": "src/models/user/UserValidator.js", "max_forks_repo_name": "mequieroir/app", "max_forks_repo_forks_event_min_datetime": "2016-12-17T00:35:22.000Z", "max_forks_repo_forks_event_max_datetime": "2016-12-17T00:35:22.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 33.4814814815, "max_line_length": 99, "alphanum_fraction": 0.6421460177}
	"use strict";

var DataAccess = require('../../repository/DataAccess');

function UserValidator() {
	this.personType = "person"
}


function checkUserPersonFields(data) {
	console.log('checkUserPersonFields');
	return (data.userName != '' && data.userName != null && data.userName.length >= 5 &&
		data.telephone != '' && data.telephone != null && data.telephone.length >= 5 &&
		data.address != '' && data.address != null && data.address.length >= 5 &&
		data.firstName != '' && data.firstName != null && data.firstName.length >= 5 &&
		data.lastName != '' && data.lastName != null && data.lastName.length >= 5 &&
		data.email != '' && data.email != null && data.email.length >= 10 &&
		data.age >= 5 );
}

function checkUserCompanyFields(data) {
	console.log('checkUserCompanyFields');
	return (data.userName != '' && data.userName != null && data.userName.length >= 5 &&
			data.telephone != '' && data.telephone != null && data.telephone.length >= 5 &&
			data.address != '' && data.address != null && data.address.length >= 5 &&
			data.companyName != '' && data.companyName != null && data.companyName.length >= 5 &&
			data.email != '' && data.email != null && data.email.length >= 5);
}

UserValidator.prototype.isValidFields = function(data) {
	if (!data || !data.type) return false;
	return (data.type == this.personType ? checkUserPersonFields(data) : checkUserCompanyFields(data))
}

UserValidator.prototype.checkUserExist = function(data) {
	return new Promise(
		function(resolve, reject) {   
			var path = "usersList/" + data.userName;
			// console.log('checkUserExist ',path);
			var dataAccess = new DataAccess();
	
			dataAccess.getData(path).then(
			    function(val) {
			    	var result = val != null; 
		    		resolve(result)
				}
			);
	
	});
};

module.exports = UserValidator;

