2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-04-29 09:32:47 +02:00
|
|
|
class RechercheController < ApplicationController
|
|
|
|
before_action :authenticate_logged_user!
|
|
|
|
ITEMS_PER_PAGE = 25
|
2024-10-07 15:00:05 +02:00
|
|
|
|
|
|
|
# the columns are generally procedure specific
|
|
|
|
# but in the search context, we are looking for dossiers from multiple procedures
|
|
|
|
# so we are faking the columns with a random procedure_id
|
2021-04-29 09:32:47 +02:00
|
|
|
PROJECTIONS = [
|
2024-10-07 15:00:05 +02:00
|
|
|
Column.new(procedure_id: 666, table: 'procedure', column: 'libelle'),
|
|
|
|
Column.new(procedure_id: 666, table: 'user', column: 'email'),
|
|
|
|
Column.new(procedure_id: 666, table: 'procedure', column: 'procedure_id')
|
2021-04-29 09:32:47 +02:00
|
|
|
]
|
|
|
|
|
2024-09-11 15:13:16 +02:00
|
|
|
def nav_bar_profile
|
|
|
|
return super if request.blank? # Controller introspection does not contains params/request, see NavBarProfileConcern
|
|
|
|
|
|
|
|
context_params = params[:context]&.to_sym
|
|
|
|
case context_params
|
|
|
|
when :instructeur, :expert
|
|
|
|
context_params
|
|
|
|
else
|
|
|
|
:user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-29 09:32:47 +02:00
|
|
|
def index
|
|
|
|
@search_terms = search_terms
|
2023-10-17 14:25:14 +02:00
|
|
|
@dossiers_count = 0
|
|
|
|
|
|
|
|
if instructeur_signed_in? && DossierSearchService.id_compatible?(@search_terms)
|
|
|
|
@deleted_dossier = current_instructeur.deleted_dossiers.find_by(dossier_id: @search_terms)
|
|
|
|
end
|
|
|
|
|
|
|
|
return if @deleted_dossier.present?
|
2021-05-17 15:42:21 +02:00
|
|
|
|
2021-05-19 14:52:54 +02:00
|
|
|
@instructeur_dossiers_ids = DossierSearchService
|
|
|
|
.matching_dossiers(current_instructeur&.dossiers, @search_terms, with_annotation: true)
|
2021-05-17 15:42:21 +02:00
|
|
|
|
2021-05-19 14:52:54 +02:00
|
|
|
expert_dossier_ids = DossierSearchService
|
|
|
|
.matching_dossiers(current_expert&.dossiers, @search_terms)
|
2021-04-29 09:32:47 +02:00
|
|
|
|
2021-05-19 14:52:54 +02:00
|
|
|
matching_dossiers_ids = (@instructeur_dossiers_ids + expert_dossier_ids).uniq
|
2021-05-17 15:42:21 +02:00
|
|
|
|
2021-04-29 09:32:47 +02:00
|
|
|
@paginated_ids = Kaminari
|
2021-05-19 14:52:54 +02:00
|
|
|
.paginate_array(matching_dossiers_ids)
|
2021-04-29 09:32:47 +02:00
|
|
|
.page(page)
|
|
|
|
.per(ITEMS_PER_PAGE)
|
|
|
|
|
|
|
|
@projected_dossiers = DossierProjectionService.project(@paginated_ids, PROJECTIONS)
|
2021-05-17 15:42:21 +02:00
|
|
|
|
2021-05-19 14:52:54 +02:00
|
|
|
@dossiers_count = matching_dossiers_ids.count
|
2021-05-17 15:42:21 +02:00
|
|
|
@followed_dossiers_id = current_instructeur&.followed_dossiers&.where(id: @paginated_ids)&.ids || []
|
2021-05-19 14:52:54 +02:00
|
|
|
@dossier_avis_ids_h = current_expert&.avis&.where(dossier_id: @paginated_ids)&.pluck(:dossier_id, :id).to_h || {}
|
2024-01-11 18:35:19 +01:00
|
|
|
@notifications_dossier_ids = current_instructeur&.notifications_for_dossiers(@paginated_ids) || []
|
2022-11-03 17:39:12 +01:00
|
|
|
|
2022-11-07 17:00:31 +01:00
|
|
|
# if an instructor search for a dossier which is in his procedures but not available to his intructor group
|
|
|
|
# we want to display an alert in view
|
2022-11-03 17:39:12 +01:00
|
|
|
|
2022-11-07 17:00:31 +01:00
|
|
|
# to make it simpler we only do it if the @search_terms is an id
|
|
|
|
return if !DossierSearchService.id_compatible?(@search_terms)
|
2022-11-03 17:39:12 +01:00
|
|
|
|
2023-08-24 15:44:22 +02:00
|
|
|
dossier_instructeur_searched_for = Dossier.state_not_brouillon.find_by(id: @search_terms)
|
2022-11-07 17:00:31 +01:00
|
|
|
|
|
|
|
return if dossier_instructeur_searched_for.nil?
|
|
|
|
return if current_instructeur&.groupe_instructeur_ids&.include?(dossier_instructeur_searched_for.groupe_instructeur_id)
|
|
|
|
|
|
|
|
if current_instructeur&.procedures&.include?(dossier_instructeur_searched_for.procedure)
|
|
|
|
@dossier_not_in_instructor_group = dossier_instructeur_searched_for
|
|
|
|
else
|
|
|
|
return
|
|
|
|
end
|
2021-04-29 09:32:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def page
|
|
|
|
params[:page].presence || 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_terms
|
|
|
|
params[:q]
|
|
|
|
end
|
|
|
|
end
|