-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getLocalPols.js
51 lines (50 loc) · 1.57 KB
/
getLocalPols.js
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
const { API } = require('../../constants'),
superagent = require('superagent');
require('dotenv').config();
module.exports = {
getLocalPols: async (req, res) => {
const URI =
process.env.REACT_APP_CIVICS_API_URI +
'?address=' +
req.body.address.replace(' ', '20%') +
'&levels=country&roles=legislatorLowerBody&key=' +
API.CIVICS.KEY;
try {
const response = await superagent.get(URI).set({
Accept: 'application/json',
});
if (JSON.parse(response.text).divisions) {
const [ocd_id] = Object.keys(JSON.parse(response.text).divisions);
res.json(ocd_id);
} else {
const normalizedInput = JSON.parse(response.text).normalizedInput;
if (
normalizedInput.line1.length &&
(!normalizedInput.city.length ||
!normalizedInput.state.length ||
!normalizedInput.zip.length)
)
res.send('prompt-requery');
else {
let retryEndpoint =
process.env.REACT_APP_CIVICS_API_URI +
'?address=' +
normalizedInput.city +
'20%' +
normalizedInput.state +
'&levels=country&roles=legislatorLowerBody&key=' +
API.CIVICS.KEY;
const retryResponse = await superagent.get(retryEndpoint).set({
Accept: 'application/json',
});
const [ocd_id] = Object.keys(
JSON.parse(retryResponse.text).divisions
);
res.json(ocd_id);
}
}
} catch (err) {
res.status(500).json(err);
}
},
};