Skip to content

Commit

Permalink
Update last event user-seen-at-home instead of creating one new every…
Browse files Browse the repository at this point in the history
… time
  • Loading branch information
Pierre-Gilles committed Sep 24, 2017
1 parent 45709dc commit 924b282
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 12 deletions.
16 changes: 16 additions & 0 deletions api/core/event/event.update.js
Original file line number Diff line number Diff line change
@@ -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];
});
}
3 changes: 2 additions & 1 deletion api/core/event/index.js
Original file line number Diff line number Diff line change
@@ -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');
module.exports.get = require('./event.get.js');
module.exports.update = require('./event.update.js');
13 changes: 13 additions & 0 deletions api/core/house/house.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.*,
(
Expand Down
33 changes: 26 additions & 7 deletions api/core/house/house.userSeen.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
};
7 changes: 7 additions & 0 deletions test/fixtures/event.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
26 changes: 26 additions & 0 deletions test/unit/api/core/event/event.update.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

});

});
11 changes: 7 additions & 4 deletions test/unit/api/core/house/house.userSeen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand Down

0 comments on commit 924b282

Please sign in to comment.