Browse Source

user connected comprovation system implemented

master
nau 8 years ago
parent
commit
846ebf23bd
5 changed files with 95 additions and 10 deletions
  1. +22
    -1
      controllers/userController.js
  2. +45
    -0
      intervalComprovations.js
  3. +2
    -1
      models/userModel.js
  4. +6
    -2
      server.js
  5. +20
    -6
      webapp/controllers.js

+ 22
- 1
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) {

+ 45
- 0
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; i<users.length; i++)
{
if(users[i].connected==true)
{
if(users[i].lastConnection<=lastIntervalDate.setMinutes(lastIntervalDate.getMinutes() - 4))
{
if(users[i].connected==true)
{
console.log("user disconnected: " + users[i].username);
users[i].connected= false;
users[i].save(function(err) {
if(err) return res.send(500, err.message);
//console.log("user: " + users[i].username + ", connected=false");
});
}
}
}
}
});
lastIntervalDate= new Date();
}, 60000);//every minute
};

+ 2
- 1
models/userModel.js

@ -12,6 +12,7 @@ var userSchema = new Schema({
web: { type: String },
projects: { type: String },
connected: { type: Boolean },
working: { type: Boolean }
working: { type: Boolean },
lastConnection: { type: Date }
})
module.exports = mongoose.model('userModel', userSchema);

+ 6
- 2
server.js

@ -32,6 +32,10 @@ var userCtrl = require('./controllers/userController');
var projectMdl = require('./models/projectModel')(app, mongoose);
var projectCtrl = require('./controllers/projectController');
var intervalComprovations = require('./intervalComprovations');
intervalComprovations.lastConnectionUser();
/*// Example Route
var router = express.Router();
router.get('/', function(req, res) {
@ -100,8 +104,8 @@ apiRoutes.route('/users')
apiRoutes.route('/users/:id')
.get(userCtrl.findById);
apiRoutes.route('/users/byusername/:username')
.get(userCtrl.findUserByUsername);
apiRoutes.route('/users/loggeduser/:username')
.get(userCtrl.findLoggedUserByUsername);
apiRoutes.route('/projects/user/:username')
.get(projectCtrl.findAllProjectsFromUsername);

+ 20
- 6
webapp/controllers.js

@ -10,18 +10,30 @@ angular.module('workApp', ['chart.js'])
$scope.users={};
$scope.projects={};
$scope.currentInclude='login.html';
var refreshTime=20000;
var refreshTime=10000;//every 10 seconds
var errorsGettingData=0;
$scope.serverNotResponding = function(){
console.log("server not responding, data error");
toastr.error("server not responding");
$scope.$broadcast('scroll.refreshComplete');//refresher stop
errorsGettingData++;
console.log("errorsGettingData: " + errorsGettingData);
if(errorsGettingData>3)
{
$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){

Loading…
Cancel
Save