Merge pull request #9977 from tchak/refactor-instructeur-timestamps

refactor(instructeur): use precomputed timestamps
This commit is contained in:
Paul Chavard 2024-02-09 08:02:28 +00:00 committed by GitHub
commit 79a077f720
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 17 deletions

View file

@ -108,24 +108,17 @@ class Instructeur < ApplicationRecord
end end
def notifications_for_dossier(dossier) def notifications_for_dossier(dossier)
follow = Follow follow = Follow.find_by(instructeur: self, dossier:)
.includes(dossier: [:champs_public, :champs_private, :avis, :commentaires])
.find_by(instructeur: self, dossier: dossier)
if follow.present? if follow.present?
demande = follow.dossier.champs_public.updated_since?(follow.demande_seen_at).any? || demande = dossier.last_champ_updated_at&.>(follow.demande_seen_at) ||
follow.dossier.groupe_instructeur_updated_at&.>(follow.demande_seen_at) || dossier.groupe_instructeur_updated_at&.>(follow.demande_seen_at) ||
dossier.identity_updated_at&.>(follow.demande_seen_at) || dossier.identity_updated_at&.>(follow.demande_seen_at) ||
false false
annotations_privees = follow.dossier.champs_private.updated_since?(follow.annotations_privees_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
avis_notif = follow.dossier.avis.updated_since?(follow.avis_seen_at).any? messagerie = dossier.last_commentaire_updated_at&.>(follow.messagerie_seen_at) || false
messagerie = dossier.commentaires
.where.not(email: OLD_CONTACT_EMAIL)
.where.not(email: CONTACT_EMAIL)
.updated_since?(follow.messagerie_seen_at).any?
annotations_hash(demande, annotations_privees, avis_notif, messagerie) annotations_hash(demande, annotations_privees, avis_notif, messagerie)
else else

View file

@ -196,7 +196,10 @@ describe Instructeur, type: :model do
end end
context 'when there is a modification on public champs' do 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 }) } it { is_expected.to match({ demande: true, annotations_privees: false, avis: false, messagerie: false }) }
end end
@ -215,20 +218,29 @@ describe Instructeur, type: :model do
end end
context 'when there is a modification on private champs' do 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 }) } it { is_expected.to match({ demande: false, annotations_privees: true, avis: false, messagerie: false }) }
end end
context 'when there is a modification on avis' do 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 }) } it { is_expected.to match({ demande: false, annotations_privees: false, avis: true, messagerie: false }) }
end end
context 'messagerie' do context 'messagerie' do
context 'when there is a new commentaire' 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 }) } it { is_expected.to match({ demande: false, annotations_privees: false, avis: false, messagerie: true }) }
end end