2024-05-30 11:43:10 +03:00
|
|
|
package amo
|
|
|
|
|
|
|
|
import "surdeus.su/core/ss/urlenc"
|
|
|
|
import "surdeus.su/core/amo/companies"
|
2024-05-30 23:45:36 +03:00
|
|
|
import "surdeus.su/core/amo/api"
|
|
|
|
import "errors"
|
2024-05-30 11:43:10 +03:00
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
func (client *Client) GetCompany(
|
2024-05-30 13:54:00 +03:00
|
|
|
companyID int,
|
2024-05-30 11:43:10 +03:00
|
|
|
opts ...urlenc.Builder,
|
2024-06-05 00:08:54 +03:00
|
|
|
) (*Company, error) {
|
2024-05-30 11:43:10 +03:00
|
|
|
deal := new(companies.Company)
|
|
|
|
resource := fmt.Sprintf(
|
|
|
|
"/api/v4/companies/%d?%s",
|
2024-05-30 13:54:00 +03:00
|
|
|
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
|
|
|
|
}
|
2024-05-30 23:45:36 +03:00
|
|
|
// Get list of leads.
|
|
|
|
func (client *Client) GetCompanies(
|
|
|
|
opts ...urlenc.Builder,
|
2024-06-05 00:08:54 +03:00
|
|
|
) ([]Company, NextFunc[[]Company], error) {
|
2024-05-30 23:45:36 +03:00
|
|
|
res := fmt.Sprintf(
|
|
|
|
"/api/v4/companies?%s",
|
|
|
|
urlenc.Join(opts...).Encode(),
|
|
|
|
)
|
|
|
|
|
2024-06-05 00:08:54 +03:00
|
|
|
return client.GetCompaniesByURL(res)
|
|
|
|
}
|
2024-05-30 23:45:36 +03:00
|
|
|
|
2024-06-05 00:08:54 +03:00
|
|
|
func (client *Client) GetCompaniesByURL(
|
|
|
|
u string,
|
|
|
|
) ([]Company, NextFunc[[]Company], error) {
|
|
|
|
var fn NextFunc[[]Company]
|
2024-05-30 23:45:36 +03:00
|
|
|
|
2024-06-05 00:08:54 +03:00
|
|
|
coms := companies.Companies{}
|
|
|
|
err := client.API.Get(u, &coms)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, api.ErrNoContent) {
|
|
|
|
return nil, nil, nil
|
2024-05-30 23:45:36 +03:00
|
|
|
}
|
2024-06-05 00:08:54 +03:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//ret = append(ret, coms.Embedded.Companies...)
|
2024-05-30 23:45:36 +03:00
|
|
|
|
2024-06-05 00:08:54 +03:00
|
|
|
nextHref := coms.Links.Next.Href
|
|
|
|
if nextHref != "" {
|
|
|
|
fn = MakeNextFunc(
|
|
|
|
nextHref,
|
|
|
|
client.GetCompaniesByURL,
|
|
|
|
)
|
2024-05-30 23:45:36 +03:00
|
|
|
}
|
2024-06-05 00:08:54 +03:00
|
|
|
return coms.Embedded.Companies, fn, nil
|
2024-05-30 23:45:36 +03:00
|
|
|
}
|
2024-05-30 11:43:10 +03:00
|
|
|
|
|
|
|
func (client *Client) UpdateCompany(
|
|
|
|
company *companies.Company,
|
|
|
|
) error {
|
|
|
|
return client.updateEntity(
|
|
|
|
"/api/v4/companies",
|
2024-05-30 13:54:00 +03:00
|
|
|
company.ID,
|
2024-05-30 11:43:10 +03:00
|
|
|
company,
|
|
|
|
)
|
|
|
|
}
|
2024-05-30 13:54:00 +03:00
|
|
|
|
|
|
|
func (client *Client) UpdateCompanies(
|
|
|
|
cs []companies.Company,
|
2024-06-05 15:50:54 +03:00
|
|
|
) ([]Company, error) {
|
2024-06-05 00:08:54 +03:00
|
|
|
if len(cs) > MEPR {
|
2024-06-05 15:50:54 +03:00
|
|
|
return nil, ErrTooManyEntities
|
2024-06-05 00:08:54 +03:00
|
|
|
}
|
2024-06-05 15:50:54 +03:00
|
|
|
|
2024-06-05 00:08:54 +03:00
|
|
|
//ret := []Company{}
|
2024-05-30 13:54:00 +03:00
|
|
|
err := client.API.Patch(
|
|
|
|
"/api/v4/companies",
|
|
|
|
cs,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-06-05 15:50:54 +03:00
|
|
|
return nil, err
|
2024-05-30 13:54:00 +03:00
|
|
|
}
|
|
|
|
|
2024-06-05 15:50:54 +03:00
|
|
|
return nil, nil
|
2024-05-30 13:54:00 +03:00
|
|
|
}
|