amo/webhooks/webhook.go

105 lines
2.2 KiB
Go
Raw Normal View History

2024-01-15 00:04:00 +03:00
package webhooks
import (
2024-05-19 21:00:41 +03:00
//"surdeus.su/core/ss/urlenc"
"surdeus.su/core/ss/jsons"
2024-01-15 08:28:11 +03:00
/*"log"
2024-01-15 00:04:00 +03:00
"net/url"
2024-01-15 08:28:11 +03:00
"strings"*/
2024-01-15 00:04:00 +03:00
)
2024-01-15 08:28:11 +03:00
type CustomFields jsons.ArrayMap[CustomField]
type CustomField struct {
Id jsons.Int `json:"id"`
Name string `json:"name"`
Values jsons.ArrayMap[any]
}
2024-01-15 00:04:00 +03:00
type Request struct {
2024-01-15 08:28:11 +03:00
Account Account `json:"account"`
Leads *Leads `json:"leads,omitempty"`
Contacts *Contacts `json:"contacts,omitempty"`
}
type Type int
const (
TypeNone Type = iota
TypeLeads
TypeContacts
)
func (r *Request) Type() Type {
if r.Leads != nil {
return TypeLeads
}
if r.Contacts != nil {
return TypeContacts
}
return TypeNone
}
type Account struct {
Id jsons.Int `json:"id"`
SubDomain string `json:"subdomain"`
Links struct {
Self string `json:"self"`
} `json:"_links"`
2024-01-15 00:04:00 +03:00
}
type Leads struct {
2024-01-15 08:28:11 +03:00
Status jsons.ArrayMap[LeadStatus] `json:"status"`
Add jsons.ArrayMap[LeadStatus] `json:"add"`
2024-01-15 00:04:00 +03:00
}
2024-01-15 08:28:11 +03:00
type LeadStatus struct {
Id jsons.Int `json:"id"`
StatusId jsons.Int `json:"status_id"`
PipelineId jsons.Int `json:"pipeline_id"`
2024-01-15 00:04:00 +03:00
2024-01-15 08:28:11 +03:00
OldStatusId jsons.Int `json:"old_status_id"`
OldPipelineId jsons.Int `json:"old_pipeline_id"`
2024-01-15 00:04:00 +03:00
}
2024-01-15 08:28:11 +03:00
type Contacts struct {
Add jsons.ArrayMap[Contact] `json:"add"`
2024-01-15 00:04:00 +03:00
}
2024-01-15 08:28:11 +03:00
type Contact struct {
Type ContactType `json:"type"`
Id jsons.Int `json:"id"`
Name string `json:"name"`
AccountId jsons.Int `json:"account_id"`
CreatedUserId jsons.Int `json:"created_user_id"`
ModifiedUserId jsons.Int `json:"modified_user_id"`
ResponsibleUserId jsons.Int `json:"responsible_user_id"`
CreatedAt jsons.Int `json:"created_at"`
UpdatedAt jsons.Int `json:"updated_at"`
LastModified jsons.Int `json:"last_modified"`
DateCreate jsons.Int `json:"date_create"`
2024-01-15 00:04:00 +03:00
2024-01-15 08:28:11 +03:00
CustomFields CustomFields`json:"custom_fields,omitempty"`
}
type ContactType string
const (
NoContactType = ""
ContactContactType = "contacts"
CompanyContactType = "company"
)
/*func NewFromString(body string) (*Request, error) {
ret := &Request{}
2024-01-15 00:04:00 +03:00
err := urlenc.Unmarsal(ret)
if err != nil {
return nil, err
}
return ret, nil
2024-01-15 08:28:11 +03:00
}*/
2024-01-15 00:04:00 +03:00
2024-01-15 08:28:11 +03:00
func (hook *Request) GetLeadId() int {
2024-01-15 00:04:00 +03:00
if hook.Leads.Status != nil {
2024-01-15 08:28:11 +03:00
return int(hook.Leads.Status[0].Id)
2024-01-15 00:04:00 +03:00
}
2024-01-15 08:28:11 +03:00
return int(hook.Leads.Add[0].Id)
2024-01-15 00:04:00 +03:00
}