Skip to content

Commit f84dd43

Browse files
Dev Agentcsg-pr-bot
authored andcommitted
fix: prioritize org namespace matches in search
1 parent c7153e0 commit f84dd43

4 files changed

Lines changed: 107 additions & 1 deletion

File tree

builder/store/database/organization.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ func (s *orgStoreImpl) Search(ctx context.Context, search string, per int, page
180180
Model(&orgs).Relation("Namespace")
181181
if search != "" {
182182
query.Where("LOWER(organization.name) like ? OR LOWER(organization.path) like ?", fmt.Sprintf("%%%s%%", search), fmt.Sprintf("%%%s%%", search))
183+
query.OrderExpr(`
184+
CASE
185+
WHEN LOWER(organization.path) = ? THEN 0
186+
WHEN LOWER(organization.path) LIKE ? THEN 1
187+
WHEN LOWER(organization.name) = ? THEN 2
188+
WHEN LOWER(organization.name) LIKE ? THEN 3
189+
ELSE 4
190+
END
191+
`, search, fmt.Sprintf("%s%%", search), search, fmt.Sprintf("%s%%", search))
183192
}
184193
if orgType != "" {
185194
query.Where("org_type = ?", orgType)

builder/store/database/organization_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"database/sql"
66
"errors"
7+
"slices"
78
"testing"
89
"time"
910

@@ -259,3 +260,85 @@ func TestOrganizationStore_FindByUUID(t *testing.T) {
259260
require.NotNil(t, err)
260261
require.Nil(t, org)
261262
}
263+
264+
func TestOrganizationStore_SearchOrder(t *testing.T) {
265+
db := tests.InitTestDB()
266+
defer db.Close()
267+
ctx := context.TODO()
268+
269+
store := database.NewOrgStoreWithDB(db)
270+
orgsToCreate := []database.Organization{
271+
{
272+
Name: "sss",
273+
Nickname: "zzz org",
274+
UUID: uuid.New(),
275+
},
276+
{
277+
Name: "sss-team",
278+
Nickname: "alpha org",
279+
UUID: uuid.New(),
280+
},
281+
{
282+
Name: "team-01",
283+
Nickname: "sss",
284+
UUID: uuid.New(),
285+
},
286+
{
287+
Name: "team-02",
288+
Nickname: "sss group",
289+
UUID: uuid.New(),
290+
},
291+
{
292+
Name: "team-03",
293+
Nickname: "group sss",
294+
UUID: uuid.New(),
295+
},
296+
}
297+
298+
for _, org := range orgsToCreate {
299+
err := store.Create(ctx, &org, &database.Namespace{Path: org.Name})
300+
require.Nil(t, err)
301+
}
302+
303+
orgs, total, err := store.Search(ctx, "sss", 10, 1, "", "")
304+
require.Nil(t, err)
305+
require.Equal(t, 5, total)
306+
307+
gotNames := make([]string, 0, len(orgs))
308+
for _, org := range orgs {
309+
gotNames = append(gotNames, org.Name)
310+
}
311+
312+
require.Equal(t, []string{"sss", "sss-team", "team-01", "team-02", "team-03"}, gotNames)
313+
}
314+
315+
func TestOrganizationStore_SearchOrderCaseInsensitive(t *testing.T) {
316+
db := tests.InitTestDB()
317+
defer db.Close()
318+
ctx := context.TODO()
319+
320+
store := database.NewOrgStoreWithDB(db)
321+
err := store.Create(ctx, &database.Organization{
322+
Name: "SSS-Exact",
323+
Nickname: "display",
324+
UUID: uuid.New(),
325+
}, &database.Namespace{Path: "SSS-Exact"})
326+
require.Nil(t, err)
327+
err = store.Create(ctx, &database.Organization{
328+
Name: "other",
329+
Nickname: "sss",
330+
UUID: uuid.New(),
331+
}, &database.Namespace{Path: "other"})
332+
require.Nil(t, err)
333+
334+
orgs, total, err := store.Search(ctx, "sss-exact", 10, 1, "", "")
335+
require.Nil(t, err)
336+
require.Equal(t, 1, total)
337+
require.Len(t, orgs, 1)
338+
require.Equal(t, "SSS-Exact", orgs[0].Name)
339+
340+
orgs, total, err = store.Search(ctx, "SSS", 10, 1, "", "")
341+
require.Nil(t, err)
342+
require.Equal(t, 2, total)
343+
require.True(t, slices.Equal([]string{"SSS-Exact", "other"}, []string{orgs[0].Name, orgs[1].Name}))
344+
}

user/component/organization.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (c *organizationComponentImpl) Index(ctx context.Context, username, search
186186
}
187187
if dborg.Namespace != nil {
188188
org.Namespace = &types.Namespace{
189-
Path: dborg.Nickname,
189+
Path: dborg.Namespace.Path,
190190
Type: string(dborg.Namespace.NamespaceType),
191191
UUID: dborg.Namespace.UUID,
192192
}

user/component/organization_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func TestOrganizationComponent_Index(t *testing.T) {
7676
Logo: "org-logo.png",
7777
OrgType: "school",
7878
Verified: false,
79+
Namespace: &database.Namespace{
80+
Path: "org1",
81+
NamespaceType: database.OrgNamespace,
82+
},
7983
})
8084
dbOrgs = append(dbOrgs, database.Organization{
8185
ID: 2,
@@ -85,6 +89,10 @@ func TestOrganizationComponent_Index(t *testing.T) {
8589
Logo: "org-logo.png",
8690
OrgType: "school",
8791
Verified: false,
92+
Namespace: &database.Namespace{
93+
Path: "org2",
94+
NamespaceType: database.OrgNamespace,
95+
},
8896
})
8997
mockOrgStore := mockdb.NewMockOrgStore(t)
9098
mockOrgStore.EXPECT().GetUserOwnOrgs(mock.Anything, "user1").Return(dbOrgs, len(dbOrgs), nil).Once()
@@ -125,6 +133,12 @@ func TestOrganizationComponent_Index(t *testing.T) {
125133
if expectedOrgs[i].Verified != dbOrgs[i].Verified {
126134
return false
127135
}
136+
if expectedOrgs[i].Namespace == nil {
137+
return false
138+
}
139+
if expectedOrgs[i].Namespace.Path != dbOrgs[i].Namespace.Path {
140+
return false
141+
}
128142
}
129143
return true
130144
})

0 commit comments

Comments
 (0)