[#835] Report failed and successful invitations

This commit is contained in:
Frederic Merizen 2018-10-31 20:38:32 +01:00
parent 8cf235f2fd
commit d564eb1225
3 changed files with 46 additions and 20 deletions

View file

@ -24,21 +24,23 @@ module CreateAvisConcern
end
)
if create_results.all?(&:persisted?)
sent_emails_addresses = create_results.map(&:email_to_display).join(", ")
flash.notice = "Une demande d'avis a été envoyée à #{sent_emails_addresses}"
persisted, failed = create_results.partition(&:persisted?)
nil
else
flash.now.alert = create_results
.map(&:errors)
.reject(&:empty?)
.map(&:full_messages)
.flatten
if persisted.any?
sent_emails_addresses = persisted.map(&:email_to_display).join(", ")
flash.notice = "Une demande d'avis a été envoyée à #{sent_emails_addresses}"
end
if failed.any?
flash.now.alert = failed
.select { |avis| avis.errors.present? }
.map { |avis| "#{avis.email} : #{avis.errors.full_messages.join(', ')}" }
# When an error occurs, return the avis back to the controller
# to give the user a chance to correct and resubmit
Avis.new(create_avis_params)
Avis.new(create_avis_params.merge(emails: [failed.map(&:email).join(", ")]))
else
nil
end
end

View file

@ -100,24 +100,37 @@ describe NewGestionnaire::AvisController, type: :controller do
describe '#create_avis' do
let!(:previous_avis) { Avis.create(dossier: dossier, claimant: claimant, gestionnaire: gestionnaire, confidentiel: previous_avis_confidentiel) }
let(:email) { 'a@b.com' }
let(:emails) { ['a@b.com'] }
let(:intro) { 'introduction' }
let(:created_avis) { Avis.last }
let!(:old_avis_count) { Avis.count }
before do
post :create_avis, params: { id: previous_avis.id, avis: { emails: [email], introduction: intro, confidentiel: asked_confidentiel } }
post :create_avis, params: { id: previous_avis.id, avis: { emails: emails, introduction: intro, confidentiel: asked_confidentiel } }
end
context 'when an invalid email' do
let(:previous_avis_confidentiel) { false }
let(:asked_confidentiel) { false }
let(:email) { "toto.fr" }
let(:emails) { ["toto.fr"] }
it { expect(response).to render_template :instruction }
it { expect(flash.alert).to eq(["Email n'est pas valide"]) }
it { expect(flash.alert).to eq(["toto.fr : Email n'est pas valide"]) }
it { expect(Avis.last).to eq(previous_avis) }
end
context 'with multiple emails' do
let(:asked_confidentiel) { false }
let(:previous_avis_confidentiel) { false }
let(:emails) { ["toto.fr,titi@titimail.com"] }
it { expect(response).to render_template :instruction }
it { expect(flash.alert).to eq(["toto.fr : Email n'est pas valide"]) }
it { expect(flash.notice).to eq("Une demande d'avis a été envoyée à titi@titimail.com") }
it { expect(Avis.count).to eq(old_avis_count + 1) }
it { expect(created_avis.email).to eq("titi@titimail.com") }
end
context 'when the previous avis is public' do
let(:previous_avis_confidentiel) { false }
@ -125,7 +138,7 @@ describe NewGestionnaire::AvisController, type: :controller do
let(:asked_confidentiel) { false }
it { expect(created_avis.confidentiel).to be(false) }
it { expect(created_avis.email).to eq(email) }
it { expect(created_avis.email).to eq(emails.last) }
it { expect(created_avis.introduction).to eq(intro) }
it { expect(created_avis.dossier).to eq(previous_avis.dossier) }
it { expect(created_avis.claimant).to eq(gestionnaire) }

View file

@ -308,12 +308,13 @@ describe NewGestionnaire::DossiersController, type: :controller do
describe "#create_avis" do
let(:saved_avis) { dossier.avis.first }
let!(:old_avis_count) { Avis.count }
subject do
post :create_avis, params: {
procedure_id: procedure.id,
dossier_id: dossier.id,
avis: { emails: [email], introduction: 'intro', confidentiel: true }
avis: { emails: emails, introduction: 'intro', confidentiel: true }
}
end
@ -321,7 +322,7 @@ describe NewGestionnaire::DossiersController, type: :controller do
subject
end
let(:email) { 'email@a.com' }
let(:emails) { ['email@a.com'] }
it { expect(saved_avis.email).to eq('email@a.com') }
it { expect(saved_avis.introduction).to eq('intro') }
@ -331,12 +332,22 @@ describe NewGestionnaire::DossiersController, type: :controller do
it { expect(response).to redirect_to(avis_gestionnaire_dossier_path(dossier.procedure, dossier)) }
context "with an invalid email" do
let(:email) { 'emaila.com' }
let(:emails) { ['emaila.com'] }
it { expect(response).to render_template :avis }
it { expect(flash.alert).to eq(["Email n'est pas valide"]) }
it { expect(flash.alert).to eq(["emaila.com : Email n'est pas valide"]) }
it { expect { subject }.not_to change(Avis, :count) }
end
context 'with multiple emails' do
let(:emails) { ["toto.fr,titi@titimail.com"] }
it { expect(response).to render_template :avis }
it { expect(flash.alert).to eq(["toto.fr : Email n'est pas valide"]) }
it { expect(flash.notice).to eq("Une demande d'avis a été envoyée à titi@titimail.com") }
it { expect(Avis.count).to eq(old_avis_count + 1) }
it { expect(saved_avis.email).to eq("titi@titimail.com") }
end
end
describe "#update_annotations" do