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-25 18:48:14 +02:00
|
|
|
after_commit :index_search_terms_later, if: -> { previously_new_record? || user_previously_changed? || mandataire_first_name_previously_changed? || mandataire_last_name_previously_changed? }
|
2023-04-22 19:54:41 +02:00
|
|
|
|
2024-04-25 17:44:05 +02:00
|
|
|
SEARCH_TERMS_DEBOUNCE = 30.seconds
|
|
|
|
|
2024-04-25 17:57:55 +02:00
|
|
|
kredis_flag :debounce_index_search_terms_flag
|
2024-04-25 17:44:05 +02:00
|
|
|
|
2024-04-25 17:57:55 +02:00
|
|
|
def index_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,
|
2024-04-25 17:46:21 +02:00
|
|
|
individual&.prenom,
|
|
|
|
mandataire_first_name,
|
|
|
|
mandataire_last_name
|
2023-04-22 19:54:41 +02:00
|
|
|
].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
|
|
|
|
|
2024-04-25 17:57:55 +02:00
|
|
|
def index_search_terms_later
|
|
|
|
return if debounce_index_search_terms_flag.marked?
|
2024-04-25 17:44:05 +02:00
|
|
|
|
2024-04-25 17:57:55 +02:00
|
|
|
debounce_index_search_terms_flag.mark(expires_in: SEARCH_TERMS_DEBOUNCE)
|
|
|
|
DossierIndexSearchTermsJob.set(wait: SEARCH_TERMS_DEBOUNCE).perform_later(self)
|
2023-04-22 19:54:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|