- Removed detailed event routes from the API. - Added new report request routes for creating and fetching report requests. - Introduced new domain models for report requests, including metadata and status handling. - Implemented report request processing logic, including CSV generation for event interval reports. - Enhanced company statistics handling with new domain models and service methods. - Updated repository interfaces and implementations to support new report functionalities. - Added error handling and logging for report file operations and notifications.
92 lines
2.5 KiB
Go
92 lines
2.5 KiB
Go
package report
|
|
|
|
import (
|
|
"context"
|
|
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidInterval = errors.New("invalid interval provided")
|
|
)
|
|
|
|
func (s *Service) GenerateEventIntervalReport(ctx context.Context, request domain.ReportRequestDetail) (string, error) {
|
|
if request.Metadata.Interval == nil {
|
|
s.mongoLogger.Error("[GenerateEventIntervalReport] Metadata interval is empty")
|
|
return "", ErrInvalidInterval
|
|
}
|
|
|
|
interval, err := domain.ParseDateInterval(*request.Metadata.Interval)
|
|
if err != nil {
|
|
s.mongoLogger.Error("[GenerateEventIntervalReport] Failed to parse date interval",
|
|
zap.String("interval", *request.Metadata.Interval),
|
|
zap.Error(err),
|
|
)
|
|
return "", ErrInvalidInterval
|
|
}
|
|
|
|
stats, err := s.eventSvc.GetTotalEventStatsByInterval(ctx, domain.EventStatsByIntervalFilter{
|
|
Interval: domain.ValidDateInterval{
|
|
Value: interval,
|
|
Valid: true,
|
|
},
|
|
})
|
|
if err != nil {
|
|
s.mongoLogger.Error("[GenerateEventIntervalReport] Failed to fetch event stats",
|
|
zap.String("interval", string(interval)),
|
|
zap.Error(err),
|
|
)
|
|
return "", fmt.Errorf("fetching event stats: %w", err)
|
|
}
|
|
|
|
rows := [][]string{{
|
|
"Period", "Total Events", "Active Events", "In-Active Events", "Featured Events", "Leagues",
|
|
"Pending", "In-Play", "To-Be-Fixed", "Ended", "Postponed", "Cancelled", "Walkover",
|
|
"Interrupted", "Abandoned", "Retired", "Suspended", "Decided-By-FA", "Removed",
|
|
}}
|
|
|
|
for _, stat := range stats {
|
|
endDate, err := domain.GetEndDateFromInterval(interval, stat.Date)
|
|
if err != nil {
|
|
s.mongoLogger.Error("[GenerateEventIntervalReport] Failed to get end date from interval",
|
|
zap.String("interval", string(interval)),
|
|
zap.Error(err),
|
|
)
|
|
return "", fmt.Errorf("invalid interval end date: %w", err)
|
|
}
|
|
|
|
period := fmt.Sprintf("%s to %s",
|
|
stat.Date.Format("2006-01-02"),
|
|
endDate.Format("2006-01-02"),
|
|
)
|
|
|
|
rows = append(rows, []string{
|
|
period,
|
|
fmt.Sprint(stat.EventCount),
|
|
fmt.Sprint(stat.TotalActiveEvents),
|
|
fmt.Sprint(stat.TotalInActiveEvents),
|
|
fmt.Sprint(stat.TotalFeaturedEvents),
|
|
fmt.Sprint(stat.TotalLeagues),
|
|
fmt.Sprint(stat.Pending),
|
|
fmt.Sprint(stat.InPlay),
|
|
fmt.Sprint(stat.ToBeFixed),
|
|
fmt.Sprint(stat.Ended),
|
|
fmt.Sprint(stat.Postponed),
|
|
fmt.Sprint(stat.Cancelled),
|
|
fmt.Sprint(stat.Walkover),
|
|
fmt.Sprint(stat.Interrupted),
|
|
fmt.Sprint(stat.Abandoned),
|
|
fmt.Sprint(stat.Retired),
|
|
fmt.Sprint(stat.Suspended),
|
|
fmt.Sprint(stat.DecidedByFa),
|
|
fmt.Sprint(stat.Removed),
|
|
})
|
|
}
|
|
|
|
return s.WriteCSV(rows, "event_interval")
|
|
}
|