weatherActions.js
3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const axios = require("axios").default;
const User = require("../models/userModel");
// handles "exception" inside of async express routes
const asyncHandler = require("express-async-handler");
// @desc Get current weather (API - Current Weather)
// @route Get /api/weather
// @access Public
const getWeather = asyncHandler(async (req, res) => {
const token = req.body.token;
const decoded = jwt.decode(token, { complete: true });
const user = await User.findById(decoded.payload.id)
.select("-password")
.select("-email");
const countryCode = user.country;
const city = user.city;
const limit = 5;
try {
const metaGeoData = await axios.get(
`http://api.openweathermap.org/geo/1.0/direct?q=${city},${countryCode}&limit=${limit}&appid=${process.env.OPENWEATHER_API_KEY}`
);
const data = metaGeoData.data[0];
const geoData = {
lat: data.lat,
lon: data.lon,
country: data.country,
state: data.state,
};
const metaData = await axios.get(
`https://api.openweathermap.org/data/2.5/weather?lat=${geoData.lat}&lon=${geoData.lon}&appid=${process.env.OPENWEATHER_API_KEY}`
);
const weatherData = metaData.data;
res.json(weatherData);
} catch (err) {
res.status(400);
throw new Error("openweathermap API error");
}
});
// @desc Get weather forecast (3-hour Forecast 5 days)
// @route GET /api/weather/forecast
// @access Public
const getForecast = asyncHandler(async (req, res) => {
const token = req.body.token;
const decoded = jwt.decode(token, { complete: true });
const user = await User.findById(decoded.payload.id)
.select("-password")
.select("-email");
const countryCode = user.country;
const city = user.city;
const limit = 5;
try {
const metaGeoData = await axios.get(
`http://api.openweathermap.org/geo/1.0/direct?q=${city},${countryCode}&limit=${limit}&appid=${process.env.OPENWEATHER_API_KEY}`
);
const data = metaGeoData.data[0];
const geoData = {
lat: data.lat,
lon: data.lon,
country: data.country,
state: data.state,
};
const metaData = await axios.get(
`http://api.openweathermap.org/data/2.5/forecast?lat=${geoData.lat}&lon=${geoData.lon}&appid=${process.env.OPENWEATHER_API_KEY}`
);
const forecastData = metaData.data;
res.json(forecastData);
} catch (err) {
res.status(400);
throw new Error("openweathermap API error");
}
});
// @desc Get air pollution (Air Pollution API)
// @route GET /api/weather/airpollution
// @access Public
const getAirPollution = asyncHandler(async (req, res) => {
const token = req.body.token;
const decoded = jwt.decode(token, { complete: true });
const user = await User.findById(decoded.payload.id)
.select("-password")
.select("-email");
const countryCode = user.country;
const city = user.city;
const limit = 5;
try {
const metaGeoData = await axios.get(
`http://api.openweathermap.org/geo/1.0/direct?q=${city},${countryCode}&limit=${limit}&appid=${process.env.OPENWEATHER_API_KEY}`
);
const data = metaGeoData.data[0];
const geoData = {
lat: data.lat,
lon: data.lon,
country: data.country,
state: data.state,
};
const metaData = await axios.get(
`http://api.openweathermap.org/data/2.5/air_pollution?lat=${geoData.lat}&lon=${geoData.lon}&appid=${process.env.OPENWEATHER_API_KEY}`
);
const airPollutionData = metaData.data;
res.json({
country: geoData.country,
state: geoData.state,
airPollutionData,
});
} catch (err) {
res.status(400);
throw new Error("openweathermap API error");
}
});
module.exports = {
getWeather,
getForecast,
getAirPollution,
};
// use geocoding