def isValidSessionId(sessionid, userid):
	if userid is None or sessionid is None:
		return False
	db = getClient()
	cursor = db['hrservice']
	docs = cursor.session.find_one({"sessionid":sessionid, "userid":ObjectId(userid)})
	if docs is None:
		return False;
	return True
def getSession(sessionid):
	db = getClient()
	cursor = db['hrservice']
	docs = cursor.session.find_one({"sessionid":sessionid})
	if docs is not None:
		response = make_response(jsonify(session_valid = True))
	else:
		response = make_response(jsonify(session_valid = False, message = "invalid session id"))
	response.headers['Access-Control-Allow-Origin'] = '*'
	response.status = "200"
	return response
def logout():
	sessionid = request.headers.get("sessionid")
	userid = request.headers.get("userid")
	db = getClient()
	cursor = db['hrservice']
	doc  = cursor.session.delete_one({"sessionid":sessionid})
	expire_date = datetime.datetime.now()
	response = make_response(jsonify(success = "true"))
	response.set_cookie("sessionid", "", expires=expire_date)
	response.set_cookie("userid", "", expires=expire_date)
	return response
def createUser():
	db = getClient()
	bodyparam = request.data
	bodyparam = json.loads(bodyparam)
	isValid = validateParams(bodyparam)
	response = {}
	if not isValid:
		response["message"] = "Invalid body param"
		response["code"] = httperrors.BAD_REQUEST_ERROR
		return jsonify(success = False, error = response["error"])
	hashedPassword  = hashlib.sha224(bodyparam["password"].encode('utf-8')).hexdigest()
	cursor = db['hrservice']
	doc  = cursor.users.find_one({"email":bodyparam["email"]})
	if doc is not None:
		response["message"] = "user with this email already exists"
		response["code"] = httperrors.BAD_REQUEST_ERROR
		return jsonify(success = False, error = response)
	result = cursor.users.insert_one({"firstname":bodyparam["firstname"], "lastname":bodyparam["lastname"], "email":bodyparam["email"], "password":bodyparam["password"]})
	if result is not None:
		return jsonify(success = True)
	return jsonify(success = False)
def login():
	db = getClient()
	cursor = db['hrservice']
	bodyparam = request.data
	bodyparam = json.loads(bodyparam)
	hashedPassword  = hashlib.sha224(bodyparam["password"].encode('utf-8')).hexdigest()
	docs = cursor.users.find_one({"email":bodyparam["email"], "password":bodyparam["password"]})
	if docs is None:
		response = make_response(jsonify(success = False, message = "Invalid email or passord"))
		response.headers['Access-Control-Allow-Origin'] = '*'
		response.status = httperrors.UNAUTHORIZED_ERROR
		return response
	else:
		sessionid = str(uuid.uuid4())
		expire_date = datetime.datetime.now()
		expire_date = expire_date + datetime.timedelta(days=10)
		createSession(docs["_id"], sessionid)
		response = make_response(jsonify(success = True, sessionid = sessionid))
		response.set_cookie("sessionid", sessionid, expires=expire_date)
		response.set_cookie("userid", str(docs["_id"]), expires=expire_date)
		response.headers['Access-Control-Allow-Origin'] = '*'
		return response
def getUserDetails(userid):
	db = getClient()
	cursor = db['hrservice']
	docs = cursor.users.find_one({"_id":ObjectId(userid)})
	return docs
def deleteUser(userid):
	db = getClient()
	cursor = db['hrservice']
	result = cursor.users.delete_one({"userid":ObjectId(userid)})
def createSession(userid, sessionid):
	db = getClient()
	cursor = db['hrservice']
	result = cursor.session.insert_one({"sessionid":sessionid, "userid":userid})