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

Graph api eventsByFilter endpoint #1062

Merged
merged 4 commits into from
Mar 16, 2022
Merged
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
87 changes: 87 additions & 0 deletions app/graphql/types/event_queries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
module Types

module EventQueries
def self.included(klass)
klass.field :event, EventType, "Find Event by ID" do
argument :id, ID
end

klass.field :event_connection, Types::EventType.connection_type

klass.field :events_by_filter, [Types::EventType] do
argument :from_date, String, required: false
argument :to_date, String, required: false
argument :neighbourhood_id, Integer, required: false
argument :tag_id, Integer, required: false
end
end

def event(id:)
Event.find(id)
end

def event_connection(**args)
Event.sort_by_time.all
end

def events_by_filter(**args)
query = Event.sort_by_time
from_date = DateTime.now.beginning_of_day

if args[:from_date].present?
if args[:from_date] =~ /^\s*(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})/
from_date = DateTime.new(
$1.to_i, # year
$2.to_i, # month
$3.to_i, # day
$4.to_i, # hour
$5.to_i, # minute
0, # seconds
)
else
raise GraphQL::ExecutionError, "fromDate not in 'YYYY-MM-DD HH:MM' format"
end
end

query = query.where('dtstart >= ?', from_date)

if args[:to_date].present?
if args[:to_date] =~ /^\s*(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})/
to_date = DateTime.new(
$1.to_i, # year
$2.to_i, # month
$3.to_i, # day
$4.to_i, # hour
$5.to_i, # minute
0, # seconds
)

if to_date <= from_date
raise GraphQL::ExecutionError, "toDate is before fromDate"
end

else
raise GraphQL::ExecutionError, "toDate not in 'YYYY-MM-DD HH:MM' format"
end

query = query.where('dtstart < ?', to_date)
end

if args[:neighbourhood_id].present?
neighbourhood = Neighbourhood.where(id: args[:neighbourhood_id]).first
raise GraphQL::ExecutionError, "Could not find neighbourhood with that ID (#{args[:neighbourhood_id]})" if neighbourhood.nil?

query = query.for_neighbourhoods(neighbourhood.subtree)
end

if args[:tag_id].present?
tag = Tag.where(id: args[:tag_id]).first
raise GraphQL::ExecutionError, "Could not find tag with that ID (#{args[:tag_id]})" if tag.nil?

query = query.for_tag(tag)
end

query.all
end
end
end
18 changes: 0 additions & 18 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,6 @@ def partner_connection(**args)
end
end

module EventQueries
def self.included(klass)
klass.field :event, EventType, "Find Event by ID" do
argument :id, ID
end

klass.field :event_connection, Types::EventType.connection_type
end

def event(id:)
Event.find(id)
end

def event_connection(**args)
Event.sort_by_time.all
end
end

module SiteQueries
def self.included(klass)
klass.field :site, SiteType, "Find Site by ID" do
Expand Down
18 changes: 18 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ class Event < ApplicationRecord
where('DATE(dtstart) >= ? AND DATE(dtstart) <= ?', week_start, week_end)
}

# For the API eventFilter find by neighbourhood
scope :for_neighbourhoods, lambda { |neighbourhoods|
neighbourhood_ids = neighbourhoods.map(&:id)

joins('left outer join partners on events.partner_id = partners.id')
.joins('left outer join addresses on partners.address_id = addresses.id')
.joins('left outer join service_areas on partners.id = service_areas.partner_id')
.where('(service_areas.neighbourhood_id in (?)) or (addresses.neighbourhood_id in (?))', neighbourhood_ids, neighbourhood_ids)
}

# For the API eventFilter find by tag
scope :for_tag, lambda { |tag|
joins(:partner)
.joins('left outer join partner_tags on partners.id = partner_tags.partner_id')
.where('partner_tags.tag_id = ?', tag.id)
}


# Filter by Site
scope :for_site, lambda { |site|
site_neighbourhood_ids = site.owned_neighbourhoods.map(&:id)
Expand Down
20 changes: 20 additions & 0 deletions test/factories/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@
end
end

factory :bare_address_1, class: 'Address' do
street_address { '123 Moss Ln E' }
street_address2 { 'Manchester' }
city { 'Manchester' }
country_code { 'UK' }
latitude { 53.4651064 }
longitude { -2.2484797 }
postcode { 'M15 5DD' }
end

factory :bare_address_2, class: 'Address' do
street_address { '42 Alexandra Rd' }
street_address2 { 'Moss Side' }
city { 'Manchester' }
country_code { 'UK' }
latitude { '53.430720' }
longitude { '-2.436610' }
postcode { 'M16 7BA' }
end

factory :moss_side_address, class: 'Address' do
street_address { '42 Alexandra Rd' }
street_address2 { 'Moss Side' }
Expand Down
Loading
Loading