From 6f290371adaca67f9e8dffe77cc38b3dc92c9258 Mon Sep 17 00:00:00 2001 From: Gregg P <117232882+GcioGregg@users.noreply.github.com> Date: Mon, 23 Dec 2024 13:22:42 -0800 Subject: [PATCH] Vebt 885 - update gibct institutions controller to handle combined name and location searches (#1272) * add name and location combined search * fix comments * fix comments * fix comments * remove unneeded location code * remove unneeded tests * update for backward compatability for non CT pages calling controller * clean up code, remove recent index changes * clean up comments --- app/controllers/v1/institutions_controller.rb | 12 +++-- .../v1/institutions_controller_spec.rb | 49 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/app/controllers/v1/institutions_controller.rb b/app/controllers/v1/institutions_controller.rb index d842e9b41..737aad472 100644 --- a/app/controllers/v1/institutions_controller.rb +++ b/app/controllers/v1/institutions_controller.rb @@ -50,9 +50,15 @@ def index def location @query ||= normalized_query_params - location_results = Institution.approved_institutions(@version).location_search(@query).filter_result_v1(@query) - results = location_results.location_select(@query).location_order + # Start with location-based search + location_results = Institution.approved_institutions(@version) + .location_search(@query) + .filter_result_v1(@query) + + # Add name search if name parameter is present + location_results = location_results.search_v1(name: @query[:name]) if @query[:name].present? + results = location_results.location_select(@query).location_order results = results.filter_high_school if @query[:excluded_school_types]&.include?('HIGH SCHOOL') @meta = { @@ -125,7 +131,7 @@ def normalized_query_params end %i[name category student_veteran_group yellow_ribbon_scholarship principles_of_excellence eight_keys_to_veteran_success stem_offered independent_study priority_enrollment - online_only distance_learning location].each do |k| + online_only distance_learning].each do |k| query[k].try(:downcase!) end %i[latitude longitude distance].each do |k| diff --git a/spec/controllers/v1/institutions_controller_spec.rb b/spec/controllers/v1/institutions_controller_spec.rb index 1f380907c..6e7ff6f5c 100644 --- a/spec/controllers/v1/institutions_controller_spec.rb +++ b/spec/controllers/v1/institutions_controller_spec.rb @@ -385,6 +385,55 @@ def check_boolean_facets(facets) expect(response.media_type).to eq('application/json') expect(response).to match_response_schema('institution_search_results') end + + # tests for coordinate-based name+location search + it 'returns filtered results when searching by both name and coordinates' do + create(:institution, :production_version, :location, institution: 'HARVARD UNIVERSITY') + create(:institution, :production_version, :location, institution: 'BOSTON UNIVERSITY') + + get(:location, params: { + latitude: '32.7876', + longitude: '-79.9403', + distance: '50', + name: 'harvard' + }) + + results = JSON.parse(response.body)['data'] + expect(results.count).to eq(1) + expect(results[0]['attributes']['name']).to eq('HARVARD UNIVERSITY') + expect(response).to match_response_schema('institution_search_results') + end + + it 'returns no results when name does not match within location radius' do + create(:institution, :production_version, :location, institution: 'HARVARD UNIVERSITY') + + get(:location, params: { + latitude: '32.7876', + longitude: '-79.9403', + distance: '50', + name: 'stanford' + }) + + expect(JSON.parse(response.body)['data'].count).to eq(0) + expect(response).to match_response_schema('institution_search_results') + end + + it 'maintains accurate count in meta for coordinate and name search' do + create_list(:institution, 3, :production_version, :location) + create(:institution, :production_version, :location, institution: 'HARVARD UNIVERSITY') + + get(:location, params: { + latitude: '32.7876', + longitude: '-79.9403', + distance: '50', + name: 'harvard' + }) + + body = JSON.parse(response.body) + expect(body['data'].count).to eq(1) + expect(body['meta']['count']).to eq(1) + expect(response).to match_response_schema('institution_search_results') + end end context 'with compare results' do