fix: find user groups by their friendly name in the admin search (#1854)

Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com>
Co-authored-by: choksi2212 <choksi2212@users.noreply.github.com>
This commit is contained in:
Manas Choksi
2026-10-19 23:16:23 +02:00
committed by GitHub
co-authored by choksi2212
parent 517ea76a9c
commit 0918a4f639
2 changed files with 41 additions and 1 deletions
@@ -35,7 +35,8 @@ func (s *UserGroupService) List(ctx context.Context, name string, listRequestOpt
Model(&model.UserGroup{})
if name != "" {
query = query.Where("name LIKE ?", "%"+name+"%")
searchPattern := "%" + name + "%"
query = query.Where("name LIKE ? OR friendly_name LIKE ?", searchPattern, searchPattern)
}
// As userCount is not a column we need to manually sort it
@@ -0,0 +1,39 @@
package service
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/pocket-id/pocket-id/backend/internal/dto"
"github.com/pocket-id/pocket-id/backend/internal/utils"
)
func TestUserGroupListSearchMatchesFriendlyName(t *testing.T) {
_, groupService := newTestUserService(t)
_, err := groupService.Create(t.Context(), dto.UserGroupCreateDto{
Name: "qa_team",
FriendlyName: "QA Engineering",
})
require.NoError(t, err)
// A search for the friendly name must find the group, since the
// friendly name is the name shown in the UI.
groups, pagination, err := groupService.List(t.Context(), "Engineering", utils.ListRequestOptions{})
require.NoError(t, err)
require.Len(t, groups, 1)
require.Equal(t, "QA Engineering", groups[0].FriendlyName)
require.EqualValues(t, 1, pagination.TotalItems)
// The internal name must remain searchable too.
groups, _, err = groupService.List(t.Context(), "qa_team", utils.ListRequestOptions{})
require.NoError(t, err)
require.Len(t, groups, 1)
require.Equal(t, "QA Engineering", groups[0].FriendlyName)
// A fragment matching neither field returns nothing.
groups, _, err = groupService.List(t.Context(), "Design", utils.ListRequestOptions{})
require.NoError(t, err)
require.Empty(t, groups)
}