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 just official events when ?official=true is given #734

Open
wants to merge 3 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
41 changes: 36 additions & 5 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 Expand Up @@ -783,9 +809,10 @@ const GraphQLPerson = new GraphQLObjectType({
type: new GraphQLList(GraphQLEvent),
args: {
within: {type: GraphQLInt},
type: {type: GraphQLString}
type: {type: GraphQLString},
officialOnly: {type: GraphQLBoolean}
},
resolve: async(person, {within, type}, {rootValue}) => {
resolve: async(person, {within, type, officialOnly}, {rootValue}) => {
let address = await getPrimaryAddress(person);
let boundingDistance = within / 69
let eventTypes = null
Expand All @@ -801,6 +828,10 @@ const GraphQLPerson = new GraphQLObjectType({
.where('flag_approval', false)
.whereNot('is_searchable', 0)

if (officialOnly) {
query = query.where('is_official', true)
}

if (eventTypes)
query = query.whereIn('event_type_id', eventTypes.map((type) => type.event_type_id))

Expand Down
22 changes: 18 additions & 4 deletions src/frontend/components/CallAssignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import SubmitCallSurvey from '../mutations/SubmitCallSurvey'
import CallStatsBar from './CallStatsBar'
import MutationHandler from './MutationHandler'
import {PhoneNumberFormat, PhoneNumberUtil} from 'google-libphonenumber'
import qs from 'qs'
import convertType from '../helpers/convertType'
const phoneUtil = PhoneNumberUtil.getInstance()

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

// Convert the query parameters 'lat', 'lon', and 'miles' into API arguments
// for the center and radius of the filter region.
const {lat, lon, miles} = convertType(qs.parse(location.search.substr(1)))
const center = isFinite(lat) && isFinite(lon) ? {lat: lat, lon: lon} : null
const radiusMeters = isFinite(miles) ? 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 +440,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
11 changes: 9 additions & 2 deletions src/frontend/components/survey-renderers/PhonebankRSVPSurvey.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import FontIcon from 'material-ui/lib/font-icon'
import SideBarLayout from '../SideBarLayout'
import GCSelectField from '../forms/GCSelectField'
import GCBooleanField from '../forms/GCBooleanField'
import qs from 'qs'
import convertType from '../../helpers/convertType'

const WEEKDAY_DATE_FORMAT = 'dddd, MMMM Do'

Expand Down Expand Up @@ -544,9 +546,14 @@ class PhonebankRSVPSurvey extends React.Component {
}
}

// Convert the query parameter 'official' into a boolean flag.
const {official} = convertType(qs.parse(location.search.substr(1)))
const officialOnly = official ? true : false

export default Relay.createContainer(PhonebankRSVPSurvey, {
initialVariables: {
type: 'phonebank'
type: 'phonebank',
officialOnly: officialOnly
},
fragments: {
currentUser: () => Relay.QL`
Expand All @@ -562,7 +569,7 @@ export default Relay.createContainer(PhonebankRSVPSurvey, {
latitude
longitude
}
nearbyEvents(within:20, type:$type) {
nearbyEvents(within:20, type:$type, officialOnly:$officialOnly) {
id
eventIdObfuscated
name
Expand Down