Files
gochat/internal/repository/integration_hook_repo.go
T

151 lines
5.1 KiB
Go

package repository
import (
"context"
"gorm.io/gorm"
"github.com/gochat/gochat/internal/model"
)
// IntegrationHookRepo implements GORM repository for IntegrationHook.
// Reference: Chatwoot Integrations::Hook model + HooksController
type IntegrationHookRepo struct {
db *gorm.DB
}
// DB returns the underlying gorm.DB for services that need adjacent models.
func (r *IntegrationHookRepo) DB() *gorm.DB {
return r.db
}
// NewIntegrationHookRepo creates a new IntegrationHook repository.
func NewIntegrationHookRepo(db *gorm.DB) *IntegrationHookRepo {
return &IntegrationHookRepo{db: db}
}
// GetByID retrieves an integration hook by primary key.
func (r *IntegrationHookRepo) GetByID(ctx context.Context, id uint) (*model.IntegrationHook, error) {
var hook model.IntegrationHook
if err := r.db.WithContext(ctx).Preload("Inbox").First(&hook, id).Error; err != nil {
return nil, err
}
return &hook, nil
}
// GetByAccountAndID retrieves an integration hook scoped to an account.
func (r *IntegrationHookRepo) GetByAccountAndID(ctx context.Context, accountID, id uint) (*model.IntegrationHook, error) {
var hook model.IntegrationHook
if err := r.db.WithContext(ctx).
Preload("Inbox").
Where("account_id = ?", accountID).
First(&hook, id).Error; err != nil {
return nil, err
}
return &hook, nil
}
// FindByAccount retrieves all integration hooks for an account with pagination.
func (r *IntegrationHookRepo) FindByAccount(ctx context.Context, accountID uint, offset, limit int) ([]model.IntegrationHook, int64, error) {
var hooks []model.IntegrationHook
var total int64
q := r.db.WithContext(ctx).Where("account_id = ?", accountID)
if err := q.Model(&model.IntegrationHook{}).Count(&total).Error; err != nil {
return nil, 0, err
}
err := q.Preload("Inbox").Offset(offset).Limit(limit).Order("id ASC").Find(&hooks).Error
return hooks, total, err
}
// FindByAccountAndApp retrieves integration hooks for a Chatwoot app id.
func (r *IntegrationHookRepo) FindByAccountAndApp(ctx context.Context, accountID uint, appID string) ([]model.IntegrationHook, error) {
var hooks []model.IntegrationHook
err := r.db.WithContext(ctx).
Preload("Inbox").
Where("account_id = ?", accountID).
Where("app_id = ? OR (app_id = '' AND hook_type = ?)", appID, appID).
Order("id ASC").
Find(&hooks).Error
return hooks, err
}
// FindByAccountAndType retrieves integration hooks for an account filtered by hook type.
func (r *IntegrationHookRepo) FindByAccountAndType(ctx context.Context, accountID uint, hookType model.HookType) ([]model.IntegrationHook, error) {
var hooks []model.IntegrationHook
err := r.db.WithContext(ctx).
Where("account_id = ? AND hook_type = ?", accountID, hookType).
Order("id ASC").
Find(&hooks).Error
return hooks, err
}
// FindByInbox retrieves integration hooks for a specific inbox.
func (r *IntegrationHookRepo) FindByInbox(ctx context.Context, accountID uint, inboxID uint) ([]model.IntegrationHook, error) {
var hooks []model.IntegrationHook
err := r.db.WithContext(ctx).
Where("account_id = ? AND inbox_id = ?", accountID, inboxID).
Order("id ASC").
Find(&hooks).Error
return hooks, err
}
// Create creates a new integration hook.
func (r *IntegrationHookRepo) Create(ctx context.Context, hook *model.IntegrationHook) error {
return r.db.WithContext(ctx).Create(hook).Error
}
// Update updates an existing integration hook.
func (r *IntegrationHookRepo) Update(ctx context.Context, hook *model.IntegrationHook) error {
return r.db.WithContext(ctx).Save(hook).Error
}
// Delete deletes an integration hook by ID (soft delete via gorm.DeletedAt).
func (r *IntegrationHookRepo) Delete(ctx context.Context, id uint) error {
return r.db.WithContext(ctx).Delete(&model.IntegrationHook{}, id).Error
}
// IntegrationAppRepo implements GORM repository for IntegrationApp.
type IntegrationAppRepo struct {
db *gorm.DB
}
// NewIntegrationAppRepo creates a new IntegrationApp repository.
func NewIntegrationAppRepo(db *gorm.DB) *IntegrationAppRepo {
return &IntegrationAppRepo{db: db}
}
// List retrieves all enabled integration apps.
func (r *IntegrationAppRepo) List(ctx context.Context) ([]model.IntegrationApp, error) {
var apps []model.IntegrationApp
err := r.db.WithContext(ctx).
Where("enabled = ?", true).
Order("name ASC").
Find(&apps).Error
return apps, err
}
// GetByID retrieves an integration app by ID.
func (r *IntegrationAppRepo) GetByID(ctx context.Context, id uint) (*model.IntegrationApp, error) {
var app model.IntegrationApp
if err := r.db.WithContext(ctx).First(&app, id).Error; err != nil {
return nil, err
}
return &app, nil
}
// GetByHookType retrieves an integration app by hook type.
func (r *IntegrationAppRepo) GetByHookType(ctx context.Context, hookType model.HookType) (*model.IntegrationApp, error) {
var app model.IntegrationApp
if err := r.db.WithContext(ctx).Where("hook_type = ?", hookType).First(&app).Error; err != nil {
return nil, err
}
return &app, nil
}
// GetByAppID retrieves an integration app by Chatwoot app id.
func (r *IntegrationAppRepo) GetByAppID(ctx context.Context, appID string) (*model.IntegrationApp, error) {
return r.GetByHookType(ctx, model.HookType(appID))
}