diff --git a/app/models/instructeur.rb b/app/models/instructeur.rb index c03c87a16..86f7aa4e4 100644 --- a/app/models/instructeur.rb +++ b/app/models/instructeur.rb @@ -108,24 +108,17 @@ class Instructeur < ApplicationRecord end def notifications_for_dossier(dossier) - follow = Follow - .includes(dossier: [:champs_public, :champs_private, :avis, :commentaires]) - .find_by(instructeur: self, dossier: dossier) + follow = Follow.find_by(instructeur: self, dossier:) if follow.present? - demande = follow.dossier.champs_public.updated_since?(follow.demande_seen_at).any? || - follow.dossier.groupe_instructeur_updated_at&.>(follow.demande_seen_at) || + demande = dossier.last_champ_updated_at&.>(follow.demande_seen_at) || + dossier.groupe_instructeur_updated_at&.>(follow.demande_seen_at) || dossier.identity_updated_at&.>(follow.demande_seen_at) || false - annotations_privees = follow.dossier.champs_private.updated_since?(follow.annotations_privees_seen_at).any? - - avis_notif = follow.dossier.avis.updated_since?(follow.avis_seen_at).any? - - messagerie = dossier.commentaires - .where.not(email: OLD_CONTACT_EMAIL) - .where.not(email: CONTACT_EMAIL) - .updated_since?(follow.messagerie_seen_at).any? + annotations_privees = dossier.last_champ_private_updated_at&.>(follow.annotations_privees_seen_at) || false + avis_notif = dossier.last_avis_updated_at&.>(follow.avis_seen_at) || false + messagerie = dossier.last_commentaire_updated_at&.>(follow.messagerie_seen_at) || false annotations_hash(demande, annotations_privees, avis_notif, messagerie) else diff --git a/spec/models/instructeur_spec.rb b/spec/models/instructeur_spec.rb index f1f8b95af..249554774 100644 --- a/spec/models/instructeur_spec.rb +++ b/spec/models/instructeur_spec.rb @@ -196,7 +196,10 @@ describe Instructeur, type: :model do end context 'when there is a modification on public champs' do - before { dossier.champs_public.first.update_attribute('value', 'toto') } + before { + dossier.champs_public.first.update(value: 'toto') + dossier.update(last_champ_updated_at: Time.zone.now) + } it { is_expected.to match({ demande: true, annotations_privees: false, avis: false, messagerie: false }) } end @@ -215,20 +218,29 @@ describe Instructeur, type: :model do end context 'when there is a modification on private champs' do - before { dossier.champs_private.first.update_attribute('value', 'toto') } + before { + dossier.champs_private.first.update(value: 'toto') + dossier.update(last_champ_private_updated_at: Time.zone.now) + } it { is_expected.to match({ demande: false, annotations_privees: true, avis: false, messagerie: false }) } end context 'when there is a modification on avis' do - before { create(:avis, dossier: dossier) } + before { + create(:avis, dossier: dossier) + dossier.update(last_avis_updated_at: Time.zone.now) + } it { is_expected.to match({ demande: false, annotations_privees: false, avis: true, messagerie: false }) } end context 'messagerie' do context 'when there is a new commentaire' do - before { create(:commentaire, dossier: dossier, email: 'a@b.com') } + before { + create(:commentaire, dossier: dossier, email: 'a@b.com') + dossier.update(last_commentaire_updated_at: Time.zone.now) + } it { is_expected.to match({ demande: false, annotations_privees: false, avis: false, messagerie: true }) } end