feat(dashboard-apps): align chatwoot payloads

This commit is contained in:
2026-06-06 13:03:16 +08:00
parent 11e5537f9b
commit 75f0e809a5
9 changed files with 312 additions and 71 deletions
+26 -21
View File
@@ -3,6 +3,7 @@ package model
import (
"encoding/json"
"fmt"
"strings"
"time"
"gorm.io/gorm"
@@ -16,25 +17,26 @@ import (
// account and optionally to a specific user (personal dashboards).
//
// Content format: JSON array of iframe configurations, e.g.:
// [{"type": "frame", "url": "https://example.com/widget"}]
//
// [{"type": "frame", "url": "https://example.com/widget"}]
//
// Validation rules (per Chatwoot):
// - content must be a JSON array (empty array allowed)
// - content must be a JSON array with at least one widget
// - each element must have type="frame" and url (http/https URI)
type DashboardApp struct {
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
AccountID uint `gorm:"not null;index" json:"account_id"`
UserID *uint `gorm:"index" json:"user_id,omitempty"` // optional: per-user dashboard
Title string `gorm:"size:255;not null" json:"title"`
Description string `gorm:"type:text" json:"description,omitempty"`
Icon string `gorm:"size:255" json:"icon,omitempty"` // icon URL or icon name
URL string `gorm:"size:512" json:"url,omitempty"` // primary iframe URL
Kind string `gorm:"size:100;default:'frame'" json:"kind"` // frame, link
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
AccountID uint `gorm:"not null;index" json:"account_id"`
UserID *uint `gorm:"index" json:"user_id,omitempty"` // optional: per-user dashboard
Title string `gorm:"size:255;not null" json:"title"`
Description string `gorm:"type:text" json:"description,omitempty"`
Icon string `gorm:"size:255" json:"icon,omitempty"` // icon URL or icon name
URL string `gorm:"size:512" json:"url,omitempty"` // primary iframe URL
Kind string `gorm:"size:100;default:'frame'" json:"kind"` // frame, link
Content json.RawMessage `gorm:"type:json;serializer:json;default:'[]'" json:"content"` // iframe config array
Active *bool `gorm:"default:true;not null" json:"active"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
Active *bool `gorm:"default:true;not null" json:"active"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
// Relations
Account *Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
@@ -52,9 +54,9 @@ func BoolPtr(b bool) *bool { return &b }
// This is an in-memory structure used for widget CRUD — not a separate DB table.
// Widgets are stored as elements in the DashboardApp.Content jsonb array.
type DashboardWidget struct {
Type string `json:"type"` // must be "frame"
URL string `json:"url"` // must be http/https URI
ID string `json:"id,omitempty"` // optional client-assigned widget ID
Type string `json:"type"` // must be "frame"
URL string `json:"url"` // must be http/https URI
ID string `json:"id,omitempty"` // optional client-assigned widget ID
Name string `json:"name,omitempty"` // optional widget display name
}
@@ -65,19 +67,22 @@ type DashboardWidget struct {
// - each element must have type="frame" and url (http/https URI)
func ValidateContent(content json.RawMessage) error {
if len(content) == 0 || string(content) == "" {
return nil // empty content is OK (will be stored as [])
return fmt.Errorf("content must be a JSON array")
}
var widgets []DashboardWidget
if err := json.Unmarshal(content, &widgets); err != nil {
return fmt.Errorf("content must be a JSON array: %w", err)
}
if len(widgets) == 0 {
return fmt.Errorf("content must contain at least one widget")
}
for i, w := range widgets {
if w.Type != "" && w.Type != "frame" {
if w.Type != "frame" {
return fmt.Errorf("widget[%d].type must be 'frame', got '%s'", i, w.Type)
}
if w.URL != "" && !IsValidHTTPURL(w.URL) {
if strings.TrimSpace(w.URL) == "" || !IsValidHTTPURL(w.URL) {
return fmt.Errorf("widget[%d].url must be http/https URI, got '%s'", i, w.URL)
}
}
@@ -87,4 +92,4 @@ func ValidateContent(content json.RawMessage) error {
// IsValidHTTPURL checks if a URL uses http or https scheme.
func IsValidHTTPURL(u string) bool {
return (len(u) >= 7 && u[:7] == "http://") || (len(u) >= 8 && u[:8] == "https://")
}
}