2024-05-30 11:43:10 +03:00
|
|
|
package amo
|
|
|
|
|
|
|
|
import "surdeus.su/core/amo/contacts"
|
|
|
|
import "surdeus.su/core/ss/urlenc"
|
2024-05-30 23:45:36 +03:00
|
|
|
import "errors"
|
|
|
|
import "surdeus.su/core/amo/api"
|
2024-05-30 11:43:10 +03:00
|
|
|
import "fmt"
|
2024-05-31 20:19:41 +03:00
|
|
|
//import "log"
|
2024-05-30 11:43:10 +03:00
|
|
|
|
|
|
|
func (client *Client) GetContact(
|
2024-05-31 14:32:18 +03:00
|
|
|
contactID int,
|
2024-05-30 11:43:10 +03:00
|
|
|
opts ...urlenc.Builder,
|
|
|
|
) (*contacts.Contact, error) {
|
|
|
|
deal := new(contacts.Contact)
|
|
|
|
res := fmt.Sprintf(
|
|
|
|
"/api/v4/contacts/%d?%s",
|
2024-05-31 14:32:18 +03:00
|
|
|
contactID,
|
2024-05-30 11:43:10 +03:00
|
|
|
urlenc.Join(opts...).Encode(),
|
|
|
|
)
|
|
|
|
|
|
|
|
err := client.API.Get(res, deal)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return deal, nil
|
|
|
|
}
|
2024-05-30 23:45:36 +03:00
|
|
|
// Get list of contacts.
|
|
|
|
func (client *Client) GetContacts(
|
|
|
|
opts ...urlenc.Builder,
|
2024-08-01 22:16:26 +03:00
|
|
|
) ([]contacts.Contact, NextFunc[[]Contact], error) {
|
2024-05-30 23:45:36 +03:00
|
|
|
res := fmt.Sprintf(
|
|
|
|
"/api/v4/contacts?%s",
|
|
|
|
urlenc.Join(opts...).Encode(),
|
|
|
|
)
|
|
|
|
|
2024-08-01 22:16:26 +03:00
|
|
|
return client.GetContactsByURL(res)
|
|
|
|
}
|
2024-05-30 23:45:36 +03:00
|
|
|
|
2024-08-01 22:16:26 +03:00
|
|
|
func (client *Client) GetContactsByURL(
|
|
|
|
u string,
|
|
|
|
) ([]Contact, NextFunc[[]Contact], error) {
|
|
|
|
var fn NextFunc[[]Contact]
|
2024-05-30 23:45:36 +03:00
|
|
|
|
2024-08-01 22:16:26 +03:00
|
|
|
contacts := contacts.Contacts{}
|
|
|
|
err := client.API.Get(u, &contacts)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, api.ErrNoContent) {
|
|
|
|
return nil, nil, nil
|
2024-05-30 23:45:36 +03:00
|
|
|
}
|
2024-08-01 22:16:26 +03:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//ret = append(ret, coms.Embedded.Companies...)
|
2024-05-30 23:45:36 +03:00
|
|
|
|
2024-08-01 22:16:26 +03:00
|
|
|
nextHref := contacts.Links.Next.Href
|
|
|
|
if nextHref != "" {
|
|
|
|
fn = MakeNextFunc(
|
|
|
|
nextHref,
|
|
|
|
client.GetContactsByURL,
|
|
|
|
)
|
2024-05-30 23:45:36 +03:00
|
|
|
}
|
2024-08-01 22:16:26 +03:00
|
|
|
return contacts.Embedded.Contacts, fn, nil
|
2024-05-30 23:45:36 +03:00
|
|
|
}
|
2024-05-30 11:43:10 +03:00
|
|
|
|
|
|
|
func (client *Client) UpdateContact(contact *contacts.Contact) error {
|
|
|
|
return client.updateEntity(
|
|
|
|
"/api/v4/contacts",
|
2024-05-31 14:32:18 +03:00
|
|
|
contact.ID,
|
2024-05-30 11:43:10 +03:00
|
|
|
contact,
|
|
|
|
)
|
2024-05-31 20:19:41 +03:00
|
|
|
|
2024-05-30 11:43:10 +03:00
|
|
|
}
|