diff --git a/app/controllers/group_health_controller.rb b/app/controllers/group_health_controller.rb index 56f50f099..3cda2aecc 100644 --- a/app/controllers/group_health_controller.rb +++ b/app/controllers/group_health_controller.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/controllers/group_health_controller_spec.rb b/spec/controllers/group_health_controller_spec.rb index 81fae3c6c..381183e5b 100644 --- a/spec/controllers/group_health_controller_spec.rb +++ b/spec/controllers/group_health_controller_spec.rb @@ -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)