Skip to content

Commit

Permalink
ID-3205: query by email and phone
Browse files Browse the repository at this point in the history
    Adds a new verifier route
      /v1/voters/contact_search
    That takes parameters, email, phone, and max_matches
    and returns the matching voter records for the given
    phone number or email address.
    If both are specified, returns the intersection of the
    matches.

[Finishes ID-3205]

Change-Id: I164441b992ab6da2946e1ece966a00eed65e24e4
Reviewed-on: https://code.brigade.zone/28072
Reviewed-by: John Miller <[email protected]>
Tested-by: Leeroy Jenkins <[email protected]>
Reviewed-by: Darius Parakh <[email protected]>
Reviewed-by: Patricia Perozo <[email protected]>
  • Loading branch information
swarup-brigade committed Feb 20, 2018
1 parent 0614f36 commit 9724d32
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
30 changes: 30 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,36 @@
]
}
},
{
"description": "Match a user to their phone and email",
"method": "POST",
"href": "/v1/voters/contact_search",
"title": "search",
"rel": "matches",
"schema": {
"properties": {
"phone": {
"type": [
"string"
]
},
"email": {
"type": [
"string"
]
},
"max_matches": {
"type": [
"integer"
],
"minimum": 0
}
},
"type": [
"object"
]
}
},
{
"description": "Find a user by ID",
"method": "GET",
Expand Down
18 changes: 18 additions & 0 deletions schema/voters.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@
"required": ["user"]
}
},
{
"description": "Match a user to their phone and email",
"method": "POST",
"href": "/v1/voters/contact_search",
"title": "search",
"rel": "matches",
"schema": {
"properties": {
"phone": { "type": "string" },
"email": { "type": "string" },
"max_matches": {
"type": "integer",
"minimum": 0
}
},
"type": ["object"]
}
},
{
"description": "Find a user by ID",
"method": "GET",
Expand Down
30 changes: 30 additions & 0 deletions web.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from jsonschema import Draft4Validator
from pyelasticsearch import ElasticHttpNotFoundError
from raven import Client as RavenClient
from sets import Set
import json
import os

Expand Down Expand Up @@ -206,6 +207,35 @@ def find_voter(id):
return json.dumps(rec), 200, {'Content-Type': 'application/json'}


@app.route('/v1/voters/contact_search', methods=['POST'])
@validate_api_request
def contact_search(params):
""" Match voter records by name, phone and email """
max_matches = params.get('max_matches', 100)
email = params.get('email', '')
phone = params.get('phone', '')

email_hits = lookup_by_email(email, max_matches) if email else []
phone_hits = lookup_by_phone(phone, max_matches) if phone else []

hits = []
if not phone:
hits = email_hits
elif not email:
hits = phone_hits
else:
# both email and phone were specified - take the intersection of their matches
phone_ids = Set([hit['id'] for hit in phone_hits])
hits = [ hit for hit in email_hits if hit['id'] in phone_ids]

resp = json.dumps({'data': hits, 'num_results': len(hits), 'max_matches': max_matches},
sort_keys=True,
indent=4,
separators=(',', ': '))

return resp, 200, {'Content-Type': 'application/json'}


@app.route('/v1/voters/search', methods=['POST'])
@statsd.timed('verifier.response_time.voters_search', tags=['revision:v1'])
@validate_api_request
Expand Down

0 comments on commit 9724d32

Please sign in to comment.