diff --git a/server/api/controllers/weather.controller.js b/server/api/controllers/weather.controller.js index 755ba1e3ff..35bfe522f5 100644 --- a/server/api/controllers/weather.controller.js +++ b/server/api/controllers/weather.controller.js @@ -24,6 +24,7 @@ module.exports = function WeatherController(gladys) { latitude: lastLocation.latitude, longitude: lastLocation.longitude, language: req.user.language, + units: req.user.distance_unit_preference, }; const weatherResult = await gladys.weather.get(options); res.json(weatherResult); @@ -53,6 +54,7 @@ module.exports = function WeatherController(gladys) { latitude: house.latitude, longitude: house.longitude, language: req.user.language, + units: req.user.distance_unit_preference, }; const weatherResult = await gladys.weather.get(options); weatherResult.house = house; diff --git a/server/lib/user/user.getById.js b/server/lib/user/user.getById.js index 777709802e..40877010d2 100644 --- a/server/lib/user/user.getById.js +++ b/server/lib/user/user.getById.js @@ -23,6 +23,7 @@ async function getById(id) { 'birthdate', 'role', 'temperature_unit_preference', + 'distance_unit_preference', 'created_at', 'updated_at', ], diff --git a/server/lib/weather/weather.command.js b/server/lib/weather/weather.command.js index 303b81baad..edac184995 100644 --- a/server/lib/weather/weather.command.js +++ b/server/lib/weather/weather.command.js @@ -32,7 +32,13 @@ async function command(message, classification, context) { if (!house || !house.latitude || !house.longitude) { throw new NoValuesFoundError(); } - const weather = await this.get(house); + const options = { + latitude: house.latitude, + longitude: house.longitude, + units: message.user.distance_unit_preference, + language: message.user.language, + }; + const weather = await this.get(options); if (`intent.${classification.intent}` === INTENTS.WEATHER.GET) { const dateEntity = classification.entities.find((entity) => entity.entity === 'date'); diff --git a/server/services/openweather/index.js b/server/services/openweather/index.js index 62a05e7291..ac828753a1 100644 --- a/server/services/openweather/index.js +++ b/server/services/openweather/index.js @@ -41,7 +41,7 @@ module.exports = function OpenWeatherService(gladys, serviceId) { * @param {number} options.longitude - The longitude to get the weather from. * @param {number} options.offset - Get weather in the future, offset is in hour. * @param {string} [options.language] - The language of the report. - * @param {string} [options.units] - Unit of the weather [auto, si, us]. + * @param {string} [options.units] - Unit of the weather [metric, us]. * @example * gladys.services.openWeather.weather.get({ * latitude: 112, @@ -52,12 +52,16 @@ module.exports = function OpenWeatherService(gladys, serviceId) { * }); */ async function get(options) { + const optionsModified = { + ...options, + units: options.units === 'us' ? 'imperial' : 'metric', + }; const DEFAULT = { language: 'en', units: 'metric', offset: 0, }; - const optionsMerged = Object.assign({}, DEFAULT, options); + const optionsMerged = Object.assign({}, DEFAULT, optionsModified); const { latitude, longitude, language, units } = optionsMerged; if (!openWeatherApiKey) { diff --git a/server/test/controllers/user/user.getMySelf.test.js b/server/test/controllers/user/user.getMySelf.test.js index 6098138e68..85769e457d 100644 --- a/server/test/controllers/user/user.getMySelf.test.js +++ b/server/test/controllers/user/user.getMySelf.test.js @@ -15,6 +15,7 @@ describe('GET /api/v1/me', () => { selector: 'john', email: 'demo@demo.com', birthdate: '12/12/1990', + distance_unit_preference: 'metric', temperature_unit_preference: 'celsius', language: 'en', role: 'admin', diff --git a/server/test/controllers/weather/weather.test.js b/server/test/controllers/weather/weather.test.js index f7e69a5c0e..4b3172cc34 100644 --- a/server/test/controllers/weather/weather.test.js +++ b/server/test/controllers/weather/weather.test.js @@ -57,7 +57,7 @@ describe('GET /api/v1/house/:selector/weather', () => { created_at: '2019-02-12T07:49:07.556Z', updated_at: '2019-02-12T07:49:07.556Z', }, - options: { latitude: 12, longitude: 12, language: 'en' }, + options: { latitude: 12, longitude: 12, language: 'en', units: 'metric' }, }); }); }); diff --git a/server/test/lib/user/user.getById.test.js b/server/test/lib/user/user.getById.test.js index ae086a38e0..8bf505fa3d 100644 --- a/server/test/lib/user/user.getById.test.js +++ b/server/test/lib/user/user.getById.test.js @@ -15,6 +15,7 @@ describe('user.getById', () => { language: 'en', birthdate: '12/12/1990', temperature_unit_preference: 'celsius', + distance_unit_preference: 'metric', role: 'admin', created_at: new Date('2019-02-12T07:49:07.556Z'), updated_at: new Date('2019-02-12T07:49:07.556Z'), diff --git a/server/test/lib/weather/weather.command.test.js b/server/test/lib/weather/weather.command.test.js index 86c2425a38..ac2cb4b070 100644 --- a/server/test/lib/weather/weather.command.test.js +++ b/server/test/lib/weather/weather.command.test.js @@ -130,6 +130,10 @@ describe('weather.command', () => { const weather = new Weather(service, event, messageManager, houses); const message = { text: 'Meteo ?', + user: { + language: 'fr', + distance_unit_preference: 'metric', + }, }; await weather.command( message, @@ -148,6 +152,10 @@ describe('weather.command', () => { const weather = new Weather(service, event, messageManager, houses); const message = { text: 'Meteo Today?', + user: { + language: 'fr', + distance_unit_preference: 'metric', + }, }; await weather.command( message, @@ -175,6 +183,10 @@ describe('weather.command', () => { const weather = new Weather(service, event, messageManager, houses); const message = { text: 'Meteo Tomorrow?', + user: { + language: 'fr', + distance_unit_preference: 'metric', + }, }; await weather.command( message, @@ -204,6 +216,10 @@ describe('weather.command', () => { const weather = new Weather(service, event, messageManager, houses); const message = { text: 'Meteo After Tomorrow?', + user: { + language: 'fr', + distance_unit_preference: 'metric', + }, }; await weather.command( message, @@ -234,6 +250,10 @@ describe('weather.command', () => { const weather = new Weather(service, event, messageManager, houses); const message = { text: 'Meteo next sunday?', + user: { + language: 'fr', + distance_unit_preference: 'metric', + }, }; await weather.command( message, @@ -265,6 +285,10 @@ describe('weather.command', () => { const weather = new Weather(service, event, messageManager, houses); const message = { text: 'Meteo next?', + user: { + language: 'fr', + distance_unit_preference: 'metric', + }, }; await weather.command( message, @@ -283,6 +307,10 @@ describe('weather.command', () => { const weather = new Weather(service, event, messageManager, houses); const message = { text: 'Meteo next far day?', + user: { + language: 'fr', + distance_unit_preference: 'metric', + }, }; await weather.command( message,