Merge pull request #5590 from Keirua/flipper-remove-cached_notifications

flipper remove :cached_notifications
This commit is contained in:
Keirua 2020-09-21 10:45:15 +02:00 committed by GitHub
commit b6fe36154f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 15 additions and 57 deletions

View file

@ -288,17 +288,7 @@ class Dossier < ApplicationRecord
scope :for_procedure, -> (procedure) { includes(:user, :groupe_instructeur).where(groupe_instructeurs: { procedure: procedure }) }
scope :for_api_v2, -> { includes(procedure: [:administrateurs], etablissement: [], individual: [], traitements: []) }
# todo: once we are sure with_cached_notifications does not introduce regressions, remove with_unoptimized_notifications
# and use with_cached_notifications instead
scope :with_notifications, -> (instructeur) do
if Flipper.enabled?(:cached_notifications, instructeur)
return with_cached_notifications
else
return with_unoptimized_notifications
end
end
scope :with_cached_notifications, -> do
scope :with_notifications, -> do
joins(:follows)
.where('last_champ_updated_at > follows.demande_seen_at' \
' OR groupe_instructeur_updated_at > follows.demande_seen_at' \
@ -308,33 +298,6 @@ class Dossier < ApplicationRecord
.distinct
end
scope :with_unoptimized_notifications, -> do
# This scope is meant to be composed, typically with Instructeur.followed_dossiers, which means that the :follows table is already INNER JOINed;
# it will fail otherwise
# rubocop:disable DS/ApplicationName
joined_dossiers = joins('LEFT OUTER JOIN "champs" ON "champs" . "dossier_id" = "dossiers" . "id" AND "champs" . "parent_id" IS NULL AND "champs" . "private" = FALSE AND "champs"."updated_at" > "follows"."demande_seen_at"')
.joins('LEFT OUTER JOIN "champs" "champs_privates_dossiers" ON "champs_privates_dossiers" . "dossier_id" = "dossiers" . "id" AND "champs_privates_dossiers" . "parent_id" IS NULL AND "champs_privates_dossiers" . "private" = TRUE AND "champs_privates_dossiers"."updated_at" > "follows"."annotations_privees_seen_at"')
.joins('LEFT OUTER JOIN "avis" ON "avis" . "dossier_id" = "dossiers" . "id" AND avis.updated_at > follows.avis_seen_at')
.joins('LEFT OUTER JOIN "commentaires" ON "commentaires" . "dossier_id" = "dossiers" . "id" and commentaires.updated_at > follows.messagerie_seen_at and "commentaires"."email" != \'contact@tps.apientreprise.fr\' AND "commentaires"."email" != \'contact@demarches-simplifiees.fr\'')
# rubocop:enable DS/ApplicationName
updated_demandes = joined_dossiers
.where('champs.updated_at > follows.demande_seen_at OR groupe_instructeur_updated_at > follows.demande_seen_at')
updated_annotations = joined_dossiers
.where('champs_privates_dossiers.updated_at > follows.annotations_privees_seen_at')
updated_avis = joined_dossiers
.where('avis.updated_at > follows.avis_seen_at')
updated_messagerie = joined_dossiers
.where('commentaires.updated_at > follows.messagerie_seen_at')
.where.not(commentaires: { email: OLD_CONTACT_EMAIL })
.where.not(commentaires: { email: CONTACT_EMAIL })
updated_demandes.or(updated_annotations).or(updated_avis).or(updated_messagerie).distinct
end
accepts_nested_attributes_for :individual
delegate :siret, :siren, to: :etablissement, allow_nil: true

View file

@ -141,14 +141,14 @@ class Instructeur < ApplicationRecord
.where(groupe_instructeur: target_groupes)
.send(scope) # :en_cours or :termine or :not_archived (or any other Dossier scope)
.merge(followed_dossiers)
.with_notifications(self)
.with_notifications
end
def procedure_ids_with_notifications(scope)
groupe_instructeur_ids = Dossier
.send(scope) # :en_cours or :termine (or any other Dossier scope)
.merge(followed_dossiers)
.with_notifications(self)
.with_notifications
.select(:groupe_instructeur_id)
GroupeInstructeur.where(id: groupe_instructeur_ids).pluck(:procedure_id)

View file

@ -91,7 +91,7 @@ class ProcedurePresentation < ApplicationRecord
case table
when 'notifications'
dossiers_id_with_notification = dossiers.merge(instructeur.followed_dossiers).with_notifications(instructeur).ids
dossiers_id_with_notification = dossiers.merge(instructeur.followed_dossiers).with_notifications.ids
if order == 'desc'
return dossiers_id_with_notification +
(dossiers.order('dossiers.updated_at desc').ids - dossiers_id_with_notification)

View file

@ -454,9 +454,9 @@ describe Instructeurs::DossiersController, type: :controller do
before { follower.follow(dossier) }
it 'the follower has a notification' do
expect(follower.followed_dossiers.with_notifications(follower)).to eq([])
expect(follower.followed_dossiers.with_notifications).to eq([])
subject
expect(follower.followed_dossiers.with_notifications(follower)).to eq([dossier.reload])
expect(follower.followed_dossiers.with_notifications).to eq([dossier.reload])
end
end
end
@ -671,8 +671,8 @@ describe Instructeurs::DossiersController, type: :controller do
it 'updates the annotations' do
Timecop.travel(now + 1.hour)
expect(instructeur.followed_dossiers.with_notifications(instructeur)).to eq([])
expect(another_instructeur.followed_dossiers.with_notifications(instructeur)).to eq([dossier.reload])
expect(instructeur.followed_dossiers.with_notifications).to eq([])
expect(another_instructeur.followed_dossiers.with_notifications).to eq([dossier.reload])
end
end

View file

@ -693,9 +693,9 @@ describe Users::DossiersController, type: :controller do
end
it 'the follower has a notification' do
expect(instructeur.reload.followed_dossiers.with_notifications(instructeur)).to eq([])
expect(instructeur.reload.followed_dossiers.with_notifications).to eq([])
subject
expect(instructeur.reload.followed_dossiers.with_notifications(instructeur)).to eq([dossier.reload])
expect(instructeur.reload.followed_dossiers.with_notifications).to eq([dossier.reload])
end
end
@ -887,8 +887,6 @@ describe Users::DossiersController, type: :controller do
before do
Timecop.freeze(now)
sign_in(user)
# Flipper.enable(:cached_notifications, instructeur_with_instant_message)
# Flipper.enable(:cached_notifications, instructeur_without_instant_message)
allow(ClamavService).to receive(:safe_file?).and_return(scan_result)
allow(DossierMailer).to receive(:notify_new_commentaire_to_instructeur).and_return(double(deliver_later: nil))
instructeur_with_instant_message.follow(dossier)
@ -913,15 +911,15 @@ describe Users::DossiersController, type: :controller do
context 'notification' do
before 'instructeurs have no notification before the message' do
expect(instructeur_with_instant_message.followed_dossiers.with_notifications(instructeur_with_instant_message)).to eq([])
expect(instructeur_without_instant_message.followed_dossiers.with_notifications(instructeur_without_instant_message)).to eq([])
expect(instructeur_with_instant_message.followed_dossiers.with_notifications).to eq([])
expect(instructeur_without_instant_message.followed_dossiers.with_notifications).to eq([])
Timecop.travel(now + 1.day)
subject
end
it 'adds them a notification' do
expect(instructeur_with_instant_message.reload.followed_dossiers.with_notifications(instructeur_with_instant_message)).to eq([dossier.reload])
expect(instructeur_without_instant_message.reload.followed_dossiers.with_notifications(instructeur_without_instant_message)).to eq([dossier.reload])
expect(instructeur_with_instant_message.reload.followed_dossiers.with_notifications).to eq([dossier.reload])
expect(instructeur_without_instant_message.reload.followed_dossiers.with_notifications).to eq([dossier.reload])
end
end
end

View file

@ -103,10 +103,9 @@ describe Dossier do
before do
create(:follow, dossier: dossier, instructeur: instructeur, messagerie_seen_at: 2.hours.ago)
Flipper.enable_actor(:cached_notifications, instructeur)
end
subject { instructeur.followed_dossiers.with_notifications(instructeur) }
subject { instructeur.followed_dossiers.with_notifications }
context('without changes') do
it { is_expected.to eq [] }

View file

@ -8,7 +8,6 @@ describe Instructeur, type: :model do
before do
assign(procedure_2)
Flipper.enable_actor(:cached_notifications, instructeur)
end
describe 'follow' do

View file

@ -262,7 +262,6 @@ describe ProcedurePresentation do
let!(:older_dossier) { create(:dossier, :en_construction, procedure: procedure) }
before do
Flipper.enable_actor(:cached_notifications, instructeur)
notified_dossier.update!(last_champ_updated_at: Time.zone.local(2018, 9, 20))
create(:follow, instructeur: instructeur, dossier: notified_dossier, demande_seen_at: Time.zone.local(2018, 9, 10))
notified_dossier.touch(time: Time.zone.local(2018, 9, 20))