forked from civictechdc/dc-campaign-finance-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
candidates.js
92 lines (71 loc) · 2.15 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
86
87
88
89
90
91
//candidates.js
//API for getting info on candidates
var logger = require('./utils/logger');
var db = require('./campaignFinanceDb')();
var q = require('q');
module.exports = {
getAllCandidates: getAllCandidates,
getCandidate: getCandidate,
getRace: getRace,
getElectedOfficials: getElectedOfficials
}
function getAllCandidates() {
var defer = q.defer();
db.candidates
.find({},{'_id':1})
.toArray()
.then(function(candidate_docs){
var candidates = candidate_docs.map(function(candidate){
var 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){
var defer = q.defer();
var id = {};
id['_id.first_name'] = firstName;
id['_id.last_name'] = lastName;
}
function getRace(position, year) {
}
function getElectedOfficials(year) {
var 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) {
var url = '/dc-campaign-finance/api/' + endpoint + '/' + id + '?';
for(key in queryParams) {
url = url + key + '=' + queryParams[key] + '&';
}
return url;
}
function calculateCorporateToGrassRootRatio(corporateDonors, individualDonors) {
var 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);
};