55 lines
2.6 KiB
Go
55 lines
2.6 KiB
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// PlatformApp represents a platform-level application (partner API / agent bot / integration)
|
|
// that authenticates via AccessToken and operates within specific accounts.
|
|
// Reference: Chatwoot app/models/platform_app.rb + app/models/agent_bot.rb + P2B M12 spec
|
|
//
|
|
// PlatformApps are used for:
|
|
// - API-based integrations (webhooks, CRM sync, etc.)
|
|
// - Agent bots that automate conversation handling
|
|
// - Third-party applications that need API access to GoChat
|
|
//
|
|
// Authentication: PlatformApps use AccessToken authentication (AccessTokenable concern).
|
|
// The token is auto-created on PlatformApp creation and rotated via regenerate endpoints.
|
|
// Corresponds to Chatwoot's AccessTokenable concern for agent bots / platform apps.
|
|
|
|
// PlatformApp represents a platform application with AccessToken authentication.
|
|
type PlatformApp struct {
|
|
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
|
Name string `gorm:"size:255;not null" json:"name"`
|
|
Description string `gorm:"type:text" json:"description,omitempty"`
|
|
Icon string `gorm:"size:255" json:"icon,omitempty"`
|
|
URL string `gorm:"size:512" json:"url,omitempty"`
|
|
Config json.RawMessage `gorm:"type:jsonb;default:'{}';serializer:json" json:"config,omitempty"`
|
|
Active *bool `gorm:"default:true;not null" json:"active"`
|
|
AccountID *uint `gorm:"index" json:"account_id,omitempty"` // nil = platform-level (no account restriction)
|
|
Type string `gorm:"size:50;default:'api'" json:"type"` // api, agent_bot, integration
|
|
Status string `gorm:"size:50;default:'active'" json:"status"` // active, disabled
|
|
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"`
|
|
Permissibles []Permissible `gorm:"foreignKey:PlatformAppID" json:"permissibles,omitempty"`
|
|
AccessToken *AccessToken `gorm:"foreignKey:OwnerID;conditions:owner_type='PlatformApp'" json:"access_token,omitempty"`
|
|
}
|
|
|
|
func (PlatformApp) TableName() string { return "platform_apps" }
|
|
|
|
// IsPlatformLevel returns true if this app is not restricted to any account (AccountID is nil).
|
|
func (a *PlatformApp) IsPlatformLevel() bool {
|
|
return a.AccountID == nil
|
|
}
|
|
|
|
// IsActive returns true if the app is both Active and Status=active.
|
|
func (a *PlatformApp) IsActive() bool {
|
|
return a.Active != nil && *a.Active && a.Status == "active"
|
|
} |