2018-10-31 14:56:01 +01:00
|
|
|
module CreateAvisConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-25 10:10:24 +01:00
|
|
|
def create_avis_from_params(dossier, instructeur_or_expert, confidentiel = false)
|
2018-10-31 14:56:01 +01:00
|
|
|
confidentiel ||= create_avis_params[:confidentiel]
|
2018-10-31 16:21:16 +01:00
|
|
|
# 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
|
2021-04-01 11:58:02 +02:00
|
|
|
expert_emails = create_avis_params[:emails].presence || [].to_json
|
|
|
|
expert_emails = JSON.parse(expert_emails).map(&:strip).map(&:downcase)
|
2019-08-21 09:09:20 +02:00
|
|
|
allowed_dossiers = [dossier]
|
|
|
|
|
|
|
|
if create_avis_params[:invite_linked_dossiers].present?
|
2021-02-25 10:10:24 +01:00
|
|
|
allowed_dossiers += dossier.linked_dossiers_for(instructeur_or_expert)
|
2019-08-21 09:09:20 +02:00
|
|
|
end
|
2018-10-31 14:56:01 +01:00
|
|
|
|
2018-10-31 16:09:11 +01:00
|
|
|
create_results = Avis.create(
|
2019-08-21 09:09:20 +02:00
|
|
|
expert_emails.flat_map do |email|
|
2021-03-23 12:25:57 +01:00
|
|
|
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
|
2019-08-21 09:09:20 +02:00
|
|
|
allowed_dossiers.map do |dossier|
|
|
|
|
{
|
|
|
|
email: email,
|
|
|
|
introduction: create_avis_params[:introduction],
|
2020-03-03 12:52:35 +01:00
|
|
|
introduction_file: create_avis_params[:introduction_file],
|
2021-02-25 10:10:24 +01:00
|
|
|
claimant: instructeur_or_expert,
|
2019-08-21 09:09:20 +02:00
|
|
|
dossier: dossier,
|
2021-01-14 20:07:31 +01:00
|
|
|
confidentiel: confidentiel,
|
|
|
|
experts_procedure: experts_procedure
|
2019-08-21 09:09:20 +02:00
|
|
|
}
|
|
|
|
end
|
2018-10-31 16:09:11 +01:00
|
|
|
end
|
|
|
|
)
|
2021-03-30 19:44:57 +02:00
|
|
|
dossier.avis.reload # unload non-persisted avis from dossier
|
2018-10-31 16:09:11 +01:00
|
|
|
|
2018-10-31 20:38:32 +01:00
|
|
|
persisted, failed = create_results.partition(&:persisted?)
|
|
|
|
|
|
|
|
if persisted.any?
|
2020-07-22 16:57:00 +02:00
|
|
|
dossier.update!(last_avis_updated_at: Time.zone.now)
|
2019-08-21 09:09:20 +02:00
|
|
|
sent_emails_addresses = []
|
2019-05-02 16:24:24 +02:00
|
|
|
persisted.each do |avis|
|
2019-08-21 09:09:20 +02:00
|
|
|
avis.dossier.demander_un_avis!(avis)
|
|
|
|
if avis.dossier == dossier
|
|
|
|
AvisMailer.avis_invitation(avis).deliver_later
|
2021-02-25 10:10:24 +01:00
|
|
|
sent_emails_addresses << avis.expert.email
|
|
|
|
# the email format is already verified, we update value to nil
|
|
|
|
avis.update_column(:email, nil)
|
2019-08-21 09:09:20 +02:00
|
|
|
end
|
2019-05-02 16:24:24 +02:00
|
|
|
end
|
2019-08-21 09:09:20 +02:00
|
|
|
flash.notice = "Une demande d'avis a été envoyée à #{sent_emails_addresses.uniq.join(", ")}"
|
2018-10-31 20:38:32 +01:00
|
|
|
end
|
2018-10-31 14:56:01 +01:00
|
|
|
|
2018-10-31 20:38:32 +01:00
|
|
|
if failed.any?
|
|
|
|
flash.now.alert = failed
|
2019-09-12 11:26:22 +02:00
|
|
|
.filter { |avis| avis.errors.present? }
|
2021-03-23 12:25:57 +01:00
|
|
|
.map { |avis| "#{avis.email} : #{avis.errors.full_messages_for(:email).join(', ')}" }
|
2018-10-31 14:56:01 +01:00
|
|
|
|
|
|
|
# When an error occurs, return the avis back to the controller
|
|
|
|
# to give the user a chance to correct and resubmit
|
2019-08-21 09:09:20 +02:00
|
|
|
Avis.new(create_avis_params.merge(emails: [failed.map(&:email).uniq.join(", ")]))
|
2018-10-31 20:38:32 +01:00
|
|
|
else
|
|
|
|
nil
|
2018-10-31 14:56:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_avis_params
|
2021-04-01 11:58:02 +02:00
|
|
|
params.require(:avis).permit(:introduction_file, :introduction, :confidentiel, :invite_linked_dossiers, :emails)
|
2018-10-31 14:56:01 +01:00
|
|
|
end
|
|
|
|
end
|