Pipedrive: extract methods in service
This commit is contained in:
parent
b7d31aac96
commit
ec3d558af0
3 changed files with 43 additions and 37 deletions
|
@ -1,10 +1,5 @@
|
|||
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
|
||||
|
@ -13,7 +8,7 @@ module Manager
|
|||
administrateur = current_administration.invite_admin(create_administrateur_params[:email])
|
||||
|
||||
if administrateur.errors.empty?
|
||||
change_person_owner(create_administrateur_params[:person_id], PIPEDRIVE_CAMILLE_ID)
|
||||
PipedriveService.update_person_owner(create_administrateur_params[:person_id], PipedriveService::PIPEDRIVE_CAMILLE_ID)
|
||||
flash.notice = "Administrateur créé"
|
||||
redirect_to manager_demandes_path
|
||||
else
|
||||
|
@ -25,14 +20,6 @@ module Manager
|
|||
|
||||
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
|
||||
|
@ -46,29 +33,7 @@ module Manager
|
|||
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
|
||||
@demandes ||= PipedriveService.fetch_people_demandes
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
38
app/services/pipedrive_service.rb
Normal file
38
app/services/pipedrive_service.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
class PipedriveService
|
||||
PIPEDRIVE_POSTE_ATTRIBUTE_ID = '33a790746f1713d712fe97bcce9ac1ca6374a4d6'
|
||||
|
||||
PIPEDRIVE_DEV_ID = '2748449'
|
||||
PIPEDRIVE_CAMILLE_ID = '3189424'
|
||||
|
||||
class << self
|
||||
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 fetch_people_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
|
|
@ -3,3 +3,6 @@ if Rails.env.production?
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue