forked from genert/pipedrive-api
/
pipelines.go
311 lines (256 loc) · 8.62 KB
/
pipelines.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package pipedrive
import (
"context"
"fmt"
"net/http"
)
// PipelinesService handles pipelines related
// methods of the Pipedrive API.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Pipelines
type PipelinesService service
// Pipeline represents a Pipedrive pipeline.
type Pipeline struct {
ID int `json:"id"`
Name string `json:"name"`
URLTitle string `json:"url_title"`
OrderNr int `json:"order_nr"`
Active bool `json:"active"`
DealProbability bool `json:"deal_probability"`
AddTime string `json:"add_time"`
UpdateTime string `json:"update_time"`
Selected bool `json:"selected"`
}
func (p Pipeline) String() string {
return Stringify(p)
}
// PipelinesResponse represents multiple pipelines response.
type PipelinesResponse struct {
Success bool `json:"success"`
Data []Pipeline `json:"data"`
AdditionalData AdditionalData `json:"additional_data"`
}
// PipelineResponse represents single pipeline response.
type PipelineResponse struct {
Success bool `json:"success"`
Data Pipeline `json:"data"`
AdditionalData AdditionalData `json:"additional_data"`
}
// PipelineDealsConversionRateResponse represents conversion response.
type PipelineDealsConversionRateResponse struct {
Success bool `json:"success"`
Data struct {
StageConversions []struct {
FromStageID int `json:"from_stage_id"`
ToStageID int `json:"to_stage_id"`
ConversionRate float64 `json:"conversion_rate"`
} `json:"stage_conversions"`
WonConversion float64 `json:"won_conversion"`
LostConversion float64 `json:"lost_conversion"`
} `json:"data"`
}
// PipelineDealsMovementResponse represents movement response.
type PipelineDealsMovementResponse struct {
Success bool `json:"success"`
Data struct {
MovementsBetweenStages struct {
Count int `json:"count"`
} `json:"movements_between_stages"`
NewDeals struct {
Count int `json:"count"`
DealIds []int `json:"deal_ids"`
Values struct {
EUR float64 `json:"EUR"`
} `json:"values"`
FormattedValues struct {
EUR string `json:"EUR"`
} `json:"formatted_values"`
} `json:"new_deals"`
DealsLeftOpen struct {
Count int `json:"count"`
DealIds []int `json:"deal_ids"`
Values struct {
EUR float64 `json:"EUR"`
} `json:"values"`
FormattedValues struct {
EUR string `json:"EUR"`
} `json:"formatted_values"`
} `json:"deals_left_open"`
WonDeals struct {
Count int `json:"count"`
DealIds []int `json:"deal_ids"`
Values struct {
EUR int `json:"EUR"`
} `json:"values"`
FormattedValues struct {
EUR string `json:"EUR"`
} `json:"formatted_values"`
} `json:"won_deals"`
LostDeals struct {
Count int `json:"count"`
DealIds []int `json:"deal_ids"`
Values struct {
EUR int `json:"EUR"`
} `json:"values"`
FormattedValues struct {
EUR string `json:"EUR"`
} `json:"formatted_values"`
} `json:"lost_deals"`
AverageAgeInDays struct {
AcrossAllStages float64 `json:"across_all_stages"`
ByStages []struct {
StageID int `json:"stage_id"`
Value int `json:"value"`
} `json:"by_stages"`
} `json:"average_age_in_days"`
} `json:"data"`
}
// List returns data about all pipelines.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Pipelines/get_pipelines
func (s *PipelinesService) List(ctx context.Context) (*PipelinesResponse, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "/pipelines", nil, nil)
if err != nil {
return nil, nil, err
}
var record *PipelinesResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// GetByID returns data about a specific pipeline.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Pipelines/get_pipelines_id
func (s *PipelinesService) GetByID(ctx context.Context, id int) (*PipelineResponse, *Response, error) {
uri := fmt.Sprintf("/pipelines/%v", id)
req, err := s.client.NewRequest(http.MethodGet, uri, nil, nil)
if err != nil {
return nil, nil, err
}
var record *PipelineResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// GetDeals returns deal in a specific pipeline.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Pipelines/get_pipelines_id_deals
func (s *PipelinesService) GetDeals(ctx context.Context, id int) (*PipelinesResponse, *Response, error) {
uri := fmt.Sprintf("/pipelines/%v/deals", id)
req, err := s.client.NewRequest(http.MethodGet, uri, nil, nil)
if err != nil {
return nil, nil, err
}
var record *PipelinesResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// GetDealsConversionRate returns deals conversion rate in a specific pipeline.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Pipelines/get_pipelines_id_conversion_statistics
func (s *PipelinesService) GetDealsConversionRate(ctx context.Context, id int, startDate Timestamp, endDate Timestamp) (*PipelineDealsConversionRateResponse, *Response, error) {
uri := fmt.Sprintf("/pipelines/%v/conversion_statistics", id)
req, err := s.client.NewRequest(http.MethodGet, uri, struct {
StartDate string `url:"start_date"`
EndDate string `url:"end_date"`
}{
startDate.Format(),
endDate.Format(),
}, nil)
if err != nil {
return nil, nil, err
}
var record *PipelineDealsConversionRateResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// GetDealsMovement returns deals movement in a specific pipeline.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Pipelines/get_pipelines_id_movement_statistics
func (s *PipelinesService) GetDealsMovement(ctx context.Context, id int, startDate Timestamp, endDate Timestamp) (*PipelineDealsMovementResponse, *Response, error) {
uri := fmt.Sprintf("/pipelines/%v/movement_statistics", id)
req, err := s.client.NewRequest(http.MethodGet, uri, struct {
StartDate string `url:"start_date"`
EndDate string `url:"end_date"`
}{
startDate.Format(),
endDate.Format(),
}, nil)
if err != nil {
return nil, nil, err
}
var record *PipelineDealsMovementResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// PipelineCreateOptions specifices the optional parameters to the
// PipelineCreateOptions.Create method.
type PipelineCreateOptions struct {
Name string `url:"name"`
DealProbability DealProbability `url:"deal_probability"`
OrderNr int `url:"order_nr"`
Active ActiveFlag `url:"active"`
}
// Create a new pipeline.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Pipelines/post_pipelines
func (s *PipelinesService) Create(ctx context.Context, opt *PipelineCreateOptions) (*PipelineResponse, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "/pipelines", nil, opt)
if err != nil {
return nil, nil, err
}
var record *PipelineResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// PipelineUpdateOptions specifices the optional parameters to the
// PipelinesService.Update method.
type PipelineUpdateOptions struct {
Name string `url:"name"`
DealProbability DealProbability `url:"deal_probability"`
OrderNr int `url:"order_nr"`
Active ActiveFlag `url:"active"`
}
// Update a specific pipeline.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Pipelines/put_pipelines_id
func (s *PipelinesService) Update(ctx context.Context, id int, opt *PipelineUpdateOptions) (*PipelineResponse, *Response, error) {
uri := fmt.Sprintf("/pipelines/%v", id)
req, err := s.client.NewRequest(http.MethodPost, uri, nil, opt)
if err != nil {
return nil, nil, err
}
var record *PipelineResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// Delete marks a specific pipeline as deleted.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Pipelines/delete_pipelines_id
func (s *PipelinesService) Delete(ctx context.Context, id int) (*Response, error) {
uri := fmt.Sprintf("/pipelines/%v", id)
req, err := s.client.NewRequest(http.MethodDelete, uri, nil, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}