-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcandidates.js
85 lines (70 loc) · 1.94 KB
/
candidates.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
//candidates.js
//API for getting info on candidates
const logger = require('./utils/logger');
const db = require('./campaignFinanceDb')();
const q = require('q');
module.exports = {
getAllCandidates: getAllCandidates,
getCandidate: getCandidate,
getRace: getRace,
getElectedOfficials: getElectedOfficials
};
function getAllCandidates() {
const defer = q.defer();
db.candidates.find({}, { _id: 1 }).toArray().then(
function(candidate_docs) {
let candidates = candidate_docs.map(function(candidate) {
const id = candidate._id.last_name;
return {
candidate: candidate._id,
resourceUrl: constructResourceUrl('candidate', id, candidate._id)
};
});
defer.resolve(candidates);
},
function(err) {
defer.reject(err);
}
);
return defer.promise;
}
function getCandidate(lastName, firstName, middleName) {
const defer = q.defer();
const id = {};
id['_id.first_name'] = firstName;
id['_id.last_name'] = lastName;
}
function getRace(position, year) {}
function getElectedOfficials(year) {
const deferred = q.defer();
db.candidates.find({ 'held_office.elected_year': year }).toArray().then(
function(electedOfficials) {
deferred.resolve(electedOfficials);
},
function(err) {
deferred.reject(err);
}
);
return deferred.promise;
}
function constructResourceUrl(endpoint, id, queryParams) {
let url = '/dc-campaign-finance/api/' + endpoint + '/' + id + '?';
for (key in queryParams) {
url = url + key + '=' + queryParams[key] + '&';
}
return url;
}
function calculateCorporateToGrassRootRatio(corporateDonors, individualDonors) {
let ratio = null;
if (corporateDonors == 0) {
ratio = 1;
} else if (individualDonors == 0) {
ratio = 0;
} else {
ratio = corporateDonors / individualDonors;
}
return ratio;
}
function calculateContributorQuality(numberOfDonors, quality) {
return quality / (numberOfDonors * 8);
}