[Fix #835] Enable asking for multiple avis at the same time

This commit is contained in:
Frederic Merizen 2018-10-31 16:09:11 +01:00
parent 31ca0552ab
commit c7358a8f53
6 changed files with 33 additions and 12 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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'