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 support for companies CRM API calls #23

Merged
merged 3 commits into from
Oct 31, 2023
Merged
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
7 changes: 7 additions & 0 deletions association.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ObjectType string
const (
ObjectTypeContact ObjectType = "contacts"
ObjectTypeDeal ObjectType = "deals"
ObjectTypeCompany ObjectType = "company"
)

// AssociationType is the name of the key used to associate the objects together.
Expand All @@ -38,6 +39,9 @@ const (
AssociationTypeDealToEngagement AssociationType = "deal_to_engagement"
AssociationTypeDealToLineItem AssociationType = "deal_to_line_item"
AssociationTypeDealToTicket AssociationType = "deal_to_ticket"

AssociationTypeCompanyToContact AssociationType = "company_to_contact"
AssociationTypeCompanyToDeal AssociationType = "company_to_deal"
)

type AssociationConfig struct {
Expand All @@ -57,6 +61,9 @@ type Associations struct {
Deals struct {
Results []AssociationResult `json:"results"`
} `json:"deals"`
Companies struct {
Results []AssociationResult `json:"results"`
} `json:"companies"`
}

type AssociationResult struct {
Expand Down
87 changes: 87 additions & 0 deletions company.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package hubspot

const (
companyBasePath = "companies"
)

// CompanyService is an interface of company endpoints of the HubSpot API.
// HubSpot companies store information about organizations.
// It can also be associated with other CRM objects such as deal and contact.
// Reference: https://developers.hubspot.com/docs/api/crm/companies
type CompanyService interface {
Get(companyID string, company interface{}, option *RequestQueryOption) (*ResponseResource, error)
Create(company interface{}) (*ResponseResource, error)
Update(companyID string, company interface{}) (*ResponseResource, error)
Delete(companyID string) error
AssociateAnotherObj(companyID string, conf *AssociationConfig) (*ResponseResource, error)
}

// CompanyServiceOp handles communication with the product related methods of the HubSpot API.
type CompanyServiceOp struct {
companyPath string
client *Client
}

var _ CompanyService = (*CompanyServiceOp)(nil)

type Company struct {
Address *HsStr `json:"address,omitempty"`
// TODO: default properties here
}

var defaultCompanyFields = []string{
"address",
// TODO: default props here
}

// Get gets a Company.
// In order to bind the get content, a structure must be specified as an argument.
// Also, if you want to gets a custom field, you need to specify the field name.
// If you specify a non-existent field, it will be ignored.
// e.g. &hubspot.RequestQueryOption{ Properties: []string{"custom_a", "custom_b"}}
func (s *CompanyServiceOp) Get(companyID string, company interface{}, option *RequestQueryOption) (*ResponseResource, error) {
resource := &ResponseResource{Properties: company}
if err := s.client.Get(s.companyPath+"/"+companyID, resource, option.setupProperties(defaultCompanyFields)); err != nil {
return nil, err
}
return resource, 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.
func (s *CompanyServiceOp) Create(company interface{}) (*ResponseResource, error) {
req := &RequestPayload{Properties: company}
resource := &ResponseResource{Properties: company}
if err := s.client.Post(s.companyPath, req, resource); err != nil {
return nil, err
}
return resource, nil
}

// Update updates a company.
// In order to bind the updated content, a structure must be specified as an argument.
// When using custom fields, please embed hubspot.Company in your own structure.
func (s *CompanyServiceOp) Update(companyID string, company interface{}) (*ResponseResource, error) {
req := &RequestPayload{Properties: company}
resource := &ResponseResource{Properties: company}
if err := s.client.Patch(s.companyPath+"/"+companyID, req, resource); err != nil {
return nil, err
}
return resource, nil
}

// Delete deletes a company.
func (s *CompanyServiceOp) Delete(companyID string) error {
return s.client.Delete(s.companyPath+"/"+companyID, nil)
}

// AssociateAnotherObj associates Company with another HubSpot objects.
// If you want to associate a custom object, please use a defined value in HubSpot.
func (s *CompanyServiceOp) AssociateAnotherObj(companyID string, conf *AssociationConfig) (*ResponseResource, error) {
resource := &ResponseResource{Properties: &Company{}}
if err := s.client.Put(s.companyPath+"/"+companyID+"/"+conf.makeAssociationPath(), nil, resource); err != nil {
return nil, err
}
return resource, nil
}
Loading
Loading