feat: added way to packetly update companies.

This commit is contained in:
Andrey Parhomenko 2024-05-30 15:54:00 +05:00
parent 5a05904589
commit 09d95941ff
8 changed files with 96 additions and 36 deletions

1
.gitignore vendored
View file

@ -2,4 +2,5 @@
*.exe~ *.exe~
.env .env
secret.json secret.json
*.json
/exe/ /exe/

View file

@ -1,6 +1,5 @@
package main package main
import "surdeus.su/core/amo" import "surdeus.su/core/amo"
import "surdeus.su/core/ss/urlenc" import "surdeus.su/core/ss/urlenc"
import "surdeus.su/core/cli/mtool" import "surdeus.su/core/cli/mtool"

View file

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

42
cmd/amocli/updatecom.go Normal file
View file

@ -0,0 +1,42 @@
package main
import "surdeus.su/core/amo"
import "surdeus.su/core/cli/mtool"
import "surdeus.su/core/amo/companies"
import "encoding/json"
import "log"
import "os"
var updateLead =
mtool.T("update-companies").Func(func(flags *mtool.Flags){
var (
secretPath string
)
flags.StringVar(
&secretPath,
"secret",
"",
"path to JSON file with AMO CRM secrets",
"AMO_SECRET",
)
flags.Parse()
client, err := amo.NewClient(secretPath)
if err != nil {
log.Fatalf("NewAmoClient(...): %s\n", err)
}
cs := []companies.Company{}
dec := json.NewDecoder(os.Stdin)
err = dec.Decode(&cs)
if err != nil {
log.Fatalf("json.Decode(...): %s\n", err)
}
err = client.UpdateCompanies(cs)
if err != nil {
log.Fatalf("client.UpdateCompanies(...): %s\n", err)
}
})

View file

