Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more associations to tasks #26

Merged
merged 7 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.17.2

* Add more params for task association in Task#create

## 0.17.1

* Add default properties for Ticket#find
Expand Down
2 changes: 1 addition & 1 deletion hubspot-api-ruby.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "hubspot-api-ruby"
s.version = "0.17.1"
s.version = "0.17.2"
s.require_paths = ["lib"]
s.authors = ["Jonathan"]
s.email = ["jonathan@hoggo.com"]
Expand Down
13 changes: 5 additions & 8 deletions lib/hubspot/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ def initialize(response_hash)
end

class << self
def create!(params = {}, ticket_id: nil)
def create!(params = {}, ticket_id: nil, contact_id: nil, company_id: nil, deal_id: nil)
associations_hash = { 'associations' => [] }
Paul-Yves marked this conversation as resolved.
Show resolved Hide resolved
if ticket_id.present?
associations_hash['associations'] << {
"to": { "id": ticket_id },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": Hubspot::Association::ASSOCIATION_DEFINITIONS['Task']['Ticket'] }]
}
end
Hubspot::Utils.append_association(associations_hash['associations'], 'Task', 'Ticket', ticket_id)
Hubspot::Utils.append_association(associations_hash['associations'], 'Task', 'Contact', contact_id)
Paul-Yves marked this conversation as resolved.
Show resolved Hide resolved
Hubspot::Utils.append_association(associations_hash['associations'], 'Task', 'Company', company_id)
Hubspot::Utils.append_association(associations_hash['associations'], 'Task', 'Deal', deal_id)
properties = { hs_task_status: 'NOT_STARTED', hs_task_type: 'TODO' }.merge(params)
post_data = associations_hash.merge({ properties: properties })
Paul-Yves marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
25 changes: 3 additions & 22 deletions lib/hubspot/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,10 @@ def initialize(response_hash)
class << self
def create!(params = {}, contact_id: nil, company_id: nil, deal_id: nil)
associations_hash = { 'associations' => [] }
if contact_id.present?
associations_hash['associations'] << {
"to": { "id": contact_id },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": Hubspot::Association::ASSOCIATION_DEFINITIONS['Ticket']['Contact'] }]
}
end
if company_id.present?
associations_hash['associations'] << {
"to": { "id": company_id },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": Hubspot::Association::ASSOCIATION_DEFINITIONS['Ticket']['Company'] }]
}
end
if deal_id.present?
associations_hash['associations'] << {
"to": { "id": deal_id },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": Hubspot::Association::ASSOCIATION_DEFINITIONS['Ticket']['Deal'] }]
}
end
Hubspot::Utils.append_association(associations_hash['associations'], 'Ticket', 'Contact', contact_id)
Hubspot::Utils.append_association(associations_hash['associations'], 'Ticket', 'Company', company_id)
Hubspot::Utils.append_association(associations_hash['associations'], 'Ticket', 'Deal', deal_id)
post_data = associations_hash.merge({ properties: params })

response = Hubspot::Connection.post_json(TICKETS_PATH, params: {}, body: post_data)
new(response)
end
Expand Down
10 changes: 10 additions & 0 deletions lib/hubspot/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ def compare_property_lists(klass, source, target)
[skip, new_groups.to_a, new_props, update_props]
end

def append_association(association_list, origin, associated, associated_id)
return unless associated_id.present?

association_list << {
Paul-Yves marked this conversation as resolved.
Show resolved Hide resolved
"to": { "id": associated_id },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": Hubspot::Association::ASSOCIATION_DEFINITIONS[origin][associated] }]
}
end
Paul-Yves marked this conversation as resolved.
Show resolved Hide resolved

private

def find_by_name(name, set)
Expand Down
24 changes: 12 additions & 12 deletions spec/fixtures/vcr_cassettes/task.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion spec/lib/hubspot/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
hs_task_subject: 'title of task',
hs_timestamp: DateTime.now.strftime('%Q')
}
described_class.create!(params, ticket_id: 16_174_569_112)
described_class.create!(params, ticket_id: 16_174_569_112, contact_id: 75_761_595_194, company_id: 25_571_271_600,
deal_id: 28_806_796_888)
end

it 'creates a new task with valid properties' do
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/hubspot/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,20 @@
end
end
end

describe '.append_association' do
it 'adds proper association hash to target list' do
associations_hash = { 'associations' => [] }
described_class.append_association(associations_hash['associations'], 'Ticket', 'Company', 1337)
expect(associations_hash).to eq(
{
'associations' => [
"to": { "id": 1337 },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": 339 }]
]
}
)
end
end
end