From b2b5b4a45608f8351c7ab0ee7f85a56ac0e19082 Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Fri, 28 Jun 2024 11:38:48 +0100 Subject: [PATCH] FE journey school search only returns FE bodies --- .../javascripts/components/college_search.js.erb | 4 +++- app/controllers/school_search_controller.rb | 3 ++- .../select_provision_form.rb | 4 ++-- app/models/school.rb | 10 ++++++++-- .../further_education_provision_search.html.erb | 1 + spec/factories/schools.rb | 4 ++++ .../happy_js_path_spec.rb | 2 +- .../happy_path_spec.rb | 2 +- spec/models/school_spec.rb | 8 +++++++- spec/requests/school_search_spec.rb | 16 ++++++++++++++++ 10 files changed, 45 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/components/college_search.js.erb b/app/assets/javascripts/components/college_search.js.erb index 1bafbf668c..4c95cdd4bb 100644 --- a/app/assets/javascripts/components/college_search.js.erb +++ b/app/assets/javascripts/components/college_search.js.erb @@ -7,6 +7,8 @@ document.addEventListener("DOMContentLoaded", function () { return; } + var feOnly = form.dataset.feOnly || false; + var searchInputContainer = form.querySelector("#autocomplete-container"); if (!searchInputContainer) { @@ -57,7 +59,7 @@ document.addEventListener("DOMContentLoaded", function () { Rails.ajax({ type: "post", url: "<%= Rails.application.routes.url_helpers.school_search_index_path %>", - data: "query=" + query, + data: "query=" + query + "&fe_only=" + feOnly.toString(), success: handleResponse, error: handleResponse }); diff --git a/app/controllers/school_search_controller.rb b/app/controllers/school_search_controller.rb index 4a25d0b5cc..87f977f848 100644 --- a/app/controllers/school_search_controller.rb +++ b/app/controllers/school_search_controller.rb @@ -15,10 +15,11 @@ def search_schools return end + fe_only = ActiveModel::Type::Boolean.new.cast(params[:fe_only]) schools = ActiveModel::Type::Boolean.new.cast(params[:exclude_closed]) ? School.open : School begin - @schools = schools.search(params[:query]) + @schools = schools.search(params[:query], fe_only:) rescue ArgumentError => e raise unless e.message == School::SEARCH_NOT_ENOUGH_CHARACTERS_ERROR diff --git a/app/forms/journeys/further_education_payments/select_provision_form.rb b/app/forms/journeys/further_education_payments/select_provision_form.rb index 094d9a5562..76b7b7ae32 100644 --- a/app/forms/journeys/further_education_payments/select_provision_form.rb +++ b/app/forms/journeys/further_education_payments/select_provision_form.rb @@ -22,9 +22,9 @@ def save def results @results ||= if journey_session.answers.school_id.present? - School.open.where(id: school_id) + School.open.fe_only.where(id: school_id) else - School.open.search(provision_search) + School.open.fe_only.search(provision_search) end end diff --git a/app/models/school.rb b/app/models/school.rb index cd2ab667e5..a9f68f456e 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -15,6 +15,8 @@ class School < ApplicationRecord validates :school_type_group, presence: true validates :school_type, presence: true + scope :fe_only, -> { where(phase: 6) } # PhaseOfEducation == 16 plus + PHASES = { not_applicable: 0, nursery: 1, @@ -119,7 +121,7 @@ class School < ApplicationRecord before_save :sanitise_postcode_search_index - def self.search(search_term) + def self.search(search_term, fe_only: false) raise ArgumentError, SEARCH_NOT_ENOUGH_CHARACTERS_ERROR if search_term.length < SEARCH_MINIMUM_LENGTH search_field = :name @@ -131,10 +133,14 @@ def self.search(search_term) search_field, search_term = [:postcode_sanitised, sanitised_search_term] end - where("#{search_field} ILIKE ?", "%#{sanitize_sql_like(search_term)}%") + sql = where("#{search_field} ILIKE ?", "%#{sanitize_sql_like(search_term)}%") .order(sanitize_sql_for_order([Arel.sql("similarity(#{search_field}, ?) DESC"), search_term])) .order(:name, close_date: :desc) .limit(SEARCH_RESULTS_LIMIT) + + sql = sql.fe_only if fe_only + + sql end def address diff --git a/app/views/further_education_payments/claims/further_education_provision_search.html.erb b/app/views/further_education_payments/claims/further_education_provision_search.html.erb index cd277659f2..6e7291dcb1 100644 --- a/app/views/further_education_payments/claims/further_education_provision_search.html.erb +++ b/app/views/further_education_payments/claims/further_education_provision_search.html.erb @@ -5,6 +5,7 @@ method: :patch, builder: GOVUKDesignSystemFormBuilder::FormBuilder, id: "further-education-provision-search-form", + data: { "fe-only" => true }, html: { novalidate: false } do |f| %> <%= f.govuk_error_summary %> diff --git a/spec/factories/schools.rb b/spec/factories/schools.rb index 70cb2fe9cd..80aa124cc8 100644 --- a/spec/factories/schools.rb +++ b/spec/factories/schools.rb @@ -133,5 +133,9 @@ open_date { 10.days.ago } close_date { nil } end + + trait :further_education do + phase { 6 } + end end end diff --git a/spec/features/further_education_payments/happy_js_path_spec.rb b/spec/features/further_education_payments/happy_js_path_spec.rb index f3eb04fc25..728b02af49 100644 --- a/spec/features/further_education_payments/happy_js_path_spec.rb +++ b/spec/features/further_education_payments/happy_js_path_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" RSpec.feature "Further education payments", js: true, flaky: true do - let(:college) { create(:school) } + let(:college) { create(:school, :further_education) } scenario "happy js path" do when_further_education_payments_journey_configuration_exists diff --git a/spec/features/further_education_payments/happy_path_spec.rb b/spec/features/further_education_payments/happy_path_spec.rb index 03bd448a56..f6213fa09f 100644 --- a/spec/features/further_education_payments/happy_path_spec.rb +++ b/spec/features/further_education_payments/happy_path_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" RSpec.feature "Further education payments" do - let(:college) { create(:school) } + let(:college) { create(:school, :further_education) } scenario "happy path claim" do when_further_education_payments_journey_configuration_exists diff --git a/spec/models/school_spec.rb b/spec/models/school_spec.rb index 2c44c5abe9..35fcccabd8 100644 --- a/spec/models/school_spec.rb +++ b/spec/models/school_spec.rb @@ -12,7 +12,7 @@ describe ".search" do let!(:first_school) { create(:school, name: "Community School London", postcode: "SW1P 3BT") } let!(:second_school) { create(:school, name: "Unity School London", postcode: "SW1P 3BT") } - let!(:third_school) { create(:school, name: "The Unity College Manchester", postcode: "M1 2WD") } + let!(:third_school) { create(:school, :further_education, name: "The Unity College Manchester", postcode: "M1 2WD") } it "returns schools with a name matching the search term" do expect(School.search("School")).to match_array([first_school, second_school]) @@ -38,6 +38,12 @@ it "orders the results by similarity" do expect(School.search("Unity School")).to eq([second_school, first_school]) end + + context "when searching for FE only" do + it "only returns FE bodies" do + expect(School.search("Unity", fe_only: true)).to eq([third_school]) + end + end end describe "#address" do diff --git a/spec/requests/school_search_spec.rb b/spec/requests/school_search_spec.rb index 3e9494835a..7b3ed2d28b 100644 --- a/spec/requests/school_search_spec.rb +++ b/spec/requests/school_search_spec.rb @@ -75,5 +75,21 @@ expect(response.body).to include(other_school_similar_name.name) end end + + context "when searching for FE only" do + let!(:school_3) { create(:school, :further_education, name: "some college") } + + before do + school_1.update(name: "some school") + school_2.update(name: "some school") + end + + it "only returns FE bodies" do + post school_search_index_path, params: {query: "some", fe_only: true} + + expect(JSON.parse(response.body)["data"].size).to eql(1) + expect(response.body).to include("some college") + end + end end end