Skip to content

Commit

Permalink
Add geolocations to group_health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
simfeld committed Jul 8, 2021
1 parent 41a1ffe commit 97686d1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
26 changes: 23 additions & 3 deletions app/controllers/group_health_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class GroupHealthController < ApplicationController
gender birthday entry_date leaving_date primary_group_id).freeze
ROLES_FIELDS = %i(id person_id group_id type created_at deleted_at).freeze
GROUPS_FIELDS = %i(id parent_id type name created_at deleted_at canton_id canton_name).freeze
GEOLOCATIONS_FIELDS = %i(id lat long).freeze
COURSES_FIELDS = %i(id name kind_id).freeze
CAMPS_FIELDS = %i(id name location j_s_kind state).freeze
EVENT_DATES_FIELDS = %i(start_at finish_at).freeze
Expand Down Expand Up @@ -65,10 +66,18 @@ def roles
end

def groups
respond(Group.from("((#{bund}) UNION (#{cantons}) UNION (#{abt_and_below})) " \
# geolocations only exist on Group::Abteilung, thus we can't include geolocations in
# the queries for other group types
abt = Group.from("((#{abteilung})) " \
"AS #{Group.quoted_table_name}")
.page(params[:page]).per(params[:size] || DEFAULT_PAGE_SIZE)
.as_json(only: GROUPS_FIELDS))
.as_json(only: GROUPS_FIELDS,
include: { geolocations: { only: GEOLOCATIONS_FIELDS } })
rest = Group.from("((#{bund}) UNION (#{cantons}) UNION (#{below_abteilung})) " \
"AS #{Group.quoted_table_name}")
.page(params[:page]).per(params[:size] || DEFAULT_PAGE_SIZE)
.as_json(only: GROUPS_FIELDS)
respond(rest.concat(abt))
end

def courses
Expand Down Expand Up @@ -193,11 +202,22 @@ def cantons
.to_sql
end

def abt_and_below
def abteilung
Group.select("#{Group.quoted_table_name}.*", 'canton.id as canton_id',
'canton.name as canton_name')
.includes(:geolocations)
.joins(CANTON_JOIN)
.joins(GROUP_HEALTH_JOIN).distinct
.where(type: Group::Abteilung)
.to_sql
end

def below_abteilung
Group.select("#{Group.quoted_table_name}.*", 'canton.id as canton_id',
'canton.name as canton_name')
.joins(CANTON_JOIN)
.joins(GROUP_HEALTH_JOIN).distinct
.where.not(type: Group::Abteilung)
.to_sql
end

Expand Down
8 changes: 8 additions & 0 deletions spec/controllers/group_health_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@
expect(groups.size).to eq(1)
end

it 'exports geolocations' do
location = Fabricate(:geolocation, geolocatable: groups(:schekka))
get :groups, format: :json
json = JSON.parse(response.body)
groups = json['groups'].select {|g| g['name'] == groups(:schekka).name}
expect(groups[0]['geolocations']).to eq([{ 'id' => location.id, 'lat' => location.lat, 'long' => location.long }])
end

it 'does only export people with roles in a group having opted in' do
get :people, format: :json
json = JSON.parse(response.body)
Expand Down

0 comments on commit 97686d1

Please sign in to comment.