add feature-flip for instructeurs on cached notifications

This commit is contained in:
clemkeirua 2020-09-02 17:44:51 +02:00 committed by Keirua (Rebase PR Action)
parent 52ea5ef89a
commit c7b96d3d43
6 changed files with 49 additions and 5 deletions

View file

@ -288,7 +288,17 @@ 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: []) }
scope :with_notifications, -> do
# 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
joins(:follows)
.where('last_champ_updated_at > follows.demande_seen_at' \
' OR groupe_instructeur_updated_at > follows.demande_seen_at' \
@ -298,6 +308,33 @@ 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
.with_notifications(self)
end
def procedures_with_notifications(scope)
dossiers = Dossier
.send(scope) # :en_cours or :termine (or any other Dossier scope)
.merge(followed_dossiers)
.with_notifications
.with_notifications(self)
Procedure
.where(id: dossiers.joins(:groupe_instructeur)
@ -213,6 +213,10 @@ class Instructeur < ApplicationRecord
def features
end
def flipper_id
"Instructeur:#{id}"
end
private
def annotations_hash(demande, annotations_privees, avis, messagerie)

View file

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

View file

@ -103,9 +103,10 @@ 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 }
subject { instructeur.followed_dossiers.with_notifications(instructeur) }
context('without changes') do
it { is_expected.to eq [] }

View file

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

View file

@ -262,6 +262,7 @@ 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))