From 042ff98509ef3a4e4b176730440a1e6bc6fc8c82 Mon Sep 17 00:00:00 2001 From: Burak Sekili Date: Mon, 18 Oct 2021 18:00:15 +0300 Subject: [PATCH] NOISSUE - Fix retrieving all users (#1477) Signed-off-by: Burak Sekili --- users/postgres/users.go | 17 +++++------------ users/postgres/users_test.go | 21 ++++++++++++++------- users/service.go | 11 +++++++++++ 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/users/postgres/users.go b/users/postgres/users.go index ebec64dc..ad7c6097 100644 --- a/users/postgres/users.go +++ b/users/postgres/users.go @@ -169,19 +169,12 @@ func (ur userRepository) RetrieveAll(ctx context.Context, offset, limit uint64, query = append(query, mq) } - if len(userIDs) == 0 { - return users.UserPage{ - Users: []users.User{}, - PageMetadata: users.PageMetadata{ - Total: 0, - Offset: offset, - Limit: limit, - }, - }, nil + if len(userIDs) > 0 { + query = append(query, fmt.Sprintf("id IN ('%s')", strings.Join(userIDs, "','"))) + } + if len(query) > 0 { + emq = fmt.Sprintf(" WHERE %s", strings.Join(query, " AND ")) } - - query = append(query, fmt.Sprintf("id IN ('%s')", strings.Join(userIDs, "','"))) - emq = fmt.Sprintf(" WHERE %s", strings.Join(query, " AND ")) q := fmt.Sprintf(`SELECT id, email, metadata FROM users %s ORDER BY email LIMIT :limit OFFSET :offset;`, emq) params := map[string]interface{}{ diff --git a/users/postgres/users_test.go b/users/postgres/users_test.go index cf659be2..972f8157 100644 --- a/users/postgres/users_test.go +++ b/users/postgres/users_test.go @@ -201,13 +201,20 @@ func TestRetrieveAll(t *testing.T) { metadata: meta, }, "retrieve all users from empty ids": { - email: "All", - offset: 1, - limit: 5, - size: 0, - total: nUsers, - ids: []string{}, - metadata: meta, + email: "All", + offset: 0, + limit: nUsers, + size: nUsers, + total: nUsers, + ids: []string{}, + }, + "retrieve all users from empty ids with offset": { + email: "All", + offset: 1, + limit: 5, + size: 5, + total: nUsers, + ids: []string{}, }, } for desc, tc := range cases { diff --git a/users/service.go b/users/service.go index 28495a8c..f7a0a950 100644 --- a/users/service.go +++ b/users/service.go @@ -309,6 +309,17 @@ func (svc usersService) ListMembers(ctx context.Context, token, groupID string, return UserPage{}, err } + if len(userIDs) == 0 { + return UserPage{ + Users: []User{}, + PageMetadata: PageMetadata{ + Total: 0, + Offset: offset, + Limit: limit, + }, + }, nil + } + return svc.users.RetrieveAll(ctx, offset, limit, userIDs, "", m) }