refactor Dossier#with_notifications

This commit is contained in:
clemkeirua 2020-09-18 15:40:26 +02:00 committed by Keirua (Rebase PR Action)
parent d01a53eb72
commit f96377d878
6 changed files with 15 additions and 15 deletions

View file

@ -288,7 +288,7 @@ class Dossier < ApplicationRecord
scope :for_procedure, -> (procedure) { includes(:user, :groupe_instructeur).where(groupe_instructeurs: { procedure: procedure }) } scope :for_procedure, -> (procedure) { includes(:user, :groupe_instructeur).where(groupe_instructeurs: { procedure: procedure }) }
scope :for_api_v2, -> { includes(procedure: [:administrateurs], etablissement: [], individual: [], traitements: []) } scope :for_api_v2, -> { includes(procedure: [:administrateurs], etablissement: [], individual: [], traitements: []) }
scope :with_notifications, -> (instructeur) do scope :with_notifications, -> do
joins(:follows) joins(:follows)
.where('last_champ_updated_at > follows.demande_seen_at' \ .where('last_champ_updated_at > follows.demande_seen_at' \
' OR groupe_instructeur_updated_at > follows.demande_seen_at' \ ' OR groupe_instructeur_updated_at > follows.demande_seen_at' \

View file

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

View file

@ -91,7 +91,7 @@ class ProcedurePresentation < ApplicationRecord
case table case table
when 'notifications' 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' if order == 'desc'
return dossiers_id_with_notification + return dossiers_id_with_notification +
(dossiers.order('dossiers.updated_at desc').ids - 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) } before { follower.follow(dossier) }
it 'the follower has a notification' do 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 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 end
end end
@ -671,8 +671,8 @@ describe Instructeurs::DossiersController, type: :controller do
it 'updates the annotations' do it 'updates the annotations' do
Timecop.travel(now + 1.hour) Timecop.travel(now + 1.hour)
expect(instructeur.followed_dossiers.with_notifications(instructeur)).to eq([]) expect(instructeur.followed_dossiers.with_notifications).to eq([])
expect(another_instructeur.followed_dossiers.with_notifications(instructeur)).to eq([dossier.reload]) expect(another_instructeur.followed_dossiers.with_notifications).to eq([dossier.reload])
end end
end end

View file

@ -693,9 +693,9 @@ describe Users::DossiersController, type: :controller do
end end
it 'the follower has a notification' do 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 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
end end
@ -911,15 +911,15 @@ describe Users::DossiersController, type: :controller do
context 'notification' do context 'notification' do
before 'instructeurs have no notification before the message' 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_with_instant_message.followed_dossiers.with_notifications).to eq([])
expect(instructeur_without_instant_message.followed_dossiers.with_notifications(instructeur_without_instant_message)).to eq([]) expect(instructeur_without_instant_message.followed_dossiers.with_notifications).to eq([])
Timecop.travel(now + 1.day) Timecop.travel(now + 1.day)
subject subject
end end
it 'adds them a notification' do 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_with_instant_message.reload.followed_dossiers.with_notifications).to eq([dossier.reload])
expect(instructeur_without_instant_message.reload.followed_dossiers.with_notifications(instructeur_without_instant_message)).to eq([dossier.reload]) expect(instructeur_without_instant_message.reload.followed_dossiers.with_notifications).to eq([dossier.reload])
end end
end end
end end

View file

@ -105,7 +105,7 @@ describe Dossier do
create(:follow, dossier: dossier, instructeur: instructeur, messagerie_seen_at: 2.hours.ago) create(:follow, dossier: dossier, instructeur: instructeur, messagerie_seen_at: 2.hours.ago)
end end
subject { instructeur.followed_dossiers.with_notifications(instructeur) } subject { instructeur.followed_dossiers.with_notifications }
context('without changes') do context('without changes') do
it { is_expected.to eq [] } it { is_expected.to eq [] }