diff --git a/api/core/house/house.checkUsersPresence.js b/api/core/house/house.checkUsersPresence.js new file mode 100644 index 0000000000..2751dddea7 --- /dev/null +++ b/api/core/house/house.checkUsersPresence.js @@ -0,0 +1,34 @@ +const queries = require('./house.queries.js'); +const Promise = require('bluebird'); + +module.exports = function checkUsersPresence(){ + sails.log.debug(`House : checkUsersPresence`); + + // first, get the time a user is considered not at home anymore + return gladys.param.getValue('USER_TIME_BEFORE_CONSIDERING_LEFT_HOME') + .then((timeBeforeLeftInMinute) => { + + // get all houses + return [timeBeforeLeftInMinute, gladys.house.getAll()]; + }) + .spread((timeBeforeLeftInMinute, houses) => { + + return [houses, + Promise.map(houses, function(house) { + return gladys.utils.sql(queries.getUserAtHomeAndNotSeenSince, [house.id, house.id, timeBeforeLeftInMinute]); + }) + ]; + }) + .spread((houses, usersArray) => { + + // foreach house + return Promise.map(houses, function(house, index) { + + // foreach user in this house and not put as + return Promise.map(usersArray[index], function(user) { + sails.log.debug(`House : checkUserPresence : Putting user ${user.id} as left house : ${house.id}`); + return gladys.event.create({code: 'left-home', user: user.id, house: house.id}); + }); + }); + }); +}; \ No newline at end of file diff --git a/api/core/house/house.queries.js b/api/core/house/house.queries.js index b2f40e4c71..d0e24980ad 100644 --- a/api/core/house/house.queries.js +++ b/api/core/house/house.queries.js @@ -33,5 +33,28 @@ module.exports = { FROM user WHERE user.id = ? HAVING lastHouseEvent = 'back-at-home' + `, + getUserAtHomeAndNotSeenSince: + ` + SELECT user.id, MAX(event.datetime) as datetime + FROM user + JOIN event ON event.user = user.id + JOIN eventtype ON event.eventtype = eventtype.id + WHERE ( eventtype.code = 'back-at-home' OR eventtype.code = 'user-seen-at-home' ) + AND house = ? + AND user.id IN ( + SELECT user.id + FROM user + WHERE ( + SELECT eventtype.code + FROM event + JOIN eventtype ON event.eventtype = eventtype.id + WHERE + ( eventtype.code = 'back-at-home' OR eventtype.code = 'left-home' ) + AND user = user.id + AND house = ? + ORDER BY datetime DESC LIMIT 1 ) = 'back-at-home' + ) + HAVING datetime < DATE_SUB(NOW(), INTERVAL ? MINUTE) ` }; \ No newline at end of file diff --git a/api/core/house/index.js b/api/core/house/index.js index 8ed11b448c..d0bb9e3173 100644 --- a/api/core/house/index.js +++ b/api/core/house/index.js @@ -1,4 +1,5 @@ +module.exports.checkUsersPresence = require('./house.checkUsersPresence.js'); module.exports.create = require('./house.create.js'); module.exports.delete = require('./house.delete.js'); module.exports.get = require('./house.get.js'); diff --git a/test/fixtures/eventtype.json b/test/fixtures/eventtype.json index d9ab6d9de0..239b9e6cdb 100644 --- a/test/fixtures/eventtype.json +++ b/test/fixtures/eventtype.json @@ -62,5 +62,13 @@ "service": "house", "faIcon": "fa fa-desktop", "iconColor": "bg-black" + }, + { + "id": 9, + "code": "left-home", + "name": "User has left home !", + "service": "house", + "faIcon": "fa fa-desktop", + "iconColor": "bg-black" } ] \ No newline at end of file diff --git a/test/fixtures/param.json b/test/fixtures/param.json index eeb1882da1..dd48c5f9ab 100644 --- a/test/fixtures/param.json +++ b/test/fixtures/param.json @@ -2,5 +2,9 @@ { "name": "quote_of_the_day", "value": "Design is not just what it looks like and feels like. Design is how it works." + }, + { + "name": "USER_TIME_BEFORE_CONSIDERING_LEFT_HOME", + "value": "20" } ] \ No newline at end of file diff --git a/test/unit/api/core/house/house.checkUsersPresence.test.js b/test/unit/api/core/house/house.checkUsersPresence.test.js new file mode 100644 index 0000000000..4a468447eb --- /dev/null +++ b/test/unit/api/core/house/house.checkUsersPresence.test.js @@ -0,0 +1,18 @@ +var should = require('should'); +var validateHouse = require('../../validator/houseValidator.js'); + +describe('House', function() { + + describe('checkUserPresence', function() { + + it('should check if user is present and not seen since a given time', function (done) { + + gladys.house.checkUsersPresence() + .then(function(result){ + + done(); + }).catch(done); + }); + + }); +}); \ No newline at end of file