implementing lead-tuples.

This commit is contained in:
Andrey Parhomenko 2024-06-05 21:53:00 +05:00
parent cbe56c94d1
commit cb8446b817
3 changed files with 141 additions and 2 deletions

View file

@ -55,7 +55,10 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) {
for page <= opts.EndPage && next != nil {
values, next, err = next()
if err != nil {
log.Fatalf("amo.GetLeads(...): %s\n", err)
log.Fatalf(
"%s(...): %s\n",
g.GetFuncName(), err,
)
}
finalValues = append(finalValues, values...)
if opts.Verbose {

View file

@ -24,5 +24,7 @@ var tool = mtool.T("amocli").Subs(
return
}
fmt.Println(bi.Main.Version)
}),
}).Desc(
"print program version",
),
)

134
lead-tuple.go Normal file
View file

@ -0,0 +1,134 @@
package amo
import "surdeus.su/core/ss/urlenc"
import "fmt"
// The type describes tuple
// contaning everything related
// to THE lead.
type LeadTuple struct {
Lead *Lead `json:"lead,omitempty"`
MainContact *Contact `json:"main_contact,omitempty"`
Company *Company `json:"company,omitempty"`
}
func (client *Client) GetLeadTuples(
opts ...urlenc.Builder,
) ([]LeadTuple, NextFunc[[]LeadTuple], error) {
res := fmt.Sprintf(
"/api/v4/leads?%s",
urlenc.Join(opts...).Encode,
)
return client.GetLeadTuplesByFuncURL(
func() ([]Lead, NextFunc[[]Lead], error){
return client.GetLeadsByURL(res)
},
)
}
func (client *Client) GetLeadTuplesByFuncURL(
// The function describes way of getting leads.
callback func() (
[]Lead,
NextFunc[[]Lead],
error,
),
) ([]LeadTuple, NextFunc[[]LeadTuple], error) {
var fn NextFunc[[]LeadTuple]
leads, next, err := callback()
if err != nil {
return nil, nil, fmt.Errorf("amo.GetLeads(...): %s\n", err)
}
comIDs := []int{}
contactIDs := []int{}
leadToCom := map[int]int{}
leadToContact := map[int]int{}
for _, lead := range leads {
coms := lead.Embedded.Companies
if len(coms) > 0 {
com := coms[0]
leadToCom[lead.ID] = com.ID
comIDs = append(comIDs, com.ID)
}
mainContact, hasMain := lead.Embedded.
Contacts.GetMain()
if hasMain {
leadToContact[lead.ID] = mainContact.ID
contactIDs = append(contactIDs, mainContact.ID)
}
}
comMap := map[int] *Company{}
var coms []Company
if len(comIDs) > 0 {
coms, _, err = client.GetCompanies(
urlenc.Array[int]{
"id",
comIDs,
},
)
if err != nil {
return nil, nil, fmt.Errorf(
"amo.GetCompanies(...): %s\n", err)
}
for i := range coms {
comMap[coms[i].ID] = &coms[i]
}
}
contactMap := map[int] *Contact{}
var contacts []Contact
if len(contactIDs) > 0 {
contacts, err = client.GetContacts(
urlenc.Array[int]{
"id",
contactIDs,
},
)
if err != nil {
return nil, nil, fmt.Errorf(
"client.GetContacts(...): %s\n", err)
}
}
for i := range contacts {
contactMap[contacts[i].ID] = &contacts[i]
}
ret := []LeadTuple{}
for i := range leads {
leadID := leads[i].ID
tup := LeadTuple{
Lead: &leads[i],
}
contactID, ok := leadToContact[leadID]
if ok {
contact, ok := contactMap[contactID]
if ok {
tup.MainContact = contact
}
}
companyID, ok := leadToCom[leadID]
if ok {
company, ok := comMap[companyID]
if ok {
tup.Company = company
}
}
ret = append(ret, tup)
}
if next != nil {
fn = NextFunc[[]LeadTuple](
func() ([]LeadTuple, NextFunc[[]LeadTuple], error) {
return client.GetLeadTuplesByFuncURL(
next,
)
},
)
}
return ret, fn, nil
}