diff --git a/internal/repository/promocode_stats.go b/internal/repository/promocode_stats.go index f05f6e7..b438d45 100644 --- a/internal/repository/promocode_stats.go +++ b/internal/repository/promocode_stats.go @@ -79,7 +79,7 @@ func (r *StatsRepository) GetStatistics(ctx context.Context, promoCodeID string) return promoCodeStats, nil } -func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) { +func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time, stream codeword_rpc.PromoCodeService_GetAllPromoActivationsServer) error { var pipeline []bson.M pipeline = append(pipeline, bson.M{ "$project": bson.M{ @@ -116,10 +116,10 @@ func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codew cursor, err := r.mdb.Aggregate(ctx, pipeline) if err != nil { - return nil, err + + return err } - result := make(map[string]*codeword_rpc.PromoActivationResp_Activations) for cursor.Next(ctx) { var data struct { ID string `bson:"_id"` @@ -130,22 +130,24 @@ func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codew } err := cursor.Decode(&data) if err != nil { - return nil, err + return err } - if _, ok := result[data.ID]; !ok { - result[data.ID] = &codeword_rpc.PromoActivationResp_Activations{} + resp := &codeword_rpc.PromoActivationResp{ + ID: data.ID, } for _, user := range data.Users { - result[data.ID].Values = append(result[data.ID].Values, &codeword_rpc.PromoActivationResp_UserTime{ + resp.Users = append(resp.Users, &codeword_rpc.PromoActivationResp_UserTime{ UserID: user.UserID, Time: user.Time, }) } + + if err := stream.Send(resp); err != nil { + return err + } } - return &codeword_rpc.PromoActivationResp{ - Response: result, - }, nil + return nil } diff --git a/internal/services/promocode_service.go b/internal/services/promocode_service.go index fdc8c24..7e3d621 100644 --- a/internal/services/promocode_service.go +++ b/internal/services/promocode_service.go @@ -29,7 +29,7 @@ type PromoCodeRepository interface { type PromoStatsRepository interface { UpdateStatistics(ctx context.Context, req *models.ActivateReq, promoCode *models.PromoCode, userID string) error GetStatistics(ctx context.Context, promoCodeID string) (models.PromoCodeStats, error) - GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) + GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time, stream codeword_rpc.PromoCodeService_GetAllPromoActivationsServer) error } type PromoDeps struct { @@ -272,12 +272,12 @@ func (s *PromoCodeService) GetStats(ctx context.Context, req models.PromoStatReq return resp, nil } -func (s *PromoCodeService) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) { - result, err := s.statsRepo.GetAllPromoActivations(ctx, req) +func (s *PromoCodeService) GetAllPromoActivations(req *codeword_rpc.Time, stream codeword_rpc.PromoCodeService_GetAllPromoActivationsServer) error { + err := s.statsRepo.GetAllPromoActivations(stream.Context(), req, stream) if err != nil { s.logger.Error("error getting all promo activations data", zap.Error(err)) - return nil, err + return err } - return result, nil + return nil }