umbrello-api/src/api.js

91 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-08-15 23:22:40 +02:00
import axios from 'axios';
import config from './config';
import logger from './logger';
2019-08-22 17:41:54 +02:00
import citySchema from './db';
2019-08-15 23:22:40 +02:00
2019-08-22 17:41:54 +02:00
export const getCurrentWeatherById = (cityId) => (
axios.get(`${config.owm.baseurl}/weather`, {
2019-08-15 23:22:40 +02:00
params: {
appid: config.owm.token,
id: cityId,
units: config.owm.units,
lang: config.owm.lang,
},
})
.then((res) => {
const { data } = res;
const currentWeather = {
city: `${data.name}, ${data.sys.country} `,
date: new Date(data.dt * 1000).toISOString(),
temperature: data.main.temp,
humidity: data.main.humidity,
rain_flg: Object.prototype.hasOwnProperty.call(data, 'rain') ? 'Y' : 'N',
};
return currentWeather;
})
.catch((error) => {
const { status } = error.response;
logger.error(`getCurrentWeatherById (cityId=${cityId}) - Status:${status}`);
throw new Error(status);
2019-08-22 17:41:54 +02:00
})
);
2019-08-15 23:22:40 +02:00
2019-08-22 17:41:54 +02:00
export const getForecastWeatherById = (cityId) => (
axios.get(`${config.owm.baseurl}/forecast`, {
2019-08-15 23:22:40 +02:00
params: {
appid: config.owm.token,
id: cityId,
units: config.owm.units,
lang: config.owm.lang,
2019-08-22 17:41:54 +02:00
},
2019-08-15 23:22:40 +02:00
})
.then((res) => {
const { data } = res;
let date = new Date(Date.now());
date = date.toISOString().substr(0, 10);
const forecastList = data.list
.filter((dato) => new Date(dato.dt * 1000).toISOString().substr(0, 10) === date)
.map((dato) => ({
date: new Date(dato.dt * 1000).toISOString(),
temperature: dato.main.temp,
humidity: dato.main.humidity,
wind_speed: dato.wind.speed,
rain_flag: Object.prototype.hasOwnProperty.call(dato, 'rain') ? 'Y' : 'N',
}
));
const forecastWeather = {
city: `${data.city.name}, ${data.city.country}`,
2019-08-22 17:41:54 +02:00
rain: forecastList.some((e) => e.rain_flag === 'Y') ? 'Y' : 'N',
2019-08-15 23:22:40 +02:00
list: forecastList,
};
return forecastWeather;
})
.catch((error) => {
const { status } = error.response;
logger.error(`getCurrentWeatherById (cityId=${cityId}) - Status:${status}`);
throw new Error(status);
2019-08-22 17:41:54 +02:00
})
);
export const getCityIdByName = (cityName) => (
new Promise((resolve, reject) => {
citySchema.find({ name: new RegExp(`^${cityName}`, 'i') }, {
_id: 0, name: 1, country: 1, id: 1,
}, (err, cities) => {
if (err) {
reject(err);
}
resolve(cities);
}).sort({ name: 1 }).limit(5);
})
);