amo/webhooks/webhook.go
2024-01-15 08:28:11 +03:00

104 lines
2.2 KiB
Go

package webhooks
import (
//"vultras.su/core/bond/urlenc"
"vultras.su/core/bond/jsons"
/*"log"
"net/url"
"strings"*/
)
type CustomFields jsons.ArrayMap[CustomField]
type CustomField struct {
Id jsons.Int `json:"id"`
Name string `json:"name"`
Values jsons.ArrayMap[any]
}
type Request struct {
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"`
}
type Leads struct {
Status jsons.ArrayMap[LeadStatus] `json:"status"`
Add jsons.ArrayMap[LeadStatus] `json:"add"`
}
type LeadStatus struct {
Id jsons.Int `json:"id"`
StatusId jsons.Int `json:"status_id"`
PipelineId jsons.Int `json:"pipeline_id"`
OldStatusId jsons.Int `json:"old_status_id"`
OldPipelineId jsons.Int `json:"old_pipeline_id"`
}
type Contacts struct {
Add jsons.ArrayMap[Contact] `json:"add"`
}
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"`
CustomFields CustomFields`json:"custom_fields,omitempty"`
}
type ContactType string
const (
NoContactType = ""
ContactContactType = "contacts"
CompanyContactType = "company"
)
/*func NewFromString(body string) (*Request, error) {
ret := &Request{}
err := urlenc.Unmarsal(ret)
if err != nil {
return nil, err
}
return ret, nil
}*/
func (hook *Request) GetLeadId() int {
if hook.Leads.Status != nil {
return int(hook.Leads.Status[0].Id)
}
return int(hook.Leads.Add[0].Id)
}