Skip to content

Commit

Permalink
fix search
Browse files Browse the repository at this point in the history
  • Loading branch information
vcantu committed Jun 26, 2017
1 parent be69e7a commit ec99568
Showing 1 changed file with 91 additions and 19 deletions.
110 changes: 91 additions & 19 deletions private/services/v1/congress.service.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,69 @@ module.exports = function (app) {
getSenate();


function searchSenateBy(ocdId, name, state) {
function searchHouseBy(results, ocdId, name, state, district) {
var deferred = q.defer();
results = [];
getSenate()
.then(function (senate) {
for (var s in senate.members) {
getHouse()
.then(function (house) {
for (var r in house.members) {
var found = false;
var sen = senate.members[s];
console.log('testing..', sen.first_name);

var rep = house.members[r];
if (!found && name) {
var names = name.split(' ');
for (var n in names) {
if (rep.first_name.toLowerCase() === names[n].toLowerCase()) {
results.push(rep);
break;
}
else if (rep.last_name.toLowerCase() === names[n].toLowerCase()) {
results.push(rep);
break;
}
}
}
if (state) {
if (sen.state === state.toUpperCase()) {
results.push(sen);
found = true;
if (rep.state === state.toUpperCase()) {
console.log(rep.first_name);
console.log(district);
if (district) {
if (rep.district == district) {
results.push(rep);
found = true;
}
}
else {
results.push(rep);
found = true;
}
}
}
else if (!found && ocdId) {
if (sen.ocd_id === ocdId) {
results.push(sen);
if (!found && ocdId) {
if (rep.ocd_id === ocdId) {
results.push(rep);
found = true;
}
}
else if (!found && name) {
}
deferred.resolve({
ocdId: ocdId,
name: name,
state: state,
district: district,
results: results
});
});
return deferred.promise;
}

function searchSenateBy(ocdId, name, state, district) {
var deferred = q.defer();
results = [];
getSenate()
.then(function (senate) {
for (var s in senate.members) {
var found = false;
var sen = senate.members[s];
if (!found && name) {
var names = name.split(' ');
for (var n in names) {
if (sen.first_name.toLowerCase() === names[n].toLowerCase()) {
Expand All @@ -60,8 +100,26 @@ module.exports = function (app) {
}
}
}
if (!found && state) {
if (sen.state === state.toUpperCase()) {
results.push(sen);
found = true;
}
}
if (!found && ocdId) {
if (sen.ocd_id === ocdId) {
results.push(sen);
found = true;
}
}
}
deferred.resolve(results);
deferred.resolve({
ocdId: ocdId,
name: name,
state: state,
district: district,
results: results
});
});
return deferred.promise;
}
Expand All @@ -77,7 +135,8 @@ module.exports = function (app) {
if (query) {
google.get('/representatives', { address: req.query.query })
.then(function (response) {
console.log(response);
var state;
var district;
for (var d in response.divisions) {
var s = d.indexOf('/state:');
var c = d.indexOf('/cd:');
Expand All @@ -89,8 +148,21 @@ module.exports = function (app) {
break;
}
}
}, function (error) {
res.sendStatus(404).send(error);
return searchSenateBy(null, req.query.query, state, district);
})
.then(function (result) {
return searchHouseBy(
result.results,
result.ocdId,
result.name,
result.state,
result.district
)
})
.then(function (result) {
res.json(result.results);
}, function (err) {
res.sendStatus(404);
})
}

Expand Down

0 comments on commit ec99568

Please sign in to comment.