Skip to content

Commit ac88150

Browse files
authored
fix(server): team workspace error (#106)
1 parent f132b07 commit ac88150

7 files changed

Lines changed: 52 additions & 23 deletions

File tree

server/internal/infrastructure/mongo/asset.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
var (
26-
assetIndexes = []string{"team"}
26+
assetIndexes = []string{"team", "workspace"}
2727
assetUniqueIndexes = []string{"id"}
2828
)
2929

@@ -180,9 +180,10 @@ func (r *Asset) FindByWorkspace(ctx context.Context, id accountdomain.WorkspaceI
180180
return nil, usecasex.EmptyPageInfo(), nil
181181
}
182182

183-
absoluteFilter := bson.M{
184-
"team": id.String(),
185-
}
183+
absoluteFilter := bson.M{"$or": []bson.M{
184+
{"workspace": id.String()},
185+
{"team": id.String()},
186+
}}
186187

187188
if f.Keyword != nil {
188189
keywordRegex := primitive.Regex{
@@ -257,7 +258,10 @@ func (r *Asset) TotalSizeByWorkspace(ctx context.Context, wid accountdomain.Work
257258
}
258259

259260
c, err := r.client.Client().Aggregate(ctx, []bson.M{
260-
{"$match": bson.M{"team": wid.String()}},
261+
{"$match": bson.M{"$or": []bson.M{
262+
{"workspace": wid.String()},
263+
{"team": wid.String()},
264+
}}},
261265
{"$group": bson.M{"_id": nil, "size": bson.M{"$sum": "$size"}}},
262266
})
263267
if err != nil {

server/internal/infrastructure/mongo/container.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,13 @@ func applyWorkspaceFilter(filter interface{}, ids user.WorkspaceIDList) interfac
142142
if ids == nil {
143143
return filter
144144
}
145-
return mongox.And(filter, "team", bson.M{"$in": ids.Strings()})
145+
return mongox.And(filter, "",
146+
bson.M{"$or": []bson.M{
147+
{"workspace": bson.M{"$in": ids.Strings()}},
148+
{"team": bson.M{"$in": ids.Strings()}},
149+
},
150+
},
151+
)
146152
}
147153

148154
func applySceneFilter(filter interface{}, ids scene.IDList) interface{} {

server/internal/infrastructure/mongo/mongodoc/asset.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
type AssetDocument struct {
1313
ID string
1414
CreatedAt time.Time
15-
Team string // DON'T CHANGE NAME'
15+
Team string `bson:"team,omitempty"` // legacy field name
16+
Workspace string `bson:"workspace,omitempty"` // new field name
1617
Name string
1718
Size int64
1819
URL string
@@ -32,7 +33,7 @@ func NewAsset(asset *asset.Asset) (*AssetDocument, string) {
3233
return &AssetDocument{
3334
ID: aid,
3435
CreatedAt: asset.CreatedAt(),
35-
Team: asset.Workspace().String(),
36+
Workspace: asset.Workspace().String(),
3637
Name: asset.Name(),
3738
Size: asset.Size(),
3839
URL: asset.URL(),
@@ -45,7 +46,11 @@ func (d *AssetDocument) Model() (*asset.Asset, error) {
4546
if err != nil {
4647
return nil, err
4748
}
48-
tid, err := accountdomain.WorkspaceIDFrom(d.Team)
49+
workspace := d.Workspace
50+
if workspace == "" {
51+
workspace = d.Team
52+
}
53+
tid, err := accountdomain.WorkspaceIDFrom(workspace)
4954
if err != nil {
5055
return nil, err
5156
}

server/internal/infrastructure/mongo/mongodoc/project.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type ProjectDocument struct {
2828
PublicImage string
2929
PublicNoIndex bool
3030
Team string // DON'T CHANGE NAME'
31+
Workspace string `bson:"workspace,omitempty"`
3132
Visualizer string
3233
PublishmentStatus string
3334
CoreSupport bool
@@ -52,6 +53,8 @@ func NewProject(project *project.Project) (*ProjectDocument, string) {
5253
imageURL = u.String()
5354
}
5455

56+
workspace := project.Workspace().String()
57+
5558
return &ProjectDocument{
5659
ID: pid,
5760
Archived: project.IsArchived(),
@@ -68,7 +71,7 @@ func NewProject(project *project.Project) (*ProjectDocument, string) {
6871
PublicDescription: project.PublicDescription(),
6972
PublicImage: project.PublicImage(),
7073
PublicNoIndex: project.PublicNoIndex(),
71-
Team: project.Workspace().String(),
74+
Workspace: workspace,
7275
Visualizer: string(project.Visualizer()),
7376
PublishmentStatus: string(project.PublishmentStatus()),
7477
CoreSupport: project.CoreSupport(),
@@ -83,7 +86,11 @@ func (d *ProjectDocument) Model() (*project.Project, error) {
8386
if err != nil {
8487
return nil, err
8588
}
86-
tid, err := accountdomain.WorkspaceIDFrom(d.Team)
89+
workspace := d.Workspace
90+
if workspace == "" {
91+
workspace = d.Team
92+
}
93+
tid, err := accountdomain.WorkspaceIDFrom(workspace)
8794
if err != nil {
8895
return nil, err
8996
}

server/internal/infrastructure/mongo/mongodoc/scene.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import (
1616
type SceneDocument struct {
1717
ID string
1818
Project string
19-
Team string // DON'T CHANGE NAME'
19+
Team string `bson:"team,omitempty"` // legacy field name
20+
Workspace string `bson:"workspace,omitempty"` // new field name
2021
RootLayer string
2122
Widgets []SceneWidgetDocument
2223
AlignSystem *WidgetAlignSystemDocument
@@ -118,7 +119,7 @@ func NewScene(scene *scene.Scene) (*SceneDocument, string) {
118119
return &SceneDocument{
119120
ID: id,
120121
Project: scene.Project().String(),
121-
Team: scene.Workspace().String(),
122+
Workspace: scene.Workspace().String(),
122123
RootLayer: scene.RootLayer().String(),
123124
Widgets: widgetsDoc,
124125
Plugins: pluginsDoc,
@@ -142,9 +143,13 @@ func (d *SceneDocument) Model() (*scene.Scene, error) {
142143
if err != nil {
143144
return nil, fmt.Errorf("mongo scene: invalid scene.property (%s): %w", d.Property, err)
144145
}
145-
tid, err := accountdomain.WorkspaceIDFrom(d.Team)
146+
workspace := d.Workspace
147+
if workspace == "" {
148+
workspace = d.Team
149+
}
150+
tid, err := accountdomain.WorkspaceIDFrom(workspace)
146151
if err != nil {
147-
return nil, fmt.Errorf("mongo scene: invalid scene.team (%s): %w", d.Team, err)
152+
return nil, fmt.Errorf("mongo scene: invalid scene.team (%s): %w", workspace, err)
148153
}
149154

150155
var lid scene.LayerID

server/internal/infrastructure/mongo/project.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
var (
23-
projectIndexes = []string{"alias", "alias,publishmentstatus", "team"}
23+
projectIndexes = []string{"alias", "alias,publishmentstatus", "team", "workspace"}
2424
projectUniqueIndexes = []string{"id"}
2525
)
2626

@@ -154,9 +154,10 @@ func (r *Project) FindByWorkspace(ctx context.Context, id accountdomain.Workspac
154154
return nil, usecasex.EmptyPageInfo(), nil
155155
}
156156

157-
absoluteFilter := bson.M{
158-
"team": id.String(),
159-
}
157+
absoluteFilter := bson.M{"$or": []bson.M{
158+
{"workspace": id.String()},
159+
{"team": id.String()},
160+
}}
160161

161162
totalCount, err := r.client.Client().CountDocuments(ctx, absoluteFilter)
162163
if err != nil {

server/internal/infrastructure/mongo/scene.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
var (
18-
sceneIndexes = []string{"project", "team"}
18+
sceneIndexes = []string{"project", "team", "workspace"}
1919
sceneUniqueIndexes = []string{"id"}
2020
)
2121

@@ -74,9 +74,10 @@ func (r *Scene) FindByWorkspace(ctx context.Context, workspaces ...accountdomain
7474
if r.f.Readable != nil {
7575
workspaces2 = workspaces2.Intersect(r.f.Readable)
7676
}
77-
res, err := r.find(ctx, bson.M{
78-
"team": bson.M{"$in": user.WorkspaceIDList(workspaces2).Strings()},
79-
})
77+
res, err := r.find(ctx, bson.M{"$or": []bson.M{
78+
{"workspace": bson.M{"$in": user.WorkspaceIDList(workspaces2).Strings()}},
79+
{"team": bson.M{"$in": user.WorkspaceIDList(workspaces2).Strings()}},
80+
}})
8081
if err != nil && err != mongo.ErrNilDocument && err != mongo.ErrNoDocuments {
8182
return nil, err
8283
}

0 commit comments

Comments
 (0)