2024-04-25 17:44:05 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-04-22 19:54:41 +02:00
|
|
|
module DossierSearchableConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2024-04-03 10:21:50 +02:00
|
|
|
after_commit :update_search_terms_later
|
2023-04-22 19:54:41 +02:00
|
|
|
|
2024-04-25 17:44:05 +02:00
|
|
|
SEARCH_TERMS_DEBOUNCE = 30.seconds
|
|
|
|
|
|
|
|
kredis_flag :debounce_update_search_terms_flag
|
|
|
|
|
2023-04-22 19:54:41 +02:00
|
|
|
def update_search_terms
|
2024-04-25 17:51:54 +02:00
|
|
|
DossierPreloader.load_one(self)
|
|
|
|
|
2024-04-03 10:21:50 +02:00
|
|
|
search_terms = [
|
2023-04-22 19:54:41 +02:00
|
|
|
user&.email,
|
|
|
|
*champs_public.flat_map(&:search_terms),
|
|
|
|
*etablissement&.search_terms,
|
|
|
|
individual&.nom,
|
|
|
|
individual&.prenom
|
|
|
|
].compact_blank.join(' ')
|
|
|
|
|
2024-04-03 10:21:50 +02:00
|
|
|
private_search_terms = champs_private.flat_map(&:search_terms).compact_blank.join(' ')
|
|
|
|
|
|
|
|
sql = "UPDATE dossiers SET search_terms = :search_terms, private_search_terms = :private_search_terms WHERE id = :id"
|
|
|
|
sanitized_sql = self.class.sanitize_sql_array([sql, search_terms:, private_search_terms:, id:])
|
|
|
|
self.class.connection.execute(sanitized_sql)
|
2023-04-22 19:54:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_search_terms_later
|
2024-04-25 17:44:05 +02:00
|
|
|
return if debounce_update_search_terms_flag.marked?
|
|
|
|
|
|
|
|
debounce_update_search_terms_flag.mark(expires_in: SEARCH_TERMS_DEBOUNCE)
|
|
|
|
DossierUpdateSearchTermsJob.set(wait: SEARCH_TERMS_DEBOUNCE).perform_later(self)
|
2023-04-22 19:54:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|