110 lines
3.8 KiB
Go
110 lines
3.8 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
} |