/
routes.js
135 lines (84 loc) · 2.92 KB
/
routes.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
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
133
134
135
'use strict';
const jsonfile = require('jsonfile');
const log = require('./common/log');
const removePermanentRep = require('./common/permanent-rep');
module.exports = server => {
server.get('/all-names', getAllNames);
server.get('/lobbyist/:name', getLobbyistByName);
server.get('/company/:name', groupCompanysLobbyistsByClient);
server.get('/client/:name', groupClientsLobbyistsByCompany);
};
function getAllNames(req, res){
res.status(200).sendFile('all-names.json', { root: 'store' });
}
function getLobbyistByName(req, res){
jsonfile.readFile('./store/data.json', (err, lobbyists) => {
if (err){
log.error(err);
return res.status(500).end();
}
const name = req.params.name;
if (!name) return res.status(400).end();
const lobbyistArr = lobbyists.filter(lobbyist => lobbyist.name === name);
if (!lobbyistArr.length) return res.status(404).end();
const lobbyist = {
company: lobbyistArr[0].company,
number: lobbyistArr[0].number,
clients: lobbyistArr.map(lobbyist => lobbyist.client).map(removePermanentRep)
};
res.status(200).json(lobbyist);
});
}
function groupCompanysLobbyistsByClient(req, res){
jsonfile.readFile('./store/data.json', (err, lobbyists) => {
if (err){
log.error(err);
return res.status(500).end();
}
const companyName = req.params.name;
if (!companyName) return res.status(400).end();
const clientsArr = lobbyists
.filter(lobbyist => lobbyist.company === companyName)
.map(lobbyist => { lobbyist.client = removePermanentRep(lobbyist.client); return lobbyist; })
.reduce(lobbyistsByClient, []);
if (!clientsArr.length) return res.status(404).end();
res.status(200).json(clientsArr);
function lobbyistsByClient(accumulator, lobbyist){
let foundClient;
accumulator.forEach(client => {
if (client.name === lobbyist.client){
foundClient = true;
client.lobbyists.push(lobbyist.name);
}
});
if (!foundClient) accumulator.push({ name: lobbyist.client, lobbyists: [lobbyist.name] });
return accumulator;
}
});
}
function groupClientsLobbyistsByCompany(req, res){
jsonfile.readFile('./store/data.json', (err, lobbyists) => {
if (err){
log.error(err);
return res.status(500).end();
}
const clientName = req.params.name;
if (!clientName) return res.status(400).end();
const companyArr = lobbyists
.filter(lobbyist => lobbyist.client === clientName || lobbyist.client === clientName+removePermanentRep.string)
.reduce(lobbyistsByCompany, []);
if (!companyArr.length) return res.status(404).end();
res.status(200).json(companyArr);
function lobbyistsByCompany(accumulator, lobbyist){
let foundCompany;
accumulator.forEach(company => {
if (company.name === lobbyist.company){
foundCompany = true;
company.lobbyists.push(lobbyist.name);
}
});
if (!foundCompany) accumulator.push({ name: lobbyist.company, lobbyists: [lobbyist.name] });
return accumulator;
}
});
}