2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-08 18:16:18 +01:00
|
|
|
class InvitesController < ApplicationController
|
2018-11-13 16:18:52 +01:00
|
|
|
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
|
2022-07-27 14:50:20 +02:00
|
|
|
email = params[:invite_email]&.downcase
|
2022-05-24 11:59:14 +02:00
|
|
|
@dossier = current_user.dossiers.visible_by_user.find(params[:dossier_id])
|
2018-10-10 09:23:08 +02:00
|
|
|
invite = Invite.create(
|
2022-05-24 11:59:14 +02:00
|
|
|
dossier: @dossier,
|
2018-03-22 09:31:07 +01:00
|
|
|
user: User.find_by(email: email),
|
|
|
|
email: email,
|
2019-07-17 18:41:07 +02:00
|
|
|
message: params[:invite_message],
|
2018-03-22 09:31:07 +01:00
|
|
|
email_sender: current_user.email
|
|
|
|
)
|
2016-02-08 18:16:18 +01:00
|
|
|
|
|
|
|
if invite.valid?
|
2020-09-25 11:10:12 +02:00
|
|
|
# The notification is sent through an after commit hook in order to avoir concurrency issues
|
2024-02-20 17:20:04 +01:00
|
|
|
flash.notice = t('views.invites.create.success', email: 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|
|
2022-05-24 11:59:14 +02:00
|
|
|
format.html { redirect_back(fallback_location: helpers.url_for_dossier(@dossier)) }
|
|
|
|
format.turbo_stream
|
2018-07-31 17:26:08 +02:00
|
|
|
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
|
|
|
|
2022-05-24 11:59:14 +02:00
|
|
|
redirect_to helpers.url_for_dossier(dossier)
|
2018-11-13 16:18:52 +01:00
|
|
|
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
|
|
|
|
|
2020-07-14 18:25:31 +02:00
|
|
|
def destroy
|
2022-11-23 18:40:49 +01:00
|
|
|
invite = Invite.find_by(id: params[:id], dossier: current_user.dossiers.visible_by_user)
|
2022-05-24 11:59:14 +02:00
|
|
|
|
2022-11-23 18:40:49 +01:00
|
|
|
if invite.present?
|
|
|
|
@dossier = invite.dossier
|
2020-07-14 18:25:31 +02:00
|
|
|
invite.destroy!
|
2024-02-20 17:20:04 +01:00
|
|
|
flash.notice = t('views.invites.destroy.success', email: invite.email)
|
2020-07-14 18:25:31 +02:00
|
|
|
else
|
2024-02-20 17:20:04 +01:00
|
|
|
flash.alert = t('views.invites.destroy.error')
|
2020-07-14 18:25:31 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
2022-11-23 18:40:49 +01:00
|
|
|
format.html { redirect_back(fallback_location: @dossier.present? ? helpers.url_for_dossier(@dossier) : root_url) }
|
2022-05-24 11:59:14 +02:00
|
|
|
format.turbo_stream
|
2020-07-14 18:25:31 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-13 16:18:52 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def store_user_location!
|
|
|
|
store_location_for(:user, request.fullpath)
|
|
|
|
end
|
|
|
|
|
|
|
|
def erase_user_location!
|
2019-01-14 16:16:23 +01:00
|
|
|
clear_stored_location_for(:user)
|
2016-09-13 12:17:56 +02:00
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|