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
|
|
}
|