Merge pull request #1686 from betagouv/fix_1601_pipedrive_manager_integration

Fix 1601 pipedrive manager integration
This commit is contained in:
LeSim 2018-03-22 15:50:25 +01:00 committed by GitHub
commit 6c3f287f68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 134 additions and 1 deletions

View file

@ -74,7 +74,13 @@ client_id: ''
client_secret: '' client_secret: ''
``` ```
*Note : les valeurs pour ces deux paramètres sont renseignées dans le Keepass* ## Connexion a Pipedrive
Dans le fichier `config/intializers/token.rb`, ajouter
`PIPEDRIVE_TOKEN = 'token'`
*Note : les valeurs pour ces paramètres sont renseignées dans le Keepass*
## Création des comptes initiaux ## Création des comptes initiaux

View file

@ -0,0 +1,74 @@
module Manager
class DemandesController < Manager::ApplicationController
PIPEDRIVE_PEOPLE_URL = 'https://api.pipedrive.com/v1/persons'
PIPEDRIVE_POSTE_ATTRIBUTE_ID = '33a790746f1713d712fe97bcce9ac1ca6374a4d6'
PIPEDRIVE_DEV_ID = '2748449'
PIPEDRIVE_CAMILLE_ID = '3189424'
def index
@pending_demandes = pending_demandes
end
def create_administrateur
administrateur = current_administration.invite_admin(create_administrateur_params[:email])
if administrateur.errors.empty?
change_person_owner(create_administrateur_params[:person_id], PIPEDRIVE_CAMILLE_ID)
flash.notice = "Administrateur créé"
redirect_to manager_demandes_path
else
flash.now.alert = administrateur.errors.full_messages
@pending_demandes = pending_demandes
render :index
end
end
private
def change_person_owner(person_id, owner_id)
url = PIPEDRIVE_PEOPLE_URL + "/#{person_id}?api_token=#{PIPEDRIVE_TOKEN}"
params = { 'owner_id': owner_id }
RestClient.put(url, params.to_json, { content_type: :json })
end
def create_administrateur_params
params.require(:administrateur).permit(:email, :person_id)
end
def pending_demandes
already_approved_emails = Administrateur
.where(email: demandes.map { |d| d[:email] })
.pluck(:email)
demandes.reject { |demande| already_approved_emails.include?(demande[:email]) }
end
def demandes
@demandes ||= fetch_demandes
end
def fetch_demandes
params = {
start: 0,
limit: 500,
user_id: PIPEDRIVE_DEV_ID,
api_token: PIPEDRIVE_TOKEN
}
response = RestClient.get(PIPEDRIVE_PEOPLE_URL, { params: params })
json = JSON.parse(response.body)
json['data'].map do |datum|
{
person_id: datum['id'],
nom: datum['name'],
poste: datum[PIPEDRIVE_POSTE_ATTRIBUTE_ID],
email: datum.dig('email', 0, 'value'),
organisation: datum['org_name']
}
end
end
end
end

9
app/models/demande.rb Normal file
View file

@ -0,0 +1,9 @@
class Demande
def self.model_name
self
end
def self.human(*args)
"Demandes"
end
end

View file

@ -0,0 +1,41 @@
<% content_for(:title) do %>
<%= display_resource_name('Demandes') %>
<% end %>
<header class="main-content__header" role="banner">
<h1 class="main-content__page-title" id="page-title">
<%= content_for(:title) %>
</h1>
</header>
<section class="main-content__body main-content__body--flush">
<table>
<thead>
<% keys = @pending_demandes.first.keys %>
<tr>
<% keys.each do |key| %>
<th class="cell-label cell-label--string cell-label--false" scope="col" role="columnheader" aria-sort="none">
<%= key %>
</th>
<% end %>
<th class="cell-label cell-label--string cell-label--false" scope="col" role="columnheader" aria-sort="none">
</th>
</thead>
<tbody>
<% @pending_demandes.each do |demande| %>
<tr>
<% keys.each do |key| %>
<td class="cell-data cell-data--string">
<%= demande[key] %>
</td>
<% end %>
<td class="cell-data cell-data--string">
<%= button_to('Créer',
manager_demandes_create_administrateur_path,
params: { administrateur: { email: demande[:email], person_id: demande[:person_id] } }) %>
</td>
</tr>
<% end %>
</tbody>
</table>
</section>

View file

@ -10,6 +10,9 @@ Rails.application.routes.draw do
post 'reinvite', on: :member post 'reinvite', on: :member
end end
resources :demandes, only: [:index]
post 'demandes/create_administrateur'
authenticate :administration do authenticate :administration do
match "/delayed_job" => DelayedJobWeb, :anchor => false, :via => [:get, :post] match "/delayed_job" => DelayedJobWeb, :anchor => false, :via => [:get, :post]
end end