Skip to content

Commit

Permalink
Add GetRewardsTotalSum func
Browse files Browse the repository at this point in the history
  • Loading branch information
kacpersaw committed Jul 16, 2024
1 parent eee370f commit 3788491
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions storage/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,26 @@ func (s *Storage) SaveReward(parent context.Context, in *model.Reward) error {
}
return err
}

func (s *Storage) GetRewardsTotalSum(ctx context.Context) (int64, error) {
groupStage := bson.D{
{Key: "$group", Value: bson.D{
{Key: "_id", Value: ""},
{Key: "totalRewards", Value: bson.D{
{Key: "$sum", Value: "$total"},
},
},
}},
}
cursor, err := s.db.Collection("rewards").Aggregate(ctx, mongo.Pipeline{groupStage})
if err != nil {
return 0, err
}

if !cursor.Next(ctx) {
log.Info("GetSmesherRewards: Empty result")
return 0, nil
}
doc := cursor.Current
return utils.GetAsInt64(doc.Lookup("totalRewards")), nil
}

0 comments on commit 3788491

Please sign in to comment.