Skip to content

Commit

Permalink
fix(server): save project updated_at when features updated (#1076)
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 Aug 1, 2024
1 parent 716bb6b commit 3ac1fca
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 5 deletions.
97 changes: 96 additions & 1 deletion server/e2e/gql_nlslayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,63 @@ func duplicateNLSLayer(e *httpexpect.Expect, layerId string) (GraphQLRequest, *h
return requestBody, res
}

func fetchProjectForNewLayers(e *httpexpect.Expect, pID string) (GraphQLRequest, *httpexpect.Value) {
fetchProjectRequestBody := GraphQLRequest{
OperationName: "GetProject",
Query: `query GetProject($projectId: ID!) {
node(id: $projectId, type: PROJECT) {
id
... on Project {
...ProjectFragment
scene {
id
__typename
}
__typename
}
__typename
}
}
fragment ProjectFragment on Project {
id
name
description
imageUrl
isArchived
isBasicAuthActive
basicAuthUsername
basicAuthPassword
publicTitle
publicDescription
publicImage
alias
enableGa
trackingId
publishmentStatus
updatedAt
createdAt
coreSupport
starred
__typename
}`,
Variables: map[string]any{
"projectId": pID,
},
}

res := e.POST("/api/graphql").
WithHeader("Origin", "https://example.com").
WithHeader("X-Reearth-Debug-User", uID.String()).
WithHeader("Content-Type", "application/json").
WithJSON(fetchProjectRequestBody).
Expect().
Status(http.StatusOK).
JSON()

return fetchProjectRequestBody, res
}

func fetchSceneForNewLayers(e *httpexpect.Expect, sID string) (GraphQLRequest, *httpexpect.Value) {
fetchSceneRequestBody := GraphQLRequest{
OperationName: "GetScene",
Expand Down Expand Up @@ -308,9 +365,15 @@ func TestNLSLayerCRUD(t *testing.T) {
}, true, baseSeeder)

pId := createProject(e, "test")

_, notUpdatedProject := fetchProjectForNewLayers(e, pId)
notUpdatedProjectUpdatedAt := notUpdatedProject.Object().
Value("data").Object().
Value("node").Object().
Value("updatedAt").Raw().(string)

_, _, sId := createScene(e, pId)

// fetch scene
_, res := fetchSceneForNewLayers(e, sId)

res.Object().
Expand Down Expand Up @@ -417,6 +480,12 @@ func TestNLSLayerCRUD(t *testing.T) {
Value("node").Object().
Value("newLayers").Array().
Length().Equal(0)

_, updatedProject := fetchProjectForNewLayers(e, pId)
updatedProject.Object().
Value("data").Object().
Value("node").Object().
Value("updatedAt").NotEqual(notUpdatedProjectUpdatedAt)
}

func createInfobox(e *httpexpect.Expect, layerId string) (GraphQLRequest, *httpexpect.Value, string) {
Expand Down Expand Up @@ -662,6 +731,13 @@ func TestInfoboxBlocksCRUD(t *testing.T) {
}, true, baseSeeder)

pId := createProject(e, "test")

_, notUpdatedProject := fetchProjectForNewLayers(e, pId)
notUpdatedProjectUpdatedAt := notUpdatedProject.Object().
Value("data").Object().
Value("node").Object().
Value("updatedAt").Raw().(string)

_, _, sId := createScene(e, pId)

// fetch scene
Expand Down Expand Up @@ -715,6 +791,12 @@ func TestInfoboxBlocksCRUD(t *testing.T) {
_, res = fetchSceneForNewLayers(e, sId)
res.Object().
Path("$.data.node.newLayers[0].infobox.blocks").Equal([]any{})

_, updatedProject := fetchProjectForNewLayers(e, pId)
updatedProject.Object().
Value("data").Object().
Value("node").Object().
Value("updatedAt").NotEqual(notUpdatedProjectUpdatedAt)
}

func addCustomProperties(
Expand Down Expand Up @@ -763,6 +845,13 @@ func TestCustomProperties(t *testing.T) {
}, true, baseSeeder)

pId := createProject(e, "test")

_, notUpdatedProject := fetchProjectForNewLayers(e, pId)
notUpdatedProjectUpdatedAt := notUpdatedProject.Object().
Value("data").Object().
Value("node").Object().
Value("updatedAt").Raw().(string)

_, _, sId := createScene(e, pId)

_, res := fetchSceneForNewLayers(e, sId)
Expand Down Expand Up @@ -828,4 +917,10 @@ func TestCustomProperties(t *testing.T) {
Value("sketch").Object().
Value("customPropertySchema").Object().
Value("extrudedHeight").Equal(10)

_, updatedProject := fetchProjectForNewLayers(e, pId)
updatedProject.Object().
Value("data").Object().
Value("node").Object().
Value("updatedAt").NotEqual(notUpdatedProjectUpdatedAt)
}
69 changes: 69 additions & 0 deletions server/internal/usecase/interactor/nlslayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type NLSLayer struct {
commonSceneLock
nlslayerRepo repo.NLSLayer
sceneLockRepo repo.SceneLock
projectRepo repo.Project
sceneRepo repo.Scene
propertyRepo repo.Property
pluginRepo repo.Plugin
transaction usecasex.Transaction
Expand All @@ -32,6 +34,8 @@ func NewNLSLayer(r *repo.Container) interfaces.NLSLayer {
commonSceneLock: commonSceneLock{sceneLockRepo: r.SceneLock},
nlslayerRepo: r.NLSLayer,
sceneLockRepo: r.SceneLock,
projectRepo: r.Project,
sceneRepo: r.Scene,
propertyRepo: r.Property,
pluginRepo: r.Plugin,
transaction: r.Transaction,
Expand Down Expand Up @@ -102,6 +106,11 @@ func (i *NLSLayer) AddLayerSimple(ctx context.Context, inp interfaces.AddNLSLaye
return nil, err
}

err = updateProjectUpdatedAtByScene(ctx, layerSimple.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return nil, err
}

tx.Commit()
return layerSimple, nil
}
Expand Down Expand Up @@ -181,6 +190,11 @@ func (i *NLSLayer) Remove(ctx context.Context, lid id.NLSLayerID, operator *usec
return lid, nil, err
}

err = updateProjectUpdatedAtByScene(ctx, l.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return lid, nil, err
}

tx.Commit()
return lid, parentLayer, nil
}
Expand Down Expand Up @@ -223,6 +237,11 @@ func (i *NLSLayer) Update(ctx context.Context, inp interfaces.UpdateNLSLayerInpu
return nil, err
}

err = updateProjectUpdatedAtByScene(ctx, layer.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return nil, err
}

tx.Commit()
return layer, nil
}
Expand Down Expand Up @@ -275,6 +294,11 @@ func (i *NLSLayer) CreateNLSInfobox(ctx context.Context, lid id.NLSLayerID, oper
return nil, err
}

err = updateProjectUpdatedAtByScene(ctx, l.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return nil, err
}

tx.Commit()
return l, nil
}
Expand Down Expand Up @@ -323,6 +347,11 @@ func (i *NLSLayer) RemoveNLSInfobox(ctx context.Context, layerID id.NLSLayerID,
return nil, err
}

