feat(profile): align chatwoot user serializer

This commit is contained in:
2026-06-05 01:07:40 +08:00
parent 6757e17c82
commit 56a8c5b806
9 changed files with 483 additions and 105 deletions
+11 -10
View File
@@ -3,15 +3,16 @@ package model
// Account represents a tenant/organization in the system.
type Account struct {
Base
Name string `gorm:"size:255;not null" json:"name"`
Domain string `gorm:"size:255" json:"domain,omitempty"`
Locale string `gorm:"size:10;default:en" json:"locale"`
Timezone string `gorm:"size:50;default:UTC" json:"timezone"`
Active bool `gorm:"default:true" json:"active"`
Status string `gorm:"size:50;default:active" json:"status"`
FeatureFlags string `gorm:"type:text" json:"feature_flags,omitempty"` // JSON-encoded feature flags
AutoResolveDuration int `gorm:"default:0" json:"auto_resolve_duration,omitempty"` // days
AgentLimit int `gorm:"default:0" json:"agent_limit,omitempty"` // max agents allowed (0 = unlimited), Chatwoot usage_limits[:agents]
Name string `gorm:"size:255;not null" json:"name"`
Domain string `gorm:"size:255" json:"domain,omitempty"`
Locale string `gorm:"size:10;default:en" json:"locale"`
Timezone string `gorm:"size:50;default:UTC" json:"timezone"`
Active bool `gorm:"default:true" json:"active"`
Status string `gorm:"size:50;default:active" json:"status"`
OnboardingStep string `gorm:"size:100" json:"onboarding_step,omitempty"`
FeatureFlags string `gorm:"type:text" json:"feature_flags,omitempty"` // JSON-encoded feature flags
AutoResolveDuration int `gorm:"default:0" json:"auto_resolve_duration,omitempty"` // days
AgentLimit int `gorm:"default:0" json:"agent_limit,omitempty"` // max agents allowed (0 = unlimited), Chatwoot usage_limits[:agents]
}
func (Account) TableName() string { return "accounts" }
func (Account) TableName() string { return "accounts" }
+53 -19
View File
@@ -1,30 +1,64 @@
package model
import (
"crypto/rand"
"encoding/hex"
"time"
"gorm.io/datatypes"
"gorm.io/gorm"
)
// User represents an agent/admin user in the system.
type User struct {
Base
AccountID uint `gorm:"index;not null" json:"account_id"`
Name string `gorm:"size:255;not null" json:"name"`
Email string `gorm:"size:255;uniqueIndex;not null" json:"email"`
Password string `gorm:"size:255;not null" json:"-"` // hashed password (bcrypt)
PasswordDigest string `gorm:"size:255" json:"-"` // alias used by auth service
Provider string `gorm:"size:50;default:email" json:"provider"` // email, google, saml
UID string `gorm:"size:255" json:"uid,omitempty"` // external ID for OAuth providers
AvatarURL string `gorm:"size:512" json:"avatar_url"`
Role string `gorm:"size:50;default:agent" json:"role"` // agent, admin
Active bool `gorm:"default:true" json:"active"`
Available bool `gorm:"default:false" json:"available"`
TOTPSecret string `gorm:"size:255" json:"totp_secret,omitempty"`
TOTPEnabled bool `gorm:"default:false" json:"totp_enabled"`
CustomRoleID *uint `gorm:"index" json:"custom_role_id,omitempty"`
SignInCount int `gorm:"default:0" json:"sign_in_count"`
ConfirmedAt *time.Time `json:"confirmed_at,omitempty"`
LastSignInAt *time.Time `json:"last_sign_in_at,omitempty"`
CurrentSignInAt *time.Time `json:"current_sign_in_at,omitempty"`
AccountID uint `gorm:"index;not null" json:"account_id"`
Name string `gorm:"size:255;not null" json:"name"`
Email string `gorm:"size:255;uniqueIndex;not null" json:"email"`
Password string `gorm:"size:255;not null" json:"-"` // hashed password (bcrypt)
PasswordDigest string `gorm:"size:255" json:"-"` // alias used by auth service
Provider string `gorm:"size:50;default:email" json:"provider"` // email, google, saml
UID string `gorm:"size:255" json:"uid,omitempty"` // external ID for OAuth providers
AvatarURL string `gorm:"size:512" json:"avatar_url"`
DisplayName string `gorm:"size:255" json:"display_name"`
MessageSignature string `gorm:"type:text" json:"message_signature"`
PubsubToken string `gorm:"size:255;uniqueIndex" json:"pubsub_token"`
UISettings datatypes.JSON `gorm:"type:jsonb;default:'{}'" json:"ui_settings"`
CustomAttributes datatypes.JSON `gorm:"type:jsonb;default:'{}'" json:"custom_attributes,omitempty"`
Role string `gorm:"size:50;default:agent" json:"role"` // agent, admin
Type string `gorm:"size:50;default:user" json:"type"`
Active bool `gorm:"default:true" json:"active"`
Available bool `gorm:"default:false" json:"available"`
TOTPSecret string `gorm:"size:255" json:"totp_secret,omitempty"`
TOTPEnabled bool `gorm:"default:false" json:"totp_enabled"`
CustomRoleID *uint `gorm:"index" json:"custom_role_id,omitempty"`
SignInCount int `gorm:"default:0" json:"sign_in_count"`
ConfirmedAt *time.Time `json:"confirmed_at,omitempty"`
LastSignInAt *time.Time `json:"last_sign_in_at,omitempty"`
CurrentSignInAt *time.Time `json:"current_sign_in_at,omitempty"`
}
func (User) TableName() string { return "users" }
func (User) TableName() string { return "users" }
// BeforeCreate mirrors Chatwoot's Pubsubable concern by assigning every user a cable token.
func (u *User) BeforeCreate(tx *gorm.DB) error {
if u.PubsubToken == "" {
token, err := generateSecureToken(32)
if err != nil {
return err
}
u.PubsubToken = token
}
if u.Type == "" {
u.Type = "User"
}
return nil
}
func generateSecureToken(size int) (string, error) {
b := make([]byte, size)
if _, err := rand.Read(b); err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}