forked from bkmz/amocrm_v4
/
contact.go
203 lines (167 loc) · 4.65 KB
/
contact.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
package amocrm_v4
import (
"fmt"
"net/http"
)
type Ct struct{}
type contactNote note
type ContactWithType string
const (
ContactWithLeads ContactWithType = "leads"
ContactWithCustomers ContactWithType = "customers"
ContactWithCatalogElements ContactWithType = "catalog_elements"
)
type GetContactsQueryParams struct {
With []ContactWithType `url:"with,omitempty"`
Limit int `url:"limit,omitempty"`
Page int `url:"page,omitempty"`
Query interface{} `url:"query,omitempty"`
Filter interface{} `url:"filter,omitempty"`
Order interface{} `url:"order,omitempty"`
}
type contact struct {
Id int `json:"id"`
Name string `json:"name"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
ResponsibleUserId int `json:"responsible_user_id"`
GroupId int `json:"group_id"`
CreatedBy int `json:"created_by"`
UpdatedBy int `json:"updated_by"`
CreatedAt int `json:"created_at"`
UpdatedAt int `json:"updated_at"`
ClosestTaskAt interface{} `json:"closest_task_at"`
IsDeleted bool `json:"is_deleted,omitempty"`
IsUnsorted bool `json:"is_unsorted,omitempty"`
CustomFieldsValues []CustomField `json:"custom_fields_values"`
AccountId int `json:"account_id"`
Links links `json:"_links"`
Embedded struct {
Customers []interface{} `json:"customers"`
Leads []*lead `json:"leads"`
CatalogElements []interface{} `json:"catalog_elements"`
Tags []Tag `json:"tags"`
Companies []interface{} `json:"companies"`
} `json:"_embedded"`
}
type allContacts struct {
Page int `json:"_page"`
Links links `json:"_links"`
Embedded struct {
Contacts []*contact `json:"contacts"`
} `json:"_embedded"`
}
type allContactNotes struct {
Page int `json:"_page"`
Links links `json:"_links"`
Embedded struct {
Notes []*contactNote `json:"notes"`
} `json:"_embedded"`
}
// New Method creates empty struct
func (c Ct) New() *contact {
return &contact{}
}
func (ct *contact) NewTask() *task {
return &task{
EntityType: TaskForContact,
EntityId: ct.Id,
}
}
func (ct *contact) NewNote() *note {
return ¬e{
EntityId: ct.Id,
EntityType: NoteEntityTypeContact,
}
}
func (c Ct) All() ([]*contact, error) {
contacts, err := c.multiplyRequest(&GetContactsQueryParams{
Limit: 250,
})
if err != nil {
return nil, err
}
return contacts, nil
}
func (c Ct) Query(params *GetContactsQueryParams) ([]*contact, error) {
if params.Limit == 0 {
params.Limit = 250
}
contacts, err := c.multiplyRequest(params)
if err != nil {
return nil, err
}
return contacts, nil
}
func (c Ct) ByID(id int, with []ContactWithType) (*contact, error) {
var ct *contact
opts := GetContactsQueryParams{
With: with,
}
err := httpRequest(requestOpts{
Method: http.MethodGet,
Path: fmt.Sprintf("/api/v4/contacts/%d", id),
URLParameters: &opts,
Ret: &ct,
})
if err != nil {
return nil, err
}
return ct, nil
}
func (ct *contact) Notes(params *GetNotesQueryParams) ([]*contactNote, error) {
notes, err := ct.noteMultiplyRequest(params)
if err != nil {
return nil, err
}
return notes, nil
}
func (ct *contact) noteMultiplyRequest(opts *GetNotesQueryParams) ([]*contactNote, error) {
var notes []*contactNote
if opts.Limit == 0 {
opts.Limit = 250
}
for {
var tmpNotes allContactNotes
path := fmt.Sprintf("/api/v4/contacts/%d/notes", ct.Id)
err := httpRequest(requestOpts{
Method: http.MethodGet,
Path: path,
URLParameters: &opts,
Ret: &tmpNotes,
})
if err != nil {
return nil, fmt.Errorf("ошибка обработки запроса %s: %s", path, err)
}
notes = append(notes, tmpNotes.Embedded.Notes...)
if len(tmpNotes.Links.Next.Href) > 0 {
opts.Page = opts.Page + 1
} else {
break
}
}
return notes, nil
}
func (c Ct) multiplyRequest(opts *GetContactsQueryParams) ([]*contact, error) {
var contacts []*contact
path := "/api/v4/contacts"
for {
var tmpContacts allContacts
err := httpRequest(requestOpts{
Method: http.MethodGet,
Path: path,
URLParameters: &opts,
Ret: &tmpContacts,
})
if err != nil {
return nil, err
}
contacts = append(contacts, tmpContacts.Embedded.Contacts...)
if len(tmpContacts.Links.Next.Href) > 0 {
opts.Page = tmpContacts.Page + 1
} else {
break
}
}
return contacts, nil
}