@ -5,13 +5,13 @@ import "surdeus.su/core/amo/companies"
import "fmt" import "fmt"
func (client *Client) GetCompany( func (client *Client) GetCompany(
companyId int, companyID int,
opts ...urlenc.Builder, opts ...urlenc.Builder,
) (*companies.Company, error) { ) (*companies.Company, error) {
deal := new(companies.Company) deal := new(companies.Company)
resource := fmt.Sprintf( resource := fmt.Sprintf(
"/api/v4/companies/%d?%s", "/api/v4/companies/%d?%s",
companyId, companyID,
urlenc.Join(opts...), urlenc.Join(opts...),
) )
@ -27,7 +27,24 @@ func (client *Client) UpdateCompany(
) error { ) error {
return client.updateEntity( return client.updateEntity(
"/api/v4/companies", "/api/v4/companies",
company.Id, company.ID,
company, 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
}

View file

@ -3,19 +3,19 @@ package companies
import "surdeus.su/core/amo/common" import "surdeus.su/core/amo/common"
type Company struct { type Company struct {
Id int `json:"id"` ID int `json:"id"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
ResponsibleUserId int `json:"responsible_user_id,omitempty"` ResponsibleUserID int `json:"responsible_user_id,omitempty"`
GroupId int `json:"group_id,omitempty"` GroupID int `json:"group_id,omitempty"`
CreatedBy int `json:"created_by,omitempty"` CreatedBy int `json:"created_by,omitempty"`
UpdatedBy int `json:"updated_by,omitempty"` UpdatedBy int `json:"updated_by,omitempty"`
CreatedAt int `json:"created_at,omitempty"` CreatedAt int `json:"created_at,omitempty"`
UpdatedAt int `json:"updated_at,omitempty"` UpdatedAt int `json:"updated_at,omitempty"`
ClosestTaskAt interface{} `json:"closest_task_at,omitempty"` ClosestTaskAt interface{} `json:"closest_task_at,omitempty"`
CustomFieldsValues []common.CustomFieldsValue `json:"custom_fields_values,omitempty"` CustomFieldsValues []common.CustomFieldsValue `json:"custom_fields_values,omitempty"`
AccountId int `json:"account_id,omitempty"` AccountID int `json:"account_id,omitempty"`
Links Links `json:"_links,omitempty"` Links Links `json:"_links,omitempty"`
Embedded Embedded `json:"_embedded,omitempty"` Embedded Embedded `json:"_embedded,omitempty"`
} }
type Self struct { type Self struct {
@ -27,10 +27,10 @@ type Links struct {
} }
type Contacts struct { type Contacts struct {
Id int `json:"id"` ID int `json:"id"`
} }
type Embedded struct { type Embedded struct {
Tags []interface{} `json:"tags"` Tags []interface{} `json:"tags"`
Contacts []*Contacts `json:"contacts"` Contacts []*Contacts `json:"contacts"`
} }

View file

@ -40,13 +40,13 @@ func (client *Client) GetLeads(
// Get lead with the specified ID. // Get lead with the specified ID.
func (client *Client) GetLead( func (client *Client) GetLead(
leadId int, leadID int,
opts ...urlenc.Builder, opts ...urlenc.Builder,
) (*leads.Lead, error) { ) (*leads.Lead, error) {
deal := new(leads.Lead) deal := new(leads.Lead)
resource := fmt.Sprintf( resource := fmt.Sprintf(
"/api/v4/leads/%d?%s", "/api/v4/leads/%d?%s",
leadId, leadID,
urlenc.Join(opts...).Encode(), urlenc.Join(opts...).Encode(),
) )
@ -57,6 +57,6 @@ func (client *Client) GetLead(
func (client *Client) UpdateLead( func (client *Client) UpdateLead(
lead *leads.Lead, lead *leads.Lead,
) error { ) error {
return client.updateEntity("/api/v4/leads", lead.Id, lead) return client.updateEntity("/api/v4/leads", lead.ID, lead)
} }

View file

@ -3,15 +3,15 @@ package leads
import "surdeus.su/core/amo/common" import "surdeus.su/core/amo/common"
type Lead struct { type Lead struct {
Id int `json:"id"` ID int `json:"id"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Price int `json:"price,omitempty"` Price int `json:"price,omitempty"`
ResponsibleUserId int `json:"responsible_user_id,omitempty"` ResponsibleUserID int `json:"responsible_user_id,omitempty"`
GroupId int `json:"group_id,omitempty"` GroupID int `json:"group_id,omitempty"`
StatusId int `json:"status_id,omitempty"` StatusID int `json:"status_id,omitempty"`
PipelineId int `json:"pipeline_id,omitempty"` PipelineID int `json:"pipeline_id,omitempty"`
LossReasonId int `json:"loss_reason_id,omitempty"` LossReasonID int `json:"loss_reason_id,omitempty"`
SourceId interface{} `json:"source_id,omitempty"` SourceID interface{} `json:"source_id,omitempty"`
CreatedBy int `json:"created_by,omitempty"` CreatedBy int `json:"created_by,omitempty"`
UpdatedBy int `json:"updated_by,omitempty"` UpdatedBy int `json:"updated_by,omitempty"`
CreatedAt int `json:"created_at,omitempty"` CreatedAt int `json:"created_at,omitempty"`
@ -21,7 +21,7 @@ type Lead struct {
IsDeleted bool `json:"is_deleted,omitempty"` IsDeleted bool `json:"is_deleted,omitempty"`
CustomFieldsValues []common.CustomFieldsValue `json:"custom_fields_values,omitempty"` CustomFieldsValues []common.CustomFieldsValue `json:"custom_fields_values,omitempty"`
Score interface{} `json:"score,omitempty"` Score interface{} `json:"score,omitempty"`
AccountId int `json:"account_id,omitempty"` AccountID int `json:"account_id,omitempty"`
IsPriceModifiedByRobot bool `json:"is_price_modified_by_robot,omitempty"` IsPriceModifiedByRobot bool `json:"is_price_modified_by_robot,omitempty"`
Links Links `json:"_links,omitempty"` Links Links `json:"_links,omitempty"`
Embedded Embedded `json:"_embedded,omitempty"` Embedded Embedded `json:"_embedded,omitempty"`
@ -36,22 +36,22 @@ type Links struct {
} }
type Tags struct { type Tags struct {
Id int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
} }
type Metadata struct { type Metadata struct {
Quantity int `json:"quantity"` Quantity int `json:"quantity"`
CatalogId int `json:"catalog_id"` CatalogID int `json:"catalog_id"`
} }
type CatalogElements struct { type CatalogElements struct {
Id int `json:"id"` ID int `json:"id"`
Metadata Metadata `json:"metadata"` Metadata Metadata `json:"metadata"`
} }
type LossReason struct { type LossReason struct {
Id int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Sort int `json:"sort"` Sort int `json:"sort"`
CreatedAt int `json:"created_at"` CreatedAt int `json:"created_at"`
@ -60,12 +60,12 @@ type LossReason struct {
} }
type Companies struct { type Companies struct {
Id int `json:"id"` ID int `json:"id"`
Links Links `json:"_links"` Links Links `json:"_links"`
} }
type Contacts struct { type Contacts struct {
Id int `json:"id"` ID int `json:"id"`
IsMain bool `json:"is_main"` IsMain bool `json:"is_main"`
Links Links `json:"_links"` Links Links `json:"_links"`
} }