Skip to content

Commit

Permalink
gladys.house.checkUsersPresence
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Mar 7, 2017
1 parent b695229 commit 141c462
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
34 changes: 34 additions & 0 deletions api/core/house/house.checkUsersPresence.js
Original file line number Diff line number Diff line change
@@ -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});
});
});
});
};
23 changes: 23 additions & 0 deletions api/core/house/house.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
`
};
1 change: 1 addition & 0 deletions api/core/house/index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/eventtype.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
4 changes: 4 additions & 0 deletions test/fixtures/param.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
18 changes: 18 additions & 0 deletions test/unit/api/core/house/house.checkUsersPresence.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

});
});

0 comments on commit 141c462

Please sign in to comment.