From 924b282142abff40e6dbc3d19fc618ba862cb6e2 Mon Sep 17 00:00:00 2001 From: Pierre-Gilles Leymarie Date: Sun, 24 Sep 2017 12:32:35 +0200 Subject: [PATCH] Update last event user-seen-at-home instead of creating one new every time --- api/core/event/event.update.js | 16 +++++++++ api/core/event/index.js | 3 +- api/core/house/house.queries.js | 13 ++++++++ api/core/house/house.userSeen.js | 33 +++++++++++++++---- test/fixtures/event.json | 7 ++++ test/unit/api/core/event/event.update.test.js | 26 +++++++++++++++ .../api/core/house/house.userSeen.test.js | 11 ++++--- 7 files changed, 97 insertions(+), 12 deletions(-) create mode 100644 api/core/event/event.update.js create mode 100644 test/unit/api/core/event/event.update.test.js diff --git a/api/core/event/event.update.js b/api/core/event/event.update.js new file mode 100644 index 0000000000..da36156552 --- /dev/null +++ b/api/core/event/event.update.js @@ -0,0 +1,16 @@ +const Promise = require('bluebird'); + +module.exports = function (event){ + var id = event.id; + delete event.id; + + return Event.update({id}, event) + .then(function(events) { + + if (events.length === 0) { + return Promise.reject(new Error('Event not found')); + } + + return events[0]; + }); +} \ No newline at end of file diff --git a/api/core/event/index.js b/api/core/event/index.js index 7f666db999..991bb2ac21 100644 --- a/api/core/event/index.js +++ b/api/core/event/index.js @@ -1,4 +1,5 @@ module.exports.create = require('./event.create.js'); module.exports.command = require('./event.command.js'); -module.exports.get = require('./event.get.js'); \ No newline at end of file +module.exports.get = require('./event.get.js'); +module.exports.update = require('./event.update.js'); \ No newline at end of file diff --git a/api/core/house/house.queries.js b/api/core/house/house.queries.js index 62fc1b9ae4..5844e3bef5 100644 --- a/api/core/house/house.queries.js +++ b/api/core/house/house.queries.js @@ -5,6 +5,19 @@ module.exports = { get: `SELECT * FROM house LIMIT ? OFFSET ?;`, getById: 'SELECT * FROM house WHERE id = ?;', getAll: `SELECT * FROM house;`, + getLastEventHouseUser: ` + SELECT event.*, eventtype.code + FROM event + JOIN eventtype ON event.eventtype = eventtype.id + WHERE ( + eventtype.code = 'back-at-home' + OR eventtype.code = 'left-home' + OR eventtype.code = 'user-seen-at-home' + ) + AND event.user = ? AND event.house = ? + ORDER BY event.datetime DESC + LIMIT 1; + `, getUsers: ` SELECT user.*, ( diff --git a/api/core/house/house.userSeen.js b/api/core/house/house.userSeen.js index 931505e3cc..ef14f59bd6 100644 --- a/api/core/house/house.userSeen.js +++ b/api/core/house/house.userSeen.js @@ -1,14 +1,33 @@ +var queries = require('./house.queries.js'); module.exports = function userSeen(options) { // see if user is at home - return gladys.house.isUserAtHome(options) - .then((atHome) => { + return gladys.utils.sql(queries.getLastEventHouseUser, [options.user, options.house]) + .then((rows) => { - // if yes, save event user seen at home - if(atHome) return gladys.event.create({code: 'user-seen-at-home', user: options.user, house: options.house}); - - // if no, save event "back-at-home" - return gladys.event.create({code: 'back-at-home', user: options.user, house: options.house}); + // if user has never had events + if(rows.length === 0) { + return gladys.event.create({code: 'user-seen-at-home', user: options.user, house: options.house}); + } + + // if user was not at home + else if(rows[0].code === 'left-home') { + return gladys.event.create({code: 'back-at-home', user: options.user, house: options.house}); + } + + // if user is at home, but has never been seen at home + else if(rows[0].code === 'back-at-home') { + return gladys.event.create({code: 'user-seen-at-home', user: options.user, house: options.house}); + } + + // else, user has been seen at home so update + else { + var newEvent = { + id: rows[0].id, + datetime: new Date() + }; + return gladys.event.update(newEvent); + } }); }; \ No newline at end of file diff --git a/test/fixtures/event.json b/test/fixtures/event.json index faf71498e1..10033eebbc 100644 --- a/test/fixtures/event.json +++ b/test/fixtures/event.json @@ -18,5 +18,12 @@ "datetime": "2015-05-12 18:00:00", "user": 1, "house": 1 + }, + { + "id": 4, + "eventtype": 9, + "datetime": "2015-05-12 18:00:00", + "user": 2, + "house": 1 } ] \ No newline at end of file diff --git a/test/unit/api/core/event/event.update.test.js b/test/unit/api/core/event/event.update.test.js new file mode 100644 index 0000000000..ee40ff6371 --- /dev/null +++ b/test/unit/api/core/event/event.update.test.js @@ -0,0 +1,26 @@ +var should = require('should'); +var validateEvent = require('../../validator/eventValidator.js'); + +describe('Event', function() { + + describe('update', function() { + + it('should return event updated', function (done) { + + var event = { + datetime: '2014-11-03 19:43:37', + id: 1, + user: 1 + }; + + gladys.event.update(event) + .then(function(result){ + validateEvent(result); + result.should.have.property('datetime', event.datetime); + done(); + }).catch(done); + }); + + }); + +}); \ No newline at end of file diff --git a/test/unit/api/core/house/house.userSeen.test.js b/test/unit/api/core/house/house.userSeen.test.js index 80c2034dd1..87187a8f0d 100644 --- a/test/unit/api/core/house/house.userSeen.test.js +++ b/test/unit/api/core/house/house.userSeen.test.js @@ -14,8 +14,9 @@ describe('House', function() { gladys.house.userSeen(options) .then(function(result){ - should.exist(result); - + result.should.have.property('user', 2); + result.should.have.property('house', 1); + result.should.have.property('eventtype', 7); done(); }).catch(done); }); @@ -29,8 +30,10 @@ describe('House', function() { gladys.house.userSeen(options) .then(function(result){ - should.exist(result); - + result.should.have.property('user', 1); + result.should.have.property('house', 1); + result.should.have.property('eventtype', 8); + done(); }).catch(done); });