Skip to content

Commit

Permalink
fix(server): project name sort bug (#1068)
Browse files Browse the repository at this point in the history
Co-authored-by: Piyush Chauhan <[email protected]>
  • Loading branch information
kawasaki124529 and pyshx authored Jul 31, 2024
1 parent 6b2f911 commit c13315e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 17 deletions.
2 changes: 1 addition & 1 deletion server/e2e/gql_featureCollection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestFeatureCollectionCRUD(t *testing.T) {
},
}, true, baseSeeder)

pId := createProject(e)
pId := createProject(e, "test")
_, _, sId := createScene(e, pId)

_, res := fetchSceneForNewLayers(e, sId)
Expand Down
6 changes: 3 additions & 3 deletions server/e2e/gql_nlslayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func TestNLSLayerCRUD(t *testing.T) {
},
}, true, baseSeeder)

pId := createProject(e)
pId := createProject(e, "test")
_, _, sId := createScene(e, pId)

// fetch scene
Expand Down Expand Up @@ -661,7 +661,7 @@ func TestInfoboxBlocksCRUD(t *testing.T) {
},
}, true, baseSeeder)

pId := createProject(e)
pId := createProject(e, "test")
_, _, sId := createScene(e, pId)

// fetch scene
Expand Down Expand Up @@ -762,7 +762,7 @@ func TestCustomProperties(t *testing.T) {
},
}, true, baseSeeder)

pId := createProject(e)
pId := createProject(e, "test")
_, _, sId := createScene(e, pId)

_, res := fetchSceneForNewLayers(e, sId)
Expand Down
47 changes: 47 additions & 0 deletions server/e2e/gql_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,50 @@ func TestCreateProject(t *testing.T) {
ValueEqual("imageUrl", "").
ValueEqual("coreSupport", true)
}

func TestSortByName(t *testing.T) {
e := StartServer(t, &config.Config{
Origins: []string{"https://example.com"},
AuthSrv: config.AuthSrvConfig{
Disabled: true,
},
},
true, baseSeeder)

createProject(e, "a-project")
createProject(e, "b-project")
createProject(e, "A-project")
createProject(e, "B-project")
seedProjectName := pName

requestBody := GraphQLRequest{
OperationName: "GetProjects",
Query: "query GetProjects($teamId: ID!, $first: Int, $last: Int, $after: Cursor, $before: Cursor, $keyword: String, $sort: ProjectSortType) {\n projects(\n teamId: $teamId\n first: $first\n last: $last\n after: $after\n before: $before\n keyword: $keyword\n sort: $sort\n ) {\n edges {\n node {\n id\n ...ProjectFragment\n scene {\n id\n __typename\n }\n __typename\n }\n __typename\n }\n nodes {\n id\n ...ProjectFragment\n scene {\n id\n __typename\n }\n __typename\n }\n pageInfo {\n endCursor\n hasNextPage\n hasPreviousPage\n startCursor\n __typename\n }\n totalCount\n __typename\n }\n}\n\nfragment ProjectFragment on Project {\n id\n name\n description\n imageUrl\n isArchived\n isBasicAuthActive\n basicAuthUsername\n basicAuthPassword\n publicTitle\n publicDescription\n publicImage\n alias\n enableGa\n trackingId\n publishmentStatus\n updatedAt\n createdAt\n coreSupport\n starred\n __typename\n}",
Variables: map[string]any{
"last": 5,
"teamId": wID.String(),
"sort": "NAME",
},
}

edges := e.POST("/api/graphql").
WithHeader("Origin", "https://example.com").
// WithHeader("authorization", "Bearer test").
WithHeader("X-Reearth-Debug-User", uID.String()).
WithHeader("Content-Type", "application/json").
WithJSON(requestBody).
Expect().
Status(http.StatusOK).
JSON().
Object().
Value("data").Object().
Value("projects").Object().
Value("edges").Array()

edges.Length().Equal(5)
edges.Element(0).Object().Value("node").Object().Value("name").Equal("a-project")
edges.Element(1).Object().Value("node").Object().Value("name").Equal("A-project")
edges.Element(2).Object().Value("node").Object().Value("name").Equal("b-project")
edges.Element(3).Object().Value("node").Object().Value("name").Equal("B-project")
edges.Element(4).Object().Value("node").Object().Value("name").Equal(seedProjectName)
}
16 changes: 8 additions & 8 deletions server/e2e/gql_storytelling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/stretchr/testify/assert"
)

