Add a method to retrieve all invited experts for all procedure dossiers

This commit is contained in:
kara Diaby 2020-12-17 14:28:21 +01:00
parent e63f93f8de
commit 2b06ee95e1
2 changed files with 52 additions and 0 deletions

View file

@ -56,6 +56,16 @@ 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],

View file

@ -200,4 +200,46 @@ 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