2021-11-25 16:26:55 +01:00
|
|
|
|
module Administrateurs
|
2021-04-02 16:00:32 +02:00
|
|
|
|
class ExpertsProceduresController < AdministrateurController
|
2024-06-14 11:36:58 +02:00
|
|
|
|
include EmailSanitizableConcern
|
2021-04-06 13:42:21 +02:00
|
|
|
|
before_action :retrieve_procedure
|
2024-06-14 11:36:58 +02:00
|
|
|
|
before_action :retrieve_experts_procedure, only: [:index, :create]
|
|
|
|
|
before_action :retrieve_experts_emails, only: [:index, :create]
|
2021-04-02 16:00:32 +02:00
|
|
|
|
|
2021-04-06 13:55:43 +02:00
|
|
|
|
def index
|
|
|
|
|
end
|
|
|
|
|
|
2021-04-06 13:42:21 +02:00
|
|
|
|
def create
|
2024-06-25 14:37:54 +02:00
|
|
|
|
email_checker = EmailChecker.new
|
2021-04-02 16:00:32 +02:00
|
|
|
|
emails = params['emails'].presence || [].to_json
|
2024-06-25 14:37:54 +02:00
|
|
|
|
emails = JSON.parse(emails).map { EmailSanitizer.sanitize(_1) }
|
|
|
|
|
@maybe_typo, emails = emails.map do |email|
|
|
|
|
|
result = email_checker.check(email: email)
|
|
|
|
|
if result[:email_suggestions].present?
|
|
|
|
|
[email, result[:email_suggestions].first]
|
|
|
|
|
else
|
|
|
|
|
[email, nil]
|
|
|
|
|
end
|
|
|
|
|
end.partition { _1[1].present? }
|
2024-06-14 11:36:58 +02:00
|
|
|
|
errors = if !@maybe_typo.empty?
|
2024-06-25 14:37:54 +02:00
|
|
|
|
["Attention, nous pensons avoir identifié une faute de frappe dans les invitations : #{@maybe_typo.map(&:first).join(', ')}"]
|
2024-06-14 11:36:58 +02:00
|
|
|
|
else
|
|
|
|
|
[]
|
|
|
|
|
end
|
|
|
|
|
emails += [EmailSanitizer.sanitize(params['maybe_typo'])] if params['maybe_typo'].present?
|
2021-04-02 16:00:32 +02:00
|
|
|
|
|
|
|
|
|
valid_users, invalid_users = emails
|
|
|
|
|
.map { |email| User.create_or_promote_to_expert(email, SecureRandom.hex) }
|
|
|
|
|
.partition(&:valid?)
|
|
|
|
|
|
|
|
|
|
if invalid_users.any?
|
2024-06-14 11:36:58 +02:00
|
|
|
|
errors += invalid_users
|
2021-04-02 16:00:32 +02:00
|
|
|
|
.filter { |user| user.errors.present? }
|
|
|
|
|
.map { |user| "#{user.email} : #{user.errors.full_messages_for(:email).join(', ')}" }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if valid_users.present?
|
|
|
|
|
valid_users.each do |user|
|
|
|
|
|
experts_procedure = ExpertsProcedure.find_or_create_by(expert: user.expert, procedure: @procedure)
|
|
|
|
|
if !experts_procedure.revoked_at.nil?
|
|
|
|
|
experts_procedure.update!(revoked_at: nil)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-06-25 14:41:17 +02:00
|
|
|
|
flash.now[:notice] = t('.experts_assignment',
|
2021-04-02 16:00:32 +02:00
|
|
|
|
count: valid_users.count,
|
|
|
|
|
value: valid_users.map(&:email).join(', '),
|
|
|
|
|
procedure: @procedure.id)
|
|
|
|
|
end
|
2024-06-14 11:36:58 +02:00
|
|
|
|
|
2024-06-25 14:41:17 +02:00
|
|
|
|
flash.now[:alert] = errors.join(". ") if !errors.empty?
|
2024-06-14 11:36:58 +02:00
|
|
|
|
render :index
|
2021-04-02 16:00:32 +02:00
|
|
|
|
end
|
|
|
|
|
|
2021-04-06 14:25:41 +02:00
|
|
|
|
def update
|
|
|
|
|
@procedure
|
|
|
|
|
.experts_procedures
|
|
|
|
|
.find(params[:id])
|
|
|
|
|
.update!(expert_procedure_params)
|
|
|
|
|
end
|
|
|
|
|
|
2021-04-06 13:42:21 +02:00
|
|
|
|
def destroy
|
2021-04-06 13:18:51 +02:00
|
|
|
|
expert_procedure = ExpertsProcedure.find_by!(procedure: @procedure, id: params[:id])
|
2021-04-02 16:00:32 +02:00
|
|
|
|
expert_email = expert_procedure.expert.email
|
|
|
|
|
expert_procedure.update!(revoked_at: Time.zone.now)
|
2021-05-26 15:16:30 +02:00
|
|
|
|
flash[:notice] = "#{expert_email} a été révoqué de la démarche et ne pourra plus déposer d’avis."
|
2021-04-06 13:55:43 +02:00
|
|
|
|
redirect_to admin_procedure_experts_path(@procedure)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2024-06-14 11:36:58 +02:00
|
|
|
|
def retrieve_experts_procedure
|
|
|
|
|
@experts_procedure ||= @procedure.experts_procedures.where(revoked_at: nil).sort_by { _1.expert.email }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def retrieve_experts_emails
|
|
|
|
|
@experts_emails ||= @experts_procedure.map { _1.expert.email }
|
2021-04-02 16:00:32 +02:00
|
|
|
|
end
|
2021-04-06 14:25:41 +02:00
|
|
|
|
|
|
|
|
|
def expert_procedure_params
|
|
|
|
|
params.require(:experts_procedure).permit(:allow_decision_access)
|
|
|
|
|
end
|
2021-04-02 16:00:32 +02:00
|
|
|
|
end
|
|
|
|
|
end
|