func createProject(e *httpexpect.Expect) string {
func createProject(e *httpexpect.Expect, name string) string {
requestBody := GraphQLRequest{
OperationName: "CreateProject",
Query: `mutation CreateProject($teamId: ID!, $visualizer: Visualizer!, $name: String!, $description: String!, $imageUrl: URL, $coreSupport: Boolean) {
Expand All @@ -28,7 +28,7 @@ func createProject(e *httpexpect.Expect) string {
}
}`,
Variables: map[string]any{
"name": "test",
"name": name,
"description": "abc",
"imageUrl": "",
"teamId": wID.String(),
Expand Down Expand Up @@ -756,7 +756,7 @@ func TestStoryCRUD(t *testing.T) {
},
}, true, baseSeeder)

pID := createProject(e)
pID := createProject(e, "test")

_, _, sID := createScene(e, pID)

Expand Down Expand Up @@ -802,7 +802,7 @@ func TestStoryPageCRUD(t *testing.T) {
},
}, true, baseSeeder)

pID := createProject(e)
pID := createProject(e, "test")

_, _, sID := createScene(e, pID)

Expand Down Expand Up @@ -896,7 +896,7 @@ func TestStoryPageLayersCRUD(t *testing.T) {
},
}, true, baseSeeder)

pID := createProject(e)
pID := createProject(e, "test")

_, _, sID := createScene(e, pID)

Expand Down Expand Up @@ -933,7 +933,7 @@ func TestStoryPageBlocksCRUD(t *testing.T) {
},
}, true, baseSeeder)

pID := createProject(e)
pID := createProject(e, "test")

_, _, sID := createScene(e, pID)

Expand Down Expand Up @@ -981,7 +981,7 @@ func TestStoryPageBlocksProperties(t *testing.T) {
},
}, true, baseSeeder)

pID := createProject(e)
pID := createProject(e, "test")

_, _, sID := createScene(e, pID)

Expand Down Expand Up @@ -1018,7 +1018,7 @@ func TestStoryPublishing(t *testing.T) {
},
}, true, baseSeeder)

pID := createProject(e)
pID := createProject(e, "test")

_, _, sID := createScene(e, pID)

Expand Down
2 changes: 1 addition & 1 deletion server/e2e/gql_style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestStyleCRUD(t *testing.T) {
},
}, true, baseSeeder)

pId := createProject(e)
pId := createProject(e, "test")
_, _, sId := createScene(e, pId)

// fetch scene
Expand Down
5 changes: 3 additions & 2 deletions server/e2e/seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
uName = "e2e"
wID = accountdomain.NewWorkspaceID()
pID = id.NewProjectID()
pName = "p1"
pAlias = "PROJECT_ALIAS"

sID = id.NewSceneID()
Expand Down Expand Up @@ -61,8 +62,8 @@ func baseSeeder(ctx context.Context, r *repo.Container) error {
}

p := project.New().ID(pID).
Name("p1").
Description("p1 desc").
Name(pName).
Description(pName + " desc").
ImageURL(lo.Must(url.Parse("https://test.com"))).
Workspace(w.ID()).
Alias(pAlias).
Expand Down
2 changes: 1 addition & 1 deletion server/internal/infrastructure/memory/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (r *Project) FindByWorkspace(ctx context.Context, id accountdomain.Workspac
case project.SortTypeUpdatedAt:
return result[i].UpdatedAt().Before(result[j].UpdatedAt())
case project.SortTypeName:
return strings.Compare(result[i].Name(), result[j].Name()) < 0
return strings.Compare(strings.ToLower(result[i].Name()), strings.ToLower(result[j].Name())) < 0
default:
return false
}
Expand Down
25 changes: 24 additions & 1 deletion server/internal/infrastructure/mongo/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"regexp"
"sort"
"strings"

"github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc"
"github.com/reearth/reearth/server/internal/usecase/repo"
Expand Down Expand Up @@ -92,7 +94,28 @@ func (r *Project) FindByWorkspace(ctx context.Context, id accountdomain.Workspac
})
}

return r.paginate(ctx, filter, uFilter.Sort, uFilter.Pagination)
projects, pageInfo, err := r.paginate(ctx, filter, uFilter.Sort, uFilter.Pagination)

if err != nil {
return projects, pageInfo, err
}

if uFilter.Sort != nil {
s := *uFilter.Sort
sort.SliceStable(projects, func(i, j int) bool {
switch s {
case project.SortTypeID:
return projects[i].ID().Compare(projects[j].ID()) < 0
case project.SortTypeUpdatedAt:
return projects[i].UpdatedAt().Before(projects[j].UpdatedAt())
case project.SortTypeName:
return strings.Compare(strings.ToLower(projects[i].Name()), strings.ToLower(projects[j].Name())) < 0
}
return false
})
}

return projects, pageInfo, err
}

func (r *Project) FindByPublicName(ctx context.Context, name string) (*project.Project, error) {
Expand Down

0 comments on commit c13315e

Please sign in to comment.