Basic demo for Tally X AutoIxpert integration to showcase setup in Go due to impossibility of open-sourcing n8n setup.
157 lines
3.5 KiB
Go
157 lines
3.5 KiB
Go
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
type Webhook struct {
|
|
EventID string `json:"eventId"`
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
Data Data `json:"data"`
|
|
}
|
|
|
|
type Data struct {
|
|
ResponseID string `json:"responseId"`
|
|
SubmissionID string `json:"submissionId"`
|
|
RespondentID string `json:"respondentId"`
|
|
FormID string `json:"formId"`
|
|
FormName string `json:"formName"`
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
Fields []Field `json:"fields"`
|
|
}
|
|
|
|
type Field struct {
|
|
Key string `json:"key"`
|
|
Label string `json:"label"`
|
|
Type string `json:"type"`
|
|
Value json.RawMessage `json:"value"`
|
|
Options []Option `json:"options,omitempty"`
|
|
}
|
|
|
|
type Option struct {
|
|
ID string `json:"id"`
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
type File struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
URL string `json:"url"`
|
|
MimeType string `json:"mimeType"`
|
|
Size int64 `json:"size"`
|
|
}
|
|
|
|
func (d Data) FieldMap() map[string]Field {
|
|
m := make(map[string]Field, len(d.Fields))
|
|
for _, f := range d.Fields {
|
|
m[f.Key] = f
|
|
}
|
|
return m
|
|
}
|
|
|
|
func GetString(m map[string]Field, key string) (string, bool) {
|
|
f, ok := m[key]
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
var s string
|
|
if err := json.Unmarshal(f.Value, &s); err != nil {
|
|
return "", false
|
|
}
|
|
return s, true
|
|
}
|
|
|
|
func GetInt(m map[string]Field, key string) (int, bool) {
|
|
f, ok := m[key]
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
var n int
|
|
if err := json.Unmarshal(f.Value, &n); err != nil {
|
|
// some systems send numbers as float64 in JSON; handle that too
|
|
var nf float64
|
|
if err2 := json.Unmarshal(f.Value, &nf); err2 != nil {
|
|
return 0, false
|
|
}
|
|
return int(nf), true
|
|
}
|
|
return n, true
|
|
}
|
|
|
|
func GetStringSlice(m map[string]Field, key string) ([]string, bool) {
|
|
f, ok := m[key]
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
var s []string
|
|
if err := json.Unmarshal(f.Value, &s); err != nil {
|
|
return nil, false
|
|
}
|
|
return s, true
|
|
}
|
|
|
|
func GetFiles(m map[string]Field, key string) ([]File, bool) {
|
|
f, ok := m[key]
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
var files []File
|
|
if err := json.Unmarshal(f.Value, &files); err != nil {
|
|
return nil, false
|
|
}
|
|
return files, true
|
|
}
|
|
|
|
func (w Webhook) ToCrashReport() CrashReport {
|
|
fm := w.Data.FieldMap()
|
|
|
|
eventID := w.EventID
|
|
submissionID := w.Data.SubmissionID
|
|
|
|
firstName, _ := GetString(fm, "question_LdEGVy")
|
|
lastName, _ := GetString(fm, "question_pL0A51")
|
|
street, _ := GetString(fm, "question_1rbKOg")
|
|
postcode, _ := GetInt(fm, "question_MArOPl")
|
|
city, _ := GetString(fm, "question_J2VRGr")
|
|
phone, _ := GetString(fm, "question_g5QAgP")
|
|
email, _ := GetString(fm, "question_yl0yg8")
|
|
|
|
licensePlate, _ := GetString(fm, "question_XeRG9P")
|
|
|
|
licenseFiles, _ := GetFiles(fm, "question_8dJk2l")
|
|
crashDate, _ := GetString(fm, "question_0EbMG9")
|
|
crashLocation, _ := GetString(fm, "question_zK0zgk")
|
|
crashDescription, _ := GetString(fm, "question_5dlkrN")
|
|
crashImages, _ := GetFiles(fm, "question_dYZAgr")
|
|
|
|
images := make([]string, 0)
|
|
for _, f := range licenseFiles {
|
|
images = append(images, f.URL)
|
|
}
|
|
for _, f := range crashImages {
|
|
images = append(images, f.URL)
|
|
}
|
|
|
|
crashDateParsed, _ := time.Parse("2006-01-02", crashDate)
|
|
|
|
return CrashReport{
|
|
EventID: eventID,
|
|
SubmissionID: submissionID,
|
|
|
|
FirstName: firstName,
|
|
LastName: lastName,
|
|
Street: street,
|
|
Postcode: postcode,
|
|
City: city,
|
|
Phone: phone,
|
|
Email: email,
|
|
|
|
LicensePlate: licensePlate,
|
|
CrashLocation: crashLocation,
|
|
CrashDescription: crashDescription,
|
|
Images: images,
|
|
CrashDate: crashDateParsed,
|
|
}
|
|
}
|