50 lines
1.8 KiB
Go
50 lines
1.8 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
} |