diff --git a/association.go b/association.go index ad85b5c..943e129 100644 --- a/association.go +++ b/association.go @@ -42,6 +42,10 @@ const ( AssociationTypeCompanyToContact AssociationType = "company_to_contact" AssociationTypeCompanyToDeal AssociationType = "company_to_deal" + + AssociationTypeCompanyToCompany AssociationType = "company_to_company" + AssociationTypeChildToParentCompany AssociationType = "child_to_parent_company" + AssociationTypeParentToChildCompany AssociationType = "parent_to_child_company" ) type AssociationConfig struct { @@ -70,3 +74,8 @@ type AssociationResult struct { ID string `json:"id"` Type string `json:"type"` } + +// AssociationsResponse is the response from the GET associations endpoint. +type AssociationsResponse struct { + Results []AssociationResult `json:"results"` +} diff --git a/company.go b/company.go index 1865063..bb6ee7d 100644 --- a/company.go +++ b/company.go @@ -10,6 +10,7 @@ const ( // Reference: https://developers.hubspot.com/docs/api/crm/companies type CompanyService interface { Get(companyID string, company interface{}, option *RequestQueryOption) (*ResponseResource, error) + GetAssociations(companyID string, toObjectType ObjectType) (*AssociationsResponse, error) Create(company interface{}) (*ResponseResource, error) Update(companyID string, company interface{}) (*ResponseResource, error) Delete(companyID string) error @@ -37,6 +38,15 @@ func (s *CompanyServiceOp) Get(companyID string, company interface{}, option *Re return resource, nil } +// GetAssociations gets company associations +func (s *CompanyServiceOp) GetAssociations(companyID string, toObjectType ObjectType) (*AssociationsResponse, error) { + result := &AssociationsResponse{} + if err := s.client.Get(s.companyPath+"/"+companyID+"/associations/"+string(toObjectType), result, nil); err != nil { + return nil, err + } + return result, nil +} + // Create creates a new company. // In order to bind the created content, a structure must be specified as an argument. // When using custom fields, please embed hubspot.Company in your own structure. diff --git a/company_model.go b/company_model.go index 659ceaa..59e3207 100644 --- a/company_model.go +++ b/company_model.go @@ -37,8 +37,8 @@ type Company struct { HsAnalyticsLatestSourceData1 *HsStr `json:"hs_analytics_latest_source_data_1,omitempty"` HsAnalyticsLatestSourceData2 *HsStr `json:"hs_analytics_latest_source_data_2,omitempty"` HsAnalyticsLatestSourceTimestamp *HsTime `json:"hs_analytics_latest_source_timestamp,omitempty"` - HsAnalyticsNumPageViews *HsInt `json:"hs_analytics_num_page_views,omitempty"` - HsAnalyticsNumVisits *HsInt `json:"hs_analytics_num_visits,omitempty"` + HsAnalyticsNumPageViews *HsStr `json:"hs_analytics_num_page_views,omitempty"` + HsAnalyticsNumVisits *HsStr `json:"hs_analytics_num_visits,omitempty"` HsAnalyticsSource *HsStr `json:"hs_analytics_source,omitempty"` HsAnalyticsSourceData1 *HsStr `json:"hs_analytics_source_data_1,omitempty"` HsAnalyticsSourceData2 *HsStr `json:"hs_analytics_source_data_2,omitempty"` @@ -53,12 +53,12 @@ type Company struct { HsLastmodifieddate *HsTime `json:"hs_lastmodifieddate,omitempty"` HsLeadStatus *HsStr `json:"hs_lead_status,omitempty"` HsMergedObjectIds *HsStr `json:"hs_merged_object_ids,omitempty"` - HsNumBlockers *HsInt `json:"hs_num_blockers,omitempty"` + HsNumBlockers *HsStr `json:"hs_num_blockers,omitempty"` HsNumChildCompanies *HsInt `json:"hs_num_child_companies,omitempty"` HsNumContactsWithBuyingRoles *HsInt `json:"hs_num_contacts_with_buying_roles,omitempty"` HsNumDecisionMakers *HsInt `json:"hs_num_decision_makers,omitempty"` HsNumOpenDeals *HsInt `json:"hs_num_open_deals,omitempty"` - HsObjectId *HsInt `json:"hs_object_id,omitempty"` + HsObjectId *HsStr `json:"hs_object_id,omitempty"` HsParentCompanyId *HsInt `json:"hs_parent_company_id,omitempty"` HsTotalDealValue *HsInt `json:"hs_total_deal_value,omitempty"` HubspotOwnerAssigneddate *HsTime `json:"hubspot_owner_assigneddate,omitempty"` @@ -81,7 +81,7 @@ type Company struct { Phone *HsStr `json:"phone,omitempty"` RecentConversionDate *HsTime `json:"recent_conversion_date,omitempty"` RecentConversionEventName *HsStr `json:"recent_conversion_event_name,omitempty"` - RecentDealAmount *HsInt `json:"recent_deal_amount,omitempty"` + RecentDealAmount *HsStr `json:"recent_deal_amount,omitempty"` RecentDealCloseDate *HsTime `json:"recent_deal_close_date,omitempty"` State *HsStr `json:"state,omitempty"` Timezone *HsStr `json:"timezone,omitempty"` diff --git a/company_test.go b/company_test.go index 0ee2eb2..2f26de2 100644 --- a/company_test.go +++ b/company_test.go @@ -304,6 +304,93 @@ func TestCompanyServiceOp_Get(t *testing.T) { } } +func TestCompanyServiceOp_GetAssociations(t *testing.T) { + type fields struct { + companyPath string + client *hubspot.Client + } + type args struct { + companyID string + toObjectType hubspot.ObjectType + result *hubspot.AssociationsResponse + } + tests := []struct { + name string + fields fields + args args + want *hubspot.AssociationsResponse + wantErr error + }{ + { + name: "Successfully get associations", + fields: fields{ + companyPath: hubspot.ExportCompanyBasePath, + client: hubspot.NewMockClient(&hubspot.MockConfig{ + Status: http.StatusOK, + Header: http.Header{}, + Body: []byte(`{"results":[{"id":"company002","type":"parent_to_child_company"},{"id":"company003","type":"parent_to_child_company"}]}`), + }), + }, + args: args{ + companyID: "company001", + toObjectType: "company", + result: &hubspot.AssociationsResponse{}, + }, + want: &hubspot.AssociationsResponse{ + Results: []hubspot.AssociationResult{ + { + ID: "company002", + Type: string(hubspot.AssociationTypeParentToChildCompany), + }, + { + ID: "company003", + Type: string(hubspot.AssociationTypeParentToChildCompany), + }, + }, + }, + wantErr: nil, + }, + { + name: "Received invalid request", + fields: fields{ + companyPath: hubspot.ExportCompanyBasePath, + client: hubspot.NewMockClient(&hubspot.MockConfig{ + Status: http.StatusBadRequest, + Header: http.Header{}, + Body: []byte(`{"message": "Invalid input (details will vary based on the error)","correlationId": "aeb5f871-7f07-4993-9211-075dc63e7cbf","category": "VALIDATION_ERROR","links": {"knowledge-base": "https://www.hubspot.com/products/service/knowledge-base"}}`), + }), + }, + args: args{ + companyID: "company001", + toObjectType: "company", + result: nil, + }, + want: nil, + wantErr: &hubspot.APIError{ + HTTPStatusCode: http.StatusBadRequest, + Message: "Invalid input (details will vary based on the error)", + CorrelationID: "aeb5f871-7f07-4993-9211-075dc63e7cbf", + Category: "VALIDATION_ERROR", + Links: hubspot.ErrLinks{ + KnowledgeBase: "https://www.hubspot.com/products/service/knowledge-base", + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.fields.client.CRM.Company.GetAssociations(tt.args.companyID, tt.args.toObjectType) + if !reflect.DeepEqual(tt.wantErr, err) { + t.Errorf("Get() error mismatch: want %s got %s", tt.wantErr, err) + return + } + if diff := cmp.Diff(tt.want, got, cmpTimeOption); diff != "" { + t.Errorf("Get() response mismatch (-want +got):%s", diff) + } + }) + } +} + func TestCompanyServiceOp_Delete(t *testing.T) { type fields struct { companyPath string