demarches-normaliennes/app/controllers/support_controller.rb

96 lines
2.2 KiB
Ruby
Raw Normal View History

2018-08-29 11:44:12 +02:00
class SupportController < ApplicationController
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)
2018-08-29 11:44:12 +02:00
elsif create_conversation
flash.notice = "Votre message a été envoyé."
2018-11-28 15:19:12 +01:00
if params[:admin]
redirect_to root_path(formulaire_contact_admin_submitted: true)
else
redirect_to root_path(formulaire_contact_general_submitted: true)
end
2018-08-29 11:44:12 +02:00
else
2018-11-08 14:48:37 +01:00
flash.now.alert = "Une erreur est survenue. Vous pouvez nous contacter à #{helpers.mail_to(CONTACT_EMAIL)}."
2018-08-29 11:44:12 +02:00
2018-11-28 15:19:12 +01:00
if params[:admin]
setup_context_admin
render :admin
else
setup_context
render :index
end
2018-08-29 11:44:12 +02:00
end
end
private
def setup_context
@dossier_id = dossier&.id
@tags = tags
2018-08-29 11:44:12 +02:00
@options = Helpscout::FormAdapter::OPTIONS
end
2018-11-28 15:19:12 +01:00
def setup_context_admin
@tags = tags
@options = Helpscout::FormAdapter::ADMIN_OPTIONS
end
2018-08-29 11:44:12 +02:00
def create_conversation
Helpscout::FormAdapter.new(
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],
file: params[:file],
dossier_id: dossier&.id,
2018-08-29 11:44:12 +02:00
browser: browser_name,
tags: tags
).send_form
end
def create_commentaire
attributes = {
2018-08-29 11:44:12 +02:00
file: params[:file],
body: "[#{params[:subject]}]<br><br>#{params[:text]}"
}
commentaire = CommentaireService.build_with_email(email, dossier, attributes)
commentaire.save!
2018-08-29 11:44:12 +02:00
end
def tags
[params[:tags], params[:type]].flatten.compact
.map { |tag| tag.split(',') }
.flatten
.reject(&: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.brouillon?
2018-08-29 11:44:12 +02:00
end
def dossier
@dossier ||= current_user&.dossiers&.find_by(id: params[:dossier_id])
end
def email
logged_user ? logged_user.email : params[:email]
end
end