Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions builder/store/database/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ func (s *orgStoreImpl) Search(ctx context.Context, search string, per int, page
Model(&orgs).Relation("Namespace")
if search != "" {
query.Where("LOWER(organization.name) like ? OR LOWER(organization.path) like ?", fmt.Sprintf("%%%s%%", search), fmt.Sprintf("%%%s%%", search))
query.OrderExpr(`
CASE
WHEN LOWER(organization.path) = ? THEN 0
WHEN LOWER(organization.path) LIKE ? THEN 1
WHEN LOWER(organization.name) = ? THEN 2
WHEN LOWER(organization.name) LIKE ? THEN 3
ELSE 4
END
`, search, fmt.Sprintf("%s%%", search), search, fmt.Sprintf("%s%%", search))
}
if orgType != "" {
query.Where("org_type = ?", orgType)
Expand Down
83 changes: 83 additions & 0 deletions builder/store/database/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
"slices"
"testing"
"time"

Expand Down Expand Up @@ -259,3 +260,85 @@ func TestOrganizationStore_FindByUUID(t *testing.T) {
require.NotNil(t, err)
require.Nil(t, org)
}

func TestOrganizationStore_SearchOrder(t *testing.T) {
db := tests.InitTestDB()
defer db.Close()
ctx := context.TODO()

store := database.NewOrgStoreWithDB(db)
orgsToCreate := []database.Organization{
{
Name: "sss",
Nickname: "zzz org",
UUID: uuid.New(),
},
{
Name: "sss-team",
Nickname: "alpha org",
UUID: uuid.New(),
},
{
Name: "team-01",
Nickname: "sss",
UUID: uuid.New(),
},
{
Name: "team-02",
Nickname: "sss group",
UUID: uuid.New(),
},
{
Name: "team-03",
Nickname: "group sss",
UUID: uuid.New(),
},
}

for _, org := range orgsToCreate {
err := store.Create(ctx, &org, &database.Namespace{Path: org.Name})
require.Nil(t, err)
}

orgs, total, err := store.Search(ctx, "sss", 10, 1, "", "")
require.Nil(t, err)
require.Equal(t, 5, total)

gotNames := make([]string, 0, len(orgs))
for _, org := range orgs {
gotNames = append(gotNames, org.Name)
}

require.Equal(t, []string{"sss", "sss-team", "team-01", "team-02", "team-03"}, gotNames)
}

func TestOrganizationStore_SearchOrderCaseInsensitive(t *testing.T) {
db := tests.InitTestDB()
defer db.Close()
ctx := context.TODO()

store := database.NewOrgStoreWithDB(db)
err := store.Create(ctx, &database.Organization{
Name: "SSS-Exact",
Nickname: "display",
UUID: uuid.New(),
}, &database.Namespace{Path: "SSS-Exact"})
require.Nil(t, err)
err = store.Create(ctx, &database.Organization{
Name: "other",
Nickname: "sss",
UUID: uuid.New(),
}, &database.Namespace{Path: "other"})
require.Nil(t, err)

orgs, total, err := store.Search(ctx, "sss-exact", 10, 1, "", "")
require.Nil(t, err)
require.Equal(t, 1, total)
require.Len(t, orgs, 1)
require.Equal(t, "SSS-Exact", orgs[0].Name)

orgs, total, err = store.Search(ctx, "SSS", 10, 1, "", "")
require.Nil(t, err)
require.Equal(t, 2, total)
require.True(t, slices.Equal([]string{"SSS-Exact", "other"}, []string{orgs[0].Name, orgs[1].Name}))
}
2 changes: 1 addition & 1 deletion user/component/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (c *organizationComponentImpl) Index(ctx context.Context, username, search
}
if dborg.Namespace != nil {
org.Namespace = &types.Namespace{
Path: dborg.Nickname,
Path: dborg.Namespace.Path,
Type: string(dborg.Namespace.NamespaceType),
UUID: dborg.Namespace.UUID,
}
Expand Down
14 changes: 14 additions & 0 deletions user/component/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func TestOrganizationComponent_Index(t *testing.T) {
Logo: "org-logo.png",
OrgType: "school",
Verified: false,
Namespace: &database.Namespace{
Path: "org1",
NamespaceType: database.OrgNamespace,
},
})
dbOrgs = append(dbOrgs, database.Organization{
ID: 2,
Expand All @@ -85,6 +89,10 @@ func TestOrganizationComponent_Index(t *testing.T) {
Logo: "org-logo.png",
OrgType: "school",
Verified: false,
Namespace: &database.Namespace{
Path: "org2",
NamespaceType: database.OrgNamespace,
},
})
mockOrgStore := mockdb.NewMockOrgStore(t)
mockOrgStore.EXPECT().GetUserOwnOrgs(mock.Anything, "user1").Return(dbOrgs, len(dbOrgs), nil).Once()
Expand Down Expand Up @@ -125,6 +133,12 @@ func TestOrganizationComponent_Index(t *testing.T) {
if expectedOrgs[i].Verified != dbOrgs[i].Verified {
return false
}
if expectedOrgs[i].Namespace == nil {
return false
}
if expectedOrgs[i].Namespace.Path != dbOrgs[i].Namespace.Path {
return false
}
}
return true
})
Expand Down
Loading