-
-
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.
Browse files
Browse the repository at this point in the history
* Fix #412: Completely rewrote the area detection algorithm * Return area name instead of area id in value field in GET /event
- Loading branch information
1 parent
c6f1cb6
commit d5067f0
Showing
13 changed files
with
205 additions
and
41 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
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,56 @@ | ||
const Promise = require('bluebird'); | ||
const queries = require('./area.queries.js'); | ||
|
||
/** | ||
* @public | ||
* @description This function return all areas the user is currently in | ||
* @name gladys.area.userIn | ||
* @param {Object} user | ||
* @param {String} user.id The id of the user | ||
* @returns [{area}] areas | ||
* @example | ||
* var user = { | ||
* id: 1 | ||
* }; | ||
* | ||
* gladys.area.userIn(user) | ||
* .then((areas) => { | ||
* // areas is an array of areas where the user is in currently | ||
* }) | ||
* .catch((err) => { | ||
* // something bad happened ! :/ | ||
* }); | ||
*/ | ||
|
||
module.exports = function userIn(user) { | ||
|
||
// Getting all areas + events linked to areas | ||
// The goal here is to rebuild the history of entering/leaving areas | ||
return Promise.all([ | ||
gladys.utils.sql(queries.get, [user.id]), | ||
gladys.utils.sql(queries.getLastAreaEventPerArea, [user.id]) | ||
]) | ||
.spread((areas, events) => { | ||
|
||
var areasWhereTheUserIsStillIn = []; | ||
|
||
areas.forEach((area) => { | ||
var found = false; | ||
var i = 0; | ||
|
||
// we see if the last event seen about this area was an "enter-area" event, | ||
// "left-area", or no event at all | ||
while(!found && i < events.length) { | ||
if(parseInt(events[i].value) === area.id) { | ||
found = true; | ||
if(events[i].code === 'enter-area') { | ||
areasWhereTheUserIsStillIn.push(area); | ||
} | ||
} | ||
i++; | ||
} | ||
}); | ||
|
||
return areasWhereTheUserIsStillIn; | ||
}); | ||
}; |
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
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
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
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
Oops, something went wrong.