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 }