2024-01-15 00:04:00 +03:00
|
|
|
package amo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-04 04:31:33 +03:00
|
|
|
"time"
|
|
|
|
"errors"
|
2024-01-15 00:04:00 +03:00
|
|
|
"vultras.su/core/amo/api"
|
|
|
|
"vultras.su/core/amo/companies"
|
|
|
|
"vultras.su/core/amo/contacts"
|
|
|
|
"vultras.su/core/amo/leads"
|
|
|
|
"vultras.su/core/amo/users"
|
2024-01-19 08:14:23 +03:00
|
|
|
"vultras.su/core/amo/events"
|
2024-01-15 00:04:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type IAmoClient interface {
|
|
|
|
GetUser(userId string) (*users.User, error)
|
|
|
|
GetLead(leadId string, query string) (*leads.Lead, error)
|
|
|
|
UpdateLead(lead *leads.Lead) error
|
|
|
|
GetCompany(companyId string, query string) (*companies.Company, error)
|
|
|
|
UpdateCompany(company *companies.Company) error
|
|
|
|
GetContact(contactId string, query string) (*contacts.Contact, error)
|
|
|
|
UpdateContact(contact *contacts.Contact) error
|
|
|
|
}
|
|
|
|
|
2024-01-15 03:48:01 +03:00
|
|
|
type TokenPair = api.TokenPair
|
|
|
|
type Options = api.ClientOptions
|
|
|
|
|
2024-01-15 00:04:00 +03:00
|
|
|
type Client struct {
|
|
|
|
Api *api.Client
|
|
|
|
}
|
|
|
|
|
2024-01-15 03:48:01 +03:00
|
|
|
type OauthTokenResponse = api.OauthTokenResponse
|
|
|
|
|
2024-01-19 08:14:23 +03:00
|
|
|
func NewAmoClient(secretPath string) (*Client, error) {
|
|
|
|
apiClient, err := api.NewApi(secretPath)
|
2024-01-15 00:04:00 +03:00
|
|
|
if err != nil {
|
2024-01-19 08:14:23 +03:00
|
|
|
return nil, err
|
2024-01-15 00:04:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
Api: apiClient,
|
2024-01-19 08:14:23 +03:00
|
|
|
}, nil
|
2024-01-15 00:04:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) updateEntity(url string, id int, body interface{}) error {
|
|
|
|
err := client.Api.Patch(fmt.Sprintf("%s/%d", url, id), body, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-15 00:20:13 +03:00
|
|
|
func (client *Client) GetUser(userId int) (*users.User, error) {
|
2024-01-15 00:04:00 +03:00
|
|
|
user := new(users.User)
|
2024-01-15 00:20:13 +03:00
|
|
|
err := client.Api.Get(fmt.Sprintf("/api/v4/users/%d", userId), user)
|
2024-01-15 00:04:00 +03:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2024-03-25 13:18:52 +03:00
|
|
|
func (client *Client) GetLead(leadId int, query string) (*leads.Lead, error) {
|
2024-01-15 00:04:00 +03:00
|
|
|
deal := new(leads.Lead)
|
2024-03-25 13:18:52 +03:00
|
|
|
resource := fmt.Sprintf("/api/v4/leads/%d", leadId)
|
|
|
|
if query != "" {
|
2024-01-15 00:04:00 +03:00
|
|
|
resource = resource + "?" + query
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.Api.Get(resource, deal)
|
|
|
|
return deal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) UpdateLead(lead *leads.Lead) error {
|
2024-01-15 00:20:13 +03:00
|
|
|
return client.updateEntity("/api/v4/leads", lead.Id, lead)
|
2024-01-15 00:04:00 +03:00
|
|
|
}
|
|
|
|
|
2024-03-25 13:18:52 +03:00
|
|
|
func (client *Client) GetCompany(companyId int, query string) (*companies.Company, error) {
|
2024-01-15 00:04:00 +03:00
|
|
|
deal := new(companies.Company)
|
2024-03-25 13:18:52 +03:00
|
|
|
resource := fmt.Sprintf("/api/v4/companies/%d", companyId)
|
2024-01-19 08:14:23 +03:00
|
|
|
if query != "" {
|
2024-01-15 00:04:00 +03:00
|
|
|
resource = resource + "?" + query
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.Api.Get(resource, deal)
|
|
|
|
return deal, err
|
|
|
|
}
|
|
|
|
|
2024-01-19 08:14:23 +03:00
|
|
|
// Returns the events from AmoCRM by specified request.
|
|
|
|
// If there are no such events returns an empty slice of events.
|
|
|
|
func (client *Client) GetEvents(req events.EventsRequest) ([]events.Event, error) {
|
|
|
|
res := "/api/v4/events"
|
|
|
|
format := req.Format()
|
|
|
|
if format != "" {
|
|
|
|
res += "?" + format
|
|
|
|
}
|
|
|
|
|
|
|
|
var abs bool
|
|
|
|
ret := []events.Event{}
|
|
|
|
for {
|
|
|
|
resp := events.EventsResponse{}
|
|
|
|
err := client.Api.Get(res, &resp, abs)
|
|
|
|
if err != nil {
|
2024-03-04 04:31:33 +03:00
|
|
|
// Return empty if no content avialable.
|
|
|
|
if errors.Is(err, api.NoContentErr) {
|
2024-01-19 08:14:23 +03:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret = append(ret, resp.Embedded.Events...)
|
|
|
|
if resp.Links.Next.Href == "" {
|
|
|
|
break
|
|
|
|
}
|
2024-03-04 04:31:33 +03:00
|
|
|
time.Sleep(time.Millisecond*300)
|
2024-01-19 08:14:23 +03:00
|
|
|
abs = true
|
|
|
|
res = resp.Links.Next.Href
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 00:04:00 +03:00
|
|
|
func (client *Client) UpdateCompany(company *companies.Company) error {
|
2024-01-15 00:20:13 +03:00
|
|
|
return client.updateEntity("/api/v4/companies", company.Id, company)
|
2024-01-15 00:04:00 +03:00
|
|
|
}
|
|
|
|
|
2024-03-25 13:18:52 +03:00
|
|
|
func (client *Client) GetContact(contactId int, query string) (*contacts.Contact, error) {
|
2024-01-15 00:04:00 +03:00
|
|
|
deal := new(contacts.Contact)
|
2024-03-25 13:18:52 +03:00
|
|
|
resource := fmt.Sprintf("/api/v4/contacts/%d", contactId)
|
2024-01-15 03:48:01 +03:00
|
|
|
if query != "" {
|
2024-01-15 00:04:00 +03:00
|
|
|
resource = resource + "?" + query
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.Api.Get(resource, deal)
|
|
|
|
return deal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) UpdateContact(contact *contacts.Contact) error {
|
2024-01-15 00:20:13 +03:00
|
|
|
return client.updateEntity("/api/v4/contacts", contact.Id, contact)
|
2024-01-15 00:04:00 +03:00
|
|
|
}
|