2024-05-30 11:43:10 +03:00
|
|
|
package amo
|
|
|
|
|
|
|
|
import "surdeus.su/core/amo/api"
|
|
|
|
import "surdeus.su/core/amo/leads"
|
|
|
|
import "surdeus.su/core/ss/urlenc"
|
|
|
|
import "errors"
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// Get list of leads.
|
|
|
|
func (client *Client) GetLeads(
|
|
|
|
opts ...urlenc.Builder,
|
|
|
|
) ([]leads.Lead, error) {
|
|
|
|
res := fmt.Sprintf(
|
|
|
|
"/api/v4/leads?%s",
|
|
|
|
urlenc.Join(opts...).Encode(),
|
|
|
|
)
|
|
|
|
|
|
|
|
ret := []leads.Lead{}
|
|
|
|
|
|
|
|
for {
|
|
|
|
var lds leads.Leads
|
|
|
|
err := client.API.Get(res, &lds)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, api.ErrNoContent) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, lds.Embedded.Leads...)
|
|
|
|
|
|
|
|
if lds.Links.Next.Href == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
res = lds.Links.Next.Href
|
|
|
|
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get lead with the specified ID.
|
|
|
|
func (client *Client) GetLead(
|
2024-05-30 13:54:00 +03:00
|
|
|
leadID int,
|
2024-05-30 11:43:10 +03:00
|
|
|
opts ...urlenc.Builder,
|
|
|
|
) (*leads.Lead, error) {
|
|
|
|
deal := new(leads.Lead)
|
|
|
|
resource := fmt.Sprintf(
|
|
|
|
"/api/v4/leads/%d?%s",
|
2024-05-30 13:54:00 +03:00
|
|
|
leadID,
|
2024-05-30 11:43:10 +03:00
|
|
|
urlenc.Join(opts...).Encode(),
|
|
|
|
)
|
|
|
|
|
|
|
|
err := client.API.Get(resource, deal)
|
|
|
|
return deal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) UpdateLead(
|
|
|
|
lead *leads.Lead,
|
|
|
|
) error {
|
2024-05-30 13:54:00 +03:00
|
|
|
return client.updateEntity("/api/v4/leads", lead.ID, lead)
|
2024-05-30 11:43:10 +03:00
|
|
|
}
|
|
|
|
|