-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update last event user-seen-at-home instead of creating one new every…
… time
- Loading branch information
1 parent
45709dc
commit 924b282
Showing
7 changed files
with
97 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters