Merge pull request #1703 from betagouv/pipedrive_for_real
Pipedrive for real
This commit is contained in:
commit
8d7c639f8f
5 changed files with 114 additions and 42 deletions
|
@ -1,10 +1,5 @@
|
||||||
module Manager
|
module Manager
|
||||||
class DemandesController < Manager::ApplicationController
|
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
|
def index
|
||||||
@pending_demandes = pending_demandes
|
@pending_demandes = pending_demandes
|
||||||
end
|
end
|
||||||
|
@ -13,7 +8,12 @@ module Manager
|
||||||
administrateur = current_administration.invite_admin(create_administrateur_params[:email])
|
administrateur = current_administration.invite_admin(create_administrateur_params[:email])
|
||||||
|
|
||||||
if administrateur.errors.empty?
|
if administrateur.errors.empty?
|
||||||
change_person_owner(create_administrateur_params[:person_id], PIPEDRIVE_CAMILLE_ID)
|
PipedriveAcceptsDealsJob.perform_later(
|
||||||
|
create_administrateur_params[:person_id],
|
||||||
|
PipedriveService::PIPEDRIVE_CAMILLE_ID,
|
||||||
|
create_administrateur_params[:stage_id]
|
||||||
|
)
|
||||||
|
|
||||||
flash.notice = "Administrateur créé"
|
flash.notice = "Administrateur créé"
|
||||||
redirect_to manager_demandes_path
|
redirect_to manager_demandes_path
|
||||||
else
|
else
|
||||||
|
@ -25,16 +25,8 @@ module Manager
|
||||||
|
|
||||||
private
|
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
|
def create_administrateur_params
|
||||||
params.require(:administrateur).permit(:email, :person_id)
|
params.permit(:email, :person_id, :stage_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def pending_demandes
|
def pending_demandes
|
||||||
|
@ -46,29 +38,7 @@ module Manager
|
||||||
end
|
end
|
||||||
|
|
||||||
def demandes
|
def demandes
|
||||||
@demandes ||= fetch_demandes
|
@demandes ||= PipedriveService.fetch_people_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
|
end
|
||||||
end
|
end
|
||||||
|
|
5
app/jobs/pipedrive_accepts_deals_job.rb
Normal file
5
app/jobs/pipedrive_accepts_deals_job.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class PipedriveAcceptsDealsJob < ApplicationJob
|
||||||
|
def perform(person_id, owner_id, stage_id)
|
||||||
|
PipedriveService.accept_deals_from_person(person_id, owner_id, stage_id)
|
||||||
|
end
|
||||||
|
end
|
79
app/services/pipedrive_service.rb
Normal file
79
app/services/pipedrive_service.rb
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
class PipedriveService
|
||||||
|
PIPEDRIVE_POSTE_ATTRIBUTE_ID = '33a790746f1713d712fe97bcce9ac1ca6374a4d6'
|
||||||
|
|
||||||
|
PIPEDRIVE_ROBOT_ID = '2748449'
|
||||||
|
PIPEDRIVE_CAMILLE_ID = '3189424'
|
||||||
|
|
||||||
|
PIPEDRIVE_ALL_NOT_DELETED_DEALS = 'all_not_deleted'
|
||||||
|
|
||||||
|
PIPEDRIVE_ADMIN_CENTRAL_STOCK_STAGE_ID = 35
|
||||||
|
PIPEDRIVE_REGIONS_STOCK_STAGE_ID = 24
|
||||||
|
PIPEDRIVE_PREFECTURES_STOCK_STAGE_ID = 20
|
||||||
|
PIPEDRIVE_DEPARTEMENTS_STOCK_STAGE_ID = 30
|
||||||
|
PIPEDRIVE_COMMUNES_STOCK_STAGE_ID = 40
|
||||||
|
PIPEDRIVE_ORGANISMES_STOCK_STAGE_ID = 1
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def accept_deals_from_person(person_id, owner_id, stage_id)
|
||||||
|
waiting_deal_ids = fetch_waiting_deal_ids(person_id)
|
||||||
|
waiting_deal_ids.each { |deal_id| update_deal_owner_and_stage(deal_id, owner_id, stage_id) }
|
||||||
|
update_person_owner(person_id, owner_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def fetch_people_demandes
|
||||||
|
params = {
|
||||||
|
start: 0,
|
||||||
|
limit: 500,
|
||||||
|
user_id: PIPEDRIVE_ROBOT_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
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def fetch_waiting_deal_ids(person_id)
|
||||||
|
url = [PIPEDRIVE_PEOPLE_URL, person_id, "deals"].join('/')
|
||||||
|
|
||||||
|
params = {
|
||||||
|
start: 0,
|
||||||
|
limit: 500,
|
||||||
|
status: PIPEDRIVE_ALL_NOT_DELETED_DEALS,
|
||||||
|
api_token: PIPEDRIVE_TOKEN
|
||||||
|
}
|
||||||
|
|
||||||
|
response = RestClient.get(url, params: params)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
|
||||||
|
json['data'].map { |datum| datum['id'] }
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_deal_owner_and_stage(deal_id, owner_id, stage_id)
|
||||||
|
url = PIPEDRIVE_DEALS_URL + "/#{deal_id}?api_token=#{PIPEDRIVE_TOKEN}"
|
||||||
|
|
||||||
|
params = { user_id: owner_id, stage_id: stage_id }
|
||||||
|
|
||||||
|
RestClient.put(url, params.to_json, { content_type: :json })
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_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
|
||||||
|
end
|
||||||
|
end
|
|
@ -29,10 +29,24 @@
|
||||||
<%= demande[key] %>
|
<%= demande[key] %>
|
||||||
</td>
|
</td>
|
||||||
<% end %>
|
<% end %>
|
||||||
<td class="cell-data cell-data--string">
|
<td class="cell-data cell-data--string" style="text-align: center;">
|
||||||
<%= button_to('Créer',
|
<%= form_tag(manager_demandes_create_administrateur_path) do -%>
|
||||||
manager_demandes_create_administrateur_path,
|
<%= select_tag "stage_id",
|
||||||
params: { administrateur: { email: demande[:email], person_id: demande[:person_id] } }) %>
|
options_for_select({
|
||||||
|
"administration centrale" => PipedriveService::PIPEDRIVE_ADMIN_CENTRAL_STOCK_STAGE_ID,
|
||||||
|
"région" => PipedriveService::PIPEDRIVE_REGIONS_STOCK_STAGE_ID,
|
||||||
|
"préfecture" => PipedriveService::PIPEDRIVE_PREFECTURES_STOCK_STAGE_ID,
|
||||||
|
"département" =>PipedriveService::PIPEDRIVE_DEPARTEMENTS_STOCK_STAGE_ID,
|
||||||
|
"commune" => PipedriveService::PIPEDRIVE_COMMUNES_STOCK_STAGE_ID,
|
||||||
|
"organisme" => PipedriveService::PIPEDRIVE_ORGANISMES_STOCK_STAGE_ID
|
||||||
|
}),
|
||||||
|
style: 'margin-bottom: 20px; width: inherit;' %>
|
||||||
|
|
||||||
|
<%= hidden_field_tag 'email', demande[:email] %>
|
||||||
|
<%= hidden_field_tag 'person_id', demande[:person_id] %>
|
||||||
|
|
||||||
|
<%= submit_tag 'Créer' %>
|
||||||
|
<% end -%>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
|
@ -3,3 +3,7 @@ if Rails.env.production?
|
||||||
else
|
else
|
||||||
API_ENTREPRISE_URL = 'https://staging.entreprise.api.gouv.fr/v2'
|
API_ENTREPRISE_URL = 'https://staging.entreprise.api.gouv.fr/v2'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
PIPEDRIVE_API_URL = 'https://api.pipedrive.com/v1/'
|
||||||
|
PIPEDRIVE_PEOPLE_URL = URI.join(PIPEDRIVE_API_URL, 'persons').to_s
|
||||||
|
PIPEDRIVE_DEALS_URL = URI.join(PIPEDRIVE_API_URL, 'deals').to_s
|
||||||
|
|
Loading…
Reference in a new issue