[fix #1697] Pipedrive: use a job to process an admin creation
This commit is contained in:
parent
31adc8e990
commit
6a74aaa42a
4 changed files with 54 additions and 7 deletions
|
@ -8,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?
|
||||||
PipedriveService.update_person_owner(create_administrateur_params[:person_id], PipedriveService::PIPEDRIVE_CAMILLE_ID)
|
PipedriveAcceptsDealsJob.perform_later(
|
||||||
|
create_administrateur_params[:person_id],
|
||||||
|
PipedriveService::PIPEDRIVE_CAMILLE_ID,
|
||||||
|
PipedriveService::PIPEDRIVE_ADMIN_CENTRAL_STOCK_STAGE_ID
|
||||||
|
)
|
||||||
|
|
||||||
flash.notice = "Administrateur créé"
|
flash.notice = "Administrateur créé"
|
||||||
redirect_to manager_demandes_path
|
redirect_to manager_demandes_path
|
||||||
else
|
else
|
||||||
|
|
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
|
|
@ -4,13 +4,15 @@ class PipedriveService
|
||||||
PIPEDRIVE_ROBOT_ID = '2748449'
|
PIPEDRIVE_ROBOT_ID = '2748449'
|
||||||
PIPEDRIVE_CAMILLE_ID = '3189424'
|
PIPEDRIVE_CAMILLE_ID = '3189424'
|
||||||
|
|
||||||
|
PIPEDRIVE_ALL_NOT_DELETED_DEALS = 'all_not_deleted'
|
||||||
|
|
||||||
|
PIPEDRIVE_ADMIN_CENTRAL_STOCK_STAGE_ID = 35
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def update_person_owner(person_id, owner_id)
|
def accept_deals_from_person(person_id, owner_id, stage_id)
|
||||||
url = PIPEDRIVE_PEOPLE_URL + "/#{person_id}?api_token=#{PIPEDRIVE_TOKEN}"
|
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) }
|
||||||
params = { owner_id: owner_id }
|
update_person_owner(person_id, owner_id)
|
||||||
|
|
||||||
RestClient.put(url, params.to_json, { content_type: :json })
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_people_demandes
|
def fetch_people_demandes
|
||||||
|
@ -34,5 +36,39 @@ class PipedriveService
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,3 +6,4 @@ end
|
||||||
|
|
||||||
PIPEDRIVE_API_URL = 'https://api.pipedrive.com/v1/'
|
PIPEDRIVE_API_URL = 'https://api.pipedrive.com/v1/'
|
||||||
PIPEDRIVE_PEOPLE_URL = URI.join(PIPEDRIVE_API_URL, 'persons').to_s
|
PIPEDRIVE_PEOPLE_URL = URI.join(PIPEDRIVE_API_URL, 'persons').to_s
|
||||||
|
PIPEDRIVE_DEALS_URL = URI.join(PIPEDRIVE_API_URL, 'deals').to_s
|
||||||
|
|
Loading…
Add table
Reference in a new issue