diff --git a/app/controllers/demandes_controller.rb b/app/controllers/demandes_controller.rb new file mode 100644 index 000000000..53fad026d --- /dev/null +++ b/app/controllers/demandes_controller.rb @@ -0,0 +1,26 @@ +class DemandesController < ApplicationController + layout "new_application" + + def new + end + + def create + PipedriveService.add_demande( + demande_params[:email], + demande_params[:phone], + demande_params[:name], + demande_params[:poste], + demande_params[:source], + demande_params[:organization_name], + demande_params[:address] + ) + flash.notice = 'Votre demande a bien été enregistrée, nous vous contacterons rapidement.' + redirect_to root_path + end + + private + + def demande_params + params.permit(:organization_name, :poste, :name, :email, :phone, :source, :address) + end +end diff --git a/app/lib/pipedrive/api.rb b/app/lib/pipedrive/api.rb index 0ce970635..8cbf1ca04 100644 --- a/app/lib/pipedrive/api.rb +++ b/app/lib/pipedrive/api.rb @@ -2,6 +2,7 @@ class Pipedrive::API PIPEDRIVE_ALL_NOT_DELETED_DEALS = 'all_not_deleted' PIPEDRIVE_DEALS_URL = [PIPEDRIVE_API_URL, 'deals'].join("/") PIPEDRIVE_PEOPLE_URL = [PIPEDRIVE_API_URL, 'persons'].join("/") + PIPEDRIVE_ORGANIZATIONS_URL = [PIPEDRIVE_API_URL, 'organizations'].join("/") def self.get_persons_owned_by_user(user_id) url = PIPEDRIVE_PEOPLE_URL @@ -23,12 +24,24 @@ class Pipedrive::API self.put(url, params) end + def self.post_deal(params) + self.post(PIPEDRIVE_DEALS_URL, params) + end + def self.put_person(person_id, params) url = [PIPEDRIVE_PEOPLE_URL, person_id].join("/") self.put(url, params) end + def self.post_person(params) + self.post(PIPEDRIVE_PEOPLE_URL, params) + end + + def self.post_organization(params) + self.post(PIPEDRIVE_ORGANIZATIONS_URL, params) + end + private def self.get(url, params) @@ -47,4 +60,10 @@ class Pipedrive::API RestClient.put(url, params.to_json, { content_type: :json }) end + + def self.post(url, params) + url = "#{url}?api_token=#{PIPEDRIVE_TOKEN}" + + RestClient.post(url, params.to_json, { content_type: :json }) + end end diff --git a/app/lib/pipedrive/deal_adapter.rb b/app/lib/pipedrive/deal_adapter.rb index a8c4ad826..1d7f969ca 100644 --- a/app/lib/pipedrive/deal_adapter.rb +++ b/app/lib/pipedrive/deal_adapter.rb @@ -33,4 +33,15 @@ class Pipedrive::DealAdapter Pipedrive::API.put_deal(deal_id, params) end + + def self.add_deal(organisation_id, person_id, title) + params = { + org_id: organisation_id, + person_id: person_id, + title: title, + user_id: Pipedrive::PersonAdapter::PIPEDRIVE_ROBOT_ID + } + + Pipedrive::API.post_deal(params) + end end diff --git a/app/lib/pipedrive/organization_adapter.rb b/app/lib/pipedrive/organization_adapter.rb new file mode 100644 index 000000000..b4fb88ea1 --- /dev/null +++ b/app/lib/pipedrive/organization_adapter.rb @@ -0,0 +1,13 @@ +class Pipedrive::OrganizationAdapter + def self.add_organization(name, address) + params = { + name: name, + owner_id: Pipedrive::PersonAdapter::PIPEDRIVE_ROBOT_ID, + address: address + } + + response = Pipedrive::API.post_organization(params) + + JSON.parse(response.body)['data']['id'] + end +end diff --git a/app/lib/pipedrive/person_adapter.rb b/app/lib/pipedrive/person_adapter.rb index f395bbea9..88d6526fd 100644 --- a/app/lib/pipedrive/person_adapter.rb +++ b/app/lib/pipedrive/person_adapter.rb @@ -1,5 +1,6 @@ class Pipedrive::PersonAdapter PIPEDRIVE_POSTE_ATTRIBUTE_ID = '33a790746f1713d712fe97bcce9ac1ca6374a4d6' + PIPEDRIVE_SOURCE_ATTRIBUTE_ID = '2fa7864f467ffa97721cbcd08df5a3d591b15f50' PIPEDRIVE_ROBOT_ID = '2748449' def self.get_demandes_from_persons_owned_by_robot @@ -19,4 +20,20 @@ class Pipedrive::PersonAdapter Pipedrive::API.put_person(person_id, params) end + + def self.add_person(email, phone, name, organization_id, poste, source) + params = { + email: email, + phone: phone, + name: name, + org_id: organization_id, + owner_id: PIPEDRIVE_ROBOT_ID, + "#{PIPEDRIVE_POSTE_ATTRIBUTE_ID}": poste, + "#{PIPEDRIVE_SOURCE_ATTRIBUTE_ID}": source + } + + response = Pipedrive::API.post_person(params) + + JSON.parse(response.body)['data']['id'] + end end diff --git a/app/services/pipedrive_service.rb b/app/services/pipedrive_service.rb index 09fc32779..f7b7c0875 100644 --- a/app/services/pipedrive_service.rb +++ b/app/services/pipedrive_service.rb @@ -14,4 +14,10 @@ class PipedriveService def self.get_demandes Pipedrive::PersonAdapter.get_demandes_from_persons_owned_by_robot end + + def self.add_demande(email, phone, name, poste, source, organization_name, address) + organization_id = Pipedrive::OrganizationAdapter.add_organization(organization_name, address) + person_id = Pipedrive::PersonAdapter.add_person(email, phone, name, organization_id, poste, source) + Pipedrive::DealAdapter.add_deal(organization_id, person_id, organization_name) + end end diff --git a/app/views/demandes/new.html.haml b/app/views/demandes/new.html.haml new file mode 100644 index 000000000..1cf5ae528 --- /dev/null +++ b/app/views/demandes/new.html.haml @@ -0,0 +1,43 @@ +.container + %h1 Demande de compte administrateur + %p Pour obtenir un compte administrateur sur demarches-simplifiees.fr, veuillez remplir le formulaire ci-dessous et un membre de notre équipe vous contactera très prochainement. + %p Tous les champs sont obligatoires. + + = form_tag({ controller: 'demandes', action: 'create' }, class: 'form') do + + = label_tag :organization_name do + Quel est le nom de votre organisme ? + %span.mandatory * + = text_field_tag :organization_name, nil, placeholder: 'service jeunesse et prévention, direction des affaires maritimes', required: true + + = label_tag :poste do + Quel est votre poste ? + %span.mandatory * + = text_field_tag :poste, nil, required: true + + = label_tag :name do + Quel est votre prénom et votre nom ? + %span.mandatory * + = text_field_tag :name, nil, required: true + + = label_tag :email do + Quelle est l'adresse email pour laquelle vous souhaitez un compte ? + %span.mandatory * + = email_field_tag :email, nil, required: true + + = label_tag :phone do + Quel est votre numéro de téléphone ? + %span.mandatory * + = text_field_tag :phone, nil, required: true + + = label_tag :source do + Comment avez-vous entendu parlé de demarches-simplifiees.fr ? + %span.mandatory * + = text_field_tag :source, nil, required: true + + = label_tag :address do + Quel est le code postal de votre institution ? + %span.mandatory * + = text_field_tag :address, nil, required: true + + = submit_tag 'Envoyer', class: 'button', data: { disable_with: "Envoi..." } diff --git a/app/views/root/landing.html.haml b/app/views/root/landing.html.haml index f518b3086..7fd087644 100644 --- a/app/views/root/landing.html.haml +++ b/app/views/root/landing.html.haml @@ -67,7 +67,7 @@ %p.role-panel-explanation Créez des formulaires en ligne en quelques minutes et instruisez les demandes des usagers sur une plateforme dédiée = link_to "Demander un compte", - "https://pipedrivewebforms.com/form/c4a5de08b0a1dcc92374e853aa11905a1871869", + new_demande_path, class: "role-panel-button-primary", target: "_blank", rel: "noopener noreferrer", diff --git a/config/routes.rb b/config/routes.rb index be4399b44..106e1002d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -69,6 +69,7 @@ Rails.application.routes.draw do resources :stats, only: [:index] resources :accessibilite, only: [:index] + resources :demandes, only: [:new, :create] namespace :france_connect do get 'particulier' => 'particulier#login'