diff --git a/app/controllers/support_controller.rb b/app/controllers/support_controller.rb index 771855f26..45f4c074d 100644 --- a/app/controllers/support_controller.rb +++ b/app/controllers/support_controller.rb @@ -5,6 +5,10 @@ class SupportController < ApplicationController setup_context end + def admin + setup_context_admin + end + def create if direct_message? && create_commentaire flash.notice = "Votre message a été envoyé sur la messagerie de votre dossier." @@ -13,12 +17,21 @@ class SupportController < ApplicationController elsif create_conversation flash.notice = "Votre message a été envoyé." - redirect_to root_path(formulaire_contact_general_submitted: true) + if params[:admin] + redirect_to root_path(formulaire_contact_admin_submitted: true) + else + redirect_to root_path(formulaire_contact_general_submitted: true) + end else - setup_context flash.now.alert = "Une erreur est survenue. Vous pouvez nous contacter à #{helpers.mail_to(CONTACT_EMAIL)}." - render :index + if params[:admin] + setup_context_admin + render :admin + else + setup_context + render :index + end end end @@ -30,10 +43,16 @@ class SupportController < ApplicationController @options = Helpscout::FormAdapter::OPTIONS end + def setup_context_admin + @tags = tags + @options = Helpscout::FormAdapter::ADMIN_OPTIONS + end + def create_conversation Helpscout::FormAdapter.new( subject: params[:subject], email: email, + phone: params[:phone], text: params[:text], file: params[:file], dossier_id: dossier&.id, diff --git a/app/lib/helpscout/api.rb b/app/lib/helpscout/api.rb index e8930d07d..fa172b8d4 100644 --- a/app/lib/helpscout/api.rb +++ b/app/lib/helpscout/api.rb @@ -3,6 +3,8 @@ class Helpscout::API CONVERSATIONS = 'conversations' TAGS = 'tags' FIELDS = 'fields' + CUSTOMERS = 'customers' + PHONES = 'phones' OAUTH2_TOKEN = 'oauth2/token' def add_tags(conversation_id, tags) @@ -42,6 +44,21 @@ class Helpscout::API call_api(:post, CONVERSATIONS, body) end + def add_phone_number(email, phone) + query = URI.encode("(email:#{email})") + response = call_api(:get, "#{CUSTOMERS}?mailbox=#{mailbox_id}&query=#{query}") + if response.success? + body = parse_response_body(response) + if body[:page][:totalElements] > 0 + customer_id = body[:_embedded][:customers].first[:id] + call_api(:post, "#{CUSTOMERS}/#{customer_id}/#{PHONES}", { + type: "work", + value: phone + }) + end + end + end + private def attachments(file) diff --git a/app/lib/helpscout/form_adapter.rb b/app/lib/helpscout/form_adapter.rb index eb607933d..de777c026 100644 --- a/app/lib/helpscout/form_adapter.rb +++ b/app/lib/helpscout/form_adapter.rb @@ -20,6 +20,18 @@ class Helpscout::FormAdapter [I18n.t(TYPE_AUTRE, scope: [:support]), TYPE_AUTRE] ] + ADMIN_TYPE_RDV = 'admin demande rdv' + ADMIN_TYPE_QUESTION = 'admin question' + ADMIN_TYPE_SOUCIS = 'admin soucis' + ADMIN_TYPE_AUTRE = 'admin autre' + + ADMIN_OPTIONS = [ + [I18n.t(ADMIN_TYPE_QUESTION, scope: [:supportadmin]), ADMIN_TYPE_QUESTION], + [I18n.t(ADMIN_TYPE_RDV, scope: [:supportadmin]), ADMIN_TYPE_RDV], + [I18n.t(ADMIN_TYPE_SOUCIS, scope: [:supportadmin]), ADMIN_TYPE_SOUCIS], + [I18n.t(ADMIN_TYPE_AUTRE, scope: [:supportadmin]), ADMIN_TYPE_AUTRE] + ] + def send_form conversation_id = create_conversation @@ -51,6 +63,11 @@ class Helpscout::FormAdapter params[:file] ) - response.success? ? response.headers['Resource-ID'] : nil + if response.success? + if params[:phone].present? + @api.add_phone_number(params[:email], params[:phone]) + end + response.headers['Resource-ID'] + end end end diff --git a/app/views/support/admin.html.haml b/app/views/support/admin.html.haml new file mode 100644 index 000000000..b1fb05145 --- /dev/null +++ b/app/views/support/admin.html.haml @@ -0,0 +1,51 @@ +- content_for(:title, 'Contact') + +#contact-form + .container + %h1.new-h1 Contactez notre équipe + + .description + En tant qu'administration, vous pouvez nous contactez via ce formulaire. Nous vous répondrons dans les plus brefs délais, par email ou par téléphone. + %br + %br + %strong + Attention, ce formulaire est réservée uniquement aux organismes publics. + Il ne concerne ni les particuliers, ni les entreprises, ni les associations (sauf celles reconnues d'utilité publique). Si c'est votre cas, rendez-vous sur notre + = link_to contact_path do + formulaire de contact public + \. + + = form_tag contact_path, method: :post, class: 'form' do |f| + - if !logged_in? + .contact-champ + = label_tag :email do + Adresse email professionnelle + %span.mandatory * + = text_field_tag :email, params[:email], required: true + + .contact-champ + = label_tag :type do + Catégorie + %span.mandatory * + = select_tag :type, options_for_select(@options, params[:type]) + + .contact-champ + = label_tag :phone, 'Numéro de téléphone professionnel (ligne directe)' + = text_field_tag :phone + + .contact-champ + = label_tag :subject do + Sujet + = text_field_tag :subject, params[:subject], required: false + + .contact-champ + = label_tag :text do + Message + %span.mandatory * + = text_area_tag :text, params[:text], rows: 6, required: true + + = hidden_field_tag :tags, @tags&.join(',') + = hidden_field_tag :admin, true + + .send-wrapper + = button_tag 'Envoyer le message', type: :submit, class: 'button send primary' diff --git a/config/locales/fr.yml b/config/locales/fr.yml index d7dd009c0..3a3fccfa7 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -61,6 +61,11 @@ fr: produit: J'ai une idée d'amélioration pour votre site usager perdu: Je ne trouve pas la démarche que je veux faire autre: Autre sujet + supportadmin: + admin demande rdv: Demande de RDV pour une présentation à distance de demarches-simplifiees.fr + admin question: J'ai une question sur demarches-simplifiees.fr + admin soucis: J'ai un problème technique avec demarches-simplifiees.fr + admin autre: Autre sujet number: currency: diff --git a/config/routes.rb b/config/routes.rb index 244e6cd8f..48532b4c2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -137,6 +137,8 @@ Rails.application.routes.draw do get "contact", to: "support#index" post "contact", to: "support#create" + get "contact-admin", to: "support#admin" + post "webhooks/helpscout", to: "webhook#helpscout" match "webhooks/helpscout", to: lambda { |_| [204, {}, nil] }, via: :head diff --git a/spec/lib/helpscout/form_adapter_spec.rb b/spec/lib/helpscout/form_adapter_spec.rb index c6054789b..0b6c56209 100644 --- a/spec/lib/helpscout/form_adapter_spec.rb +++ b/spec/lib/helpscout/form_adapter_spec.rb @@ -66,5 +66,45 @@ describe Helpscout::FormAdapter do .with(conversation_id, nil, nil) end end + + context 'add_phone' do + before do + allow(api).to receive(:create_conversation) + .and_return( + double( + success?: true, + headers: { + 'Resource-ID' => conversation_id + } + ) + ) + + described_class.new(params, api).send_form + end + + let(:params) { + { + email: email, + subject: subject, + text: text, + phone: '0666666666' + } + } + let(:phone) { '0666666666' } + let(:email) { 'paul.chavard@beta.gouv.fr' } + let(:subject) { 'Bonjour' } + let(:text) { "J'ai un problem" } + let(:tags) { ['info demarche'] } + let(:conversation_id) { '123' } + + it 'should call method' do + expect(api).to have_received(:create_conversation) + .with(email, subject, text, nil) + expect(api).to have_received(:add_phone_number) + .with(email, phone) + expect(api).to have_received(:add_custom_fields) + .with(conversation_id, nil, nil) + end + end end end