From 9b8d2f6fd38dd6394e756e16310c05c2e3abdd89 Mon Sep 17 00:00:00 2001 From: kara Diaby Date: Mon, 25 Jan 2021 13:02:22 +0100 Subject: [PATCH] instead of call Avis table, we now call Experts Procedure table to see the experts list --- .../procedures_controller.rb | 2 +- app/models/avis.rb | 10 ----- app/models/experts_procedure.rb | 11 +++++ spec/models/avis_spec.rb | 42 ------------------- spec/models/experts_procedure_spec.rb | 42 +++++++++++++++++++ .../invited_expert_list.html.haml_spec.rb | 19 +++++---- 6 files changed, 65 insertions(+), 61 deletions(-) create mode 100644 spec/models/experts_procedure_spec.rb diff --git a/app/controllers/new_administrateur/procedures_controller.rb b/app/controllers/new_administrateur/procedures_controller.rb index 2f250ae2e..1e74e2312 100644 --- a/app/controllers/new_administrateur/procedures_controller.rb +++ b/app/controllers/new_administrateur/procedures_controller.rb @@ -186,7 +186,7 @@ module NewAdministrateur end def invited_expert_list - @invited_expert_emails = Avis.invited_expert_emails(@procedure) + @invited_expert_emails = ExpertsProcedure.invited_expert_emails(@procedure) end private diff --git a/app/models/avis.rb b/app/models/avis.rb index a0d74704b..ba410c41f 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -67,16 +67,6 @@ class Avis < ApplicationRecord Avis.find_by(id: avis_id)&.email == email end - def self.invited_expert_emails(procedure) - Avis - .joins(dossier: :revision) - .left_joins(instructeur: :user) - .where(procedure_revisions: { procedure_id: procedure }) - .map(&:email_to_display) - .uniq - .sort - end - def spreadsheet_columns [ ['Dossier ID', dossier_id.to_s], diff --git a/app/models/experts_procedure.rb b/app/models/experts_procedure.rb index 203f927fc..d874fdb36 100644 --- a/app/models/experts_procedure.rb +++ b/app/models/experts_procedure.rb @@ -14,4 +14,15 @@ class ExpertsProcedure < ApplicationRecord belongs_to :procedure has_many :avis, dependent: :destroy + + def email_to_display + expert&.email + end + + def self.invited_expert_emails(procedure) + joins(:expert) + .where(procedure: procedure) + .map(&:email_to_display) + .sort + end end diff --git a/spec/models/avis_spec.rb b/spec/models/avis_spec.rb index 2e66a90b2..90f10ce76 100644 --- a/spec/models/avis_spec.rb +++ b/spec/models/avis_spec.rb @@ -217,46 +217,4 @@ RSpec.describe Avis, type: :model do end end end - - describe '#invited_expert_emails' do - let!(:procedure) { create(:procedure, :published) } - - subject { Avis.invited_expert_emails(procedure) } - - context 'when there is one dossier' do - let!(:dossier) { create(:dossier, procedure: procedure) } - - context 'when a procedure has one avis and unknown instructeur' do - let!(:avis) { create(:avis, dossier: dossier, email: 'expert@expert.com') } - - it { is_expected.to eq(['expert@expert.com']) } - end - - context 'when a procedure has one avis and known instructeur' do - let!(:avis) { create(:avis, dossier: dossier, instructeur: create(:instructeur, email: 'expert@expert.com')) } - - it { is_expected.to eq(['expert@expert.com']) } - end - - context 'when a dossier has 2 avis from the same expert' do - let!(:avis) { create(:avis, dossier: dossier, email: 'expert@expert.com') } - let!(:avis2) { create(:avis, dossier: dossier, email: 'expert@expert.com') } - - it { is_expected.to eq(['expert@expert.com']) } - end - end - - context 'when there are two dossiers' do - let!(:dossier) { create(:dossier, procedure: procedure) } - let!(:dossier2) { create(:dossier, procedure: procedure) } - - context 'and each one has an avis from 3 different experts' do - let!(:avis) { create(:avis, dossier: dossier, instructeur: create(:instructeur, email: '2_expert@expert.com')) } - let!(:unaffected_avis) { create(:avis, dossier: dossier2, email: '3_expert@expert.com') } - let!(:unaffected_avis2) { create(:avis, dossier: dossier2, email: '1_expert@expert.com') } - - it { is_expected.to eq(['1_expert@expert.com', '2_expert@expert.com', '3_expert@expert.com']) } - end - end - end end diff --git a/spec/models/experts_procedure_spec.rb b/spec/models/experts_procedure_spec.rb new file mode 100644 index 000000000..efd227593 --- /dev/null +++ b/spec/models/experts_procedure_spec.rb @@ -0,0 +1,42 @@ +RSpec.describe ExpertsProcedure, type: :model do + describe '#invited_expert_emails' do + let!(:procedure) { create(:procedure, :published) } + let(:expert) { create(:expert) } + let(:expert2) { create(:expert) } + let(:expert3) { create(:expert) } + let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) } + let(:experts_procedure2) { ExpertsProcedure.create(expert: expert2, procedure: procedure) } + let(:experts_procedure3) { ExpertsProcedure.create(expert: expert3, procedure: procedure) } + subject { ExpertsProcedure.invited_expert_emails(procedure) } + + context 'when there is one dossier' do + let!(:dossier) { create(:dossier, procedure: procedure) } + + context 'when a procedure has one avis and known instructeur' do + let!(:avis) { create(:avis, dossier: dossier, instructeur: create(:instructeur, email: expert.email), experts_procedure: experts_procedure) } + + it { is_expected.to eq([expert.email]) } + end + + context 'when a dossier has 2 avis from the same expert' do + let!(:avis) { create(:avis, dossier: dossier, experts_procedure: experts_procedure) } + let!(:avis2) { create(:avis, dossier: dossier, experts_procedure: experts_procedure) } + + it { is_expected.to eq([expert.email]) } + end + end + + context 'when there are two dossiers' do + let!(:dossier) { create(:dossier, procedure: procedure) } + let!(:dossier2) { create(:dossier, procedure: procedure) } + + context 'and each one has an avis from 3 different experts' do + let!(:avis) { create(:avis, dossier: dossier, experts_procedure: experts_procedure) } + let!(:avis2) { create(:avis, dossier: dossier2, experts_procedure: experts_procedure2) } + let!(:avis3) { create(:avis, dossier: dossier2, experts_procedure: experts_procedure3) } + + it { is_expected.to eq([expert.email, expert2.email, expert3.email].sort) } + end + end + end +end diff --git a/spec/views/new_administrateur/procedures/invited_expert_list.html.haml_spec.rb b/spec/views/new_administrateur/procedures/invited_expert_list.html.haml_spec.rb index ff575ed94..675353a80 100644 --- a/spec/views/new_administrateur/procedures/invited_expert_list.html.haml_spec.rb +++ b/spec/views/new_administrateur/procedures/invited_expert_list.html.haml_spec.rb @@ -12,7 +12,7 @@ describe 'new_administrateur/procedures/invited_expert_list.html.haml', type: :v context 'when the procedure has 0 avis' do let!(:dossier) { create(:dossier, procedure: procedure) } before do - @invited_expert_emails = Avis.invited_expert_emails(procedure) + @invited_expert_emails = ExpertsProcedure.invited_expert_emails(procedure) subject end @@ -24,18 +24,21 @@ describe 'new_administrateur/procedures/invited_expert_list.html.haml', type: :v context 'when the procedure has 3 avis from 2 experts and 1 unasigned' do let!(:dossier) { create(:dossier, procedure: procedure) } - let!(:avis) { create(:avis, dossier: dossier, instructeur: create(:instructeur, email: '1_expert@expert.com')) } - let!(:avis2) { create(:avis, dossier: dossier, instructeur: create(:instructeur, email: '2_expert@expert.com')) } - let!(:unasigned_avis) { create(:avis, dossier: dossier, email: 'expert@expert.com') } + let(:expert) { create(:expert) } + let(:expert2) { create(:expert) } + let(:experts_procedure) { ExpertsProcedure.create(procedure: procedure, expert: expert) } + let(:experts_procedure2) { ExpertsProcedure.create(procedure: procedure, expert: expert2) } + let!(:avis) { create(:avis, dossier: dossier, experts_procedure: experts_procedure) } + let!(:avis2) { create(:avis, dossier: dossier, experts_procedure: experts_procedure2) } before do - @invited_expert_emails = Avis.invited_expert_emails(procedure) + @invited_expert_emails = ExpertsProcedure.invited_expert_emails(procedure) subject end - it 'has 3 experts and match array' do - expect(@invited_expert_emails.count).to eq(3) - expect(@invited_expert_emails).to eq(['1_expert@expert.com', '2_expert@expert.com', 'expert@expert.com']) + it 'has 2 experts and match array' do + expect(@invited_expert_emails.count).to eq(2) + expect(@invited_expert_emails).to eq([expert.email, expert2.email]) end end end