diff --git a/app/controllers/api/v1/locations_controller.rb b/app/controllers/api/v1/locations_controller.rb index 8567799e..07ae6998 100644 --- a/app/controllers/api/v1/locations_controller.rb +++ b/app/controllers/api/v1/locations_controller.rb @@ -198,7 +198,16 @@ def closest_by_lat_lon param :no_details, Integer, desc: 'Omit data that app does not need from pull', required: false formats %w[json geojson] def within_bounding_box - except = params[:no_details] ? %i[country last_updated_by_user_id description region_id zone_id website phone ic_active is_stern_army date_last_updated created_at] : nil + if params[:no_details] == '1' + except = %i[country last_updated_by_user_id description region_id zone_id website phone ic_active is_stern_army date_last_updated created_at] + except2 = %i[machine_names_first machine_ids num_machines] + elsif params[:no_details] == '2' + except = %i[name street state zip country updated_at location_type_id operator_id country last_updated_by_user_id description region_id zone_id website phone ic_active is_stern_army date_last_updated created_at] + except2 = [] + else + except = [] + except2 = %i[machine_names_first machine_ids num_machines] + end bounds = [params[:swlat], params[:swlon], params[:nelat], params[:nelon]] if params[:user_faved] @@ -206,6 +215,8 @@ def within_bounding_box fave_locations = UserFaveLocation.select(:location_id).where(user_id: user) locations_within = apply_scopes(Location.where(id: fave_locations)).includes(:machines).within_bounding_box(bounds).uniq + elsif params[:no_details] == '2' + locations_within = apply_scopes(Location).within_bounding_box(bounds).uniq else locations_within = apply_scopes(Location).includes(:machines).within_bounding_box(bounds).uniq end @@ -243,7 +254,7 @@ def within_bounding_box if !locations_within.empty? respond_to do |format| - format.json { return_response(locations_within, 'locations', [], %i[machine_names_first machine_ids num_machines], 200, except) } + format.json { return_response(locations_within, 'locations', [], except2, 200, except) } format.geojson { render json: container_geojson.to_json } end else diff --git a/spec/requests/api/v1/locations_controller_spec.rb b/spec/requests/api/v1/locations_controller_spec.rb index 08553bb9..4c91d311 100644 --- a/spec/requests/api/v1/locations_controller_spec.rb +++ b/spec/requests/api/v1/locations_controller_spec.rb @@ -726,6 +726,40 @@ expect(response.body).to include('FeatureCollection') expect(response.body).to include('Point') end + + it 'respects no_details and shows fewer location fields' do + close_location_one = FactoryBot.create(:location, id: 5555, street: '123 Main St', name: 'Close_1', phone: '111-222-3333', website: 'https://website.gov', region: @region, lat: 45.526112069408704, lon: -122.60884314086321, location_type_id: 1, operator_id: 1) + machine = FactoryBot.create(:machine) + FactoryBot.create(:location_machine_xref, location: close_location_one, machine_id: machine.id) + + get '/api/v1/locations/within_bounding_box.json', params: { swlat: 45.478363717877436, swlon: -122.64672405963799, nelat: 45.54521396088108, nelon: -122.56878059990427, no_details: 1} + + parsed_body = JSON.parse(response.body) + expect(parsed_body.size).to eq(1) + + locations = parsed_body['locations'] + expect(response.body).to include('Close_1') + expect(response.body).to include('5555') + expect(response.body).to include('45.5261120') + expect(response.body).to include('-122.608843') + expect(response.body).to include('123 Main St') + expect(response.body).to_not include('111-222-3333') + expect(response.body).to_not include('https://website.gov') + + get '/api/v1/locations/within_bounding_box.json', params: { swlat: 45.478363717877436, swlon: -122.64672405963799, nelat: 45.54521396088108, nelon: -122.56878059990427, no_details: 2} + + parsed_body = JSON.parse(response.body) + expect(parsed_body.size).to eq(1) + + locations = parsed_body['locations'] + expect(response.body).to_not include('Close_1') + expect(response.body).to include('5555') + expect(response.body).to include('45.5261120') + expect(response.body).to include('-122.608843') + expect(response.body).to_not include('123 Main St') + expect(response.body).to_not include('111-222-3333') + expect(response.body).to_not include('https://website.gov') + end end describe '#machine_details' do