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.
84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// AuditRepo implements GORM repository for Audit (audit log entries).
|
|
// Reference: Chatwoot enterprise Audit model + P2B M11 spec
|
|
type AuditRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewAuditRepo creates a new Audit repository.
|
|
func NewAuditRepo(db *gorm.DB) *AuditRepo {
|
|
return &AuditRepo{db: db}
|
|
}
|
|
|
|
// FindByID retrieves an audit log entry by primary key.
|
|
func (r *AuditRepo) FindByID(ctx context.Context, id uint) (*model.Audit, error) {
|
|
var audit model.Audit
|
|
if err := r.db.WithContext(ctx).First(&audit, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &audit, nil
|
|
}
|
|
|
|
// FindByIDForAccount retrieves an audit log entry scoped to the account's
|
|
// associated audits, matching Chatwoot's Current.account.associated_audits.
|
|
func (r *AuditRepo) FindByIDForAccount(ctx context.Context, accountID uint, id uint) (*model.Audit, error) {
|
|
var audit model.Audit
|
|
if err := r.accountScopeQuery(ctx, accountID).Where("id = ?", id).First(&audit).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &audit, nil
|
|
}
|
|
|
|
// FindByAccount retrieves audit log entries for an account with optional filters.
|
|
// Returns the filtered list and total count for pagination.
|
|
func (r *AuditRepo) FindByAccount(ctx context.Context, accountID uint, actionFilter string, auditableTypeFilter string, offset int, limit int) ([]model.Audit, int64, error) {
|
|
query := r.accountScopeQuery(ctx, accountID)
|
|
|
|
if actionFilter != "" {
|
|
query = query.Where("action = ?", actionFilter)
|
|
}
|
|
if auditableTypeFilter != "" {
|
|
query = query.Where("auditable_type = ?", auditableTypeFilter)
|
|
}
|
|
|
|
var total int64
|
|
if err := query.Model(&model.Audit{}).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
var audits []model.Audit
|
|
if err := query.Order("created_at DESC").Offset(offset).Limit(limit).Find(&audits).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return audits, total, nil
|
|
}
|
|
|
|
func (r *AuditRepo) accountScopeQuery(ctx context.Context, accountID uint) *gorm.DB {
|
|
return r.db.WithContext(ctx).Where(
|
|
"account_id = ? OR (associated_type = ? AND associated_id = ?)",
|
|
accountID,
|
|
"Account",
|
|
accountID,
|
|
)
|
|
}
|
|
|
|
// Create creates a new audit log entry.
|
|
func (r *AuditRepo) Create(ctx context.Context, audit *model.Audit) error {
|
|
return r.db.WithContext(ctx).Create(audit).Error
|
|
}
|
|
|
|
// Delete deletes an audit log entry by primary key.
|
|
func (r *AuditRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.Audit{}, id).Error
|
|
}
|