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.
97 lines
2.9 KiB
Go
97 lines
2.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
applogger "github.com/gochat/gochat/pkg/logger"
|
|
)
|
|
|
|
// ReportingEventService implements business logic for ReportingEvent operations.
|
|
// Reference: Chatwoot app/controllers/api/v1/accounts/reporting_events_controller.rb
|
|
type ReportingEventService struct {
|
|
repo *repository.ReportingEventRepo
|
|
}
|
|
|
|
type ReportingEventListFilter struct {
|
|
Since *time.Time
|
|
Until *time.Time
|
|
InboxID uint
|
|
UserID uint
|
|
Name string
|
|
Page int
|
|
PerPage int
|
|
}
|
|
|
|
type ReportingEventListResult struct {
|
|
Events []model.ReportingEvent
|
|
Total int64
|
|
CurrentPage int
|
|
TotalPages int
|
|
}
|
|
|
|
// NewReportingEventService creates a new ReportingEvent service.
|
|
func NewReportingEventService(repo *repository.ReportingEventRepo) *ReportingEventService {
|
|
return &ReportingEventService{repo: repo}
|
|
}
|
|
|
|
// ListByAccount retrieves reporting events for an account within a date range.
|
|
func (s *ReportingEventService) ListByAccount(ctx context.Context, accountID uint, since, until time.Time) ([]model.ReportingEvent, error) {
|
|
events, err := s.repo.FindByAccountID(ctx, accountID, since, until)
|
|
if err != nil {
|
|
applogger.L().Errorf("ReportingEventService.ListByAccount error: %v", err)
|
|
return nil, err
|
|
}
|
|
return events, nil
|
|
}
|
|
|
|
// GetByMetric retrieves reporting events filtered by a specific metric/event type.
|
|
func (s *ReportingEventService) GetByMetric(ctx context.Context, accountID uint, metricName string, since, until time.Time) ([]model.ReportingEvent, error) {
|
|
events, err := s.repo.FindByMetric(ctx, accountID, metricName, since, until)
|
|
if err != nil {
|
|
applogger.L().Errorf("ReportingEventService.GetByMetric error: %v", err)
|
|
return nil, err
|
|
}
|
|
return events, nil
|
|
}
|
|
|
|
func (s *ReportingEventService) ListAccountEvents(ctx context.Context, accountID uint, filter ReportingEventListFilter) (*ReportingEventListResult, error) {
|
|
if filter.Page < 1 {
|
|
filter.Page = 1
|
|
}
|
|
if filter.PerPage <= 0 {
|
|
filter.PerPage = 25
|
|
}
|
|
|
|
events, total, err := s.repo.FindByAccountFiltered(ctx, accountID, repository.ReportingEventListFilter{
|
|
Since: filter.Since,
|
|
Until: filter.Until,
|
|
InboxID: filter.InboxID,
|
|
UserID: filter.UserID,
|
|
Name: filter.Name,
|
|
Offset: (filter.Page - 1) * filter.PerPage,
|
|
Limit: filter.PerPage,
|
|
})
|
|
if err != nil {
|
|
applogger.L().Errorf("ReportingEventService.ListAccountEvents error: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
totalPages := 0
|
|
if total > 0 {
|
|
totalPages = int((total + int64(filter.PerPage) - 1) / int64(filter.PerPage))
|
|
}
|
|
return &ReportingEventListResult{Events: events, Total: total, CurrentPage: filter.Page, TotalPages: totalPages}, nil
|
|
}
|
|
|
|
// Create creates a new reporting event.
|
|
func (s *ReportingEventService) Create(ctx context.Context, event *model.ReportingEvent) error {
|
|
if err := s.repo.Create(ctx, event); err != nil {
|
|
applogger.L().Errorf("ReportingEventService.Create error: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|