diff --git a/app/controllers/concerns/create_avis_concern.rb b/app/controllers/concerns/create_avis_concern.rb index c086ee726..d87a86a29 100644 --- a/app/controllers/concerns/create_avis_concern.rb +++ b/app/controllers/concerns/create_avis_concern.rb @@ -5,22 +5,39 @@ module CreateAvisConcern def create_avis_from_params(dossier, confidentiel = false) confidentiel ||= create_avis_params[:confidentiel] - avis = Avis.new(create_avis_params.merge(claimant: current_gestionnaire, dossier: dossier, confidentiel: confidentiel)) + emails = create_avis_params[:emails].split(',').map(&:strip) - if avis.save - flash.notice = "Une demande d'avis a été envoyée à #{avis.email_to_display}" + create_results = Avis.create( + emails.map do |email| + { + email: email, + introduction: create_avis_params[:introduction], + claimant: current_gestionnaire, + dossier: dossier, + confidentiel: confidentiel + } + 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}" nil else - flash.now.alert = @avis.errors.full_messages + flash.now.alert = create_results + .map(&:errors) + .reject(&:empty?) + .map(&:full_messages) + .flatten # When an error occurs, return the avis back to the controller # to give the user a chance to correct and resubmit - avis + Avis.new(create_avis_params) end end def create_avis_params - params.require(:avis).permit(:email, :introduction, :confidentiel) + params.require(:avis).permit(:emails, :introduction, :confidentiel) end end diff --git a/app/models/avis.rb b/app/models/avis.rb index 024323fea..e755f946b 100644 --- a/app/models/avis.rb +++ b/app/models/avis.rb @@ -19,6 +19,10 @@ class Avis < ApplicationRecord scope :by_latest, -> { order(updated_at: :desc) } scope :updated_since?, -> (date) { where('avis.updated_at > ?', date) } + # The form allows subtmitting avis requests to several emails at once, + # hence this virtual attribute. + attr_accessor :emails + def email_to_display gestionnaire&.email || email end diff --git a/app/views/new_gestionnaire/shared/avis/_form.html.haml b/app/views/new_gestionnaire/shared/avis/_form.html.haml index 29aefebfa..71c7442f4 100644 --- a/app/views/new_gestionnaire/shared/avis/_form.html.haml +++ b/app/views/new_gestionnaire/shared/avis/_form.html.haml @@ -1,9 +1,9 @@ %section.ask-avis - %h1.tab-title Inviter une personne à donner son avis - %p.avis-notice L'invité pourra consulter, donner un avis sur le dossier et contribuer au fil de messagerie, mais il ne pourra le modifier. + %h1.tab-title Inviter des personnes à donner leur avis + %p.avis-notice Les invités pourront consulter, donner un avis sur le dossier et contribuer au fil de messagerie, mais ils ne pourront pas le modifier. = form_for avis, url: url, html: { class: 'form' } do |f| - = f.email_field :email, placeholder: 'Adresse email', required: true + = f.text_field :emails, placeholder: 'Adresses email, séparées par des virgules', required: true = f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true .flex.justify-between.align-baseline - if must_be_confidentiel diff --git a/spec/controllers/new_gestionnaire/avis_controller_spec.rb b/spec/controllers/new_gestionnaire/avis_controller_spec.rb index 08c0f980c..d73c8e686 100644 --- a/spec/controllers/new_gestionnaire/avis_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/avis_controller_spec.rb @@ -105,7 +105,7 @@ describe NewGestionnaire::AvisController, type: :controller do let(:created_avis) { Avis.last } before do - post :create_avis, params: { id: previous_avis.id, avis: { email: email, introduction: intro, confidentiel: asked_confidentiel } } + post :create_avis, params: { id: previous_avis.id, avis: { emails: email, introduction: intro, confidentiel: asked_confidentiel } } end context 'when an invalid email' do diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index bd074d314..f076b87fe 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -313,7 +313,7 @@ describe NewGestionnaire::DossiersController, type: :controller do post :create_avis, params: { procedure_id: procedure.id, dossier_id: dossier.id, - avis: { email: email, introduction: 'intro', confidentiel: true } + avis: { emails: email, introduction: 'intro', confidentiel: true } } end diff --git a/spec/features/new_gestionnaire/gestionnaire_spec.rb b/spec/features/new_gestionnaire/gestionnaire_spec.rb index 95cc0896d..7c1b95f2c 100644 --- a/spec/features/new_gestionnaire/gestionnaire_spec.rb +++ b/spec/features/new_gestionnaire/gestionnaire_spec.rb @@ -192,7 +192,7 @@ feature 'The gestionnaire part' do end def ask_confidential_avis(to, introduction) - fill_in 'avis_email', with: to + fill_in 'avis_emails', with: to fill_in 'avis_introduction', with: introduction select 'confidentiel', from: 'avis_confidentiel' click_on 'Demander un avis'