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

Search for contacts and deals #37

Open
wants to merge 2 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
48 changes: 48 additions & 0 deletions contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ContactService interface {
Update(contactID string, contact interface{}) (*ResponseResource, error)
Delete(contactID string) error
AssociateAnotherObj(contactID string, conf *AssociationConfig) (*ResponseResource, error)
SearchByEmail(email string) (*ContactSearchResponse, error)
}

// ContactServiceOp handles communication with the product related methods of the HubSpot API.
Expand All @@ -24,6 +25,29 @@ type ContactServiceOp struct {

var _ ContactService = (*ContactServiceOp)(nil)

// ContactSearchRequest represents the request body for searching contacts.
type ContactSearchRequest struct {
FilterGroups []FilterGroup `json:"filterGroups"`
}

// FilterGroup represents a group of filters.
type FilterGroup struct {
Filters []Filter `json:"filters"`
}

// Filter represents a single filter.
type Filter struct {
PropertyName string `json:"propertyName"`
Operator string `json:"operator"`
Values []string `json:"values,omitempty"`
Value string `json:"value,omitempty"`
}

// ContactSearchResponse represents the response from searching contacts.
type ContactSearchResponse struct {
Results []Contact `json:"results"`
}

type Contact struct {
Address *HsStr `json:"address,omitempty"`
AnnualRevenue *HsStr `json:"annualrevenue,omitempty"`
Expand Down Expand Up @@ -367,3 +391,27 @@ func (s *ContactServiceOp) AssociateAnotherObj(contactID string, conf *Associati
}
return resource, nil
}

// SearchByEmail searches for a contact by email.
func (s *ContactServiceOp) SearchByEmail(email string) (*ContactSearchResponse, error) {
req := &ContactSearchRequest{
FilterGroups: []FilterGroup{
{
Filters: []Filter{
{
PropertyName: "email",
Operator: "EQ",
Value: email,
},
},
},
},
}

resource := &ContactSearchResponse{}
if err := s.client.Post(s.contactPath+"/search", req, resource); err != nil {
return nil, err
}

return resource, nil
}
44 changes: 44 additions & 0 deletions deal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ type DealServiceOp struct {
client *Client
}

// DealSearchRequest represents the request body for searching deals.
type DealSearchRequest struct {
FilterGroups []FilterGroup `json:"filterGroups,omitempty"`
}

// DealSearchResponse represents the structure of the response from the deal search endpoint.
type DealSearchResponse struct {
Total int `json:"total,omitempty"`
Results []DealResponse `json:"results,omitempty"`
}

type DealResponse struct {
ID string `json:"id,omitempty"`
Properties Deal `json:"properties,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
UpdatedAt string `json:"updatedAt,omitempty"`
Archived bool `json:"archived,omitempty"`
}

var _ DealService = (*DealServiceOp)(nil)

// Deal represents a HubSpot deal.
Expand Down Expand Up @@ -137,3 +156,28 @@ func (s *DealServiceOp) AssociateAnotherObj(dealID string, conf *AssociationConf
}
return resource, nil
}

// SearchDeals searches for deals by deal name.
func (s *DealServiceOp) SearchDeals(dealName string) (*DealSearchResponse, error) {
resource := &DealSearchResponse{Total: 0, Results: []DealResponse{}}
req := &DealSearchRequest{
FilterGroups: []FilterGroup{
{
Filters: []Filter{
{
PropertyName: "dealname",
Operator: "EQ",
Value: dealName,
},
},
},
},
}

// Send the POST request to HubSpot API
if err := s.client.Post(dealBasePath+"/search", req, resource); err != nil {
return nil, err
}

return resource, nil
}