change getting all promo, now search first promo user activation

This commit is contained in:
Pavel 2024-04-26 14:54:05 +03:00
parent 6ca31c1a43
commit e45a21de2e

@ -81,38 +81,35 @@ func (r *StatsRepository) GetStatistics(ctx context.Context, promoCodeID string)
func (r *StatsRepository) GetAllPromoActivations(ctx context.Context, req *codeword_rpc.Time) (*codeword_rpc.PromoActivationResp, error) {
var pipeline []bson.M
pipeline = append(pipeline, []bson.M{
{
"$project": bson.M{
"_id": 1,
"usageArray": bson.M{"$objectToArray": "$usageMap"},
},
pipeline = append(pipeline, bson.M{
"$project": bson.M{
"_id": 1,
"usageArray": bson.M{"$objectToArray": "$usageMap"},
},
{
"$unwind": "$usageArray",
},
{
"$unwind": "$usageArray.v",
},
}...)
})
if req.To != 0 && req.From != 0 {
pipeline = append(pipeline, bson.M{
"$match": bson.M{
"usageArray.v.time": bson.M{
"$gte": req.From,
"$lte": req.To,
},
},
})
}
pipeline = append(pipeline, bson.M{
"$unwind": "$usageArray",
})
pipeline = append(pipeline, bson.M{
"$unwind": "$usageArray.v",
})
pipeline = append(pipeline, bson.M{
"$group": bson.M{
"_id": "$_id",
"_id": "$usageArray.v.userID",
"promoID": bson.M{"$first": "$_id"},
"Time": bson.M{"$first": "$usageArray.v.time"},
},
})
pipeline = append(pipeline, bson.M{
"$group": bson.M{
"_id": "$promoID",
"users": bson.M{"$push": bson.M{
"UserID": "$usageArray.v.userID",
"Time": "$usageArray.v.time",
"UserID": "$_id",
"Time": "$Time",
}},
},
})