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.
90 lines
3.4 KiB
Go
90 lines
3.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CaptainAutoReplyRuleRepo provides data access for CaptainAutoReplyRule.
|
|
type CaptainAutoReplyRuleRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewCaptainAutoReplyRuleRepo creates a new CaptainAutoReplyRuleRepo.
|
|
func NewCaptainAutoReplyRuleRepo(db *gorm.DB) *CaptainAutoReplyRuleRepo {
|
|
return &CaptainAutoReplyRuleRepo{db: db}
|
|
}
|
|
|
|
// Create inserts a new auto-reply rule.
|
|
func (r *CaptainAutoReplyRuleRepo) Create(ctx context.Context, rule *model.CaptainAutoReplyRule) error {
|
|
return r.db.WithContext(ctx).Create(rule).Error
|
|
}
|
|
|
|
// GetByID retrieves a rule by its primary key.
|
|
func (r *CaptainAutoReplyRuleRepo) GetByID(ctx context.Context, id uint) (*model.CaptainAutoReplyRule, error) {
|
|
var rule model.CaptainAutoReplyRule
|
|
if err := r.db.WithContext(ctx).First(&rule, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &rule, nil
|
|
}
|
|
|
|
// Update saves changes to an existing rule.
|
|
func (r *CaptainAutoReplyRuleRepo) Update(ctx context.Context, rule *model.CaptainAutoReplyRule) error {
|
|
return r.db.WithContext(ctx).Save(rule).Error
|
|
}
|
|
|
|
// Delete removes a rule by ID.
|
|
func (r *CaptainAutoReplyRuleRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.CaptainAutoReplyRule{}, id).Error
|
|
}
|
|
|
|
// ListByAccount retrieves all rules for an account, ordered by priority desc.
|
|
func (r *CaptainAutoReplyRuleRepo) ListByAccount(ctx context.Context, accountID uint, offset, limit int) ([]model.CaptainAutoReplyRule, int64, error) {
|
|
var rules []model.CaptainAutoReplyRule
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.CaptainAutoReplyRule{}).Where("account_id = ?", accountID)
|
|
db.Count(&count)
|
|
if err := db.Order("priority DESC, id ASC").Offset(offset).Limit(limit).Find(&rules).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return rules, count, nil
|
|
}
|
|
|
|
// ListByAssistant retrieves all rules for a specific assistant.
|
|
func (r *CaptainAutoReplyRuleRepo) ListByAssistant(ctx context.Context, assistantID uint, offset, limit int) ([]model.CaptainAutoReplyRule, int64, error) {
|
|
var rules []model.CaptainAutoReplyRule
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.CaptainAutoReplyRule{}).Where("assistant_id = ?", assistantID)
|
|
db.Count(&count)
|
|
if err := db.Order("priority DESC, id ASC").Offset(offset).Limit(limit).Find(&rules).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return rules, count, nil
|
|
}
|
|
|
|
// FindActiveByAssistant retrieves all active rules for a specific assistant.
|
|
func (r *CaptainAutoReplyRuleRepo) FindActiveByAssistant(ctx context.Context, assistantID uint) ([]model.CaptainAutoReplyRule, error) {
|
|
var rules []model.CaptainAutoReplyRule
|
|
if err := r.db.WithContext(ctx).
|
|
Where("assistant_id = ? AND status = ?", assistantID, model.AutoReplyRuleStatusActive).
|
|
Order("priority DESC, id ASC").
|
|
Find(&rules).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return rules, nil
|
|
}
|
|
|
|
// FindActiveByInbox retrieves active rules scoped to an inbox or account-wide (inbox_id IS NULL).
|
|
func (r *CaptainAutoReplyRuleRepo) FindActiveByInbox(ctx context.Context, accountID, inboxID uint) ([]model.CaptainAutoReplyRule, error) {
|
|
var rules []model.CaptainAutoReplyRule
|
|
if err := r.db.WithContext(ctx).
|
|
Where("account_id = ? AND status = ? AND (inbox_id = ? OR inbox_id IS NULL)", accountID, model.AutoReplyRuleStatusActive, inboxID).
|
|
Order("priority DESC, id ASC").
|
|
Find(&rules).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return rules, nil
|
|
} |