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 ( import (
"vultras.su/core/amo" "vultras.su/core/amo"
"vultras.su/core/amo/webhooks"
"vultras.su/core/bond" "vultras.su/core/bond"
"vultras.su/core/bond/statuses" "vultras.su/core/bond/statuses"
"os" "os"
"fmt" "fmt"
"io" "io"
"encoding/json"
) )
type Context = bond.Context type Context = bond.Context
@ -18,12 +20,16 @@ var root = bond.Root(bond.Path().
Def( Def(
"hook", "hook",
bond.Func(func(c *Context){ bond.Func(func(c *Context){
v := map[string]any{} v := webhooks.Request{}
c.Scan(&v) c.Scan(&v)
if c.ScanErr() != nil { if c.ScanErr() != nil {
fmt.Println("scan-err:", c.ScanErr()) 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) c.SetStatus(statuses.OK)
}), }),

View file

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