diff --git a/.gitignore b/.gitignore index e5ddf76..93a8447 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.exe~ .env secret.json +*.json /exe/ diff --git a/cmd/amocli/getlead.go b/cmd/amocli/getlead.go index a7d5a6d..7507263 100644 --- a/cmd/amocli/getlead.go +++ b/cmd/amocli/getlead.go @@ -1,6 +1,5 @@ package main - import "surdeus.su/core/amo" import "surdeus.su/core/ss/urlenc" import "surdeus.su/core/cli/mtool" diff --git a/cmd/amocli/main.go b/cmd/amocli/main.go index 1512b61..f9f9791 100644 --- a/cmd/amocli/main.go +++ b/cmd/amocli/main.go @@ -9,4 +9,5 @@ func main() { var tool = mtool.T("amocli").Subs( getLead, + updateLead, ) diff --git a/cmd/amocli/updatecom.go b/cmd/amocli/updatecom.go new file mode 100644 index 0000000..2a9d4f5 --- /dev/null +++ b/cmd/amocli/updatecom.go @@ -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) + } +}) diff --git a/companies.go b/companies.go index bc1b2ae..df17c1e 100644 --- a/companies.go +++ b/companies.go @@ -5,13 +5,13 @@ import "surdeus.su/core/amo/companies" import "fmt" func (client *Client) GetCompany( - companyId int, + companyID int, opts ...urlenc.Builder, ) (*companies.Company, error) { deal := new(companies.Company) resource := fmt.Sprintf( "/api/v4/companies/%d?%s", - companyId, + companyID, urlenc.Join(opts...), ) @@ -27,7 +27,24 @@ func (client *Client) UpdateCompany( ) error { return client.updateEntity( "/api/v4/companies", - company.Id, + company.ID, 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 +} diff --git a/companies/companies.go b/companies/companies.go index fcdbf7e..688c158 100644 --- a/companies/companies.go +++ b/companies/companies.go @@ -3,19 +3,19 @@ package companies import "surdeus.su/core/amo/common" type Company struct { - Id int `json:"id"` - Name string `json:"name,omitempty"` - ResponsibleUserId int `json:"responsible_user_id,omitempty"` - GroupId int `json:"group_id,omitempty"` - CreatedBy int `json:"created_by,omitempty"` - UpdatedBy int `json:"updated_by,omitempty"` - CreatedAt int `json:"created_at,omitempty"` - UpdatedAt int `json:"updated_at,omitempty"` - ClosestTaskAt interface{} `json:"closest_task_at,omitempty"` + ID int `json:"id"` + Name string `json:"name,omitempty"` + ResponsibleUserID int `json:"responsible_user_id,omitempty"` + GroupID int `json:"group_id,omitempty"` + CreatedBy int `json:"created_by,omitempty"` + UpdatedBy int `json:"updated_by,omitempty"` + CreatedAt int `json:"created_at,omitempty"` + UpdatedAt int `json:"updated_at,omitempty"` + ClosestTaskAt interface{} `json:"closest_task_at,omitempty"` CustomFieldsValues []common.CustomFieldsValue `json:"custom_fields_values,omitempty"` - AccountId int `json:"account_id,omitempty"` - Links Links `json:"_links,omitempty"` - Embedded Embedded `json:"_embedded,omitempty"` + AccountID int `json:"account_id,omitempty"` + Links Links `json:"_links,omitempty"` + Embedded Embedded `json:"_embedded,omitempty"` } type Self struct { @@ -27,10 +27,10 @@ type Links struct { } type Contacts struct { - Id int `json:"id"` + ID int `json:"id"` } type Embedded struct { - Tags []interface{} `json:"tags"` - Contacts []*Contacts `json:"contacts"` + Tags []interface{} `json:"tags"` + Contacts []*Contacts `json:"contacts"` } diff --git a/leads.go b/leads.go index d258eb1..5346f96 100644 --- a/leads.go +++ b/leads.go @@ -40,13 +40,13 @@ func (client *Client) GetLeads( // Get lead with the specified ID. func (client *Client) GetLead( - leadId int, + leadID int, opts ...urlenc.Builder, ) (*leads.Lead, error) { deal := new(leads.Lead) resource := fmt.Sprintf( "/api/v4/leads/%d?%s", - leadId, + leadID, urlenc.Join(opts...).Encode(), ) @@ -57,6 +57,6 @@ func (client *Client) GetLead( func (client *Client) UpdateLead( lead *leads.Lead, ) error { - return client.updateEntity("/api/v4/leads", lead.Id, lead) + return client.updateEntity("/api/v4/leads", lead.ID, lead) } diff --git a/leads/leads.go b/leads/leads.go index f30d10a..345986c 100644 --- a/leads/leads.go +++ b/leads/leads.go @@ -3,15 +3,15 @@ package leads import "surdeus.su/core/amo/common" type Lead struct { - Id int `json:"id"` + ID int `json:"id"` Name string `json:"name,omitempty"` Price int `json:"price,omitempty"` - ResponsibleUserId int `json:"responsible_user_id,omitempty"` - GroupId int `json:"group_id,omitempty"` - StatusId int `json:"status_id,omitempty"` - PipelineId int `json:"pipeline_id,omitempty"` - LossReasonId int `json:"loss_reason_id,omitempty"` - SourceId interface{} `json:"source_id,omitempty"` + ResponsibleUserID int `json:"responsible_user_id,omitempty"` + GroupID int `json:"group_id,omitempty"` + StatusID int `json:"status_id,omitempty"` + PipelineID int `json:"pipeline_id,omitempty"` + LossReasonID int `json:"loss_reason_id,omitempty"` + SourceID interface{} `json:"source_id,omitempty"` CreatedBy int `json:"created_by,omitempty"` UpdatedBy int `json:"updated_by,omitempty"` CreatedAt int `json:"created_at,omitempty"` @@ -21,7 +21,7 @@ type Lead struct { IsDeleted bool `json:"is_deleted,omitempty"` CustomFieldsValues []common.CustomFieldsValue `json:"custom_fields_values,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"` Links Links `json:"_links,omitempty"` Embedded Embedded `json:"_embedded,omitempty"` @@ -36,22 +36,22 @@ type Links struct { } type Tags struct { - Id int `json:"id"` + ID int `json:"id"` Name string `json:"name"` } type Metadata struct { Quantity int `json:"quantity"` - CatalogId int `json:"catalog_id"` + CatalogID int `json:"catalog_id"` } type CatalogElements struct { - Id int `json:"id"` + ID int `json:"id"` Metadata Metadata `json:"metadata"` } type LossReason struct { - Id int `json:"id"` + ID int `json:"id"` Name string `json:"name"` Sort int `json:"sort"` CreatedAt int `json:"created_at"` @@ -60,12 +60,12 @@ type LossReason struct { } type Companies struct { - Id int `json:"id"` + ID int `json:"id"` Links Links `json:"_links"` } type Contacts struct { - Id int `json:"id"` + ID int `json:"id"` IsMain bool `json:"is_main"` Links Links `json:"_links"` }