From 846ebf23bd6bfef473ebda2b2a7e54fd67ed6225 Mon Sep 17 00:00:00 2001 From: nau Date: Sat, 10 Sep 2016 14:12:45 +0200 Subject: [PATCH] user connected comprovation system implemented --- controllers/userController.js | 23 +++++++++++++++++- intervalComprovations.js | 45 +++++++++++++++++++++++++++++++++++ models/userModel.js | 3 ++- server.js | 8 +++++-- webapp/controllers.js | 26 +++++++++++++++----- 5 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 intervalComprovations.js diff --git a/controllers/userController.js b/controllers/userController.js index 9a58dd0..7c1f137 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -53,7 +53,7 @@ exports.findUserByUsername = function(req, res) { // return the information including token as JSON //res.jsonp(user); user.password=""; - console.log(user); + //console.log(user); res.status(200).jsonp(user[0]); @@ -61,6 +61,27 @@ exports.findUserByUsername = function(req, res) { }); }; +exports.findLoggedUserByUsername = function(req, res) { + userModel.find({ + username: req.params.username + }, function(err, user) { + if (err) throw err; + if (!user) { + res.json({ success: false, message: 'no user found' }); + } else if (user) { + //console.log(user[0]); + //res.status(200).jsonp(user[0]); + user[0].connected= true; + user[0].lastConnection= new Date(); //adds the last connection time + user[0].save(function(err) { + if(err) return res.send(500, err.message); + + user[0].password=""; + res.status(200).jsonp(user[0]); + }); + } + }); +}; //POST - Insert a new User in the DB exports.addUser = function(req, res) { diff --git a/intervalComprovations.js b/intervalComprovations.js new file mode 100644 index 0000000..f1830b6 --- /dev/null +++ b/intervalComprovations.js @@ -0,0 +1,45 @@ + +var mongoose = require('mongoose'); +var moment = require('moment'); +var projectModel = mongoose.model('projectModel'); + +var userModel = mongoose.model('userModel'); + + +exports.lastConnectionUser = function() { + var lastIntervalDate = new Date(); + var intervalObject = setInterval(function () { + var actualDate= new Date(); + //console.log(actualDate); + lastIntervalDate.setMinutes(lastIntervalDate.getMinutes() - 4); + console.log(lastIntervalDate); + + userModel.find(function(err, users) { + if(err) res.send(500, err.message); + + + for(var i=0; i3) + { + $interval.cancel(intervalGetData); + $scope.currentInclude="login.html"; + } + }; $scope.getLoggedUser = function(){ //get logged user - $http.get(urlapi + 'users/byusername/' + $scope.user.username) + $http.get(urlapi + 'users/loggeduser/' + $scope.user.username) .success(function(data, status, headers,config){ console.log(data); $scope.user=data; }) .error(function(data, status, headers,config){ - console.log("server not responding, data error"); - toastr.error("server not responding"); - $scope.$broadcast('scroll.refreshComplete');//refresher stop + $scope.serverNotResponding(); }) .then(function(result){ users = result.data; @@ -37,10 +49,12 @@ angular.module('workApp', ['chart.js']) if(localStorage.getItem("owt_user")){ $scope.user=JSON.parse(localStorage.getItem("owt_user")); }else{ - $scope.currentInclude="login.html"; + + $scope.serverNotResponding(); } $scope.getLoggedUser(); + //getting users $http.get(urlapi + 'users') .success(function(data, status, headers,config){