From 32633a475256a7fbb2c76a0d5edb46a0a0528cd7 Mon Sep 17 00:00:00 2001 From: Johannes Degn Date: Mon, 18 Dec 2023 11:10:37 +0100 Subject: [PATCH] Add ability to list company associations --- association.go | 11 +++++ company.go | 9 ++++ company_test.go | 109 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) diff --git a/association.go b/association.go index ad85b5c..a0508c8 100644 --- a/association.go +++ b/association.go @@ -70,3 +70,14 @@ type AssociationResult struct { ID string `json:"id"` Type string `json:"type"` } + +type AssocationType struct { + Category string `json:"category"` + TypeID int `json:"typeId"` + Label string `json:"label"` +} + +// 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..8f7f235 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, toObjectType string, result *AssociationsResponse) (*AssociationsResponse, error) Create(company interface{}) (*ResponseResource, error) Update(companyID string, company interface{}) (*ResponseResource, error) Delete(companyID string) error @@ -37,6 +38,14 @@ func (s *CompanyServiceOp) Get(companyID string, company interface{}, option *Re return resource, nil } +// GetAssociations gets company associations +func (s *CompanyServiceOp) GetAssociations(companyID, toObjectType string, result *AssociationsResponse) (*AssociationsResponse, error) { + if err := s.client.Get(s.companyPath+"/"+companyID+"/associations/"+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_test.go b/company_test.go index 0ee2eb2..da01a7f 100644 --- a/company_test.go +++ b/company_test.go @@ -304,6 +304,115 @@ 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 string + 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":[{"toObjectId":"company002","associationTypes":[{"category":"HUBSPOT_DEFINED","typeId":13,"label":"Child Company"},{"category":"HUBSPOT_DEFINED","typeId":450,"label":null}]},{"toObjectId":"company003","associationTypes":[{"category":"HUBSPOT_DEFINED","typeId":13,"label":"Child Company"},{"category":"HUBSPOT_DEFINED","typeId":450,"label":null}]}]}`), + }), + }, + args: args{ + companyID: "company001", + toObjectType: "company", + result: &hubspot.AssociationsResponse{}, + }, + want: &hubspot.AssociationsResponse{ + Results: []hubspot.AssociationResult{ + { + ID: "company002", + Type: []hubspot.AssocationType{ + { + Category: "HUBSPOT_DEFINED", + TypeID: 13, + Label: "Child Company", + }, + { + Category: "HUBSPOT_DEFINED", + TypeID: 450, + Label: "", + }, + }, + }, + { + ID: "company003", + Type: []hubspot.AssocationType{ + { + Category: "HUBSPOT_DEFINED", + TypeID: 13, + Label: "Child Company", + }, + { + Category: "HUBSPOT_DEFINED", + TypeID: 450, + Label: "", + }, + }, + }, + }, + }, + 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, tt.args.result) + 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