-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathdeal.go
183 lines (166 loc) · 6.65 KB
/
deal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package hubspot
const (
dealBasePath = "deals"
)
// DealService is an interface of deal endpoints of the HubSpot API.
// HubSpot deal can be used to manage transactions.
// It can also be associated with other CRM objects such as contact and company.
// Reference: https://developers.hubspot.com/docs/api/crm/deals
type DealService interface {
Get(dealID string, deal interface{}, option *RequestQueryOption) (*ResponseResource, error)
Create(deal interface{}) (*ResponseResource, error)
Update(dealID string, deal interface{}) (*ResponseResource, error)
AssociateAnotherObj(dealID string, conf *AssociationConfig) (*ResponseResource, error)
}
// DealServiceOp handles communication with the product related methods of the HubSpot API.
type DealServiceOp struct {
dealPath string
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.
type Deal struct {
Amount *HsStr `json:"amount,omitempty"`
AmountInCompanyCurrency *HsStr `json:"amount_in_home_currency,omitempty"`
AnnualContractValue *HsStr `json:"hs_acv,omitempty"`
AnnualRecurringRevenue *HsStr `json:"hs_arr,omitempty"`
ClosedLostReason *HsStr `json:"closed_lost_reason,omitempty"`
ClosedWonReason *HsStr `json:"closed_won_reason,omitempty"`
DealDescription *HsStr `json:"description,omitempty"`
DealName *HsStr `json:"dealname,omitempty"`
DealOwnerID *HsStr `json:"hubspot_owner_id,omitempty"`
DealStage *HsStr `json:"dealstage,omitempty"`
DealType *HsStr `json:"dealtype,omitempty"`
ForecastAmount *HsStr `json:"hs_forecast_amount,omitempty"`
ForecastCategory *HsStr `json:"hs_forecast_category,omitempty"`
ForecastProbability *HsStr `json:"hs_forecast_probability,omitempty"`
MonthlyRecurringRevenue *HsStr `json:"hs_mrr,omitempty"`
NextStep *HsStr `json:"hs_next_step,omitempty"`
NumberOfContacts *HsStr `json:"num_associated_contacts,omitempty"`
NumberOfSalesActivities *HsStr `json:"num_notes,omitempty"`
NumberOfTimesContacted *HsStr `json:"num_contacted_notes,omitempty"`
ObjectID *HsStr `json:"hs_object_id,omitempty"`
PipeLine *HsStr `json:"pipeline,omitempty"`
TeamID *HsStr `json:"hubspot_team_id,omitempty"`
TotalContractValue *HsStr `json:"hs_tcv,omitempty"`
CreateDate *HsTime `json:"createdate,omitempty"`
CloseDate *HsTime `json:"closedate,omitempty"`
LastActivityDate *HsTime `json:"notes_last_updated,omitempty"`
LastContacted *HsTime `json:"notes_last_contacted,omitempty"`
LastModifiedDate *HsTime `json:"hs_lastmodifieddate,omitempty"`
NextActivityDate *HsTime `json:"notes_next_activity_date,omitempty"`
OwnerAssignedDate *HsTime `json:"hubspot_owner_assigneddate,omitempty"`
}
var defaultDealFields = []string{
"amount",
"amount_in_home_currency",
"hs_acv",
"hs_arr",
"closed_lost_reason",
"closed_won_reason",
"description",
"dealname",
"hubspot_owner_id",
"dealstage",
"dealtype",
"hs_forecast_amount",
"hs_forecast_category",
"hs_forecast_probability",
"hs_mrr",
"hs_next_step",
"num_associated_contacts",
"num_notes",
"num_contacted_notes",
"hs_object_id",
"hubspot_owner_assigneddate",
"pipeline",
"hubspot_team_id",
"hs_tcv",
"createdate",
"closedate",
"notes_last_updated",
"notes_last_contacted",
"hs_lastmodifieddate",
"notes_next_activity_date",
}
// Get gets a deal.
// 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 *DealServiceOp) Get(dealID string, deal interface{}, option *RequestQueryOption) (*ResponseResource, error) {
resource := &ResponseResource{Properties: deal}
if err := s.client.Get(s.dealPath+"/"+dealID, resource, option.setupProperties(defaultDealFields)); err != nil {
return nil, err
}
return resource, nil
}
// Create creates a new deal.
// In order to bind the created content, a structure must be specified as an argument.
// When using custom fields, please embed hubspot.Deal in your own structure.
func (s *DealServiceOp) Create(deal interface{}) (*ResponseResource, error) {
req := &RequestPayload{Properties: deal}
resource := &ResponseResource{Properties: deal}
if err := s.client.Post(s.dealPath, req, resource); err != nil {
return nil, err
}
return resource, nil
}
// Update updates a deal.
// In order to bind the updated content, a structure must be specified as an argument.
// When using custom fields, please embed hubspot.Deal in your own structure.
func (s *DealServiceOp) Update(dealID string, deal interface{}) (*ResponseResource, error) {
req := &RequestPayload{Properties: deal}
resource := &ResponseResource{Properties: deal}
if err := s.client.Patch(s.dealPath+"/"+dealID, req, resource); err != nil {
return nil, err
}
return resource, nil
}
// AssociateAnotherObj associates Deal with another HubSpot objects.
// If you want to associate a custom object, please use a defined value in HubSpot.
func (s *DealServiceOp) AssociateAnotherObj(dealID string, conf *AssociationConfig) (*ResponseResource, error) {
resource := &ResponseResource{Properties: &Deal{}}
if err := s.client.Put(s.dealPath+"/"+dealID+"/"+conf.makeAssociationPath(), nil, resource); err != nil {
return nil, err
}
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
}