Files
gochat/backend/internal/model/account_user.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

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:"column:inviter_id;default:0" json:"inviter_id"` // Chatwoot: inviter_id 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"
}