2016-02-05 17:40:58 +01:00
|
|
|
class Admin::GestionnairesController < AdminController
|
|
|
|
include SmartListing::Helper::ControllerExtensions
|
|
|
|
helper SmartListing::Helper
|
|
|
|
|
|
|
|
def index
|
|
|
|
@gestionnaires = smart_listing_create :gestionnaires,
|
2017-06-12 13:49:51 +02:00
|
|
|
current_administrateur.gestionnaires,
|
|
|
|
partial: "admin/gestionnaires/list",
|
|
|
|
array: true
|
|
|
|
|
2016-02-05 17:40:58 +01:00
|
|
|
@gestionnaire ||= Gestionnaire.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-08-28 15:36:34 +02:00
|
|
|
email = params[:gestionnaire][:email].downcase
|
2018-03-06 13:44:29 +01:00
|
|
|
@gestionnaire = Gestionnaire.find_by(email: email)
|
2016-05-26 16:44:10 +02:00
|
|
|
procedure_id = params[:procedure_id]
|
2016-05-20 15:39:17 +02:00
|
|
|
|
2016-05-24 16:39:39 +02:00
|
|
|
if @gestionnaire.nil?
|
2018-04-27 12:29:01 +02:00
|
|
|
invite_gestionnaire(params[:gestionnaire][:email])
|
2016-02-05 17:40:58 +01:00
|
|
|
else
|
2016-05-24 16:39:39 +02:00
|
|
|
assign_gestionnaire!
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
2016-11-15 05:19:30 +01:00
|
|
|
if procedure_id.present?
|
2016-10-18 15:49:46 +02:00
|
|
|
redirect_to admin_procedure_accompagnateurs_path(procedure_id: procedure_id)
|
|
|
|
else
|
|
|
|
redirect_to admin_gestionnaires_path
|
|
|
|
end
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
2016-02-09 12:23:49 +01:00
|
|
|
def destroy
|
2016-05-24 16:39:39 +02:00
|
|
|
Gestionnaire.find(params[:id]).administrateurs.delete current_administrateur
|
2016-02-09 12:23:49 +01:00
|
|
|
redirect_to admin_gestionnaires_path
|
|
|
|
end
|
|
|
|
|
2016-05-24 16:39:39 +02:00
|
|
|
private
|
|
|
|
|
2018-04-27 12:29:01 +02:00
|
|
|
def invite_gestionnaire(email)
|
|
|
|
password = SecureRandom.hex
|
2016-10-18 15:49:46 +02:00
|
|
|
|
2018-01-15 18:48:14 +01:00
|
|
|
@gestionnaire = Gestionnaire.create(
|
2018-04-27 12:29:01 +02:00
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
password_confirmation: password,
|
|
|
|
administrateurs: [current_administrateur]
|
2018-01-15 18:48:14 +01:00
|
|
|
)
|
2016-05-24 16:39:39 +02:00
|
|
|
|
|
|
|
if @gestionnaire.errors.messages.empty?
|
2018-04-27 12:29:01 +02:00
|
|
|
@gestionnaire.invite!
|
|
|
|
|
2018-04-11 19:37:53 +02:00
|
|
|
if User.exists?(email: @gestionnaire.email)
|
2018-05-25 18:05:28 +02:00
|
|
|
GestionnaireMailer.user_to_gestionnaire(@gestionnaire.email).deliver_later
|
2018-04-11 19:37:53 +02:00
|
|
|
else
|
2018-06-06 16:30:03 +02:00
|
|
|
User.create(email: email, password: password, confirmed_at: DateTime.now)
|
2018-04-11 19:37:53 +02:00
|
|
|
end
|
2016-05-24 16:39:39 +02:00
|
|
|
flash.notice = 'Accompagnateur ajouté'
|
|
|
|
else
|
2017-07-13 09:46:13 +02:00
|
|
|
flash.alert = @gestionnaire.errors.full_messages
|
2016-05-24 16:39:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_gestionnaire!
|
|
|
|
if current_administrateur.gestionnaires.include? @gestionnaire
|
|
|
|
flash.alert = 'Accompagnateur déjà ajouté'
|
|
|
|
else
|
|
|
|
@gestionnaire.administrateurs.push current_administrateur
|
|
|
|
flash.notice = 'Accompagnateur ajouté'
|
2018-01-15 19:14:09 +01:00
|
|
|
# TODO Mailer no assign_to
|
2016-05-24 16:39:39 +02:00
|
|
|
end
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
2016-10-18 15:49:46 +02:00
|
|
|
end
|