feat: added getting for companies and contacts.

This commit is contained in:
Andrey Parhomenko 2024-05-31 01:45:36 +05:00
parent ab6d5fb118
commit f970498504
8 changed files with 216 additions and 5 deletions

55
cmd/amocli/getcom.go Normal file
View file

@ -0,0 +1,55 @@
package main
import "surdeus.su/core/amo"
import "surdeus.su/core/ss/urlenc"
import "surdeus.su/core/cli/mtool"
import "encoding/json"
import "strconv"
import "log"
import "fmt"
//import "os"
var getComs = mtool.T("get-coms").Func(func(flags *mtool.Flags){
var (
secretPath string
)
flags.StringVar(
&secretPath,
"secret",
"",
"path to JSON file with AMO CRM secrets",
"AMO_SECRET",
)
idStrs := flags.Parse()
ids := make([]int, len(idStrs))
for i, idStr := range idStrs {
var err error
ids[i], err = strconv.Atoi(idStr)
if err != nil {
log.Printf("Error: Atoi(%q): %s\n", err)
return
}
}
c, err := amo.NewClient(secretPath)
if err != nil {
log.Fatalf("NewAmoClient(...): %s\n", err)
}
coms, err := c.GetCompanies(
urlenc.Array[int]{
"id",
ids,
},
)
if err != nil {
log.Fatalf("GetCompanies(...): %s\n", err)
}
bts, err := json.MarshalIndent(coms, "", " ")
if err != nil {
log.Fatalf("json.Marshal(...): %s\n", err)
}
fmt.Printf("%s\n", bts)
})

56
cmd/amocli/getcontact.go Normal file
View file

@ -0,0 +1,56 @@
package main
import "surdeus.su/core/amo"
import "surdeus.su/core/ss/urlenc"
import "surdeus.su/core/cli/mtool"
import "encoding/json"
import "strconv"
import "log"
import "fmt"
//import "os"
var getContacts = mtool.T("get-contacts").
Func(func(flags *mtool.Flags){
var (
secretPath string
)
flags.StringVar(
&secretPath,
"secret",
"",
"path to JSON file with AMO CRM secrets",
"AMO_SECRET",
)
idStrs := flags.Parse()
ids := make([]int, len(idStrs))
for i, idStr := range idStrs {
var err error
ids[i], err = strconv.Atoi(idStr)
if err != nil {
log.Printf("Error: Atoi(%q): %s\n", err)
return
}
}
c, err := amo.NewClient(secretPath)
if err != nil {
log.Fatalf("NewAmoClient(...): %s\n", err)
}
contacts, err := c.GetContacts(
urlenc.Array[int]{
"id",
ids,
},
)
if err != nil {
log.Fatalf("GetContacts(...): %s\n", err)
}
bts, err := json.MarshalIndent(contacts, "", " ")
if err != nil {
log.Fatalf("json.Marshal(...): %s\n", err)
}
fmt.Printf("%s\n", bts)
})

View file

@ -9,5 +9,9 @@ func main() {
var tool = mtool.T("amocli").Subs(
getLead,
updateLead,
getComs,
updateComs,
getContacts,
)

View file

@ -7,8 +7,8 @@ import "encoding/json"
import "log"
import "os"
var updateLead =
mtool.T("update-companies").Func(func(flags *mtool.Flags){
var updateComs =
mtool.T("update-coms").Func(func(flags *mtool.Flags){
var (
secretPath string
)

View file

@ -2,6 +2,8 @@ package amo
import "surdeus.su/core/ss/urlenc"
import "surdeus.su/core/amo/companies"
import "surdeus.su/core/amo/api"
import "errors"
import "fmt"
func (client *Client) GetCompany(
@ -21,6 +23,36 @@ func (client *Client) GetCompany(
}
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
}
func (client *Client) UpdateCompany(
company *companies.Company,

View file

@ -34,3 +34,18 @@ type Embedded struct {
Tags []interface{} `json:"tags"`
Contacts []*Contacts `json:"contacts"`
}
type Companies struct {
Page int `json:"_page"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
Next struct {
Href string `json:"href"`
} `json:"next"`
} `json:"_links"`
Embedded struct {
Companies []Company `json:"companies"`
} `json:"_embedded"`
}

View file

@ -2,6 +2,8 @@ package amo
import "surdeus.su/core/amo/contacts"
import "surdeus.su/core/ss/urlenc"
import "errors"
import "surdeus.su/core/amo/api"
import "fmt"
func (client *Client) GetContact(
@ -21,6 +23,37 @@ func (client *Client) GetContact(
}
return deal, nil
}
// Get list of contacts.
func (client *Client) GetContacts(
opts ...urlenc.Builder,
) ([]contacts.Contact, error) {
res := fmt.Sprintf(
"/api/v4/contacts?%s",
urlenc.Join(opts...).Encode(),
)
ret := []contacts.Contact{}
for {
var cts contacts.Contacts
err := client.API.Get(res, &cts)
if err != nil {
if errors.Is(err, api.ErrNoContent) {
break
}
return nil, err
}
ret = append(ret, cts.Embedded.Contacts...)
if cts.Links.Next.Href == "" {
break
}
res = cts.Links.Next.Href
}
return ret, nil
}
func (client *Client) UpdateContact(contact *contacts.Contact) error {
return client.updateEntity(

View file

@ -43,7 +43,7 @@ type Customers struct {
Links Links `json:"_links"`
}
type Companies struct {
type Company struct {
Id int `json:"id"`
Links Links `json:"_links"`
}
@ -53,5 +53,21 @@ type Embedded struct {
Leads []Leads `json:"leads"`
Customers []Customers `json:"customers"`
CatalogElements []interface{} `json:"catalog_elements"`
Companies []Companies `json:"companies"`
Companies []Company `json:"companies"`
}
type Contacts struct {
Page int `json:"_page"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
Next struct {
Href string `json:"href"`
} `json:"next"`
} `json:"_links"`
Embedded struct {
Contacts []Contact `json:"contacts"`
} `json:"_embedded"`
}