err = updateProjectUpdatedAtByScene(ctx, layer.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return nil, err
}

tx.Commit()
return layer, nil
}
Expand Down Expand Up @@ -421,6 +450,11 @@ func (i *NLSLayer) AddNLSInfoboxBlock(ctx context.Context, inp interfaces.AddNLS
return nil, nil, err
}

err = updateProjectUpdatedAtByScene(ctx, l.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return nil, nil, err
}

tx.Commit()
return block, l, err
}
Expand Down Expand Up @@ -463,6 +497,11 @@ func (i *NLSLayer) MoveNLSInfoboxBlock(ctx context.Context, inp interfaces.MoveN
return inp.InfoboxBlockID, nil, -1, err
}

err = updateProjectUpdatedAtByScene(ctx, layer.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return inp.InfoboxBlockID, nil, -1, err
}

tx.Commit()
return inp.InfoboxBlockID, layer, inp.Index, err
}
Expand Down Expand Up @@ -505,6 +544,11 @@ func (i *NLSLayer) RemoveNLSInfoboxBlock(ctx context.Context, inp interfaces.Rem
return inp.InfoboxBlockID, nil, err
}

err = updateProjectUpdatedAtByScene(ctx, layer.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return inp.InfoboxBlockID, nil, err
}

tx.Commit()
return inp.InfoboxBlockID, layer, err
}
Expand Down Expand Up @@ -537,6 +581,11 @@ func (i *NLSLayer) Duplicate(ctx context.Context, lid id.NLSLayerID, operator *u
return nil, err
}

err = updateProjectUpdatedAtByScene(ctx, layer.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return nil, err
}

tx.Commit()
return duplicatedLayer, nil
}
Expand Down Expand Up @@ -580,6 +629,11 @@ func (i *NLSLayer) AddCustomProperties(ctx context.Context, inp interfaces.AddCu
return nil, err
}

