package model import ( "encoding/json" "fmt" "time" "gorm.io/gorm" ) // DashboardApp represents a dashboard application configuration. // Reference: Chatwoot app/models/dashboard_app.rb — P2B M12 spec // // A DashboardApp is a custom dashboard widget configuration that can be // embedded as an iframe in the GoChat dashboard. Each app belongs to an // 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"}] // // Validation rules (per Chatwoot): // - content must be a JSON array (empty array allowed) // - 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 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"` // Relations Account *Account `gorm:"foreignKey:AccountID" json:"account,omitempty"` User *User `gorm:"foreignKey:UserID" json:"user,omitempty"` } func (DashboardApp) TableName() string { return "dashboard_apps" } // BoolPtr returns a pointer to the given bool value. // Useful for setting *bool fields like Active where false must be // distinguishable from "not set" (nil) to avoid GORM zero-value skipping. func BoolPtr(b bool) *bool { return &b } // DashboardWidget represents a single widget/iframe within a DashboardApp's content. // 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 Name string `json:"name,omitempty"` // optional widget display name } // ValidateContent checks that content is a valid JSON array of iframe configs. // Returns nil if valid, or an error describing what's wrong. // Per Chatwoot M12 spec: // - content must be a JSON array // - 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 []) } var widgets []DashboardWidget if err := json.Unmarshal(content, &widgets); err != nil { return fmt.Errorf("content must be a JSON array: %w", err) } for i, w := range widgets { if w.Type != "" && w.Type != "frame" { return fmt.Errorf("widget[%d].type must be 'frame', got '%s'", i, w.Type) } if w.URL != "" && !IsValidHTTPURL(w.URL) { return fmt.Errorf("widget[%d].url must be http/https URI, got '%s'", i, w.URL) } } return nil } // 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://") }