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.
152 lines
5.1 KiB
Go
152 lines
5.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// AgentBotRepo implements GORM repository for AgentBot.
|
|
// Reference: Chatwoot app/models/agent_bot.rb
|
|
type AgentBotRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// DB returns the underlying gorm.DB for advanced query building.
|
|
func (r *AgentBotRepo) DB() *gorm.DB {
|
|
if r == nil {
|
|
return nil
|
|
}
|
|
return r.db
|
|
}
|
|
|
|
// NewAgentBotRepo creates a new AgentBot repository.
|
|
func NewAgentBotRepo(db *gorm.DB) *AgentBotRepo {
|
|
return &AgentBotRepo{db: db}
|
|
}
|
|
|
|
// GetByID retrieves an agent_bot by primary key (alias for FindByID).
|
|
func (r *AgentBotRepo) GetByID(ctx context.Context, id uint) (*model.AgentBot, error) {
|
|
return r.FindByID(ctx, id)
|
|
}
|
|
|
|
// FindByID retrieves an agent_bot by primary key, preloading Inboxes.
|
|
func (r *AgentBotRepo) FindByID(ctx context.Context, id uint) (*model.AgentBot, error) {
|
|
var bot model.AgentBot
|
|
err := r.db.WithContext(ctx).Preload("Inboxes").First(&bot, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &bot, nil
|
|
}
|
|
|
|
// FindAccessibleByID retrieves a bot visible to the given account. Chatwoot
|
|
// allows show/index to include system bots and current-account bots.
|
|
func (r *AgentBotRepo) FindAccessibleByID(ctx context.Context, accountID uint, id uint) (*model.AgentBot, error) {
|
|
var bot model.AgentBot
|
|
err := r.db.WithContext(ctx).Preload("Inboxes").
|
|
Where("id = ? AND (account_id IS NULL OR account_id = ?)", id, accountID).
|
|
First(&bot).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &bot, nil
|
|
}
|
|
|
|
// FindByAccountAndID retrieves a bot owned by the account. Chatwoot mutation
|
|
// actions use Current.account.agent_bots and must not mutate global/cross-account bots.
|
|
func (r *AgentBotRepo) FindByAccountAndID(ctx context.Context, accountID uint, id uint) (*model.AgentBot, error) {
|
|
var bot model.AgentBot
|
|
err := r.db.WithContext(ctx).Preload("Inboxes").
|
|
Where("id = ? AND account_id = ?", id, accountID).
|
|
First(&bot).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &bot, nil
|
|
}
|
|
|
|
// FindByAccount retrieves all agent_bots for an account with pagination.
|
|
func (r *AgentBotRepo) FindByAccount(ctx context.Context, accountID uint, offset, limit int) ([]model.AgentBot, int64, error) {
|
|
var bots []model.AgentBot
|
|
var total int64
|
|
|
|
q := r.db.WithContext(ctx).Where("account_id = ?", accountID)
|
|
if err := q.Model(&model.AgentBot{}).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err := q.Offset(offset).Limit(limit).
|
|
Order("id ASC").Find(&bots).Error
|
|
return bots, total, err
|
|
}
|
|
|
|
// FindByAccountID retrieves all agent_bots for an account (alias matching service expectations).
|
|
func (r *AgentBotRepo) FindByAccountID(ctx context.Context, accountID uint) ([]model.AgentBot, error) {
|
|
bots, _, err := r.FindByAccount(ctx, accountID, 0, 1000)
|
|
return bots, err
|
|
}
|
|
|
|
// Create creates a new agent_bot.
|
|
func (r *AgentBotRepo) Create(ctx context.Context, bot *model.AgentBot) error {
|
|
return r.db.WithContext(ctx).Create(bot).Error
|
|
}
|
|
|
|
// CreateWithToken creates a new agent_bot with an access token already set.
|
|
// The token is assigned to the bot's AccessToken field before persisting.
|
|
func (r *AgentBotRepo) CreateWithToken(ctx context.Context, bot *model.AgentBot, token string) error {
|
|
bot.AccessToken = token
|
|
return r.db.WithContext(ctx).Create(bot).Error
|
|
}
|
|
|
|
// Update updates an existing agent_bot.
|
|
func (r *AgentBotRepo) Update(ctx context.Context, bot *model.AgentBot) error {
|
|
return r.db.WithContext(ctx).Save(bot).Error
|
|
}
|
|
|
|
// UpdateAccessToken updates only the access token field for an agent_bot.
|
|
func (r *AgentBotRepo) UpdateAccessToken(ctx context.Context, id uint, token string) error {
|
|
return r.db.WithContext(ctx).Model(&model.AgentBot{}).Where("id = ?", id).
|
|
Update("access_token", token).Error
|
|
}
|
|
|
|
// Delete deletes an agent_bot by ID.
|
|
func (r *AgentBotRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.AgentBot{}, id).Error
|
|
}
|
|
|
|
// DeleteByAccount deletes only an account-owned bot.
|
|
func (r *AgentBotRepo) DeleteByAccount(ctx context.Context, accountID uint, id uint) error {
|
|
return r.db.WithContext(ctx).
|
|
Where("account_id = ?", accountID).
|
|
Delete(&model.AgentBot{}, id).Error
|
|
}
|
|
|
|
// FindAccessible retrieves agent_bots accessible to an account (account_id = nil OR account_id = given).
|
|
// Reference: Chatwoot AgentBot.scope :accessible_to(account) — includes global bots + account bots.
|
|
func (r *AgentBotRepo) FindAccessible(ctx context.Context, accountID uint) ([]model.AgentBot, error) {
|
|
var bots []model.AgentBot
|
|
err := r.db.WithContext(ctx).
|
|
Where("account_id IS NULL OR account_id = ?", accountID).
|
|
Order("id ASC").
|
|
Find(&bots).Error
|
|
return bots, err
|
|
}
|
|
|
|
// FindAccessiblePaginated retrieves accessible agent_bots with pagination.
|
|
func (r *AgentBotRepo) FindAccessiblePaginated(ctx context.Context, accountID uint, offset, limit int) ([]model.AgentBot, int64, error) {
|
|
var bots []model.AgentBot
|
|
var total int64
|
|
|
|
q := r.db.WithContext(ctx).Where("account_id IS NULL OR account_id = ?", accountID)
|
|
if err := q.Model(&model.AgentBot{}).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err := q.Offset(offset).Limit(limit).
|
|
Order("id ASC").Find(&bots).Error
|
|
return bots, total, err
|
|
}
|