82 lines
2.8 KiB
Go
82 lines
2.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// AgentBotInboxRepo implements GORM repository for AgentBotInbox.
|
|
// Reference: Chatwoot app/models/agent_bot_inbox.rb
|
|
type AgentBotInboxRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewAgentBotInboxRepo creates a new AgentBotInbox repository.
|
|
func NewAgentBotInboxRepo(db *gorm.DB) *AgentBotInboxRepo {
|
|
return &AgentBotInboxRepo{db: db}
|
|
}
|
|
|
|
// FindByID retrieves an agent_bot_inbox by primary key.
|
|
func (r *AgentBotInboxRepo) FindByID(ctx context.Context, id uint) (*model.AgentBotInbox, error) {
|
|
var abi model.AgentBotInbox
|
|
err := r.db.WithContext(ctx).First(&abi, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &abi, nil
|
|
}
|
|
|
|
// FindByAgentBotID retrieves all agent_bot_inboxes for a given agent_bot.
|
|
func (r *AgentBotInboxRepo) FindByAgentBotID(ctx context.Context, agentBotID uint) ([]model.AgentBotInbox, error) {
|
|
var abis []model.AgentBotInbox
|
|
err := r.db.WithContext(ctx).Where("agent_bot_id = ?", agentBotID).
|
|
Order("id ASC").Find(&abis).Error
|
|
return abis, err
|
|
}
|
|
|
|
// FindByInboxID retrieves all agent_bot_inboxes for a given inbox.
|
|
func (r *AgentBotInboxRepo) FindByInboxID(ctx context.Context, inboxID uint) ([]model.AgentBotInbox, error) {
|
|
var abis []model.AgentBotInbox
|
|
err := r.db.WithContext(ctx).Where("inbox_id = ?", inboxID).
|
|
Order("id ASC").Find(&abis).Error
|
|
return abis, err
|
|
}
|
|
|
|
// FindActiveByInboxID retrieves only active agent_bot_inboxes for a given inbox (status=0).
|
|
func (r *AgentBotInboxRepo) FindActiveByInboxID(ctx context.Context, inboxID uint) ([]model.AgentBotInbox, error) {
|
|
var abis []model.AgentBotInbox
|
|
err := r.db.WithContext(ctx).
|
|
Where("inbox_id = ? AND status = ?", inboxID, model.AgentBotInboxActive).
|
|
Order("id ASC").Find(&abis).Error
|
|
return abis, err
|
|
}
|
|
|
|
// FindByAgentBotAndInbox retrieves a single binding by agent_bot_id + inbox_id.
|
|
func (r *AgentBotInboxRepo) FindByAgentBotAndInbox(ctx context.Context, agentBotID, inboxID uint) (*model.AgentBotInbox, error) {
|
|
var abi model.AgentBotInbox
|
|
err := r.db.WithContext(ctx).
|
|
Where("agent_bot_id = ? AND inbox_id = ?", agentBotID, inboxID).
|
|
First(&abi).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &abi, nil
|
|
}
|
|
|
|
// Create persists a new AgentBotInbox record.
|
|
func (r *AgentBotInboxRepo) Create(ctx context.Context, abi *model.AgentBotInbox) error {
|
|
return r.db.WithContext(ctx).Create(abi).Error
|
|
}
|
|
|
|
// Update persists changes to an existing AgentBotInbox record.
|
|
func (r *AgentBotInboxRepo) Update(ctx context.Context, abi *model.AgentBotInbox) error {
|
|
return r.db.WithContext(ctx).Save(abi).Error
|
|
}
|
|
|
|
// Delete removes an AgentBotInbox record by primary key.
|
|
func (r *AgentBotInboxRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.AgentBotInbox{}, id).Error
|
|
} |