2018-07-31 12:03:01 +02:00
class DossierSearchService
2018-07-31 12:04:49 +02:00
def self . matching_dossiers_for_gestionnaire ( search_terms , gestionnaire )
2018-07-31 12:03:01 +02:00
# exact id match?
2018-07-31 14:29:37 +02:00
dossiers = dossier_by_exact_id_for_gestionnaire ( search_terms , gestionnaire )
2018-07-31 12:03:01 +02:00
# full text search
if dossiers . empty?
dossiers = Search . new (
2018-07-31 12:04:49 +02:00
gestionnaire : gestionnaire ,
2018-07-31 12:03:01 +02:00
query : search_terms
) . results
end
dossiers
end
private
2018-07-31 14:29:37 +02:00
def self . dossier_by_exact_id_for_gestionnaire ( search_terms , gestionnaire )
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 , gestionnaire )
end
if dossiers . nil?
dossiers = Dossier . none
end
end
2018-07-31 12:04:49 +02:00
def self . dossiers_by_id ( id , gestionnaire )
dossiers = gestionnaire . dossiers . where ( id : id ) +
gestionnaire . dossiers_from_avis . where ( id : id )
2018-07-31 12:03:01 +02:00
dossiers . uniq
end
def self . id_compatible? ( number )
begin
ActiveRecord :: Type :: Integer . new . serialize ( number )
true
rescue ActiveModel :: RangeError
false
end
end
end