Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
fix: users cannot creates a new team and scene
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Mar 17, 2022
1 parent 570fe7a commit 5df25f2
Show file tree
Hide file tree
Showing 29 changed files with 66 additions and 39 deletions.
2 changes: 1 addition & 1 deletion internal/adapter/gql/resolver_mutation_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (r *mutationResolver) CreateTeam(ctx context.Context, input gqlmodel.CreateTeamInput) (*gqlmodel.CreateTeamPayload, error) {
res, err := usecases(ctx).Team.Create(ctx, input.Name, getUser(ctx).ID())
res, err := usecases(ctx).Team.Create(ctx, input.Name, getUser(ctx).ID(), getOperator(ctx))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/fs/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewPlugin(fs afero.Fs) repo.Plugin {
func (r *pluginRepo) Filtered(f repo.SceneFilter) repo.Plugin {
return &pluginRepo{
fs: r.fs,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/fs/property_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewPropertySchema(fs afero.Fs) repo.PropertySchema {
func (r *propertySchema) Filtered(f repo.SceneFilter) repo.PropertySchema {
return &propertySchema{
fs: r.fs,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/memory/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (r *Asset) Filtered(f repo.TeamFilter) repo.Asset {
return &Asset{
// note data is shared between the source repo and mutex cannot work well
data: r.data,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/memory/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (r *Dataset) Filtered(f repo.SceneFilter) repo.Dataset {
return &Dataset{
// note data is shared between the source repo and mutex cannot work well
data: r.data,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/memory/dataset_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (r *DatasetSchema) Filtered(f repo.SceneFilter) repo.DatasetSchema {
return &DatasetSchema{
// note data is shared between the source repo and mutex cannot work well
data: r.data,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/memory/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (r *Layer) Filtered(f repo.SceneFilter) repo.Layer {
return &Layer{
// note data is shared between the source repo and mutex cannot work well
data: r.data,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/memory/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r *Plugin) Filtered(f repo.SceneFilter) repo.Plugin {
return &Plugin{
// note data is shared between the source repo and mutex cannot work well
data: r.data,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/memory/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r *Project) Filtered(f repo.TeamFilter) repo.Project {
return &Project{
// note data is shared between the source repo and mutex cannot work well
data: r.data,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/memory/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r *Property) Filtered(f repo.SceneFilter) repo.Property {
return &Property{
// note data is shared between the source repo and mutex cannot work well
data: r.data,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/memory/property_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (r *PropertySchema) Filtered(f repo.SceneFilter) repo.PropertySchema {
return &PropertySchema{
// note data is shared between the source repo and mutex cannot work well
data: r.data,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/memory/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r *Scene) Filtered(f repo.TeamFilter) repo.Scene {
return &Scene{
// note data is shared between the source repo and mutex cannot work well
data: r.data,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/memory/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (r *Tag) Filtered(f repo.SceneFilter) repo.Tag {
return &Tag{
// note data is shared between the source repo and mutex cannot work well
data: r.data,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewAsset(client *mongodoc.Client) repo.Asset {
func (r *assetRepo) Filtered(f repo.TeamFilter) repo.Asset {
return &assetRepo{
client: r.client,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewDataset(client *mongodoc.Client) repo.Dataset {
func (r *datasetRepo) Filtered(f repo.SceneFilter) repo.Dataset {
return &datasetRepo{
client: r.client,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/dataset_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (r *datasetSchemaRepo) init() {
func (r *datasetSchemaRepo) Filtered(f repo.SceneFilter) repo.DatasetSchema {
return &datasetSchemaRepo{
client: r.client,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (r *layerRepo) init() {
func (r *layerRepo) Filtered(f repo.SceneFilter) repo.Layer {
return &layerRepo{
client: r.client,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (r *pluginRepo) init() {
func (r *pluginRepo) Filtered(f repo.SceneFilter) repo.Plugin {
return &pluginRepo{
client: r.client,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (r *projectRepo) init() {
func (r *projectRepo) Filtered(f repo.TeamFilter) repo.Project {
return &projectRepo{
client: r.client,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (r *propertyRepo) init() {
func (r *propertyRepo) Filtered(f repo.SceneFilter) repo.Property {
return &propertyRepo{
client: r.client,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/property_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (r *propertySchemaRepo) init() {
func (r *propertySchemaRepo) Filtered(f repo.SceneFilter) repo.PropertySchema {
return &propertySchemaRepo{
client: r.client,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (r *sceneRepo) init() {
func (r *sceneRepo) Filtered(f repo.TeamFilter) repo.Scene {
return &sceneRepo{
client: r.client,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (r *tagRepo) init() {
func (r *tagRepo) Filtered(f repo.SceneFilter) repo.Tag {
return &tagRepo{
client: r.client,
f: f.Clone(),
f: r.f.Merge(f),
}
}

Expand Down
14 changes: 8 additions & 6 deletions internal/usecase/interactor/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func (i *Scene) Create(ctx context.Context, pid id.ProjectID, operator *usecase.
if err != nil {
return nil, err
}
if err := i.CanWriteTeam(prj.Team(), operator); err != nil {
team := prj.Team()
if err := i.CanWriteTeam(team, operator); err != nil {
return nil, err
}

Expand All @@ -100,7 +101,7 @@ func (i *Scene) Create(ctx context.Context, pid id.ProjectID, operator *usecase.
g := p.GetOrCreateGroupList(schema, property.PointItemBySchema(tiles))
g.Add(property.NewGroup().NewID().SchemaGroup(tiles).MustBuild(), -1)

scene, err := scene.New().
res, err := scene.New().
ID(sceneID).
Project(pid).
Team(prj.Team()).
Expand All @@ -114,24 +115,25 @@ func (i *Scene) Create(ctx context.Context, pid id.ProjectID, operator *usecase.
}

if p != nil {
err = i.propertyRepo.Save(ctx, p)
err = i.propertyRepo.Filtered(repo.SceneFilter{Writable: scene.IDList{sceneID}}).Save(ctx, p)
if err != nil {
return nil, err
}
}

err = i.layerRepo.Save(ctx, rootLayer)
err = i.layerRepo.Filtered(repo.SceneFilter{Writable: scene.IDList{sceneID}}).Save(ctx, rootLayer)
if err != nil {
return nil, err
}

err = i.sceneRepo.Save(ctx, scene)
err = i.sceneRepo.Save(ctx, res)
if err != nil {
return nil, err
}

operator.AddNewScene(team, sceneID)
tx.Commit()
return scene, err
return res, err
}

func (s *Scene) FetchLock(ctx context.Context, ids []id.SceneID, operator *usecase.Operator) ([]scene.LockMode, error) {
Expand Down
9 changes: 4 additions & 5 deletions internal/usecase/interactor/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (i *Team) FindByUser(ctx context.Context, id id.UserID, operator *usecase.O
return res2, err
}

func (i *Team) Create(ctx context.Context, name string, firstUser id.UserID) (_ *user.Team, err error) {
func (i *Team) Create(ctx context.Context, name string, firstUser id.UserID, operator *usecase.Operator) (_ *user.Team, err error) {
tx, err := i.transaction.Begin()
if err != nil {
return
Expand All @@ -64,16 +64,15 @@ func (i *Team) Create(ctx context.Context, name string, firstUser id.UserID) (_
return nil, err
}

err = team.Members().Join(firstUser, user.RoleOwner)
if err != nil {
if err := team.Members().Join(firstUser, user.RoleOwner); err != nil {
return nil, err
}

err = i.teamRepo.Save(ctx, team)
if err != nil {
if err := i.teamRepo.Save(ctx, team); err != nil {
return nil, err
}

operator.AddNewTeam(team.ID())
tx.Commit()
return team, nil
}
Expand Down
8 changes: 4 additions & 4 deletions internal/usecase/interactor/team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ func TestCreateTeam(t *testing.T) {

db := memory.InitRepos(nil)

user := user.New().NewID().Team(id.NewTeamID()).MustBuild()

u := user.New().NewID().Team(id.NewTeamID()).MustBuild()
teamUC := NewTeam(db)

team, err := teamUC.Create(ctx, "team name", user.ID())
op := &usecase.Operator{User: u.ID()}
team, err := teamUC.Create(ctx, "team name", u.ID(), op)

assert.Nil(t, err)
assert.NotNil(t, team)
Expand All @@ -33,4 +32,5 @@ func TestCreateTeam(t *testing.T) {
assert.NotEmpty(t, resultTeams)
assert.Equal(t, resultTeams[0].ID(), team.ID())
assert.Equal(t, resultTeams[0].Name(), "team name")
assert.Equal(t, user.TeamIDList{resultTeams[0].ID()}, op.OwningTeams)
}
2 changes: 1 addition & 1 deletion internal/usecase/interfaces/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
type Team interface {
Fetch(context.Context, []id.TeamID, *usecase.Operator) ([]*user.Team, error)
FindByUser(context.Context, id.UserID, *usecase.Operator) ([]*user.Team, error)
Create(context.Context, string, id.UserID) (*user.Team, error)
Create(context.Context, string, id.UserID, *usecase.Operator) (*user.Team, error)
Update(context.Context, id.TeamID, string, *usecase.Operator) (*user.Team, error)
AddMember(context.Context, id.TeamID, id.UserID, user.Role, *usecase.Operator) (*user.Team, error)
RemoveMember(context.Context, id.TeamID, id.UserID, *usecase.Operator) (*user.Team, error)
Expand Down
12 changes: 12 additions & 0 deletions internal/usecase/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,15 @@ func (o *Operator) IsWritableScene(scene ...id.SceneID) bool {
func (o *Operator) IsOwningScene(scene ...id.SceneID) bool {
return o.AllOwningScenes().Includes(scene...)
}

func (o *Operator) AddNewTeam(team id.TeamID) {
o.OwningTeams = append(o.OwningTeams, team)
}

func (o *Operator) AddNewScene(team id.TeamID, scene id.SceneID) {
if o.IsOwningTeam(team) {
o.OwningScenes = append(o.OwningScenes, scene)
} else if o.IsWritableTeam(team) {
o.WritableScenes = append(o.WritableScenes, scene)
}
}
14 changes: 14 additions & 0 deletions internal/usecase/repo/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ func (f TeamFilter) Clone() TeamFilter {
}
}

func (f TeamFilter) Merge(g TeamFilter) TeamFilter {
return TeamFilter{
Readable: append(f.Readable, g.Readable...),
Writable: append(f.Writable, g.Writable...),
}
}

func (f TeamFilter) CanRead(id user.TeamID) bool {
return f.Readable == nil || f.Readable.Includes(id)
}
Expand All @@ -93,6 +100,13 @@ func SceneFilterFromOperator(o *usecase.Operator) SceneFilter {
}
}

func (f SceneFilter) Merge(g SceneFilter) SceneFilter {
return SceneFilter{
Readable: append(f.Readable, g.Readable...),
Writable: append(f.Writable, g.Writable...),
}
}

func (f SceneFilter) Clone() SceneFilter {
return SceneFilter{
Readable: f.Readable.Clone(),
Expand Down

0 comments on commit 5df25f2

Please sign in to comment.