demarches-normaliennes/app/controllers/concerns/create_avis_concern.rb

90 lines
3.2 KiB
Ruby
Raw Normal View History

module CreateAvisConcern
extend ActiveSupport::Concern
private
def create_avis_from_params(dossier, instructeur_or_expert, confidentiel = false)
2024-05-15 23:16:55 +02:00
if create_avis_params[:emails].blank?
avis = Avis.new(create_avis_params)
errors = avis.errors
errors.add(:emails, :blank)
flash.alert = errors.full_message(:emails, errors[:emails].first)
return avis
2023-03-10 12:31:49 +01:00
end
confidentiel ||= create_avis_params[:confidentiel]
# Because of a limitation of the email_field rails helper,
# the :emails parameter is a 1-element array.
# Hence the call to first
# https://github.com/rails/rails/issues/17225
2024-05-15 23:16:55 +02:00
expert_emails = create_avis_params[:emails].presence || []
expert_emails = expert_emails.map(&:strip).map(&:downcase)
allowed_dossiers = [dossier]
if create_avis_params[:invite_linked_dossiers].present?
allowed_dossiers += dossier.linked_dossiers_for(instructeur_or_expert)
end
if (instructeur_or_expert.is_a?(Instructeur)) && !instructeur_or_expert.follows.exists?(dossier: dossier)
instructeur_or_expert.follow(dossier)
end
create_results = Avis.create(
expert_emails.flat_map do |email|
user = User.create_or_promote_to_expert(email, SecureRandom.hex)
experts_procedure = user.valid? ? ExpertsProcedure.find_or_create_by(procedure: dossier.procedure, expert: user.expert) : nil
allowed_dossiers.map do |dossier|
{
email: email,
introduction: create_avis_params[:introduction],
introduction_file: create_avis_params[:introduction_file],
claimant: instructeur_or_expert,
dossier: dossier,
2021-01-14 20:07:31 +01:00
confidentiel: confidentiel,
experts_procedure: experts_procedure,
question_label: create_avis_params[:question_label]
}
end
end
)
dossier.avis.reload # unload non-persisted avis from dossier
persisted, failed = create_results.partition(&:persisted?)
if persisted.any?
dossier.update!(last_avis_updated_at: Time.zone.now)
sent_emails_addresses = []
2019-05-02 16:24:24 +02:00
persisted.each do |avis|
avis.dossier.demander_un_avis!(avis)
if avis.dossier == dossier
2024-03-15 17:42:27 +01:00
if avis.experts_procedure.notify_on_new_avis?
AvisMailer.avis_invitation(avis).deliver_later
end
sent_emails_addresses << avis.expert.email
# the email format is already verified, we update value to nil
avis.update_column(:email, nil)
end
2019-05-02 16:24:24 +02:00
end
2021-05-26 15:16:30 +02:00
flash.notice = "Une demande davis a été envoyée à #{sent_emails_addresses.uniq.join(", ")}"
end
if failed.any?
flash.now.alert = failed
2019-09-12 11:26:22 +02:00
.filter { |avis| avis.errors.present? }
.map { |avis| "#{avis.email} : #{avis.errors.full_messages_for(:email).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.merge(emails: [failed.map(&:email).uniq.join(", ")]))
else
nil
end
end
def create_avis_params
2024-05-15 23:16:55 +02:00
params.require(:avis).permit(:introduction_file, :introduction, :confidentiel, :invite_linked_dossiers, :question_label, emails: [])
end
end