diff --git a/app/controllers/new_gestionnaire/recherche_controller.rb b/app/controllers/new_gestionnaire/recherche_controller.rb index 0f7dcf159..58658a08b 100644 --- a/app/controllers/new_gestionnaire/recherche_controller.rb +++ b/app/controllers/new_gestionnaire/recherche_controller.rb @@ -4,10 +4,9 @@ module NewGestionnaire @search_terms = params[:q] # exact id match? - if @search_terms.to_i != 0 - @dossiers = current_gestionnaire.dossiers.where(id: @search_terms.to_i) + - current_gestionnaire.dossiers_from_avis.where(id: @search_terms.to_i) - @dossiers.uniq! + id = @search_terms.to_i + if id != 0 && id_compatible?(id) # Sometimes gestionnaire is searching dossiers with a big number (ex: SIRET), ActiveRecord can't deal with them and throws ActiveModel::RangeError. id_compatible? prevents this. + @dossiers = dossiers_by_id(id) end if @dossiers.nil? @@ -23,5 +22,22 @@ module NewGestionnaire ).results end end + + private + + def dossiers_by_id(id) + dossiers = current_gestionnaire.dossiers.where(id: id) + + current_gestionnaire.dossiers_from_avis.where(id: id) + dossiers.uniq + end + + def id_compatible?(number) + begin + ActiveRecord::Type::Integer.new.serialize(number) + true + rescue ActiveModel::RangeError + false + end + end end end diff --git a/spec/controllers/new_gestionnaire/recherche_controller_spec.rb b/spec/controllers/new_gestionnaire/recherche_controller_spec.rb index 7e4a67039..4e3ac6288 100644 --- a/spec/controllers/new_gestionnaire/recherche_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/recherche_controller_spec.rb @@ -36,6 +36,17 @@ describe NewGestionnaire::RechercheController, type: :controller do expect(assigns(:dossiers).count).to eq(0) end end + + context 'with an id out of range' do + let(:query) { 123456789876543234567 } + + it { is_expected.to have_http_status(200) } + + it 'does not return the dossier' do + subject + expect(assigns(:dossiers).count).to eq(0) + end + end end end end