137 lines
4.6 KiB
Go
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
|
|
}
|