Skip to content

Commit

Permalink
Add ability to list company associations
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Degn committed Dec 18, 2023
1 parent 5bfb417 commit 32633a4
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
11 changes: 11 additions & 0 deletions association.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
9 changes: 9 additions & 0 deletions company.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
109 changes: 109 additions & 0 deletions company_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Check failure on line 343 in company_test.go

View workflow job for this annotation

GitHub Actions / test

cannot use []hubspot.AssocationType{…} (value of type []hubspot.AssocationType) as string value in struct literal (typecheck)
{
Category: "HUBSPOT_DEFINED",
TypeID: 13,
Label: "Child Company",
},
{
Category: "HUBSPOT_DEFINED",
TypeID: 450,
Label: "",
},
},
},
{
ID: "company003",
Type: []hubspot.AssocationType{

Check failure on line 358 in company_test.go

View workflow job for this annotation

GitHub Actions / test

cannot use []hubspot.AssocationType{…} (value of type []hubspot.AssocationType) as string value in struct literal (typecheck)
{
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
Expand Down

0 comments on commit 32633a4

Please sign in to comment.