forked from genert/pipedrive-api
/
organizations.go
254 lines (212 loc) · 8.88 KB
/
organizations.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
package pipedrive
import (
"context"
"fmt"
"net/http"
)
// OrganizationsService handles organization related
// methods of the Pipedrive API.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Organizations
type OrganizationsService service
// Organization represents a Pipedrive organization.
type Organization struct {
ID int `json:"id"`
CompanyID int `json:"company_id"`
OwnerID struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
HasPic bool `json:"has_pic"`
PicHash string `json:"pic_hash"`
ActiveFlag bool `json:"active_flag"`
Value int `json:"value"`
} `json:"owner_id"`
Name string `json:"name"`
OpenDealsCount int `json:"open_deals_count"`
RelatedOpenDealsCount int `json:"related_open_deals_count"`
ClosedDealsCount int `json:"closed_deals_count"`
RelatedClosedDealsCount int `json:"related_closed_deals_count"`
EmailMessagesCount int `json:"email_messages_count"`
PeopleCount int `json:"people_count"`
ActivitiesCount int `json:"activities_count"`
DoneActivitiesCount int `json:"done_activities_count"`
UndoneActivitiesCount int `json:"undone_activities_count"`
ReferenceActivitiesCount int `json:"reference_activities_count"`
FilesCount int `json:"files_count"`
NotesCount int `json:"notes_count"`
FollowersCount int `json:"followers_count"`
WonDealsCount int `json:"won_deals_count"`
RelatedWonDealsCount int `json:"related_won_deals_count"`
LostDealsCount int `json:"lost_deals_count"`
RelatedLostDealsCount int `json:"related_lost_deals_count"`
ActiveFlag bool `json:"active_flag"`
CategoryID interface{} `json:"category_id"`
PictureID interface{} `json:"picture_id"`
CountryCode interface{} `json:"country_code"`
FirstChar string `json:"first_char"`
UpdateTime string `json:"update_time"`
AddTime string `json:"add_time"`
VisibleTo string `json:"visible_to"`
NextActivityDate string `json:"next_activity_date"`
NextActivityTime interface{} `json:"next_activity_time"`
NextActivityID int `json:"next_activity_id"`
LastActivityID int `json:"last_activity_id"`
LastActivityDate string `json:"last_activity_date"`
TimelineLastActivityTime interface{} `json:"timeline_last_activity_time"`
TimelineLastActivityTimeByOwner interface{} `json:"timeline_last_activity_time_by_owner"`
Address interface{} `json:"address"`
AddressSubpremise interface{} `json:"address_subpremise"`
AddressStreetNumber interface{} `json:"address_street_number"`
AddressRoute interface{} `json:"address_route"`
AddressSublocality interface{} `json:"address_sublocality"`
AddressLocality interface{} `json:"address_locality"`
AddressAdminAreaLevel1 interface{} `json:"address_admin_area_level_1"`
AddressAdminAreaLevel2 interface{} `json:"address_admin_area_level_2"`
AddressCountry interface{} `json:"address_country"`
AddressPostalCode interface{} `json:"address_postal_code"`
AddressFormattedAddress interface{} `json:"address_formatted_address"`
OwnerName string `json:"owner_name"`
CcEmail string `json:"cc_email"`
}
func (o Organization) String() string {
return Stringify(o)
}
// OrganizationsResponse represents multiple organizations response.
type OrganizationsResponse struct {
Success bool `json:"success"`
Data []Organization `json:"data"`
AdditionalData AdditionalData `json:"additional_data,omitempty"`
}
// OrganizationResponse represents single organization response.
type OrganizationResponse struct {
Success bool `json:"success"`
Data Organization `json:"data"`
AdditionalData AdditionalData `json:"additional_data,omitempty"`
}
// OrganizationFindOptions specifices the optional parameters to the
// OrganizationsService.Create method.
type OrganizationFindOptions struct {
Term string `url:"term"`
}
// OrganizationCreateOptions specifices the optional parameters to the
// OrganizationsService.Create method.
type OrganizationCreateOptions struct {
Name string `json:"name"`
OwnerID uint `json:"owner_id"`
VisibleTo VisibleTo `json:"visible_to"`
AddTime Timestamp `json:"add_time"`
}
// Find all organizations.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Organizations/get_organizations_find
func (s *OrganizationsService) Find(ctx context.Context, opt *OrganizationFindOptions) (*OrganizationsResponse, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "/organizations/find", struct {
Term string `url:"term"`
}{
opt.Term,
}, nil)
if err != nil {
return nil, nil, err
}
var record *OrganizationsResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// List all organizations.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Organizations/get_organizations
func (s *OrganizationsService) List(ctx context.Context) (*OrganizationsResponse, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "/organizations", nil, nil)
if err != nil {
return nil, nil, err
}
var record *OrganizationsResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// Create a new organization.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Organizations/post_organizations
func (s *OrganizationsService) Create(ctx context.Context, opt *OrganizationCreateOptions) (*OrganizationResponse, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "/organizations", nil, struct {
Name string `json:"name"`
OwnerID uint `json:"owner_id"`
VisibleTo VisibleTo `json:"visible_to"`
AddTime string `json:"add_time"`
}{
opt.Name,
opt.OwnerID,
opt.VisibleTo,
opt.AddTime.FormatFull(),
})
if err != nil {
return nil, nil, err
}
var record *OrganizationResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// Merge organizations.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Persons/put_persons_id_merge
func (s *OrganizationsService) Merge(ctx context.Context, id int, mergeWithID int) (*OrganizationResponse, *Response, error) {
uri := fmt.Sprintf("/organizations/%v/merge", id)
req, err := s.client.NewRequest(http.MethodPut, uri, nil, struct {
MergeWithID int `url:"merge_with_id"`
}{
mergeWithID,
})
if err != nil {
return nil, nil, err
}
var record *OrganizationResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// DeleteFollower deletes a follower from an organization.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Organizations/delete_organizations_id_followers_follower_id
func (s *OrganizationsService) DeleteFollower(ctx context.Context, id int, followerID int) (*Response, error) {
uri := fmt.Sprintf("/organizations/%v/followers/%v", id, followerID)
req, err := s.client.NewRequest(http.MethodDelete, uri, nil, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Delete marks an organization as deleted.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Organizations/delete_organizations_id
func (s *OrganizationsService) Delete(ctx context.Context, id int) (*Response, error) {
uri := fmt.Sprintf("/organizations/%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)
}
// DeleteMultiple deletes multiple organizations in bulk.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Organizations/delete_organizations
func (s *OrganizationsService) DeleteMultiple(ctx context.Context, ids []int) (*Response, error) {
req, err := s.client.NewRequest(http.MethodDelete, "/organizations", &DeleteMultipleOptions{
Ids: arrayToString(ids, ","),
}, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}