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 ability to list company associations #27

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions association.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"`
}
10 changes: 10 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 string, toObjectType ObjectType) (*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,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.
Expand Down
10 changes: 5 additions & 5 deletions company_model.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I could unmarshal to HsInt using #29, but is this necessary?

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

87 changes: 87 additions & 0 deletions company_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading