2016-02-08 18:16:18 +01:00
|
|
|
class InvitesController < ApplicationController
|
2016-09-13 12:17:56 +02:00
|
|
|
before_action :gestionnaire_or_user?
|
|
|
|
|
2016-02-08 18:16:18 +01:00
|
|
|
def create
|
2016-09-13 12:17:56 +02:00
|
|
|
email_sender = @current_devise_profil.email
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2016-09-13 15:54:26 +02:00
|
|
|
class_var = @current_devise_profil.class == User ? InviteUser : InviteGestionnaire
|
2017-07-10 14:44:26 +02:00
|
|
|
dossier = @current_devise_profil.dossiers.find(params[:dossier_id])
|
2016-09-13 15:54:26 +02:00
|
|
|
|
2016-12-13 14:22:54 +01:00
|
|
|
email = params[:email].downcase
|
|
|
|
|
|
|
|
user = User.find_by_email(email)
|
2017-07-10 14:44:26 +02:00
|
|
|
invite = class_var.create(dossier: dossier, user: user, email: email, email_sender: email_sender)
|
2016-02-08 18:16:18 +01:00
|
|
|
|
|
|
|
if invite.valid?
|
2018-01-11 19:04:39 +01:00
|
|
|
if invite.user.present?
|
|
|
|
InviteMailer.invite_user(invite).deliver_now!
|
|
|
|
else
|
|
|
|
InviteMailer.invite_guest(invite).deliver_now!
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
|
|
|
|
flash.notice = "Invitation envoyée (#{invite.email})"
|
|
|
|
else
|
2017-07-13 09:46:13 +02:00
|
|
|
flash.alert = invite.errors.full_messages
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if gestionnaire_signed_in?
|
|
|
|
redirect_to url_for(controller: 'backoffice/dossiers', action: :show, id: params['dossier_id'])
|
2016-09-13 12:17:56 +02:00
|
|
|
else
|
|
|
|
redirect_to url_for(controller: 'users/recapitulatif', action: :show, dossier_id: params['dossier_id'])
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|
|
|
|
end
|
2016-09-13 12:17:56 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def gestionnaire_or_user?
|
2018-01-11 19:04:39 +01:00
|
|
|
if !user_signed_in? && !gestionnaire_signed_in?
|
|
|
|
return redirect_to root_path
|
|
|
|
end
|
2016-09-13 12:17:56 +02:00
|
|
|
|
|
|
|
@current_devise_profil = current_user if user_signed_in?
|
|
|
|
@current_devise_profil = current_gestionnaire if gestionnaire_signed_in?
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|