amo/companies.go

83 lines
1.4 KiB
Go
Raw Normal View History

2024-05-30 11:43:10 +03:00
package amo
import "surdeus.su/core/ss/urlenc"
import "surdeus.su/core/amo/companies"
import "surdeus.su/core/amo/api"
import "errors"
2024-05-30 11:43:10 +03:00
import "fmt"
func (client *Client) GetCompany(
companyID int,
2024-05-30 11:43:10 +03:00
opts ...urlenc.Builder,
) (*companies.Company, error) {
deal := new(companies.Company)
resource := fmt.Sprintf(
"/api/v4/companies/%d?%s",
companyID,
2024-05-30 11:43:10 +03:00
urlenc.Join(opts...),
)
err := client.API.Get(resource, deal)
if err != nil {
return nil, err
}
return deal, nil
}
// Get list of leads.
func (client *Client) GetCompanies(
opts ...urlenc.Builder,
) ([]companies.Company, error) {
res := fmt.Sprintf(
"/api/v4/companies?%s",
urlenc.Join(opts...).Encode(),
)
ret := []companies.Company{}
for {
var coms companies.Companies
err := client.API.Get(res, &coms)
if err != nil {
if errors.Is(err, api.ErrNoContent) {
break
}
return nil, err
}
ret = append(ret, coms.Embedded.Companies...)
if coms.Links.Next.Href == "" {
break
}
res = coms.Links.Next.Href
}
return ret, nil
}
2024-05-30 11:43:10 +03:00
func (client *Client) UpdateCompany(
company *companies.Company,
) error {
return client.updateEntity(
"/api/v4/companies",
company.ID,
2024-05-30 11:43:10 +03:00
company,
)
}
func (client *Client) UpdateCompanies(
cs []companies.Company,
) error {
//ret := []companies.Company{}
err := client.API.Patch(
"/api/v4/companies",
cs,
nil,
)
if err != nil {
return err
}
return nil
}