demarches-normaliennes/app/controllers/support_controller.rb

102 lines
2.4 KiB
Ruby
Raw Normal View History

2018-08-29 11:44:12 +02:00
class SupportController < ApplicationController
invisible_captcha only: [:create], on_spam: :redirect_to_root
2018-08-29 11:44:12 +02:00
def index
setup_context
end
2018-11-28 15:19:12 +01:00
def admin
setup_context_admin
end
2018-08-29 11:44:12 +02:00
def create
if direct_message? && create_commentaire
flash.notice = "Votre message a été envoyé sur la messagerie de votre dossier."
2018-09-27 17:14:00 +02:00
redirect_to messagerie_dossier_path(dossier)
return
end
create_conversation_later
flash.notice = "Votre message a été envoyé."
if params[:admin]
redirect_to root_path(formulaire_contact_admin_submitted: true)
2018-08-29 11:44:12 +02:00
else
redirect_to root_path(formulaire_contact_general_submitted: true)
2018-08-29 11:44:12 +02:00
end
end
private
def setup_context
@dossier_id = dossier&.id
@tags = tags
@options = Helpscout::FormAdapter.options
2018-08-29 11:44:12 +02:00
end
2018-11-28 15:19:12 +01:00
def setup_context_admin
@tags = tags
@options = Helpscout::FormAdapter.admin_options
2018-11-28 15:19:12 +01:00
end
def create_conversation_later
if params[:piece_jointe]
blob = ActiveStorage::Blob.create_and_upload!(
io: params[:piece_jointe].tempfile,
filename: params[:piece_jointe].original_filename,
content_type: params[:piece_jointe].content_type,
identify: false
).tap(&:scan_for_virus_later)
end
HelpscoutCreateConversationJob.perform_later(
blob_id: blob&.id,
2018-08-29 11:44:12 +02:00
subject: params[:subject],
email: email,
2018-11-28 15:19:12 +01:00
phone: params[:phone],
2018-08-29 11:44:12 +02:00
text: params[:text],
dossier_id: dossier&.id,
2018-08-29 11:44:12 +02:00
browser: browser_name,
tags: tags
)
2018-08-29 11:44:12 +02:00
end
def create_commentaire
attributes = {
piece_jointe: params[:piece_jointe],
2018-08-29 11:44:12 +02:00
body: "[#{params[:subject]}]<br><br>#{params[:text]}"
}
2021-11-30 18:56:12 +01:00
CommentaireService.create!(current_user, dossier, attributes)
2018-08-29 11:44:12 +02:00
end
def tags
[params[:tags], params[:type]].flatten.compact
.map { |tag| tag.split(',') }
.flatten
2023-04-19 10:59:21 +02:00
.compact_blank.uniq
2018-08-29 11:44:12 +02:00
end
def browser_name
if browser.known?
"#{browser.name} #{browser.version} (#{browser.platform.name})"
end
end
def direct_message?
user_signed_in? && params[:type] == Helpscout::FormAdapter::TYPE_INSTRUCTION && dossier.present? && dossier.messagerie_available?
2018-08-29 11:44:12 +02:00
end
def dossier
@dossier ||= current_user&.dossiers&.find_by(id: params[:dossier_id])
end
def email
current_user&.email || params[:email]
2018-08-29 11:44:12 +02:00
end
def redirect_to_root
redirect_to root_path, alert: t('invisible_captcha.sentence_for_humans')
end
2018-08-29 11:44:12 +02:00
end