Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter interviewees by geo distance, using #query[ll]=...&query[miles]=... #731

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/backend/data/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,14 @@ const GraphQLListContainer = new GraphQLObjectType({
interfaces: [nodeInterface]
})

const GeoPoint = new GraphQLInputObjectType({
name: 'GeoPoint',
fields: {
lat: {type: GraphQLFloat},
lon: {type: GraphQLFloat},
}
})

const GraphQLUser = new GraphQLObjectType({
name: 'User',
description: 'User of ground control',
Expand Down Expand Up @@ -554,10 +562,13 @@ const GraphQLUser = new GraphQLObjectType({
intervieweeForCallAssignment: {
type: GraphQLPerson,
args: {
callAssignmentId: {type: GraphQLString}
callAssignmentId: {type: GraphQLString},
center: {type: GeoPoint},
radiusMeters: {type: GraphQLFloat}
},
resolve: async(user, {callAssignmentId}, {rootValue}) => {

resolve: async(
user, {callAssignmentId, center, radiusMeters}, {rootValue}
) => {
let localCallAssignmentId = fromGlobalId(callAssignmentId)
if (localCallAssignmentId.type !== 'CallAssignment')
localCallAssignmentId = callAssignmentId
Expand Down Expand Up @@ -647,6 +658,21 @@ const GraphQLUser = new GraphQLObjectType({
if (userAddress)
query = query.whereNot('bsd_people.cons_id', userAddress.cons_id)

// Filter by distance from a geographical point.
// Spatial ref 4326 is WGS 84, in degrees
// Spatial ref 900913 is Google Web Mercator, in meters
if (center && radiusMeters > 0) {
query = query.whereRaw(`
ST_DWithin(bsd_addresses.geom,
ST_Transform(
ST_SetSRID(ST_MakePoint(${center.lon}, ${center.lat}), 4326),
900913
),
${radiusMeters}
)
`)
}

log.info(`Running query: ${query}`)

let person = await query
Expand Down
27 changes: 23 additions & 4 deletions src/frontend/components/CallAssignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SubmitCallSurvey from '../mutations/SubmitCallSurvey'
import CallStatsBar from './CallStatsBar'
import MutationHandler from './MutationHandler'
import {PhoneNumberFormat, PhoneNumberUtil} from 'google-libphonenumber'
import getDefaultRelayParams from '../helpers/getDefaultRelayParams'
const phoneUtil = PhoneNumberUtil.getInstance()

const SurveyRenderers = {
Expand Down Expand Up @@ -408,8 +409,24 @@ ${userFirstName}`
}
}

// Convert the hash parameters 'query[lat]', 'query[lon]', and 'query[miles]'
// into the API arguments for the center and radius of the filter region.
const hashParams = getDefaultRelayParams({
lat: null, // latitude in degrees north
lon: null, // longitude in degrees east
miles: null // radius in miles
})
const center = (isFinite(hashParams.lat) && isFinite(hashParams.lon)) ?
{lat: hashParams.lat, lon: hashParams.lon} : null
const radiusMeters = isFinite(hashParams.miles) ?
hashParams.miles * 1609.34 : null

export default Relay.createContainer(CallAssignment, {
initialVariables: { id: '' },
initialVariables: {
id: '',
center: center,
radiusMeters: radiusMeters
},
fragments: {
callAssignment: () => Relay.QL`
fragment on CallAssignment {
Expand All @@ -428,9 +445,11 @@ export default Relay.createContainer(CallAssignment, {
fragment on User {
id
firstName
allCallsMade:callsMade(forAssignmentId:$id)
completedCallsMade:callsMade(forAssignmentId:$id,completed:true)
intervieweeForCallAssignment(callAssignmentId:$id) {
allCallsMade: callsMade(forAssignmentId: $id)
completedCallsMade: callsMade(forAssignmentId: $id, completed: true)
intervieweeForCallAssignment(
callAssignmentId: $id, center: $center, radiusMeters: $radiusMeters
) {
id
prefix
firstName
Expand Down