2016-02-08 18:16:18 +01:00
|
|
|
class InvitesController < ApplicationController
|
2018-11-13 16:18:52 +01:00
|
|
|
SESSION_USER_RETURN_LOCATION = 'user_return_to'
|
|
|
|
|
|
|
|
before_action :authenticate_user!, only: [:create]
|
|
|
|
before_action :store_user_location!, only: [:show]
|
2016-09-13 12:17:56 +02:00
|
|
|
|
2016-02-08 18:16:18 +01:00
|
|
|
def create
|
2018-08-01 17:23:25 +02:00
|
|
|
email = params[:invite_email].downcase
|
2018-08-01 17:27:50 +02:00
|
|
|
dossier = current_user.dossiers.find(params[:dossier_id])
|
2016-12-13 14:22:54 +01:00
|
|
|
|
2018-10-10 09:23:08 +02:00
|
|
|
invite = Invite.create(
|
2018-08-01 17:27:50 +02:00
|
|
|
dossier: dossier,
|
2018-03-22 09:31:07 +01:00
|
|
|
user: User.find_by(email: email),
|
|
|
|
email: email,
|
|
|
|
email_sender: current_user.email
|
|
|
|
)
|
2016-02-08 18:16:18 +01:00
|
|
|
|
|
|
|
if invite.valid?
|
2018-01-11 19:04:39 +01:00
|
|
|
if invite.user.present?
|
2018-05-25 18:05:28 +02:00
|
|
|
InviteMailer.invite_user(invite).deliver_later
|
2018-01-11 19:04:39 +01:00
|
|
|
else
|
2018-05-25 18:05:28 +02:00
|
|
|
InviteMailer.invite_guest(invite).deliver_later
|
2018-01-11 19:04:39 +01:00
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2018-08-01 17:27:50 +02:00
|
|
|
flash.notice = "Une invitation a été envoyée à #{invite.email}."
|
2016-02-08 18:16:18 +01:00
|
|
|
else
|
2017-07-13 09:46:13 +02:00
|
|
|
flash.alert = invite.errors.full_messages
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|
|
|
|
|
2018-07-31 17:26:08 +02:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_back(fallback_location: helpers.url_for_dossier(dossier)) }
|
|
|
|
format.js { @dossier = dossier }
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|
2016-09-13 12:17:56 +02:00
|
|
|
|
2018-11-13 16:18:52 +01:00
|
|
|
def show
|
|
|
|
if user_signed_in?
|
|
|
|
erase_user_location!
|
|
|
|
|
|
|
|
dossier = Dossier.joins(:invites)
|
|
|
|
.find_by!(invites: { email: current_user.email, id: params[:id] })
|
2016-09-13 12:17:56 +02:00
|
|
|
|
2018-11-13 16:18:52 +01:00
|
|
|
if dossier.brouillon?
|
|
|
|
redirect_to brouillon_dossier_path(dossier)
|
|
|
|
else
|
|
|
|
redirect_to dossier_path(dossier)
|
|
|
|
end
|
|
|
|
elsif params[:email].present? && !User.find_by(email: params[:email])
|
|
|
|
redirect_to new_user_registration_path(user: { email: params[:email] })
|
|
|
|
else
|
|
|
|
authenticate_user!
|
2018-01-11 19:04:39 +01:00
|
|
|
end
|
2018-11-13 16:18:52 +01:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
flash.alert = t('errors.messages.dossier_not_found')
|
|
|
|
redirect_to dossiers_path
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def store_user_location!
|
|
|
|
store_location_for(:user, request.fullpath)
|
|
|
|
end
|
|
|
|
|
|
|
|
def erase_user_location!
|
|
|
|
session.delete(SESSION_USER_RETURN_LOCATION)
|
2016-09-13 12:17:56 +02:00
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|