Skip to content

Commit

Permalink
Fix GladysAssistant#1102: unit preferences was not editable, and was …
Browse files Browse the repository at this point in the history
…not working in dashboard & chat (GladysAssistant#1107)
  • Loading branch information
Pierre-Gilles authored and Jean-Philippe Danrée committed Aug 27, 2021
1 parent 663a6f4 commit 357bfce
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 4 deletions.
2 changes: 2 additions & 0 deletions server/api/controllers/weather.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions server/lib/user/user.getById.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async function getById(id) {
'birthdate',
'role',
'temperature_unit_preference',
'distance_unit_preference',
'created_at',
'updated_at',
],
Expand Down
8 changes: 7 additions & 1 deletion server/lib/weather/weather.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 6 additions & 2 deletions server/services/openweather/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions server/test/controllers/user/user.getMySelf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion server/test/controllers/weather/weather.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
});
});
});
Expand Down
1 change: 1 addition & 0 deletions server/test/lib/user/user.getById.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
28 changes: 28 additions & 0 deletions server/test/lib/weather/weather.command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 357bfce

Please sign in to comment.