demarches-normaliennes/app/services/dossier_search_service.rb

43 lines
1.2 KiB
Ruby
Raw Normal View History

class DossierSearchService
def self.matching_dossiers_for_gestionnaire(search_terms, gestionnaire)
# exact id match?
dossiers = dossier_by_exact_id_for_gestionnaire(search_terms, gestionnaire)
# full text search
if dossiers.empty?
dossier = dossier_by_full_text_for_gestionnaire(search_terms, gestionnaire)
end
dossiers
end
private
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_by_id(id, gestionnaire)
else
Dossier.none
end
end
def self.dossiers_by_id(id, gestionnaire)
2018-07-31 14:53:22 +02:00
(gestionnaire.dossiers.where(id: id) + gestionnaire.dossiers_from_avis.where(id: id)).uniq
end
def self.id_compatible?(number)
2018-07-31 14:45:50 +02:00
ActiveRecord::Type::Integer.new.serialize(number)
true
rescue ActiveModel::RangeError
false
end
def self.dossier_by_full_text_for_gestionnaire(search_terms, gestionnaire)
Search.new(
gestionnaire: gestionnaire,
query: search_terms
).results
end
end