Skip to content

Commit

Permalink
move append_association to Hubspot::Association
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Yves committed Nov 21, 2024
1 parent 1db62ca commit bde22b7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 41 deletions.
11 changes: 11 additions & 0 deletions lib/hubspot/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def all(object_type, object_id, to_object_type)
response['results'].map { |result| klass.find(result["toObjectId"]) }
end

# Utility function to build the association list required by some API endpoints
def append_association(association_list, origin, associated, associated_id)
return unless associated_id.present?

association_list << {
"to": { "id": associated_id },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": Hubspot::Association::ASSOCIATION_DEFINITIONS[origin][associated] }]
}
end

private

def build_create_association_body(association, definition_id)
Expand Down
8 changes: 4 additions & 4 deletions lib/hubspot/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def initialize(response_hash)
class << self
def create!(params = {}, ticket_id: nil, contact_id: nil, company_id: nil, deal_id: nil)
associations_hash = { 'associations' => [] }
Hubspot::Utils.append_association(associations_hash['associations'], 'Task', 'Ticket', ticket_id)
Hubspot::Utils.append_association(associations_hash['associations'], 'Task', 'Contact', contact_id)
Hubspot::Utils.append_association(associations_hash['associations'], 'Task', 'Company', company_id)
Hubspot::Utils.append_association(associations_hash['associations'], 'Task', 'Deal', deal_id)
Hubspot::Association.append_association(associations_hash['associations'], 'Task', 'Ticket', ticket_id)
Hubspot::Association.append_association(associations_hash['associations'], 'Task', 'Contact', contact_id)
Hubspot::Association.append_association(associations_hash['associations'], 'Task', 'Company', company_id)
Hubspot::Association.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 })

Expand Down
6 changes: 3 additions & 3 deletions lib/hubspot/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def initialize(response_hash)
class << self
def create!(params = {}, contact_id: nil, company_id: nil, deal_id: nil)
associations_hash = { 'associations' => [] }
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)
Hubspot::Association.append_association(associations_hash['associations'], 'Ticket', 'Contact', contact_id)
Hubspot::Association.append_association(associations_hash['associations'], 'Ticket', 'Company', company_id)
Hubspot::Association.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)
Expand Down
10 changes: 0 additions & 10 deletions lib/hubspot/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,6 @@ 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 << {
"to": { "id": associated_id },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": Hubspot::Association::ASSOCIATION_DEFINITIONS[origin][associated] }]
}
end

private

def find_by_name(name, set)
Expand Down
24 changes: 24 additions & 0 deletions spec/lib/hubspot/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,28 @@
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

context 'when no associated_id' do
it 'does not change the list' do
associations_hash = { 'associations' => [] }
described_class.append_association(associations_hash['associations'], 'Ticket', 'Company', nil)
expect(associations_hash).to eq({ 'associations' => [] })
end
end
end
end
24 changes: 0 additions & 24 deletions spec/lib/hubspot/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,4 @@
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

context 'when no associated_id' do
it 'does not change the list' do
associations_hash = { 'associations' => [] }
described_class.append_association(associations_hash['associations'], 'Ticket', 'Company', nil)
expect(associations_hash).to eq({ 'associations' => [] })
end
end
end
end

0 comments on commit bde22b7

Please sign in to comment.