Merge pull request #1763 from betagouv/pipedrive-refactor
Pipedrive refactor
This commit is contained in:
commit
efaa99c489
9 changed files with 132 additions and 111 deletions
|
@ -10,7 +10,7 @@ module Manager
|
|||
if administrateur.errors.empty?
|
||||
PipedriveAcceptsDealsJob.perform_later(
|
||||
create_administrateur_params[:person_id],
|
||||
PipedriveService::PIPEDRIVE_CAMILLE_ID,
|
||||
Pipedrive::DealAdapter::PIPEDRIVE_CAMILLE_ID,
|
||||
create_administrateur_params[:stage_id]
|
||||
)
|
||||
|
||||
|
@ -26,7 +26,7 @@ module Manager
|
|||
def refuse_administrateur
|
||||
PipedriveRefusesDealsJob.perform_later(
|
||||
refuse_administrateur_params[:person_id],
|
||||
PipedriveService::PIPEDRIVE_CAMILLE_ID
|
||||
Pipedrive::DealAdapter::PIPEDRIVE_CAMILLE_ID
|
||||
)
|
||||
|
||||
AdministrationMailer
|
||||
|
@ -56,7 +56,7 @@ module Manager
|
|||
end
|
||||
|
||||
def demandes
|
||||
@demandes ||= PipedriveService.fetch_people_demandes
|
||||
@demandes ||= PipedriveService.get_demandes
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
class PipedriveAcceptsDealsJob < ApplicationJob
|
||||
def perform(person_id, owner_id, stage_id)
|
||||
PipedriveService.accept_deals_from_person(person_id, owner_id, stage_id)
|
||||
PipedriveService.accept_demande_from_person(person_id, owner_id, stage_id)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
class PipedriveRefusesDealsJob < ApplicationJob
|
||||
def perform(person_id, owner_id)
|
||||
PipedriveService.refuse_deals_from_person(person_id, owner_id)
|
||||
PipedriveService.refuse_demande_from_person(person_id, owner_id)
|
||||
end
|
||||
end
|
||||
|
|
50
app/lib/pipedrive/api.rb
Normal file
50
app/lib/pipedrive/api.rb
Normal file
|
@ -0,0 +1,50 @@
|
|||
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("/")
|
||||
|
||||
def self.get_persons_owned_by_user(user_id)
|
||||
url = PIPEDRIVE_PEOPLE_URL
|
||||
params = { user_id: user_id }
|
||||
|
||||
self.get(url, params)
|
||||
end
|
||||
|
||||
def self.get_deals_for_person(person_id)
|
||||
url = [PIPEDRIVE_PEOPLE_URL, person_id, "deals"].join('/')
|
||||
params = { status: PIPEDRIVE_ALL_NOT_DELETED_DEALS }
|
||||
|
||||
self.get(url, params)
|
||||
end
|
||||
|
||||
def self.put_deal(deal_id, params)
|
||||
url = [PIPEDRIVE_DEALS_URL, deal_id].join("/")
|
||||
|
||||
self.put(url, params)
|
||||
end
|
||||
|
||||
def self.put_person(person_id, params)
|
||||
url = [PIPEDRIVE_PEOPLE_URL, person_id].join("/")
|
||||
|
||||
self.put(url, params)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.get(url, params)
|
||||
params.merge!({
|
||||
start: 0,
|
||||
limit: 500,
|
||||
api_token: PIPEDRIVE_TOKEN
|
||||
})
|
||||
|
||||
response = RestClient.get(url, params: params)
|
||||
JSON.parse(response.body)['data']
|
||||
end
|
||||
|
||||
def self.put(url, params)
|
||||
url = "#{url}?api_token=#{PIPEDRIVE_TOKEN}"
|
||||
|
||||
RestClient.put(url, params.to_json, { content_type: :json })
|
||||
end
|
||||
end
|
36
app/lib/pipedrive/deal_adapter.rb
Normal file
36
app/lib/pipedrive/deal_adapter.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
class Pipedrive::DealAdapter
|
||||
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
|
||||
PIPEDRIVE_ORGANISMES_REFUSES_STOCK_STAGE_ID = 45
|
||||
|
||||
PIPEDRIVE_LOST_STATUS = "lost"
|
||||
PIPEDRIVE_LOST_REASON = "refusé depuis DS"
|
||||
|
||||
PIPEDRIVE_CAMILLE_ID = '3189424'
|
||||
|
||||
def self.refuse_deal(deal_id, owner_id)
|
||||
params = {
|
||||
user_id: owner_id,
|
||||
stage_id: PIPEDRIVE_ORGANISMES_REFUSES_STOCK_STAGE_ID,
|
||||
status: PIPEDRIVE_LOST_STATUS,
|
||||
lost_reason: PIPEDRIVE_LOST_REASON
|
||||
}
|
||||
|
||||
Pipedrive::API.put_deal(deal_id, params)
|
||||
end
|
||||
|
||||
def self.get_deals_ids_for_person(person_id)
|
||||
Pipedrive::API.get_deals_for_person(person_id)
|
||||
.map { |datum| datum['id'] }
|
||||
end
|
||||
|
||||
def self.update_deal_owner_and_stage(deal_id, owner_id, stage_id)
|
||||
params = { user_id: owner_id, stage_id: stage_id }
|
||||
|
||||
Pipedrive::API.put_deal(deal_id, params)
|
||||
end
|
||||
end
|
22
app/lib/pipedrive/person_adapter.rb
Normal file
22
app/lib/pipedrive/person_adapter.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
class Pipedrive::PersonAdapter
|
||||
PIPEDRIVE_POSTE_ATTRIBUTE_ID = '33a790746f1713d712fe97bcce9ac1ca6374a4d6'
|
||||
PIPEDRIVE_ROBOT_ID = '2748449'
|
||||
|
||||
def self.get_demandes_from_persons_owned_by_robot
|
||||
Pipedrive::API.get_persons_owned_by_user(PIPEDRIVE_ROBOT_ID).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
|
||||
|
||||
def self.update_person_owner(person_id, owner_id)
|
||||
params = { owner_id: owner_id }
|
||||
|
||||
Pipedrive::API.put_person(person_id, params)
|
||||
end
|
||||
end
|
|
@ -1,102 +1,17 @@
|
|||
class PipedriveService
|
||||
PIPEDRIVE_POSTE_ATTRIBUTE_ID = '33a790746f1713d712fe97bcce9ac1ca6374a4d6'
|
||||
def self.accept_demande_from_person(person_id, owner_id, stage_id)
|
||||
person_deals_ids = Pipedrive::DealAdapter.get_deals_ids_for_person(person_id)
|
||||
person_deals_ids.each { |deal_id| Pipedrive::DealAdapter.update_deal_owner_and_stage(deal_id, owner_id, stage_id) }
|
||||
Pipedrive::PersonAdapter.update_person_owner(person_id, owner_id)
|
||||
end
|
||||
|
||||
PIPEDRIVE_ROBOT_ID = '2748449'
|
||||
PIPEDRIVE_CAMILLE_ID = '3189424'
|
||||
def self.refuse_demande_from_person(person_id, owner_id)
|
||||
person_deals_ids = Pipedrive::DealAdapter.get_deals_ids_for_person(person_id)
|
||||
person_deals_ids.each { |deal_id| Pipedrive::DealAdapter.refuse_deal(deal_id, owner_id) }
|
||||
Pipedrive::PersonAdapter.update_person_owner(person_id, owner_id)
|
||||
end
|
||||
|
||||
PIPEDRIVE_ALL_NOT_DELETED_DEALS = 'all_not_deleted'
|
||||
|
||||
PIPEDRIVE_LOST_STATUS = "lost"
|
||||
PIPEDRIVE_LOST_REASON = "refusé depuis DS"
|
||||
|
||||
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
|
||||
PIPEDRIVE_ORGANISMES_REFUSES_STOCK_STAGE_ID = 45
|
||||
|
||||
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 refuse_deals_from_person(person_id, owner_id)
|
||||
waiting_deal_ids = fetch_waiting_deal_ids(person_id)
|
||||
waiting_deal_ids.each { |deal_id| refuse_deal(deal_id, owner_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 refuse_deal(deal_id, owner_id)
|
||||
url = PIPEDRIVE_DEALS_URL + "/#{deal_id}?api_token=#{PIPEDRIVE_TOKEN}"
|
||||
|
||||
params = {
|
||||
user_id: owner_id,
|
||||
stage_id: PIPEDRIVE_ORGANISMES_REFUSES_STOCK_STAGE_ID,
|
||||
status: PIPEDRIVE_LOST_STATUS,
|
||||
lost_reason: PIPEDRIVE_LOST_REASON
|
||||
}
|
||||
|
||||
RestClient.put(url, params.to_json, { content_type: :json })
|
||||
end
|
||||
|
||||
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
|
||||
def self.get_demandes
|
||||
Pipedrive::PersonAdapter.get_demandes_from_persons_owned_by_robot
|
||||
end
|
||||
end
|
||||
|
|
|
@ -33,12 +33,12 @@
|
|||
<%= form_tag(manager_demandes_create_administrateur_path) do -%>
|
||||
<%= select_tag "stage_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
|
||||
"administration centrale" => Pipedrive::DealAdapter::PIPEDRIVE_ADMIN_CENTRAL_STOCK_STAGE_ID,
|
||||
"région" => Pipedrive::DealAdapter::PIPEDRIVE_REGIONS_STOCK_STAGE_ID,
|
||||
"préfecture" => Pipedrive::DealAdapter::PIPEDRIVE_PREFECTURES_STOCK_STAGE_ID,
|
||||
"département" =>Pipedrive::DealAdapter::PIPEDRIVE_DEPARTEMENTS_STOCK_STAGE_ID,
|
||||
"commune" => Pipedrive::DealAdapter::PIPEDRIVE_COMMUNES_STOCK_STAGE_ID,
|
||||
"organisme" => Pipedrive::DealAdapter::PIPEDRIVE_ORGANISMES_STOCK_STAGE_ID
|
||||
}),
|
||||
style: 'margin-bottom: 20px; width: inherit;' %>
|
||||
|
||||
|
|
|
@ -4,6 +4,4 @@ else
|
|||
API_ENTREPRISE_URL = 'https://staging.entreprise.api.gouv.fr/v2'
|
||||
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
|
||||
PIPEDRIVE_API_URL = 'https://api.pipedrive.com/v1'
|
||||
|
|
Loading…
Reference in a new issue