forked from genert/pipedrive-api
/
filters.go
198 lines (158 loc) · 5.19 KB
/
filters.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
package pipedrive
import (
"context"
"fmt"
"net/http"
)
// FiltersService handles activities related
// methods of the Pipedrive API.
//
// Pipedrive API dcos: https://developers.pipedrive.com/docs/api/v1/#!/Filters
type FiltersService service
// Filter represents a Pipedrive filter.
type Filter struct {
ID int `json:"id"`
Name string `json:"name"`
ActiveFlag bool `json:"active_flag"`
Type string `json:"type"`
TemporaryFlag interface{} `json:"temporary_flag"`
UserID int `json:"user_id"`
AddTime string `json:"add_time"`
UpdateTime string `json:"update_time"`
VisibleTo string `json:"visible_to"`
CustomViewID int `json:"custom_view_id"`
}
func (f Filter) String() string {
return Stringify(f)
}
// FilterConditions represents filter conditions.
type FilterConditions struct {
Glue string `json:"glue"`
Conditions []struct {
Glue string `json:"glue"`
Conditions []struct {
Object string `json:"object"`
FieldID string `json:"field_id"`
Operator string `json:"operator"`
Value string `json:"value"`
ExtraValue interface{} `json:"extra_value"`
} `json:"conditions"`
} `json:"conditions"`
}
// FilterResponse represents single filter response.
type FilterResponse struct {
Success bool `json:"success"`
Data struct {
Filter
Conditions FilterConditions `json:"conditions"`
} `json:"data"`
}
// FiltersResponse represents multiple filters response.
type FiltersResponse struct {
Success bool `json:"success"`
Data []Filter `json:"data"`
AdditionalData AdditionalData `json:"additional_data,omitempty"`
}
// FiltersListOptions specifices the optional parameters to the
// FiltersService.List method.
type FiltersListOptions struct {
Type string `url:"type,omitempty"`
}
// List filters.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Filters/get_filters
func (s *FiltersService) List(ctx context.Context, opt *FiltersListOptions) (*FiltersResponse, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "/filters", opt, nil)
if err != nil {
return nil, nil, err
}
var record *FiltersResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// GetByID returns specific filter.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Filters/get_filters_id
func (s *FiltersService) GetByID(ctx context.Context, id int) (*FilterResponse, *Response, error) {
uri := fmt.Sprintf("/filters/%v", id)
req, err := s.client.NewRequest(http.MethodGet, uri, nil, nil)
if err != nil {
return nil, nil, err
}
var record *FilterResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// FilterCreateOptions specifices the optional parameters to the
// FiltersService.Create method.
type FilterCreateOptions struct {
Name string `url:"name,omitempty"`
Conditions string `url:"conditions,omitempty"`
Type string `url:"type,omitempty"`
}
// Create a filter.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Filters/post_filters
func (s *FiltersService) Create(ctx context.Context, opt *FilterCreateOptions) (*FilterResponse, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "/filters", nil, opt)
if err != nil {
return nil, nil, err
}
var record *FilterResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// FilterUpdateOptions specifices the optional parameters to the
// FiltersService.Update method.
type FilterUpdateOptions struct {
Name string `url:"name,omitempty"`
Conditions string `url:"conditions,omitempty"`
}
// Update a specific filter.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Filters/put_filters_id
func (s *FiltersService) Update(ctx context.Context, id int, opt *FilterUpdateOptions) (*FilterResponse, *Response, error) {
uri := fmt.Sprintf("/filters/%v", id)
req, err := s.client.NewRequest(http.MethodPut, uri, nil, opt)
if err != nil {
return nil, nil, err
}
var record *FilterResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// DeleteMultiple deletes multiple filters in bulk.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Filters/delete_filters
func (s *FiltersService) DeleteMultiple(ctx context.Context, ids []int) (*Response, error) {
req, err := s.client.NewRequest(http.MethodDelete, "/filter", &DeleteMultipleOptions{
Ids: arrayToString(ids, ","),
}, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Delete a filter.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Filters/delete_filters_id
func (s *FiltersService) Delete(ctx context.Context, id int) (*Response, error) {
uri := fmt.Sprintf("/filters/%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)
}