Eric Whale

Edit weatherActions.js for code reusability

...@@ -9,28 +9,8 @@ const asyncHandler = require("express-async-handler"); ...@@ -9,28 +9,8 @@ const asyncHandler = require("express-async-handler");
9 // @route Get /api/weather 9 // @route Get /api/weather
10 // @access Public 10 // @access Public
11 const getWeather = asyncHandler(async (req, res) => { 11 const getWeather = asyncHandler(async (req, res) => {
12 - const token = req.body.token;
13 - const decoded = jwt.decode(token, { complete: true });
14 - const user = await User.findById(decoded.payload.id)
15 - .select("-password")
16 - .select("-email");
17 -
18 - const countryCode = user.country;
19 - const city = user.city;
20 -
21 - const limit = 5;
22 try { 12 try {
23 - const metaGeoData = await axios.get( 13 + const geoData = await getUser_GeoData(req, res);
24 - `http://api.openweathermap.org/geo/1.0/direct?q=${city},${countryCode}&limit=${limit}&appid=${process.env.OPENWEATHER_API_KEY}`
25 - );
26 - const data = metaGeoData.data[0];
27 - const geoData = {
28 - lat: data.lat,
29 - lon: data.lon,
30 - country: data.country,
31 - state: data.state,
32 - };
33 -
34 const metaData = await axios.get( 14 const metaData = await axios.get(
35 `https://api.openweathermap.org/data/2.5/weather?lat=${geoData.lat}&lon=${geoData.lon}&appid=${process.env.OPENWEATHER_API_KEY}` 15 `https://api.openweathermap.org/data/2.5/weather?lat=${geoData.lat}&lon=${geoData.lon}&appid=${process.env.OPENWEATHER_API_KEY}`
36 ); 16 );
...@@ -47,28 +27,8 @@ const getWeather = asyncHandler(async (req, res) => { ...@@ -47,28 +27,8 @@ const getWeather = asyncHandler(async (req, res) => {
47 // @route GET /api/weather/forecast 27 // @route GET /api/weather/forecast
48 // @access Public 28 // @access Public
49 const getForecast = asyncHandler(async (req, res) => { 29 const getForecast = asyncHandler(async (req, res) => {
50 - const token = req.body.token;
51 - const decoded = jwt.decode(token, { complete: true });
52 - const user = await User.findById(decoded.payload.id)
53 - .select("-password")
54 - .select("-email");
55 -
56 - const countryCode = user.country;
57 - const city = user.city;
58 -
59 - const limit = 5;
60 try { 30 try {
61 - const metaGeoData = await axios.get( 31 + const geoData = await getUser_GeoData(req, res);
62 - `http://api.openweathermap.org/geo/1.0/direct?q=${city},${countryCode}&limit=${limit}&appid=${process.env.OPENWEATHER_API_KEY}`
63 - );
64 - const data = metaGeoData.data[0];
65 - const geoData = {
66 - lat: data.lat,
67 - lon: data.lon,
68 - country: data.country,
69 - state: data.state,
70 - };
71 -
72 const metaData = await axios.get( 32 const metaData = await axios.get(
73 `http://api.openweathermap.org/data/2.5/forecast?lat=${geoData.lat}&lon=${geoData.lon}&appid=${process.env.OPENWEATHER_API_KEY}` 33 `http://api.openweathermap.org/data/2.5/forecast?lat=${geoData.lat}&lon=${geoData.lon}&appid=${process.env.OPENWEATHER_API_KEY}`
74 ); 34 );
...@@ -85,6 +45,26 @@ const getForecast = asyncHandler(async (req, res) => { ...@@ -85,6 +45,26 @@ const getForecast = asyncHandler(async (req, res) => {
85 // @route GET /api/weather/airpollution 45 // @route GET /api/weather/airpollution
86 // @access Public 46 // @access Public
87 const getAirPollution = asyncHandler(async (req, res) => { 47 const getAirPollution = asyncHandler(async (req, res) => {
48 + try {
49 + const geoData = await getUser_GeoData(req, res);
50 + const metaData = await axios.get(
51 + `http://api.openweathermap.org/data/2.5/air_pollution?lat=${geoData.lat}&lon=${geoData.lon}&appid=${process.env.OPENWEATHER_API_KEY}`
52 + );
53 + const airPollutionData = metaData.data;
54 +
55 + res.json({
56 + country: geoData.country,
57 + state: geoData.state,
58 + airPollutionData,
59 + });
60 + } catch (err) {
61 + res.status(400);
62 + throw new Error("openweathermap API error");
63 + }
64 +});
65 +
66 +// Get user & geodata
67 +async function getUser_GeoData(req, res) {
88 const token = req.body.token; 68 const token = req.body.token;
89 const decoded = jwt.decode(token, { complete: true }); 69 const decoded = jwt.decode(token, { complete: true });
90 const user = await User.findById(decoded.payload.id) 70 const user = await User.findById(decoded.payload.id)
...@@ -106,22 +86,12 @@ const getAirPollution = asyncHandler(async (req, res) => { ...@@ -106,22 +86,12 @@ const getAirPollution = asyncHandler(async (req, res) => {
106 country: data.country, 86 country: data.country,
107 state: data.state, 87 state: data.state,
108 }; 88 };
109 - 89 + return geoData;
110 - const metaData = await axios.get(
111 - `http://api.openweathermap.org/data/2.5/air_pollution?lat=${geoData.lat}&lon=${geoData.lon}&appid=${process.env.OPENWEATHER_API_KEY}`
112 - );
113 - const airPollutionData = metaData.data;
114 -
115 - res.json({
116 - country: geoData.country,
117 - state: geoData.state,
118 - airPollutionData,
119 - });
120 } catch (err) { 90 } catch (err) {
121 res.status(400); 91 res.status(400);
122 - throw new Error("openweathermap API error"); 92 + throw new Error("openweathermap Geodata API error");
123 } 93 }
124 -}); 94 +}
125 95
126 module.exports = { 96 module.exports = {
127 getWeather, 97 getWeather,
......