91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/campaign"
|
|
)
|
|
|
|
// 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 {
|
|
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) {
|
|
var c campaign.Campaign
|
|
if err := r.db.WithContext(ctx).
|
|
Where("id = ? AND account_id = ?", id, accountID).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
|
|
}
|
|
|
|
err := r.db.WithContext(ctx).
|
|
Where("account_id = ?", accountID).
|
|
Order("created_at DESC").Offset(offset).Limit(limit).
|
|
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
|
|
}
|