diff --git a/app/controllers/instructeurs/recherche_controller.rb b/app/controllers/instructeurs/recherche_controller.rb deleted file mode 100644 index 298abb3ea..000000000 --- a/app/controllers/instructeurs/recherche_controller.rb +++ /dev/null @@ -1,12 +0,0 @@ -module Instructeurs - class RechercheController < InstructeurController - def index - @search_terms = params[:q] - @dossiers = DossierSearchService.matching_dossiers_for_instructeur(@search_terms, current_instructeur) - @followed_dossiers_id = current_instructeur - .followed_dossiers - .where(groupe_instructeur_id: @dossiers.pluck(:groupe_instructeur_id)) - .pluck(:id) - end - end -end diff --git a/app/controllers/recherche_controller.rb b/app/controllers/recherche_controller.rb new file mode 100644 index 000000000..681ddb166 --- /dev/null +++ b/app/controllers/recherche_controller.rb @@ -0,0 +1,53 @@ +class RechercheController < ApplicationController + before_action :authenticate_logged_user! + ITEMS_PER_PAGE = 25 + PROJECTIONS = [ + { "table" => 'procedure', "column" => 'libelle' }, + { "table" => 'user', "column" => 'email' }, + { "table" => 'procedure', "column" => 'procedure_id' } + ] + + def index + @search_terms = search_terms + matching_dossiers_ids = [] + instructeur_dossiers_ids = [] + expert_dossiers_ids = [] + + if instructeur_signed_in? + instructeur_dossiers_ids.concat(current_instructeur.dossiers.ids) + @followed_dossiers_id = current_instructeur.followed_dossiers.where(id: instructeur_dossiers_ids).ids + + if instructeur_dossiers_ids.present? + matching_dossiers_ids.concat(DossierSearchService.matching_dossiers(instructeur_dossiers_ids, @search_terms, true)) + end + end + + if expert_signed_in? + @dossier_avis_ids_h = current_expert.avis.pluck(:dossier_id, :id).to_h + expert_dossiers_ids.concat(@dossier_avis_ids_h.keys) + + if expert_dossiers_ids.present? + matching_dossiers_ids.concat(DossierSearchService.matching_dossiers(expert_dossiers_ids, @search_terms)) + end + end + + @paginated_ids = Kaminari + .paginate_array(matching_dossiers_ids.uniq) + .page(page) + .per(ITEMS_PER_PAGE) + + @dossiers_count = matching_dossiers_ids.count + + @projected_dossiers = DossierProjectionService.project(@paginated_ids, PROJECTIONS) + end + + private + + def page + params[:page].presence || 1 + end + + def search_terms + params[:q] + end +end