Files
gochat/internal/model/account_user.go
T

60 lines
3.0 KiB
Go

package model
// AccountUser represents the association between a User and an Account.
// Reference: Chatwoot app/models/account_user.rb
// Each user can belong to multiple accounts with different roles in each.
//
// This is the core RBAC association — it determines what role a user plays
// in a specific account context. The role field drives the PolicyContext construction.
import (
"time"
"gorm.io/gorm"
)
// AccountUser represents a user's membership in an account with a specific role.
type AccountUser struct {
ID uint `gorm:"primaryKey" json:"id"`
UserID uint `gorm:"not null;uniqueIndex:idx_user_account" json:"user_id"`
AccountID uint `gorm:"not null;uniqueIndex:idx_user_account" json:"account_id"`
Role string `gorm:"size:50;not null;default:'agent'" json:"role"` // Chatwoot: agent or administrator; custom role is represented by CustomRoleID.
CustomRoleID uint `gorm:"default:0" json:"custom_role_id,omitempty"` // 0 means no custom role; >0 references CustomRole
AgentCapacityPolicyID *uint `gorm:"index" json:"agent_capacity_policy_id,omitempty"` // enterprise capacity policy assignment
Availability string `gorm:"size:50;default:'offline'" json:"availability"` // online/offline/busy
AutoOffline bool `gorm:"default:true" json:"auto_offline"` // auto-offline when idle
ActiveAt *time.Time `json:"active_at,omitempty"` // last active timestamp in this account
InvitedBy uint `gorm:"default:0" json:"invited_by"` // UserID of the user who invited this member
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
User User `gorm:"foreignKey:UserID" json:"user,omitempty"`
Account Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
CustomRole *CustomRole `gorm:"foreignKey:CustomRoleID" json:"custom_role,omitempty"`
AgentCapacityPolicy *AgentCapacityPolicy `gorm:"foreignKey:AgentCapacityPolicyID" json:"agent_capacity_policy,omitempty"`
}
func (AccountUser) TableName() string { return "account_users" }
// IsAdministrator returns true if the AccountUser has administrator role.
func (au *AccountUser) IsAdministrator() bool {
return au.Role == "administrator"
}
// IsAgent returns true if the AccountUser has agent role.
func (au *AccountUser) IsAgent() bool {
return au.Role == "agent"
}
// HasCustomRole returns true if the AccountUser has a custom enterprise role.
func (au *AccountUser) HasCustomRole() bool {
return au.CustomRoleID > 0
}
// IsOnline returns true if the agent availability is "online".
func (au *AccountUser) IsOnline() bool {
return au.Availability == "online"
}