feat: better webhooks.

This commit is contained in:
Andrey Parhomenko 2024-01-15 08:28:11 +03:00
parent 121e1470dc
commit 2e5805b97b
2 changed files with 87 additions and 25 deletions

View file

@ -2,11 +2,13 @@ package main
import (
"vultras.su/core/amo"
"vultras.su/core/amo/webhooks"
"vultras.su/core/bond"
"vultras.su/core/bond/statuses"
"os"
"fmt"
"io"
"encoding/json"
)
type Context = bond.Context
@ -18,12 +20,16 @@ var root = bond.Root(bond.Path().
Def(
"hook",
bond.Func(func(c *Context){
v := map[string]any{}
v := webhooks.Request{}
c.Scan(&v)
if c.ScanErr() != nil {
fmt.Println("scan-err:", c.ScanErr())
}
fmt.Println("request:", v)
bts, err := json.MarshalIndent(v, "", "\t")
if err != nil {
fmt.Println("marsh-error:", err)
}
fmt.Println("request:", string(bts))
c.SetStatus(statuses.OK)
}),

View file

@ -1,48 +1,104 @@
package webhooks
import (
"vultras.su/core/bond/urlenc"
//"vultras.su/core/bond/urlenc"
"vultras.su/core/bond/jsons"
"log"
/*"log"
"net/url"
"strings"
"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 {
Leads Leads `schema:"leads"`
Account Account `schema:"account"`
Account Account `json:"account"`
Leads *Leads `json:"leads,omitempty"`
Contacts *Contacts `json:"contacts,omitempty"`
}
type Type int
const (
TypeNone Type = iota
TypeLeads
TypeContacts
)
type Leads struct {
Status jsons.ArrayMap[Status] `schema:"status"`
Add jsons.ArrayMap[Status] `schema:"add"`
}
func (r *Request) Type() Type {
if r.Leads != nil {
return TypeLeads
}
if r.Contacts != nil {
return TypeContacts
}
type Status struct {
Id jsons.Int `schema:"id"`
StatusId jsons.Int `schema:"status_id"`
PipelineId jsons.Int `schema:"pipeline_id"`
OldStatusId jsons.Int `schema:"old_status_id"`
OldPipelineId jsons.Int `schema:"old_pipeline_id"`
return TypeNone
}
type Account struct {
Id jsons.Int `schema:"id"`
SubDomain string `schema:"subdomain"`
Id jsons.Int `json:"id"`
SubDomain string `json:"subdomain"`
Links struct {
Self string `json:"self"`
} `json:"_links"`
}
func NewFromString(body string) (*WebhookRequest, error) {
ret := &WebhookRequest{}
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 *WebhookRequest) GetLeadId() string {
func (hook *Request) GetLeadId() int {
if hook.Leads.Status != nil {
return hook.Leads.Status[0].Id
return int(hook.Leads.Status[0].Id)
}
return hook.Leads.Add[0].Id
return int(hook.Leads.Add[0].Id)
}