From cb8446b81717b0afd87c86393466d40f585dc17f Mon Sep 17 00:00:00 2001 From: surdeus Date: Wed, 5 Jun 2024 21:53:00 +0500 Subject: [PATCH] implementing lead-tuples. --- cmd/amocli/getter.go | 5 +- cmd/amocli/main.go | 4 +- lead-tuple.go | 134 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 lead-tuple.go diff --git a/cmd/amocli/getter.go b/cmd/amocli/getter.go index 218aa2a..6dd31f0 100644 --- a/cmd/amocli/getter.go +++ b/cmd/amocli/getter.go @@ -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 { diff --git a/cmd/amocli/main.go b/cmd/amocli/main.go index 653d819..cfb4f04 100644 --- a/cmd/amocli/main.go +++ b/cmd/amocli/main.go @@ -24,5 +24,7 @@ var tool = mtool.T("amocli").Subs( return } fmt.Println(bi.Main.Version) - }), + }).Desc( + "print program version", + ), ) diff --git a/lead-tuple.go b/lead-tuple.go new file mode 100644 index 0000000..a5f4bc9 --- /dev/null +++ b/lead-tuple.go @@ -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 + +}