add check on nil time req
This commit is contained in:
parent
e5f4768a51
commit
6ca31c1a43
@ -79,8 +79,9 @@ func (r *StatsRepository) GetStatistics(ctx context.Context, promoCodeID string)
|
|||||||
return promoCodeStats, nil
|
return promoCodeStats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *StatsRepository) GetAllPromoActivations(ctx context.Context) (*codeword_rpc.PromoActivationResp, error) {
|
func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) {
|
||||||
pipeline := []bson.M{
|
var pipeline []bson.M
|
||||||
|
pipeline = append(pipeline, []bson.M{
|
||||||
{
|
{
|
||||||
"$project": bson.M{
|
"$project": bson.M{
|
||||||
"_id": 1,
|
"_id": 1,
|
||||||
@ -93,17 +94,29 @@ func (r *StatsRepository) GetAllPromoActivations(ctx context.Context) (*codeword
|
|||||||
{
|
{
|
||||||
"$unwind": "$usageArray.v",
|
"$unwind": "$usageArray.v",
|
||||||
},
|
},
|
||||||
{
|
}...)
|
||||||
"$group": bson.M{
|
|
||||||
"_id": "$_id",
|
if req.To != 0 && req.From != 0 {
|
||||||
"users": bson.M{"$push": bson.M{
|
pipeline = append(pipeline, bson.M{
|
||||||
"UserID": "$usageArray.v.userID",
|
"$match": bson.M{
|
||||||
"Time": "$usageArray.v.time",
|
"usageArray.v.time": bson.M{
|
||||||
}},
|
"$gte": req.From,
|
||||||
|
"$lte": req.To,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pipeline = append(pipeline, bson.M{
|
||||||
|
"$group": bson.M{
|
||||||
|
"_id": "$_id",
|
||||||
|
"users": bson.M{"$push": bson.M{
|
||||||
|
"UserID": "$usageArray.v.userID",
|
||||||
|
"Time": "$usageArray.v.time",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
cursor, err := r.mdb.Aggregate(ctx, pipeline)
|
cursor, err := r.mdb.Aggregate(ctx, pipeline)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"google.golang.org/protobuf/types/known/emptypb"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,7 +29,7 @@ type PromoCodeRepository interface {
|
|||||||
type PromoStatsRepository interface {
|
type PromoStatsRepository interface {
|
||||||
UpdateStatistics(ctx context.Context, req *models.ActivateReq, promoCode *models.PromoCode, userID string) error
|
UpdateStatistics(ctx context.Context, req *models.ActivateReq, promoCode *models.PromoCode, userID string) error
|
||||||
GetStatistics(ctx context.Context, promoCodeID string) (models.PromoCodeStats, error)
|
GetStatistics(ctx context.Context, promoCodeID string) (models.PromoCodeStats, error)
|
||||||
GetAllPromoActivations(ctx context.Context) (*codeword_rpc.PromoActivationResp, error)
|
GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type PromoDeps struct {
|
type PromoDeps struct {
|
||||||
@ -273,8 +272,8 @@ func (s *PromoCodeService) GetStats(ctx context.Context, req models.PromoStatReq
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PromoCodeService) GetAllPromoActivations(ctx context.Context, _ *emptypb.Empty) (*codeword_rpc.PromoActivationResp, error) {
|
func (s *PromoCodeService) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) {
|
||||||
result, err := s.statsRepo.GetAllPromoActivations(ctx)
|
result, err := s.statsRepo.GetAllPromoActivations(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Error("error getting all promo activations data", zap.Error(err))
|
s.logger.Error("error getting all promo activations data", zap.Error(err))
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user