Refactor Pipedrive: create adapters and an API files
This commit is contained in:
parent
82d6f0b8a8
commit
e71d3a76fa
6 changed files with 111 additions and 102 deletions
9
app/lib/pipedrive/api.rb
Normal file
9
app/lib/pipedrive/api.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
class Pipedrive::API
|
||||
def self.put(url, params)
|
||||
RestClient.put(url, params, { content_type: :json })
|
||||
end
|
||||
|
||||
def self.get(url, params)
|
||||
RestClient.get(url, params: params)
|
||||
end
|
||||
end
|
53
app/lib/pipedrive/deal_adapter.rb
Normal file
53
app/lib/pipedrive/deal_adapter.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
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_ALL_NOT_DELETED_DEALS = 'all_not_deleted'
|
||||
|
||||
PIPEDRIVE_LOST_STATUS = "lost"
|
||||
PIPEDRIVE_LOST_REASON = "refusé depuis DS"
|
||||
|
||||
PIPEDRIVE_CAMILLE_ID = '3189424'
|
||||
|
||||
def self.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
|
||||
}
|
||||
|
||||
Pipedrive::API.put(url, params.to_json)
|
||||
end
|
||||
|
||||
def self.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 = Pipedrive::API.get(url, params)
|
||||
json_data = JSON.parse(response.body)['data']
|
||||
|
||||
json_data.map { |datum| datum['id'] }
|
||||
end
|
||||
|
||||
def self.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 }
|
||||
|
||||
Pipedrive::API.put(url, params.to_json)
|
||||
end
|
||||
end
|
34
app/lib/pipedrive/person_adapter.rb
Normal file
34
app/lib/pipedrive/person_adapter.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
class Pipedrive::PersonAdapter
|
||||
PIPEDRIVE_POSTE_ATTRIBUTE_ID = '33a790746f1713d712fe97bcce9ac1ca6374a4d6'
|
||||
PIPEDRIVE_ROBOT_ID = '2748449'
|
||||
|
||||
def self.get_demandes_from_persons_owned_by_robot
|
||||
params = {
|
||||
start: 0,
|
||||
limit: 500,
|
||||
user_id: PIPEDRIVE_ROBOT_ID,
|
||||
api_token: PIPEDRIVE_TOKEN
|
||||
}
|
||||
|
||||
response = Pipedrive::API.get(PIPEDRIVE_PEOPLE_URL, params)
|
||||
json_data = JSON.parse(response.body)['data']
|
||||
|
||||
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
|
||||
|
||||
def self.update_person_owner(person_id, owner_id)
|
||||
url = PIPEDRIVE_PEOPLE_URL + "/#{person_id}?api_token=#{PIPEDRIVE_TOKEN}"
|
||||
|
||||
params = { owner_id: owner_id }
|
||||
|
||||
Pipedrive::API.put(url, params.to_json)
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue