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~
.env
secret.json
*.json
/exe/

View file

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

View file

@ -9,4 +9,5 @@ func main() {
var tool = mtool.T("amocli").Subs(
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"
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
}

View file

@ -3,17 +3,17 @@ package companies
import "surdeus.su/core/amo/common"
type Company struct {
Id int `json:"id"`
ID int `json:"id"`
Name string `json:"name,omitempty"`
ResponsibleUserId int `json:"responsible_user_id,omitempty"`
GroupId int `json:"group_id,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"`
AccountID int `json:"account_id,omitempty"`
Links Links `json:"_links,omitempty"`
Embedded Embedded `json:"_embedded,omitempty"`
}
@ -27,7 +27,7 @@ type Links struct {
}
type Contacts struct {
Id int `json:"id"`
ID int `json:"id"`
}
type Embedded struct {

View file

@ -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)
}

View file

@ -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"`
}