Skip to content

Commit

Permalink
Merge pull request #23 from joidegn/main
Browse files Browse the repository at this point in the history
Add support for companies CRM API calls
  • Loading branch information
kk-no authored Oct 31, 2023
2 parents 234ec35 + 9f98925 commit 4f5adb2
Show file tree
Hide file tree
Showing 5 changed files with 497 additions and 0 deletions.
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

0 comments on commit 4f5adb2

Please sign in to comment.