err = updateProjectUpdatedAtByScene(ctx, layer.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return nil, err
}

tx.Commit()
return layer, nil
}
Expand Down Expand Up @@ -641,6 +695,11 @@ func (i *NLSLayer) AddGeoJSONFeature(ctx context.Context, inp interfaces.AddNLSL
return nlslayer.Feature{}, err
}

err = updateProjectUpdatedAtByScene(ctx, layer.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return nlslayer.Feature{}, err
}

tx.Commit()
return *feature, nil
}
Expand Down Expand Up @@ -693,6 +752,11 @@ func (i *NLSLayer) UpdateGeoJSONFeature(ctx context.Context, inp interfaces.Upda
return nlslayer.Feature{}, err
}

err = updateProjectUpdatedAtByScene(ctx, layer.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return nlslayer.Feature{}, err
}

tx.Commit()
return updatedFeature, nil
}
Expand Down Expand Up @@ -729,6 +793,11 @@ func (i *NLSLayer) DeleteGeoJSONFeature(ctx context.Context, inp interfaces.Dele
return id.FeatureID{}, err
}

err = updateProjectUpdatedAtByScene(ctx, layer.Scene(), i.projectRepo, i.sceneRepo)
if err != nil {
return id.FeatureID{}, err
}

tx.Commit()
return inp.FeatureID, nil
}
17 changes: 13 additions & 4 deletions server/internal/usecase/interactor/nlslayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/reearth/reearth/server/internal/usecase/interfaces"
"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearth/server/pkg/nlslayer"
"github.com/reearth/reearth/server/pkg/project"
"github.com/reearth/reearth/server/pkg/scene"
"github.com/reearth/reearthx/account/accountdomain"
"github.com/stretchr/testify/assert"
Expand All @@ -18,7 +19,9 @@ func TestAddCustomProperties(t *testing.T) {
ctx := context.Background()

db := memory.New()
scene, _ := scene.New().NewID().Workspace(accountdomain.NewWorkspaceID()).Project(id.NewProjectID()).RootLayer(id.NewLayerID()).Build()
prj, _ := project.New().NewID().Build()
_ = db.Project.Save(ctx, prj)
scene, _ := scene.New().NewID().Workspace(accountdomain.NewWorkspaceID()).Project(prj.ID()).RootLayer(id.NewLayerID()).Build()
_ = db.Scene.Save(ctx, scene)
il := NewNLSLayer(db)

Expand Down Expand Up @@ -50,7 +53,9 @@ func TestAddGeoJSONFeature(t *testing.T) {
ctx := context.Background()

db := memory.New()
scene, _ := scene.New().NewID().Workspace(accountdomain.NewWorkspaceID()).Project(id.NewProjectID()).RootLayer(id.NewLayerID()).Build()
prj, _ := project.New().NewID().Build()
_ = db.Project.Save(ctx, prj)
scene, _ := scene.New().NewID().Workspace(accountdomain.NewWorkspaceID()).Project(prj.ID()).RootLayer(id.NewLayerID()).Build()
_ = db.Scene.Save(ctx, scene)
il := NewNLSLayer(db)

Expand Down Expand Up @@ -103,7 +108,9 @@ func TestUpdateGeoJSONFeature(t *testing.T) {
ctx := context.Background()

db := memory.New()
scene, _ := scene.New().NewID().Workspace(accountdomain.NewWorkspaceID()).Project(id.NewProjectID()).RootLayer(id.NewLayerID()).Build()
prj, _ := project.New().NewID().Build()
_ = db.Project.Save(ctx, prj)
scene, _ := scene.New().NewID().Workspace(accountdomain.NewWorkspaceID()).Project(prj.ID()).RootLayer(id.NewLayerID()).Build()
_ = db.Scene.Save(ctx, scene)
il := NewNLSLayer(db)

Expand Down Expand Up @@ -178,7 +185,9 @@ func TestDeleteGeoJSONFeature(t *testing.T) {
ctx := context.Background()

db := memory.New()
scene, _ := scene.New().NewID().Workspace(accountdomain.NewWorkspaceID()).Project(id.NewProjectID()).RootLayer(id.NewLayerID()).Build()
prj, _ := project.New().NewID().Build()
_ = db.Project.Save(ctx, prj)
scene, _ := scene.New().NewID().Workspace(accountdomain.NewWorkspaceID()).Project(prj.ID()).RootLayer(id.NewLayerID()).Build()
_ = db.Scene.Save(ctx, scene)
il := NewNLSLayer(db)

Expand Down
Loading

0 comments on commit 3ac1fca

Please sign in to comment.