feat(search): debounce update search terms
This commit is contained in:
parent
5a2ddcb471
commit
ee465b38ff
2 changed files with 28 additions and 9 deletions
|
@ -1,9 +1,15 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module DossierSearchableConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
after_commit :update_search_terms_later
|
||||
|
||||
SEARCH_TERMS_DEBOUNCE = 30.seconds
|
||||
|
||||
kredis_flag :debounce_update_search_terms_flag
|
||||
|
||||
def update_search_terms
|
||||
search_terms = [
|
||||
user&.email,
|
||||
|
@ -21,7 +27,10 @@ module DossierSearchableConcern
|
|||
end
|
||||
|
||||
def update_search_terms_later
|
||||
DossierUpdateSearchTermsJob.perform_later(self)
|
||||
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)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,8 +2,6 @@ describe DossierSearchableConcern do
|
|||
let(:champ_public) { dossier.champs_public.first }
|
||||
let(:champ_private) { dossier.champs_private.first }
|
||||
|
||||
subject { dossier }
|
||||
|
||||
describe '#update_search_terms' do
|
||||
let(:etablissement) { dossier.etablissement }
|
||||
let(:dossier) { create(:dossier, :with_entreprise, user: user) }
|
||||
|
@ -19,32 +17,44 @@ describe DossierSearchableConcern do
|
|||
).first
|
||||
end
|
||||
|
||||
before do
|
||||
it "update columns" do
|
||||
champ_public.update_attribute(:value, "champ public")
|
||||
champ_private.update_attribute(:value, "champ privé")
|
||||
|
||||
perform_enqueued_jobs(only: DossierUpdateSearchTermsJob)
|
||||
end
|
||||
|
||||
it "update columns" do
|
||||
expect(result["search_terms"]).to eq("#{user.email} champ public #{etablissement.entreprise_siren} #{etablissement.entreprise_numero_tva_intracommunautaire} #{etablissement.entreprise_forme_juridique} #{etablissement.entreprise_forme_juridique_code} #{etablissement.entreprise_nom_commercial} #{etablissement.entreprise_raison_sociale} #{etablissement.entreprise_siret_siege_social} #{etablissement.entreprise_nom} #{etablissement.entreprise_prenom} #{etablissement.association_rna} #{etablissement.association_titre} #{etablissement.association_objet} #{etablissement.siret} #{etablissement.naf} #{etablissement.libelle_naf} #{etablissement.adresse} #{etablissement.code_postal} #{etablissement.localite} #{etablissement.code_insee_localite}")
|
||||
expect(result["private_search_terms"]).to eq('champ privé')
|
||||
end
|
||||
|
||||
context 'with an update' do
|
||||
before do
|
||||
stub_const("DossierSearchableConcern::SEARCH_TERMS_DEBOUNCE", 1.second)
|
||||
end
|
||||
|
||||
it "update columns" do
|
||||
dossier.update(
|
||||
champs_public_attributes: [{ id: champ_public.id, value: 'nouvelle valeur publique' }],
|
||||
champs_private_attributes: [{ id: champ_private.id, value: 'nouvelle valeur privee' }]
|
||||
)
|
||||
|
||||
perform_enqueued_jobs(only: DossierUpdateSearchTermsJob)
|
||||
end
|
||||
|
||||
it "update columns" do
|
||||
expect(result["search_terms"]).to include('nouvelle valeur publique')
|
||||
expect(result["private_search_terms"]).to include('nouvelle valeur privee')
|
||||
end
|
||||
|
||||
it "debounce jobs" do
|
||||
assert_enqueued_jobs(1, only: DossierUpdateSearchTermsJob) do
|
||||
3.times { dossier.index_search_terms_later }
|
||||
end
|
||||
|
||||
# wait redis key expiration
|
||||
sleep 1.01.seconds
|
||||
|
||||
assert_enqueued_jobs(1, only: DossierUpdateSearchTermsJob) do
|
||||
dossier.update(champs_public_attributes: [{ id: champ_public.id, value: rand(10).to_s }])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue