From 2b836b719d854eac4287b4bacd33c8fa80364577 Mon Sep 17 00:00:00 2001 From: Ben Greenberg Date: Wed, 19 May 2021 09:09:23 +0300 Subject: [PATCH 1/2] check notes on people --- Gemfile.lock | 2 +- README.md | 13 ++++ bin/pipedrive_orbit | 10 +++ lib/pipedrive_orbit/client.rb | 9 +++ .../interactions/person_note.rb | 76 +++++++++++++++++++ lib/pipedrive_orbit/orbit.rb | 9 +++ lib/pipedrive_orbit/pipedrive.rb | 54 +++++++++++++ scripts/check_people_notes.rb | 17 +++++ spec/interactions/person_note_spec.rb | 57 ++++++++++++++ 9 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 lib/pipedrive_orbit/interactions/person_note.rb create mode 100644 scripts/check_people_notes.rb create mode 100644 spec/interactions/person_note_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 7b709df..a46ad8b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - pipedrive_orbit (0.0.1) + pipedrive_orbit (0.0.2) activesupport (~> 6.1) dotenv (~> 2.7) http (~> 4.4) diff --git a/README.md b/README.md index 2d5a6f0..b256785 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,13 @@ You can fetch Pipedrive activities and send them to Orbit by invoking the `#acti ```ruby client.activities ``` +### Fetching Pipedrive Notes on People + +You can fetch Pipedrive notes on people and send them to Orbit by invoking the `#people_notes` instance method: + +```ruby +client.people_notes +``` ## CLI Usage You can also use this package with the included CLI. To use the CLI pass in the required environment variables on the command line before invoking the CLI. @@ -83,6 +90,12 @@ To check for new activities: $ ORBIT_API_KEY=... ORBIT_WORKSPACE_ID=... PIPEDRIVE_API_KEY=... PIPEDRIVE_URL=... bundle exec pipedrive_orbit --check_activities ``` +To check for new notes on people: + +```bash +$ ORBIT_API_KEY=... ORBIT_WORKSPACE_ID=... PIPEDRIVE_API_KEY=... PIPEDRIVE_URL=... bundle exec pipedrive_orbit --check_people_notes +``` + ## GitHub Actions Automation Setup ⚡ You can set up this integration in a matter of minutes using our GitHub Actions template. It will run regularly to add new activities to your Orbit workspace. All you need is a GitHub account. diff --git a/bin/pipedrive_orbit b/bin/pipedrive_orbit index dbc552f..c7f8f6e 100755 --- a/bin/pipedrive_orbit +++ b/bin/pipedrive_orbit @@ -4,6 +4,7 @@ require 'optparse' check_notes = false check_activities = false +check_people_notes = false options = {} choices = OptionParser.new do |opts| @@ -18,6 +19,9 @@ choices = OptionParser.new do |opts| opts.on("--check-check_activities", "Check for new Pipedrive activities") do check_activities = true end + opts.on("--check-people-notes", "Check for new Pipedrive notes on people") do + check_people_notes = true + end end.parse! $LOAD_PATH.unshift(File.expand_path('../lib/pipedrive_orbit', __dir__)) @@ -35,4 +39,10 @@ if check_activities puts "Checking for new Pipedrive activities and posting them to your Orbit workspace..." ARGV[0] = 'render' PipedriveOrbit::Scripts::CheckActivities.start(ARGV) +end + +if check_people_notes + puts "Checking for new Pipedrive people notes and posting them to your Orbit workspace..." + ARGV[0] = 'render' + PipedriveOrbit::Scripts::CheckPeopleNotes.start(ARGV) end \ No newline at end of file diff --git a/lib/pipedrive_orbit/client.rb b/lib/pipedrive_orbit/client.rb index ce803a5..9c33621 100644 --- a/lib/pipedrive_orbit/client.rb +++ b/lib/pipedrive_orbit/client.rb @@ -56,5 +56,14 @@ def activities orbit_workspace: @orbit_workspace ).process_activities end + + def people_notes + PipedriveOrbit::Pipedrive.new( + pipedrive_api_key: @pipedrive_api_key, + pipedrive_url: @pipedrive_url, + orbit_api_key: @orbit_api_key, + orbit_workspace: @orbit_workspace + ).process_people_notes + end end end \ No newline at end of file diff --git a/lib/pipedrive_orbit/interactions/person_note.rb b/lib/pipedrive_orbit/interactions/person_note.rb new file mode 100644 index 0000000..c2a0fc4 --- /dev/null +++ b/lib/pipedrive_orbit/interactions/person_note.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +require "json" + +module PipedriveOrbit + module Interactions + class PersonNote + def initialize(note:, pipedrive_url:, orbit_workspace:, orbit_api_key:) + @note = note + @pipedrive_url = pipedrive_url + @orbit_workspace = orbit_workspace + @orbit_api_key = orbit_api_key + + after_initialize! + end + + def after_initialize! + OrbitActivities::Request.new( + api_key: @orbit_api_key, + workspace_id: @orbit_workspace, + user_agent: "community-ruby-pipedrive-orbit/#{PipedriveOrbit::VERSION}", + body: construct_body.to_json + ) + end + + def construct_url + return nil if @note["deal_id"].nil? + + if @pipedrive_url.end_with?("/") + return "#{pipedrive_url}deal/#{@note["deal_id"]}" + end + + "#{@pipedrive_url}/deal/#{@note["deal_id"]}" + end + + def construct_name + return @note["person"]["name"] if @note["person"] + + @note["organization"]["name"] + end + + def construct_body + hash = { + activity: { + activity_type: "pipedrive:person_note", + tags: ["channel:pipedrive"], + title: "Added Note to Person in Pipedrive", + description: construct_description, + occurred_at: @note["add_time"], + key: @note["id"], + member: { + name: construct_name + } + }, + identity: { + source: "pipedrive", + name: construct_name + } + } + + hash[:activity].merge!(link: construct_url) unless construct_url.nil? || construct_url == "" + hash[:activity][:member].merge!(company: @note["organization"]["name"]) if @note["organization"] + + hash + end + + def construct_description + note = @note["content"].dup + + note.prepend("Note added for person in connection to #{@note["deal"]["title"]}:
") unless @note["deal"] == nil + + note + end + end + end +end \ No newline at end of file diff --git a/lib/pipedrive_orbit/orbit.rb b/lib/pipedrive_orbit/orbit.rb index f88430f..6ae8b52 100644 --- a/lib/pipedrive_orbit/orbit.rb +++ b/lib/pipedrive_orbit/orbit.rb @@ -20,6 +20,15 @@ def self.call(type:, data:, orbit_workspace:, orbit_api_key:) orbit_api_key: orbit_api_key ) end + + if type == "person_note" + PipedriveOrbit::Interactions::PersonNote.new( + note: data[:note], + pipedrive_url: data[:pipedrive_url], + orbit_workspace: orbit_workspace, + orbit_api_key: orbit_api_key + ) + end end end end \ No newline at end of file diff --git a/lib/pipedrive_orbit/pipedrive.rb b/lib/pipedrive_orbit/pipedrive.rb index a69f03f..a29b721 100644 --- a/lib/pipedrive_orbit/pipedrive.rb +++ b/lib/pipedrive_orbit/pipedrive.rb @@ -49,6 +49,60 @@ def process_activities end end + def process_people_notes + people = get_people + + return people["error"] if people["success"] == false + return "No people found!" if people.nil? + + people["data"].each do |person| + next if person["notes_count"] <= 0 || person["notes_count"].nil? + + notes = get_people_notes(person) + + return notes["error"] if notes["success"] == false + return "No notes found!" if notes.nil? + + notes["data"].each do |note| + PipedriveOrbit::Orbit.call( + type: "person_note", + data: { + note: note, + pipedrive_url: @pipedrive_url + }, + orbit_workspace: @orbit_workspace, + orbit_api_key: @orbit_api_key + ) + end + end + end + + def get_people + url = URI("https://api.pipedrive.com/v1/persons") + url.query = "api_token=#{@pipedrive_api_key}" + https = Net::HTTP.new(url.host, url.port) + https.use_ssl = true + + request = Net::HTTP::Get.new(url) + + response = https.request(request) + + response = JSON.parse(response.body) + end + + def get_people_notes(person) + url = URI("https://api.pipedrive.com/v1/notes") + url.query = "person_id=#{person["id"]}&sort=add_time DESC&api_token=#{@pipedrive_api_key}" + https = Net::HTTP.new(url.host, url.port) + https.use_ssl = true + + request = Net::HTTP::Get.new(url) + + response = https.request(request) + + response = JSON.parse(response.body) + end + def get_activities url = URI("https://api.pipedrive.com/v1/activities") url.query = "user_id=0&start_date=#{create_start_date}&end_date=#{create_end_date}&api_token=#{@pipedrive_api_key}" diff --git a/scripts/check_people_notes.rb b/scripts/check_people_notes.rb new file mode 100644 index 0000000..df8b147 --- /dev/null +++ b/scripts/check_people_notes.rb @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "pipedrive_orbit" +require "thor" + +module PipedriveOrbit + module Scripts + class CheckPeopleNotes < Thor + desc "render", "check for new Pipedrive people notes and push them to Orbit" + def render + client = PipedriveOrbit::Client.new + client.people_notes + end + end + end +end \ No newline at end of file diff --git a/spec/interactions/person_note_spec.rb b/spec/interactions/person_note_spec.rb new file mode 100644 index 0000000..43cd0ef --- /dev/null +++ b/spec/interactions/person_note_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require "spec_helper" + + +RSpec.describe PipedriveOrbit::Interactions::PersonNote do + let(:subject) do + PipedriveOrbit::Interactions::PersonNote.new( + note: { + "id" => "82", + "user_id" => "12301519", + "deal_id" => "1234", + "person_id" => "123", + "org_id" => "444", + "content" => "Test note", + "add_time" => "2021-05-18 05:11:52", + "organization" => { + "name" => "Testing Org" + }, + "person" => { + "name" => "Testing Person" + }, + "deal" => { + "title" => "Testing Deal" + } + }, + pipedrive_url: "https://example.com", + orbit_workspace: "1234", + orbit_api_key: "12345", + + ) + end + + describe "#call" do + context "when the type is a note" do + it "returns a Note Object" do + stub_request(:post, "https://app.orbit.love/api/v1/1234/activities") + .with( + headers: { 'Authorization' => "Bearer 12345", 'Content-Type' => 'application/json', 'User-Agent'=>"community-ruby-pipedrive-orbit/#{PipedriveOrbit::VERSION}" }, + body: "{\"activity\":{\"activity_type\":\"pipedrive:person_note\",\"tags\":[\"channel:pipedrive\"],\"title\":\"Added Note to Person in Pipedrive\",\"description\":\"Note added for person in connection to Testing Deal:
Test note\",\"occurred_at\":\"2021-05-18 05:11:52\",\"key\":\"82\",\"member\":{\"name\":\"Testing Person\",\"company\":\"Testing Org\"},\"link\":\"https://example.com/deal/1234\"},\"identity\":{\"source\":\"pipedrive\",\"name\":\"Testing Person\"}}" + ) + .to_return( + status: 200, + body: { + response: { + code: 'SUCCESS' + } + }.to_json.to_s + ) + + content = subject.construct_body + + expect(content[:activity][:key]).to eql("82") + end + end + end +end \ No newline at end of file From e38dfa98f54170d0af603de4a2dd37005dc0f3ad Mon Sep 17 00:00:00 2001 From: Ben Greenberg Date: Wed, 19 May 2021 09:09:49 +0300 Subject: [PATCH 2/2] increment version --- lib/pipedrive_orbit/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pipedrive_orbit/version.rb b/lib/pipedrive_orbit/version.rb index fd17317..f350ff0 100644 --- a/lib/pipedrive_orbit/version.rb +++ b/lib/pipedrive_orbit/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal = true module PipedriveOrbit - VERSION = "0.0.2" + VERSION = "0.0.3" end \ No newline at end of file