Skip to content

Commit

Permalink
Merge pull request #10644 from colinux/fix-helpscout-invalid-email
Browse files Browse the repository at this point in the history
ETQ utilisateur, le form de contact détecte les typos d'email et valide les champs avant de l'envoyer à HS
  • Loading branch information
colinux authored Jul 31, 2024
2 parents 665289f + e415c79 commit 3b82621
Show file tree
Hide file tree
Showing 28 changed files with 799 additions and 767 deletions.
11 changes: 2 additions & 9 deletions app/components/dsfr/input_errorable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ def default_hint
end
end

def hint? = hint.present?

def password?
false
end
Expand All @@ -142,15 +144,6 @@ def autoresize?
def hintable?
false
end

def hint?
return true if get_slot(:hint).present?

maybe_hint = I18n.exists?("activerecord.attributes.#{object.class.name.underscore}.hints.#{@attribute}")
maybe_hint_html = I18n.exists?("activerecord.attributes.#{object.class.name.underscore}.hints.#{@attribute}_html")

maybe_hint || maybe_hint_html
end
end
end
end
76 changes: 76 additions & 0 deletions app/controllers/contact_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class ContactController < ApplicationController
invisible_captcha only: [:create], on_spam: :redirect_to_root

def index
@form = ContactForm.new(tags: contact_form_params.fetch(:tags, []), dossier_id: dossier&.id)
@form.user = current_user
end

def admin
@form = ContactForm.new(tags: contact_form_params.fetch(:tags, []), for_admin: true)
@form.user = current_user
end

def create
if direct_message?
create_commentaire!
flash.notice = t('.direct_message_sent')

redirect_to messagerie_dossier_path(dossier)
return
end

@form = ContactForm.new(contact_form_params)
@form.user = current_user

if @form.save
@form.create_conversation_later
flash.notice = t('.message_sent')

redirect_to root_path
else
flash.alert = @form.errors.full_messages
render @form.for_admin ? :admin : :index
end
end

private

def create_commentaire!
attributes = {
piece_jointe: contact_form_params[:piece_jointe],
body: "[#{contact_form_params[:subject]}]<br><br>#{contact_form_params[:text]}"
}
CommentaireService.create!(current_user, dossier, attributes)
end

def browser_name
if browser.known?
"#{browser.name} #{browser.version} (#{browser.platform.name})"
end
end

def direct_message?
return false unless user_signed_in?
return false unless contact_form_params[:question_type] == ContactForm::TYPE_INSTRUCTION

dossier&.messagerie_available?
end

def dossier
@dossier ||= current_user&.dossiers&.find_by(id: contact_form_params[:dossier_id])
end

def redirect_to_root
redirect_to root_path, alert: t('invisible_captcha.sentence_for_humans')
end

def contact_form_params
keys = [:email, :subject, :text, :question_type, :dossier_id, :piece_jointe, :phone, :for_admin, tags: []]
if params.key?(:contact_form) # submitting form
params.require(:contact_form).permit(*keys)
else
params.permit(:dossier_id, tags: []) # prefilling form
end
end
end
101 changes: 0 additions & 101 deletions app/controllers/support_controller.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def contact_link(title, options = {})
tags, type, dossier_id = options.values_at(:tags, :type, :dossier_id)
options.except!(:tags, :type, :dossier_id)

params = { tags: tags, type: type, dossier_id: dossier_id }.compact
params = { tags: Array.wrap(tags), type: type, dossier_id: dossier_id }.compact
link_to title, contact_url(params), options
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApplicationController } from './application_controller';
import { hide, show } from '@utils';

export class SupportController extends ApplicationController {
export class ContactController extends ApplicationController {
static targets = ['inputRadio', 'content'];

declare readonly inputRadioTargets: HTMLInputElement[];
Expand Down
47 changes: 41 additions & 6 deletions app/jobs/helpscout_create_conversation_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,49 @@ class FileNotScannedYetError < StandardError

retry_on FileNotScannedYetError, wait: :exponentially_longer, attempts: 10

def perform(blob_id: nil, **args)
if blob_id.present?
blob = ActiveStorage::Blob.find(blob_id)
raise FileNotScannedYetError if blob.virus_scanner.pending?
attr_reader :contact_form
attr_reader :api

blob = nil unless blob.virus_scanner.safe?
def perform(contact_form)
@contact_form = contact_form

if contact_form.piece_jointe.attached?
raise FileNotScannedYetError if contact_form.piece_jointe.virus_scanner.pending?
end

Helpscout::FormAdapter.new(**args, blob:).send_form
@api = Helpscout::API.new

create_conversation

contact_form.destroy
end

private

def create_conversation
response = api.create_conversation(
contact_form.email,
contact_form.subject,
contact_form.text,
safe_blob
)

if response.success?
conversation_id = response.headers['Resource-ID']

if contact_form.phone.present?
api.add_phone_number(contact_form.email, contact_form.phone)
end

api.add_tags(conversation_id, contact_form.tags)
else
fail "Error while creating conversation: #{response.response_code} '#{response.body}'"
end
end

def safe_blob
return if !contact_form.piece_jointe.virus_scanner&.safe?

contact_form.piece_jointe
end
end
81 changes: 0 additions & 81 deletions app/lib/helpscout/form_adapter.rb

This file was deleted.

Loading

0 comments on commit 3b82621

Please sign in to comment.