Files
gochat/backend/internal/repository/campaign_repo.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

137 lines
4.6 KiB
Go

package repository
import (
"context"
"gorm.io/gorm"
"github.com/gochat/gochat/internal/campaign"
"github.com/gochat/gochat/internal/model"
)
// CampaignRepo implements GORM repository for Campaign.
// Reference: Chatwoot app/models/campaign.rb
type CampaignRepo struct {
db *gorm.DB
}
// NewCampaignRepo creates a new Campaign repository.
func NewCampaignRepo(db *gorm.DB) *CampaignRepo {
return &CampaignRepo{db: db}
}
// Create creates a new campaign.
func (r *CampaignRepo) Create(ctx context.Context, c *campaign.Campaign) error {
if c.DisplayID == 0 {
var next uint
if err := r.db.WithContext(ctx).Model(&campaign.Campaign{}).
Select("COALESCE(MAX(display_id), 0) + 1").Scan(&next).Error; err != nil {
return err
}
c.DisplayID = next
}
return r.db.WithContext(ctx).Create(c).Error
}
// FindByID retrieves a campaign by primary key.
func (r *CampaignRepo) FindByID(ctx context.Context, id uint) (*campaign.Campaign, error) {
var c campaign.Campaign
if err := r.db.WithContext(ctx).First(&c, id).Error; err != nil {
return nil, err
}
return &c, nil
}
// FindByIDAndAccount retrieves a campaign scoped to an account.
func (r *CampaignRepo) FindByIDAndAccount(ctx context.Context, id, accountID uint) (*campaign.Campaign, error) {
return r.FindByDisplayIDAndAccountOrID(ctx, id, accountID)
}
// FindInboxByIDAndAccount retrieves the campaign inbox scoped to the same account.
func (r *CampaignRepo) FindInboxByIDAndAccount(ctx context.Context, inboxID, accountID uint) (*model.Inbox, error) {
var inbox model.Inbox
if err := r.db.WithContext(ctx).Where("id = ? AND account_id = ?", inboxID, accountID).First(&inbox).Error; err != nil {
return nil, err
}
return &inbox, nil
}
// AccountHasUser returns whether the user belongs to the account.
func (r *CampaignRepo) AccountHasUser(ctx context.Context, accountID, userID uint) (bool, error) {
var count int64
if err := r.db.WithContext(ctx).Model(&model.AccountUser{}).
Where("account_id = ? AND user_id = ?", accountID, userID).
Count(&count).Error; err != nil {
return false, err
}
return count > 0, nil
}
// FindByDisplayIDAndAccountOrID retrieves a campaign by Chatwoot display_id, falling back to primary key for legacy callers/tests.
func (r *CampaignRepo) FindByDisplayIDAndAccountOrID(ctx context.Context, routeID, accountID uint) (*campaign.Campaign, error) {
var c campaign.Campaign
base := r.db.WithContext(ctx).Preload("Inbox").Preload("Sender")
if err := base.Where("account_id = ? AND display_id = ?", accountID, routeID).First(&c).Error; err == nil {
return &c, nil
} else if err != gorm.ErrRecordNotFound {
return nil, err
}
if err := r.db.WithContext(ctx).Preload("Inbox").Preload("Sender").Where("account_id = ? AND id = ?", accountID, routeID).First(&c).Error; err != nil {
return nil, err
}
return &c, nil
}
// ListByAccount retrieves all campaigns for an account with pagination.
func (r *CampaignRepo) ListByAccount(ctx context.Context, accountID uint, offset, limit int) ([]campaign.Campaign, int64, error) {
var campaigns []campaign.Campaign
var total int64
if err := r.db.WithContext(ctx).Model(&campaign.Campaign{}).
Where("account_id = ?", accountID).Count(&total).Error; err != nil {
return nil, 0, err
}
query := r.db.WithContext(ctx).
Preload("Inbox").Preload("Sender").
Where("account_id = ?", accountID).
Order("created_at DESC")
if offset > 0 {
query = query.Offset(offset)
}
if limit > 0 {
query = query.Limit(limit)
}
err := query.Find(&campaigns).Error
if err != nil {
return nil, 0, err
}
return campaigns, total, nil
}
// ListByInbox retrieves campaigns for a specific inbox.
func (r *CampaignRepo) ListByInbox(ctx context.Context, accountID, inboxID uint) ([]campaign.Campaign, error) {
var campaigns []campaign.Campaign
err := r.db.WithContext(ctx).
Where("account_id = ? AND inbox_id = ?", accountID, inboxID).
Order("created_at DESC").Find(&campaigns).Error
return campaigns, err
}
// Update updates a campaign.
func (r *CampaignRepo) Update(ctx context.Context, c *campaign.Campaign) error {
return r.db.WithContext(ctx).Save(c).Error
}
// UpdateFields updates specific fields on a campaign.
func (r *CampaignRepo) UpdateFields(ctx context.Context, id uint, updates map[string]interface{}) error {
return r.db.WithContext(ctx).Model(&campaign.Campaign{}).Where("id = ?", id).Updates(updates).Error
}
// Delete soft-deletes a campaign scoped to an account.
func (r *CampaignRepo) Delete(ctx context.Context, id, accountID uint) error {
return r.db.WithContext(ctx).
Where("id = ? AND account_id = ?", id, accountID).
Delete(&campaign.Campaign{}).Error
}