Skip to content

Commit

Permalink
Merge pull request #4 from orbit-love/user-notes
Browse files Browse the repository at this point in the history
Check notes on people
  • Loading branch information
hummusonrails authored May 19, 2021
2 parents 21b92f6 + e38dfa9 commit 6e31ffd
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions bin/pipedrive_orbit
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require 'optparse'

check_notes = false
check_activities = false
check_people_notes = false

options = {}
choices = OptionParser.new do |opts|
Expand All @@ -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__))
Expand All @@ -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
9 changes: 9 additions & 0 deletions lib/pipedrive_orbit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
76 changes: 76 additions & 0 deletions lib/pipedrive_orbit/interactions/person_note.rb
Original file line number Diff line number Diff line change
@@ -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"]}:<br>") unless @note["deal"] == nil

note
end
end
end
end
9 changes: 9 additions & 0 deletions lib/pipedrive_orbit/orbit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 54 additions & 0 deletions lib/pipedrive_orbit/pipedrive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 1 addition & 1 deletion lib/pipedrive_orbit/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal = true

module PipedriveOrbit
VERSION = "0.0.2"
VERSION = "0.0.3"
end
17 changes: 17 additions & 0 deletions scripts/check_people_notes.rb
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions spec/interactions/person_note_spec.rb
Original file line number Diff line number Diff line change
@@ -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:<br>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

0 comments on commit 6e31ffd

Please sign in to comment.