Skip to content

Commit

Permalink
Fix #433 : add fallback to house in weather get command
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Nov 26, 2018
1 parent c4e0578 commit 1a81fce
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions api/core/weather/weather.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,33 @@ module.exports = function command(scope) {
// get user position
// accuracy is not that important, so put high value
return gladys.location.getUser(scope.user, {accuracy: 3000})

// fallback to house latitude/longitude if location does not exist
.catch(() => {
sails.log.info(`Weather : command : Location of user ${scope.user.id} not found, so taking house coordinate`);
return gladys.house.getAll()
.then((houses) => {

if(houses.length === 0) {
return new Error('Weather : command : No house found, unable to calculate weather');
}

var house = houses[0];

if (!house.latitude || !house.longitude) {
return new Error('Weather : command : House do not have latitude & longitude, unable to calculate weather');
}

return {
latitude: house.latitude,
longitude: house.longitude
};
});
})
.then((location) => {

sails.log.info(`Weather : command : Calculating weather at location latitude = "${location.latitude}", longitude = "${location.longitude}".`);

var params = {
latitude: location.latitude,
longitude: location.longitude,
Expand Down

0 comments on commit 1a81fce

Please sign in to comment.