From 9e566596fac580b647ae00f62eeffeb8179336fd Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 20 Mar 2018 16:33:51 +0100 Subject: [PATCH 1/2] [fix #1601] show pipedrive demandes in manager --- README.md | 8 +++- .../manager/demandes_controller.rb | 47 +++++++++++++++++++ app/models/demande.rb | 9 ++++ app/views/manager/demandes/index.html.erb | 34 ++++++++++++++ config/routes.rb | 2 + 5 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 app/controllers/manager/demandes_controller.rb create mode 100644 app/models/demande.rb create mode 100644 app/views/manager/demandes/index.html.erb diff --git a/README.md b/README.md index c375c5a97..138540bc1 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,13 @@ client_id: '' 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 diff --git a/app/controllers/manager/demandes_controller.rb b/app/controllers/manager/demandes_controller.rb new file mode 100644 index 000000000..87e7bcb74 --- /dev/null +++ b/app/controllers/manager/demandes_controller.rb @@ -0,0 +1,47 @@ +module Manager + class DemandesController < Manager::ApplicationController + PIPEDRIVE_PEOPLE_URL = 'https://api.pipedrive.com/v1/persons' + PIPEDRIVE_POSTE_ATTRIBUTE_ID = '33a790746f1713d712fe97bcce9ac1ca6374a4d6' + PIPEDRIVE_DEV_ID = '2748449' + + def index + @pending_demandes = pending_demandes + end + + private + + 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 diff --git a/app/models/demande.rb b/app/models/demande.rb new file mode 100644 index 000000000..132282727 --- /dev/null +++ b/app/models/demande.rb @@ -0,0 +1,9 @@ +class Demande + def self.model_name + self + end + + def self.human(*args) + "Demandes" + end +end diff --git a/app/views/manager/demandes/index.html.erb b/app/views/manager/demandes/index.html.erb new file mode 100644 index 000000000..21470f7b2 --- /dev/null +++ b/app/views/manager/demandes/index.html.erb @@ -0,0 +1,34 @@ +<% content_for(:title) do %> + <%= display_resource_name('Demandes') %> +<% end %> + + + +
+ + + <% keys = @pending_demandes.first.keys %> + + <% keys.each do |key| %> + + <% end %> + + + <% @pending_demandes.each do |demande| %> + + <% keys.each do |key| %> + + <% end %> + + <% end %> + +
+ <%= key %> +
+ <%= demande[key] %> +
+
diff --git a/config/routes.rb b/config/routes.rb index 3c2c56c1e..2dcbd0b23 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,6 +10,8 @@ Rails.application.routes.draw do post 'reinvite', on: :member end + resources :demandes, only: [:index] + authenticate :administration do match "/delayed_job" => DelayedJobWeb, :anchor => false, :via => [:get, :post] end From 9eb28faa10e5bdf99885c2252aade24e16f54b4b Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 21 Mar 2018 10:28:17 +0100 Subject: [PATCH 2/2] [fix #1601] accept demandes in manager --- .../manager/demandes_controller.rb | 27 +++++++++++++++++++ app/views/manager/demandes/index.html.erb | 7 +++++ config/routes.rb | 1 + 3 files changed, 35 insertions(+) diff --git a/app/controllers/manager/demandes_controller.rb b/app/controllers/manager/demandes_controller.rb index 87e7bcb74..513bbcf38 100644 --- a/app/controllers/manager/demandes_controller.rb +++ b/app/controllers/manager/demandes_controller.rb @@ -3,13 +3,40 @@ module Manager 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] }) diff --git a/app/views/manager/demandes/index.html.erb b/app/views/manager/demandes/index.html.erb index 21470f7b2..526619635 100644 --- a/app/views/manager/demandes/index.html.erb +++ b/app/views/manager/demandes/index.html.erb @@ -18,6 +18,8 @@ <%= key %> <% end %> + + <% @pending_demandes.each do |demande| %> @@ -27,6 +29,11 @@ <%= demande[key] %> <% end %> + + <%= button_to('Créer', + manager_demandes_create_administrateur_path, + params: { administrateur: { email: demande[:email], person_id: demande[:person_id] } }) %> + <% end %> diff --git a/config/routes.rb b/config/routes.rb index 2dcbd0b23..e694b323b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,7 @@ Rails.application.routes.draw do end resources :demandes, only: [:index] + post 'demandes/create_administrateur' authenticate :administration do match "/delayed_job" => DelayedJobWeb, :anchor => false, :via => [:get, :post]