-
Notifications
You must be signed in to change notification settings - Fork 8
/
serverApi.js
334 lines (331 loc) · 8.22 KB
/
serverApi.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import {
Schema,
arrayOf,
unionOf,
valuesOf,
normalize
}
from 'normalizr';
import {
camelizeKeys
} from 'humps'
import assign from 'lodash/assign';
import 'isomorphic-fetch';
const API_ROOT = 'http://54.213.83.132/hackoregon/http/';
// local
// const API_ROOT = 'http://localhost:8080/hackoregon/http/';
function callApi(endpoint, schema) {
const fullUrl = (endpoint.indexOf(API_ROOT) === -1) ? API_ROOT + endpoint : endpoint;
return fetch(fullUrl)
.then((response) => {
return response.json()
.then(json => ({
json,
response
}))
return {
json,
response
}
})
.then(({
json,
response
}) => {
if (!response.ok) {
return Promise.reject(json)
}
const camelizedJson = camelizeKeys(json);
return normalize(camelizedJson, schema)
})
}
const campaign = new Schema('campaigns', {
idAttribute: 'filerId'
});
const donor = new Schema('donors', {
idAttribute: 'tranId'
});
const transaction = new Schema('transactions', {
idAttribute: 'tranId'
});
const list = {
campaigns: campaign
};
//
// function mungeExpenditures(data) {
// let expenditures = {};
// data.forEach((val) => {
// let key = val['purposeCodes'];
//
// if (!_(expenditures)
// .has(key)) {
// expenditures[key] = 0;
// }
// expenditures[key] += val.total;
// });
//
// return expenditures;
// }
// var self = {};
// var CONTRIBUTION = {
// PAC: 'PAC',
// BUSINESS: 'Business',
// GRASSROOTS: 'Grassroots',
// INDIVIDUAL: 'Individual',
// PARTY: 'Party',
// UNION: 'Union',
// NA: 'NA'
// };
// self.CONTRIBUTION = CONTRIBUTION;
//
// function mungeContribs(data) {
// var keyMap = {
// 'Political Committee': CONTRIBUTION.PAC,
// 'Large Donor': CONTRIBUTION.INDIVIDUAL,
// 'Grassroot': CONTRIBUTION.GRASSROOTS,
// 'Political Party Committee': CONTRIBUTION.PARTY,
// 'Business Entity': CONTRIBUTION.BUSINESS,
// 'Labor Organization': CONTRIBUTION.UNION,
// 'Other': CONTRIBUTION.NA
// }
// var result = {};
// Array.forEach(data, function (val) {
// var key = keyMap[val['contributionType']];
// if (key) {
// if (!_(result)
// .has(key)) {
// result[key] = {
// amount: 0
// };
// }
// result[key].amount += val.total;
// }
// });
// return result;
// }
//
// (function(){
// return function(){
//
// }
// })
// function munge(t) {
// var contributions = {};
// // _(self.CONTRIBUTION)
// // .each(function (type) {
// // contributions[type] = {
// // amount: 0,
// // number: 0
// // };
// // });
// var expenditures = {};
// // var donors = {
// // indiv: {},
// // corp: {},
// // pac: {}
// // };
// // var committee_codes = {};
//
// donors.indiv = _.map(donors.indiv, function (amount, donor) {
// return {
// payee: donor,
// amount: amount
// };
// });
// donors.indiv.sort(sortEntry);
// donors.corp = _.map(donors.corp, function (amount, donor) {
// return {
// payee: donor,
// amount: amount
// };
// });
// donors.corp.sort(sortEntry);
// donors.pac = _.map(donors.pac, function (amount, donor) {
// return {
// payee: donor,
// amount: amount
// };
// })
// donors.pac.sort(sortEntry);
// _.each(donors.pac, function (val) {
// if (_.has(committee_codes, val.payee)) {
// val['filerId'] = committee_codes[val.payee];
// }
// })
//
// return ({contributions:contributions,expenditures:expenditures, donors: donors});
// }
// function mungeExp(data) {
// var expenditures = {};
// Array.forEach(data, function(val) {
// var key = val['purposeCodes'];
// if (!_(expenditures).has(key)) {
// expenditures[key] = 0;
// }
// expenditures[key] += val.total;
// });
//
// return expenditures;
// }
//
// function makeSpending(transactions) {
// console.log(transactions)
// let expenditures = {};
// _(transactions)
// .chain()
// .each(function (row) {
//
// var subType = row['subType'];
// switch (subType) {
// case 'Cash Expenditure':
// var purposeCodes = (row['purposeCodes'] || '')
// .split('; ');
// console.log(purposeCodes)
// _(purposeCodes)
// .each(function (purposeCode) {
// if (!_(expenditures)
// .has(purposeCode)) {
// expenditures[purposeCode] = 0;
// }
// expenditures[purposeCode] += (Number(row['amount']) / purposeCodes.length);
// });
// break;
// }
// })
// return expenditures;
// }
// function splitPurposeCodes(str){
// str.split('; ')
// }
//
// function makeSpendId(){
// return
// }
//
// let options = {
// idAttribute: 'purposeCodes',
// assignEntity: (output, key, value, input) => {
// if (key === 'purposeCodes') {}
// if (key in obj) {
// obj[key] += value;
// } else {
// obj[key] = value;
// }
// }
// }
// const expenditure = new Schema('expenditure', );
// const contribution = new Schema('contributions', {
// idAttribute: 'contributorPayee'
// });
const stateContribution = new Schema('stateContributions', {
idAttribute: 'state'
});
const indivContribution = new Schema('indivContributions', {
idAttribute: 'contributorPayee'
});
const pacContribution = new Schema('pacContributions', {
idAttribute: 'contributorPayee'
});
const businessContribution = new Schema('businessContributions', {
idAttribute: 'contributorPayee'
});
const contributions = ({
owner: campaign,
individual: arrayOf(indivContribution),
business: arrayOf(businessContribution),
pac: arrayOf(pacContribution),
states: arrayOf(stateContribution)
});
campaign.define({
listByName: valuesOf(campaign, {
schemaAttribute: 'candidateName'
})
});
const search = new Schema('searches');
search.define({
list: arrayOf(campaign)
});
// contribution.define({
// owner: campaign
// });
indivContribution.define({
owner: campaign,
listByType: valuesOf(transaction, {
schemaAttribute: 'bookType'
})
});
donor.define({
owner: transaction,
listByName: valuesOf(transaction, {
schemaAttribute: 'contributorPayee'
}),
relationships: valuesOf(campaign)
});
export const Schemas = {
CAMPAIGN: campaign,
CAMPAIGN_ARRAY: arrayOf(campaign),
TRANSACTION: transaction,
TRANSACTION_ARRAY: arrayOf(transaction),
EXPENDITURE: transaction,
EXPENDITURE_ARRAY: arrayOf(transaction),
DONOR: donor,
DONOR_ARRAY: arrayOf(donor),
LIST: list,
STATE_CONTRIBUTION: stateContribution,
STATE_CONTRIBUTION_ARRAY: arrayOf(stateContribution),
INDIV_CONTRIBUTION: indivContribution,
INDIV_CONTRIBUTION_ARRAY: arrayOf(indivContribution),
BIZ_CONTRIBUTION: businessContribution,
BIZ_CONTRIBUTION_ARRAY: arrayOf(businessContribution),
PAC_CONTRIBUTION: pacContribution,
PAC_CONTRIBUTION_ARRAY: arrayOf(pacContribution)
}
export const CALL_API = Symbol('Call API');
export default store => next => action => {
const callAPI = action[CALL_API]
if (typeof callAPI === 'undefined') {
return next(action)
}
let {
endpoint
} = callAPI
const {
schema,
types
} = callAPI
if (typeof endpoint === 'function') {
endpoint = endpoint(store.getState())
}
if (typeof endpoint !== 'string') {
throw new Error('Specify a string endpoint URL.')
}
if (!schema) {
throw new Error('Specify one of the exported Schemas.')
}
if (!Array.isArray(types) || types.length !== 3) {
throw new Error('Expected an array of three action types.')
}
if (!types.every(type => typeof type === 'string')) {
throw new Error('Expected action types to be strings.')
}
function actionWith(data) {
const finalAction = assign({}, action, data)
delete finalAction[CALL_API]
return finalAction
}
const [requestType, successType, failureType] = types
next(actionWith({
type: requestType
}))
return callApi(endpoint, schema)
.then(
response => next(actionWith({
response,
type: successType
})),
error => next(actionWith({
type: failureType,
error: error.message || 'Something bad happened'
}))
)
}