Yimaru-BackEnd/internal/services/report/process.go
Samuel Tariku 0ffba57ec5 feat: Refactor report generation and management
- 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.
2025-10-28 00:51:52 +03:00

108 lines
2.6 KiB
Go

package report
import (
"context"
"fmt"
"time"
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
"go.uber.org/zap"
)
func (s *Service) ProcessReportRequests(ctx context.Context) error {
requests, total, err := s.GetAllReportRequests(ctx, domain.ReportRequestFilter{
Status: domain.ValidReportRequestStatus{
Value: domain.PendingReportRequest,
Valid: true,
},
})
if err != nil {
s.mongoLogger.Error("failed to get pending report requests", zap.Error(err))
return err
}
for i, req := range requests {
if err := s.processSingleReportRequest(ctx, req); err != nil {
s.mongoLogger.Error("failed to process report request",
zap.Int64("id", req.ID),
zap.Int("index", i),
zap.Int64("total", total),
zap.String("type", string(req.Type)),
zap.Error(err),
)
}
}
return nil
}
func (s *Service) processSingleReportRequest(ctx context.Context, req domain.ReportRequestDetail) error {
var (
filePath string
rejectReason string
status = domain.SuccessReportRequest
)
start := time.Now()
defer func() {
s.mongoLogger.Info("report request processed",
zap.Int64("id", req.ID),
zap.String("type", string(req.Type)),
zap.String("status", string(status)),
zap.Duration("duration", time.Since(start)),
)
}()
switch req.Type {
case domain.EventIntervalReportRequest:
if req.Metadata.Interval == nil {
status = domain.RejectReportRequest
rejectReason = "invalid interval provided"
break
}
fp, genErr := s.GenerateEventIntervalReport(ctx, req)
if genErr != nil {
status = domain.RejectReportRequest
rejectReason = fmt.Sprintf("failed to generate report: %v", genErr)
} else {
filePath = fp
}
default:
status = domain.RejectReportRequest
rejectReason = fmt.Sprintf("unsupported report type: %s", req.Type)
}
update := domain.UpdateRequestRequest{
ID: req.ID,
Status: domain.ValidReportRequestStatus{
Value: status,
Valid: true,
},
FilePath: domain.ValidString{
Value: filePath,
Valid: filePath != "",
},
RejectReason: domain.ValidString{
Value: rejectReason,
Valid: rejectReason != "",
},
}
if err := s.UpdateReportRequest(ctx, update); err != nil {
return fmt.Errorf("failed to update report request: %w", err)
}
// Prepare updated object for notification
updatedReq := req
updatedReq.FilePath = update.FilePath
updatedReq.Status = update.Status.Value
updatedReq.RejectReason = update.RejectReason
if err := s.SendReportRequestNotification(ctx, updatedReq); err != nil {
s.mongoLogger.Warn("failed to send notification", zap.Int64("id", req.ID), zap.Error(err))
}
return nil
}