From 82d6f0b8a87d6d751ad3356d5ddb38a19be60758 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Fri, 6 Apr 2018 09:15:35 +0200 Subject: [PATCH 01/14] Add a method name to better separate responsabilities Also rename a method to better reflect what it does --- app/controllers/manager/demandes_controller.rb | 2 +- app/services/pipedrive_service.rb | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/controllers/manager/demandes_controller.rb b/app/controllers/manager/demandes_controller.rb index f3367f70b..722030215 100644 --- a/app/controllers/manager/demandes_controller.rb +++ b/app/controllers/manager/demandes_controller.rb @@ -56,7 +56,7 @@ module Manager end def demandes - @demandes ||= PipedriveService.fetch_people_demandes + @demandes ||= PipedriveService.get_demandes end end end diff --git a/app/services/pipedrive_service.rb b/app/services/pipedrive_service.rb index e6e920cf2..583874835 100644 --- a/app/services/pipedrive_service.rb +++ b/app/services/pipedrive_service.rb @@ -30,7 +30,11 @@ class PipedriveService update_person_owner(person_id, owner_id) end - def fetch_people_demandes + def get_demandes + get_demandes_from_persons_owned_by_robot + end + + def get_demandes_from_persons_owned_by_robot params = { start: 0, limit: 500, @@ -39,9 +43,9 @@ class PipedriveService } response = RestClient.get(PIPEDRIVE_PEOPLE_URL, { params: params }) - json = JSON.parse(response.body) + json_data = JSON.parse(response.body)['data'] - json['data'].map do |datum| + json_data.map do |datum| { person_id: datum['id'], nom: datum['name'], From e71d3a76faafb24de1aa17ad58253baa080d3d03 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Thu, 5 Apr 2018 15:59:24 +0200 Subject: [PATCH 02/14] Refactor Pipedrive: create adapters and an API files --- .../manager/demandes_controller.rb | 4 +- app/lib/pipedrive/api.rb | 9 ++ app/lib/pipedrive/deal_adapter.rb | 53 +++++++++ app/lib/pipedrive/person_adapter.rb | 34 ++++++ app/services/pipedrive_service.rb | 101 ++---------------- app/views/manager/demandes/index.html.erb | 12 +-- 6 files changed, 111 insertions(+), 102 deletions(-) create mode 100644 app/lib/pipedrive/api.rb create mode 100644 app/lib/pipedrive/deal_adapter.rb create mode 100644 app/lib/pipedrive/person_adapter.rb diff --git a/app/controllers/manager/demandes_controller.rb b/app/controllers/manager/demandes_controller.rb index 722030215..09690f077 100644 --- a/app/controllers/manager/demandes_controller.rb +++ b/app/controllers/manager/demandes_controller.rb @@ -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 diff --git a/app/lib/pipedrive/api.rb b/app/lib/pipedrive/api.rb new file mode 100644 index 000000000..bf4196961 --- /dev/null +++ b/app/lib/pipedrive/api.rb @@ -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 diff --git a/app/lib/pipedrive/deal_adapter.rb b/app/lib/pipedrive/deal_adapter.rb new file mode 100644 index 000000000..f96bc37e1 --- /dev/null +++ b/app/lib/pipedrive/deal_adapter.rb @@ -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 diff --git a/app/lib/pipedrive/person_adapter.rb b/app/lib/pipedrive/person_adapter.rb new file mode 100644 index 000000000..d928bd6c9 --- /dev/null +++ b/app/lib/pipedrive/person_adapter.rb @@ -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 diff --git a/app/services/pipedrive_service.rb b/app/services/pipedrive_service.rb index 583874835..8e4261ae7 100644 --- a/app/services/pipedrive_service.rb +++ b/app/services/pipedrive_service.rb @@ -1,106 +1,19 @@ class PipedriveService - PIPEDRIVE_POSTE_ATTRIBUTE_ID = '33a790746f1713d712fe97bcce9ac1ca6374a4d6' - - PIPEDRIVE_ROBOT_ID = '2748449' - PIPEDRIVE_CAMILLE_ID = '3189424' - - 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) + waiting_deal_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(person_id) + waiting_deal_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 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) + waiting_deal_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(person_id) + waiting_deal_ids.each { |deal_id| Pipedrive::DealAdapter.refuse_deal(deal_id, owner_id) } + Pipedrive::PersonAdapter.update_person_owner(person_id, owner_id) end def get_demandes - get_demandes_from_persons_owned_by_robot - end - - def get_demandes_from_persons_owned_by_robot - params = { - start: 0, - limit: 500, - user_id: PIPEDRIVE_ROBOT_ID, - api_token: PIPEDRIVE_TOKEN - } - - response = RestClient.get(PIPEDRIVE_PEOPLE_URL, { params: 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 - - 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 }) + Pipedrive::PersonAdapter.get_demandes_from_persons_owned_by_robot end end end diff --git a/app/views/manager/demandes/index.html.erb b/app/views/manager/demandes/index.html.erb index 2249e5285..f662a32a1 100644 --- a/app/views/manager/demandes/index.html.erb +++ b/app/views/manager/demandes/index.html.erb @@ -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;' %> From 1f226d882cad383d23623f91a93fd7506205612e Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 28 Mar 2018 11:56:19 +0200 Subject: [PATCH 03/14] Dont use the `class << self` pattern --- app/services/pipedrive_service.rb | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/app/services/pipedrive_service.rb b/app/services/pipedrive_service.rb index 8e4261ae7..b38425f2e 100644 --- a/app/services/pipedrive_service.rb +++ b/app/services/pipedrive_service.rb @@ -1,19 +1,17 @@ class PipedriveService - class << self - def accept_deals_from_person(person_id, owner_id, stage_id) - waiting_deal_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(person_id) - waiting_deal_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 + def self.accept_deals_from_person(person_id, owner_id, stage_id) + waiting_deal_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(person_id) + waiting_deal_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 - def refuse_deals_from_person(person_id, owner_id) - waiting_deal_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(person_id) - waiting_deal_ids.each { |deal_id| Pipedrive::DealAdapter.refuse_deal(deal_id, owner_id) } - Pipedrive::PersonAdapter.update_person_owner(person_id, owner_id) - end + def self.refuse_deals_from_person(person_id, owner_id) + waiting_deal_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(person_id) + waiting_deal_ids.each { |deal_id| Pipedrive::DealAdapter.refuse_deal(deal_id, owner_id) } + Pipedrive::PersonAdapter.update_person_owner(person_id, owner_id) + end - def get_demandes - Pipedrive::PersonAdapter.get_demandes_from_persons_owned_by_robot - end + def self.get_demandes + Pipedrive::PersonAdapter.get_demandes_from_persons_owned_by_robot end end From e31b839e0cb275fabd5c433efeee6326a6b93486 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 28 Mar 2018 14:31:07 +0200 Subject: [PATCH 04/14] Create Pipedrive::API put helpers --- app/lib/pipedrive/api.rb | 22 ++++++++++++++++++---- app/lib/pipedrive/deal_adapter.rb | 8 ++------ app/lib/pipedrive/person_adapter.rb | 4 +--- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/lib/pipedrive/api.rb b/app/lib/pipedrive/api.rb index bf4196961..5e511dc34 100644 --- a/app/lib/pipedrive/api.rb +++ b/app/lib/pipedrive/api.rb @@ -1,9 +1,23 @@ 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 + + def self.put_deal(deal_id, params) + url = PIPEDRIVE_DEALS_URL + "/#{deal_id}?api_token=#{PIPEDRIVE_TOKEN}" + + self.put(url, params) + end + + def self.put_person(person_id, params) + url = PIPEDRIVE_PEOPLE_URL + "/#{person_id}?api_token=#{PIPEDRIVE_TOKEN}" + + self.put(url, params) + end + + private + + def self.put(url, params) + RestClient.put(url, params, { content_type: :json }) + end end diff --git a/app/lib/pipedrive/deal_adapter.rb b/app/lib/pipedrive/deal_adapter.rb index f96bc37e1..391f4efc0 100644 --- a/app/lib/pipedrive/deal_adapter.rb +++ b/app/lib/pipedrive/deal_adapter.rb @@ -15,8 +15,6 @@ class Pipedrive::DealAdapter 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, @@ -24,7 +22,7 @@ class Pipedrive::DealAdapter lost_reason: PIPEDRIVE_LOST_REASON } - Pipedrive::API.put(url, params.to_json) + Pipedrive::API.put_deal(deal_id, params.to_json) end def self.fetch_waiting_deal_ids(person_id) @@ -44,10 +42,8 @@ class Pipedrive::DealAdapter 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) + Pipedrive::API.put_deal(deal_id, params.to_json) end end diff --git a/app/lib/pipedrive/person_adapter.rb b/app/lib/pipedrive/person_adapter.rb index d928bd6c9..3178abe04 100644 --- a/app/lib/pipedrive/person_adapter.rb +++ b/app/lib/pipedrive/person_adapter.rb @@ -25,10 +25,8 @@ class Pipedrive::PersonAdapter 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) + Pipedrive::API.put_person(person_id, params.to_json) end end From 4ff07f2b937f5dc9337fecfac6be6e5e80b701e9 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 28 Mar 2018 14:20:54 +0200 Subject: [PATCH 05/14] Mutualize a call Pipedrive::API.put --- app/lib/pipedrive/api.rb | 2 +- app/lib/pipedrive/deal_adapter.rb | 4 ++-- app/lib/pipedrive/person_adapter.rb | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/lib/pipedrive/api.rb b/app/lib/pipedrive/api.rb index 5e511dc34..910e1be46 100644 --- a/app/lib/pipedrive/api.rb +++ b/app/lib/pipedrive/api.rb @@ -18,6 +18,6 @@ class Pipedrive::API private def self.put(url, params) - RestClient.put(url, params, { content_type: :json }) + RestClient.put(url, params.to_json, { content_type: :json }) end end diff --git a/app/lib/pipedrive/deal_adapter.rb b/app/lib/pipedrive/deal_adapter.rb index 391f4efc0..27cfc98d2 100644 --- a/app/lib/pipedrive/deal_adapter.rb +++ b/app/lib/pipedrive/deal_adapter.rb @@ -22,7 +22,7 @@ class Pipedrive::DealAdapter lost_reason: PIPEDRIVE_LOST_REASON } - Pipedrive::API.put_deal(deal_id, params.to_json) + Pipedrive::API.put_deal(deal_id, params) end def self.fetch_waiting_deal_ids(person_id) @@ -44,6 +44,6 @@ class Pipedrive::DealAdapter 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.to_json) + Pipedrive::API.put_deal(deal_id, params) end end diff --git a/app/lib/pipedrive/person_adapter.rb b/app/lib/pipedrive/person_adapter.rb index 3178abe04..6fdf5bb47 100644 --- a/app/lib/pipedrive/person_adapter.rb +++ b/app/lib/pipedrive/person_adapter.rb @@ -27,6 +27,6 @@ class Pipedrive::PersonAdapter def self.update_person_owner(person_id, owner_id) params = { owner_id: owner_id } - Pipedrive::API.put_person(person_id, params.to_json) + Pipedrive::API.put_person(person_id, params) end end From d287eb0e5f0f4e824d9fbb92062991f7119d7ecb Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 28 Mar 2018 14:43:20 +0200 Subject: [PATCH 06/14] Create Pipedrive::API get helpers --- app/lib/pipedrive/api.rb | 30 +++++++++++++++++++++++++++-- app/lib/pipedrive/deal_adapter.rb | 13 +------------ app/lib/pipedrive/person_adapter.rb | 9 +-------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/app/lib/pipedrive/api.rb b/app/lib/pipedrive/api.rb index 910e1be46..29c66350e 100644 --- a/app/lib/pipedrive/api.rb +++ b/app/lib/pipedrive/api.rb @@ -1,6 +1,28 @@ class Pipedrive::API - def self.get(url, params) - RestClient.get(url, params: params) + PIPEDRIVE_ALL_NOT_DELETED_DEALS = 'all_not_deleted' + + def self.get_persons_owned_by_user(user_id) + params = { + start: 0, + limit: 500, + user_id: user_id, + api_token: PIPEDRIVE_TOKEN + } + + self.get(PIPEDRIVE_PEOPLE_URL, params) + end + + def self.get_deals_for_person(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 + } + + self.get(url, params) end def self.put_deal(deal_id, params) @@ -17,6 +39,10 @@ class Pipedrive::API private + def self.get(url, params) + RestClient.get(url, params: params) + end + def self.put(url, params) RestClient.put(url, params.to_json, { content_type: :json }) end diff --git a/app/lib/pipedrive/deal_adapter.rb b/app/lib/pipedrive/deal_adapter.rb index 27cfc98d2..46379c61b 100644 --- a/app/lib/pipedrive/deal_adapter.rb +++ b/app/lib/pipedrive/deal_adapter.rb @@ -7,8 +7,6 @@ class Pipedrive::DealAdapter 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" @@ -26,16 +24,7 @@ class Pipedrive::DealAdapter 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) + response = Pipedrive::API.get_deals_for_person(person_id) json_data = JSON.parse(response.body)['data'] json_data.map { |datum| datum['id'] } diff --git a/app/lib/pipedrive/person_adapter.rb b/app/lib/pipedrive/person_adapter.rb index 6fdf5bb47..87cc74eba 100644 --- a/app/lib/pipedrive/person_adapter.rb +++ b/app/lib/pipedrive/person_adapter.rb @@ -3,14 +3,7 @@ class Pipedrive::PersonAdapter 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) + response = Pipedrive::API.get_persons_owned_by_user(PIPEDRIVE_ROBOT_ID) json_data = JSON.parse(response.body)['data'] json_data.map do |datum| From d202e0173e11046c5b7745ff237ae83a706fe565 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 28 Mar 2018 14:33:51 +0200 Subject: [PATCH 07/14] Mutualize some code in Pipedrive::API#get --- app/lib/pipedrive/api.rb | 3 ++- app/lib/pipedrive/deal_adapter.rb | 6 ++---- app/lib/pipedrive/person_adapter.rb | 5 +---- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/app/lib/pipedrive/api.rb b/app/lib/pipedrive/api.rb index 29c66350e..0b14da134 100644 --- a/app/lib/pipedrive/api.rb +++ b/app/lib/pipedrive/api.rb @@ -40,7 +40,8 @@ class Pipedrive::API private def self.get(url, params) - RestClient.get(url, params: params) + response = RestClient.get(url, params: params) + JSON.parse(response.body)['data'] end def self.put(url, params) diff --git a/app/lib/pipedrive/deal_adapter.rb b/app/lib/pipedrive/deal_adapter.rb index 46379c61b..91a28b39b 100644 --- a/app/lib/pipedrive/deal_adapter.rb +++ b/app/lib/pipedrive/deal_adapter.rb @@ -24,10 +24,8 @@ class Pipedrive::DealAdapter end def self.fetch_waiting_deal_ids(person_id) - response = Pipedrive::API.get_deals_for_person(person_id) - json_data = JSON.parse(response.body)['data'] - - json_data.map { |datum| datum['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) diff --git a/app/lib/pipedrive/person_adapter.rb b/app/lib/pipedrive/person_adapter.rb index 87cc74eba..f395bbea9 100644 --- a/app/lib/pipedrive/person_adapter.rb +++ b/app/lib/pipedrive/person_adapter.rb @@ -3,10 +3,7 @@ class Pipedrive::PersonAdapter PIPEDRIVE_ROBOT_ID = '2748449' def self.get_demandes_from_persons_owned_by_robot - response = Pipedrive::API.get_persons_owned_by_user(PIPEDRIVE_ROBOT_ID) - json_data = JSON.parse(response.body)['data'] - - json_data.map do |datum| + Pipedrive::API.get_persons_owned_by_user(PIPEDRIVE_ROBOT_ID).map do |datum| { person_id: datum['id'], nom: datum['name'], From e688fc421242aecfcc7e84afef80214e2fd240bb Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 28 Mar 2018 14:35:34 +0200 Subject: [PATCH 08/14] Mutualize some code in Pipedrive::API#get --- app/lib/pipedrive/api.rb | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/app/lib/pipedrive/api.rb b/app/lib/pipedrive/api.rb index 0b14da134..0ff9ced04 100644 --- a/app/lib/pipedrive/api.rb +++ b/app/lib/pipedrive/api.rb @@ -2,12 +2,7 @@ class Pipedrive::API PIPEDRIVE_ALL_NOT_DELETED_DEALS = 'all_not_deleted' def self.get_persons_owned_by_user(user_id) - params = { - start: 0, - limit: 500, - user_id: user_id, - api_token: PIPEDRIVE_TOKEN - } + params = { user_id: user_id } self.get(PIPEDRIVE_PEOPLE_URL, params) end @@ -15,12 +10,7 @@ class Pipedrive::API def self.get_deals_for_person(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 - } + params = { status: PIPEDRIVE_ALL_NOT_DELETED_DEALS } self.get(url, params) end @@ -40,6 +30,12 @@ class Pipedrive::API 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 From 298d7d050cf4b88ea872461a251817ff987e7fd4 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 28 Mar 2018 14:41:14 +0200 Subject: [PATCH 09/14] Mutualize some code in Pipedrive::API#put --- app/lib/pipedrive/api.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/lib/pipedrive/api.rb b/app/lib/pipedrive/api.rb index 0ff9ced04..1b08b361d 100644 --- a/app/lib/pipedrive/api.rb +++ b/app/lib/pipedrive/api.rb @@ -16,13 +16,13 @@ class Pipedrive::API end def self.put_deal(deal_id, params) - url = PIPEDRIVE_DEALS_URL + "/#{deal_id}?api_token=#{PIPEDRIVE_TOKEN}" + url = PIPEDRIVE_DEALS_URL + "/#{deal_id}" self.put(url, params) end def self.put_person(person_id, params) - url = PIPEDRIVE_PEOPLE_URL + "/#{person_id}?api_token=#{PIPEDRIVE_TOKEN}" + url = PIPEDRIVE_PEOPLE_URL + "/#{person_id}" self.put(url, params) end @@ -41,6 +41,8 @@ class Pipedrive::API end def self.put(url, params) + url = "#{url}?api_token=#{PIPEDRIVE_TOKEN}" + RestClient.put(url, params.to_json, { content_type: :json }) end end From c5953f8aef5c28a300e6c34a8799430e9a7330aa Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 28 Mar 2018 14:42:01 +0200 Subject: [PATCH 10/14] Use .join to generate urls (unification) --- app/lib/pipedrive/api.rb | 6 ++++-- config/initializers/urls.rb | 4 +--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/lib/pipedrive/api.rb b/app/lib/pipedrive/api.rb index 1b08b361d..d0c783dcf 100644 --- a/app/lib/pipedrive/api.rb +++ b/app/lib/pipedrive/api.rb @@ -1,5 +1,7 @@ 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) params = { user_id: user_id } @@ -16,13 +18,13 @@ class Pipedrive::API end def self.put_deal(deal_id, params) - url = PIPEDRIVE_DEALS_URL + "/#{deal_id}" + 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}" + url = [PIPEDRIVE_PEOPLE_URL, person_id].join("/") self.put(url, params) end diff --git a/config/initializers/urls.rb b/config/initializers/urls.rb index 17bccab1a..510cd60ea 100644 --- a/config/initializers/urls.rb +++ b/config/initializers/urls.rb @@ -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' From 71e9a6056d22d5800428d83c1f6916a18abca9a5 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 28 Mar 2018 14:53:12 +0200 Subject: [PATCH 11/14] Structure the helpers methods the same way --- app/lib/pipedrive/api.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/lib/pipedrive/api.rb b/app/lib/pipedrive/api.rb index d0c783dcf..0ce970635 100644 --- a/app/lib/pipedrive/api.rb +++ b/app/lib/pipedrive/api.rb @@ -4,14 +4,14 @@ class Pipedrive::API 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(PIPEDRIVE_PEOPLE_URL, params) + 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) From 4c24c8346a396c75639b5b9c90e654787faf0376 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Wed, 28 Mar 2018 15:19:34 +0200 Subject: [PATCH 12/14] Use better names for PipedriveService methods --- app/jobs/pipedrive_accepts_deals_job.rb | 2 +- app/jobs/pipedrive_refuses_deals_job.rb | 2 +- app/services/pipedrive_service.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/jobs/pipedrive_accepts_deals_job.rb b/app/jobs/pipedrive_accepts_deals_job.rb index 4df5a0406..84a4e96d5 100644 --- a/app/jobs/pipedrive_accepts_deals_job.rb +++ b/app/jobs/pipedrive_accepts_deals_job.rb @@ -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 diff --git a/app/jobs/pipedrive_refuses_deals_job.rb b/app/jobs/pipedrive_refuses_deals_job.rb index 14f0a3ca9..61c157efb 100644 --- a/app/jobs/pipedrive_refuses_deals_job.rb +++ b/app/jobs/pipedrive_refuses_deals_job.rb @@ -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 diff --git a/app/services/pipedrive_service.rb b/app/services/pipedrive_service.rb index b38425f2e..774628b7d 100644 --- a/app/services/pipedrive_service.rb +++ b/app/services/pipedrive_service.rb @@ -1,11 +1,11 @@ class PipedriveService - def self.accept_deals_from_person(person_id, owner_id, stage_id) + def self.accept_demande_from_person(person_id, owner_id, stage_id) waiting_deal_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(person_id) waiting_deal_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 - def self.refuse_deals_from_person(person_id, owner_id) + def self.refuse_demande_from_person(person_id, owner_id) waiting_deal_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(person_id) waiting_deal_ids.each { |deal_id| Pipedrive::DealAdapter.refuse_deal(deal_id, owner_id) } Pipedrive::PersonAdapter.update_person_owner(person_id, owner_id) From 136f15b89c4aad4070eb00aff0485cb49881de1b Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Thu, 5 Apr 2018 16:49:39 +0200 Subject: [PATCH 13/14] Improve some variable names --- app/services/pipedrive_service.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/services/pipedrive_service.rb b/app/services/pipedrive_service.rb index 774628b7d..1ff086d67 100644 --- a/app/services/pipedrive_service.rb +++ b/app/services/pipedrive_service.rb @@ -1,13 +1,13 @@ class PipedriveService def self.accept_demande_from_person(person_id, owner_id, stage_id) - waiting_deal_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(person_id) - waiting_deal_ids.each { |deal_id| Pipedrive::DealAdapter.update_deal_owner_and_stage(deal_id, owner_id, stage_id) } + person_deals_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(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 def self.refuse_demande_from_person(person_id, owner_id) - waiting_deal_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(person_id) - waiting_deal_ids.each { |deal_id| Pipedrive::DealAdapter.refuse_deal(deal_id, owner_id) } + person_deals_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(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 From 26b66aac963830bef3f4184f2216673ffe6eb906 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Thu, 5 Apr 2018 16:50:57 +0200 Subject: [PATCH 14/14] Improve a method name --- app/lib/pipedrive/deal_adapter.rb | 2 +- app/services/pipedrive_service.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/lib/pipedrive/deal_adapter.rb b/app/lib/pipedrive/deal_adapter.rb index 91a28b39b..a8c4ad826 100644 --- a/app/lib/pipedrive/deal_adapter.rb +++ b/app/lib/pipedrive/deal_adapter.rb @@ -23,7 +23,7 @@ class Pipedrive::DealAdapter Pipedrive::API.put_deal(deal_id, params) end - def self.fetch_waiting_deal_ids(person_id) + def self.get_deals_ids_for_person(person_id) Pipedrive::API.get_deals_for_person(person_id) .map { |datum| datum['id'] } end diff --git a/app/services/pipedrive_service.rb b/app/services/pipedrive_service.rb index 1ff086d67..09fc32779 100644 --- a/app/services/pipedrive_service.rb +++ b/app/services/pipedrive_service.rb @@ -1,12 +1,12 @@ class PipedriveService def self.accept_demande_from_person(person_id, owner_id, stage_id) - person_deals_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(person_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 def self.refuse_demande_from_person(person_id, owner_id) - person_deals_ids = Pipedrive::DealAdapter.fetch_waiting_deal_ids(person_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