Skip to content

Commit

Permalink
Merge pull request #236 from Viva-con-Agua/bugfix/userlist
Browse files Browse the repository at this point in the history
Bugfix/userlist
  • Loading branch information
deinelieblings authored Feb 25, 2025
2 parents a4eb824 + f3794d1 commit 2bd9908
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
21 changes: 12 additions & 9 deletions dao/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/Viva-con-Agua/vcago/vmdb"
"github.com/Viva-con-Agua/vcago/vmod"

"go.mongodb.org/mongo-driver/bson"
)
Expand Down Expand Up @@ -35,7 +34,7 @@ func UserInsert(ctx context.Context, i *models.UserDatabase) (result *models.Use
return
}

func UsersGet(i *models.UserQuery, token *models.AccessToken) (result *[]models.ListUser, list_size int64, err error) {
func UsersGet(i *models.UserQuery, token *models.AccessToken) (result *[]models.ListUser, listSize int64, err error) {
if err = models.UsersPermission(token); err != nil {
return
}
Expand All @@ -48,14 +47,18 @@ func UsersGet(i *models.UserQuery, token *models.AccessToken) (result *[]models.
if err = UserCollection.Aggregate(ctx, pipeline, result); err != nil {
return
}

count := vmod.Count{}
var cErr error
if cErr = UserViewCollection.AggregateOne(context.Background(), models.UserPermittedPipeline(token).Match(filter).Count().Pipe, &count); cErr != nil {
log.Print(cErr)
list_size = 1
count := new([]Count)
if err = UserCollection.Aggregate(
context.Background(),
models.UserCountPipeline(filter).Pipe,
count,
); err != nil {
return
}
if len(*count) == 0 {
listSize = 0
} else {
list_size = int64(count.Total)
listSize = (*count)[0].ListSize
}
return
}
Expand Down
13 changes: 13 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,19 @@ func UserMatchEmail(email string) bson.D {
return filter.Bson()
}

func UserCountPipeline(filter bson.D) *vmdb.Pipeline {
pipe := UserPipeline(false)
pipe.Match(filter)
pipe.Append(bson.D{
{Key: "$group", Value: bson.D{
{Key: "_id", Value: nil}, {Key: "list_size", Value: bson.D{
{Key: "$sum", Value: 1},
}},
}},
})
pipe.Append(bson.D{{Key: "$project", Value: bson.D{{Key: "_id", Value: 0}}}})
return pipe
}
func (i *User) AuthToken() (r *vcago.AuthToken, err error) {
accessToken := &AccessToken{
ID: i.ID,
Expand Down

0 comments on commit 2bd9908

Please sign in to comment.