Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): logical deletion for item #698

Closed
wants to merge 15 commits into from
Closed
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
15 changes: 12 additions & 3 deletions server/internal/infrastructure/memory/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/reearth/reearth-cms/server/pkg/version"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/usecasex"
"github.com/reearth/reearthx/util"
"github.com/samber/lo"
"golang.org/x/exp/slices"
)
Expand Down Expand Up @@ -47,9 +48,11 @@ func (r *Item) FindByID(_ context.Context, itemID id.ItemID, ref *version.Ref) (
}

item, ok := r.data.Load(itemID, ref.OrLatest().OrVersion())
if !ok {
b, _ := r.IsArchived(context.Background(), itemID)
if !ok || b {
return nil, rerror.ErrNotFound
}

return item, nil
}

Expand Down Expand Up @@ -111,7 +114,10 @@ func (r *Item) FindByIDs(_ context.Context, list id.ItemIDList, ref *version.Ref
return nil, r.err
}

return r.data.LoadAll(list, lo.ToPtr(ref.OrLatest().OrVersion())), nil
return util.Filter(r.data.LoadAll(list, lo.ToPtr(ref.OrLatest().OrVersion())), func(versioned item.Versioned) bool {
b, _ := r.IsArchived(context.Background(), versioned.Value().ID())
return !b
}), nil
}

func (r *Item) FindAllVersionsByID(_ context.Context, id id.ItemID) (item.VersionedList, error) {
Expand Down Expand Up @@ -265,7 +271,10 @@ func (r *Item) Search(_ context.Context, q *item.Query, sort *usecasex.Sort, pag
}
return true
})
return res, nil, nil
return util.Filter(res, func(versioned item.Versioned) bool {
b, _ := r.IsArchived(context.Background(), versioned.Value().ID())
return !b
}), nil, nil
}

func (r *Item) FindByModelAndValue(_ context.Context, modelID id.ModelID, fields []repo.FieldAndValue, ref *version.Ref) (item.VersionedList, error) {
Expand Down
56 changes: 55 additions & 1 deletion server/internal/infrastructure/mongo/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/reearth/reearthx/mongox"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/usecasex"
"github.com/reearth/reearthx/util"
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
Expand Down Expand Up @@ -268,6 +270,13 @@ func (r *Item) paginate(ctx context.Context, filter bson.M, ref *version.Ref, so
if err != nil {
return nil, nil, rerror.ErrInternalBy(err)
}
if len(c.Result) > 0 {
ml, err := r.findMeta(ctx, nil, lo.ToPtr(c.Result[0].Value().Project()))
if err != nil {
return nil, nil, err
}
return filterArchived(ml, c.Result), pageInfo, nil
}
return c.Result, pageInfo, nil
}

Expand All @@ -276,6 +285,14 @@ func (r *Item) find(ctx context.Context, filter any, ref *version.Ref) (item.Ver
if err := r.client.Find(ctx, r.readFilter(filter), version.Eq(ref.OrLatest().OrVersion()), c); err != nil {
return nil, err
}

if len(c.Result) > 0 {
ml, err := r.findMeta(ctx, nil, lo.ToPtr(c.Result[0].Value().Project()))
if err != nil {
return nil, err
}
return filterArchived(ml, c.Result), nil
}
return c.Result, nil
}

Expand All @@ -284,7 +301,34 @@ func (r *Item) findOne(ctx context.Context, filter any, ref *version.Ref) (item.
if err := r.client.FindOne(ctx, r.readFilter(filter), version.Eq(ref.OrLatest().OrVersion()), c); err != nil {
return nil, err
}
return c.Result[0], nil

iid := c.Result[0].Value().ID()
ml, err := r.findMeta(ctx, id.ItemIDList{iid}, nil)
if err != nil {
return nil, err
}
res := filterArchived(ml, c.Result)
if len(res) > 0 {
return res[0], nil
}
return nil, rerror.ErrNotFound
}

func (r *Item) findMeta(ctx context.Context, ids id.ItemIDList, pid *id.ProjectID) ([]mongodoc.ItemMetaDocument, error) {
filter := bson.M{}
if len(ids) > 0 {
filter["id"] = bson.M{
"$in": ids.Strings(),
}
}
if pid != nil {
filter["project"] = pid.String()
}
c := mongox.SliceConsumer[mongodoc.ItemMetaDocument]{}
if err := r.client.FindMeta(ctx, filter, &c); err != nil {
return nil, err
}
return c.Result, nil
}

func filterItems(ids []id.ItemID, rows item.VersionedList) item.VersionedList {
Expand All @@ -300,6 +344,16 @@ func filterItems(ids []id.ItemID, rows item.VersionedList) item.VersionedList {
return res
}

func filterArchived(ids []mongodoc.ItemMetaDocument, rows item.VersionedList) item.VersionedList {
res := util.Filter(rows, func(versioned item.Versioned) bool {
_, b := lo.Find(ids, func(document mongodoc.ItemMetaDocument) bool {
return versioned.Value().ID().String() == document.ID && document.Archived
})
return !b
})
return res
}

func (r *Item) readFilter(filter any) any {
return applyProjectFilter(filter, r.f.Readable)
}
Expand Down
6 changes: 6 additions & 0 deletions server/internal/infrastructure/mongo/mongodoc/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ type ItemFieldDocument struct {
Value any `bson:"value,omitempty"` // compat
}

type ItemMetaDocument struct {
ID string
Project string
Archived bool `bson:"archived,omitempty"`
}

type ItemConsumer = mongox.SliceFuncConsumer[*ItemDocument, *item.Item]

func NewItemConsumer() *ItemConsumer {
Expand Down
14 changes: 14 additions & 0 deletions server/internal/infrastructure/mongo/mongogit/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ func (c *Collection) ArchiveOne(ctx context.Context, filter bson.M, archived boo
return nil
}

func (c *Collection) FindMeta(ctx context.Context, filter any, cons mongox.Consumer) error {
q := mongox.And(filter, "", bson.M{
metaKey: true,
})

if err := c.client.Find(ctx, q, cons); err != nil {
if errors.Is(rerror.ErrNotFound, err) || err == io.EOF {
return nil
}
return err
}
return nil
}

func (c *Collection) Timestamp(ctx context.Context, filter any, q version.Query) (time.Time, error) {
consumer := mongox.SliceConsumer[Meta]{}
f := apply(q, filter)
Expand Down
15 changes: 11 additions & 4 deletions server/internal/usecase/interactor/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,18 @@ func (i Item) Delete(ctx context.Context, itemID id.ItemID, operator *usecase.Op
return err
}

if !operator.CanUpdate(itm.Value()) {
return interfaces.ErrOperationDenied
a, err := i.repos.Item.IsArchived(ctx, itemID)
if err != nil {
return err
}

return i.repos.Item.Remove(ctx, itemID)
if !a {
pid := itm.Value().Project()
if !operator.CanUpdate(itm.Value()) {
return interfaces.ErrOperationDenied
}
return i.repos.Item.Archive(ctx, itemID, pid, true)
}
return nil
})
}

Expand Down
5 changes: 2 additions & 3 deletions server/internal/usecase/interactor/item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,8 @@ func TestItem_Delete(t *testing.T) {
itemUC.ignoreEvent = true
err = itemUC.Delete(ctx, id1, op)
assert.NoError(t, err)
_, err = itemUC.FindByID(ctx, id1, op)
assert.Error(t, err)

// invalid operator
err = db.Item.Save(ctx, i2)
Expand All @@ -959,9 +961,6 @@ func TestItem_Delete(t *testing.T) {
})
assert.Equal(t, rerror.ErrNotFound, err)

_, err = itemUC.FindByID(ctx, id1, op)
assert.Error(t, err)

// mock item error
wantErr := rerror.ErrNotFound
err = itemUC.Delete(ctx, id.NewItemID(), op)
Expand Down
11 changes: 11 additions & 0 deletions server/pkg/item/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,14 @@ func (l VersionedList) Item(iid id.ItemID) Versioned {
}
return nil
}

func (l VersionedList) IDs() id.ItemIDList {
if l == nil {
return nil
}
res := id.ItemIDList{}
for _, versioned := range l {
res = res.Add(versioned.Value().ID())
}
return res
}
41 changes: 41 additions & 0 deletions server/pkg/item/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/reearth/reearth-cms/server/pkg/id"
"github.com/reearth/reearth-cms/server/pkg/value"
"github.com/reearth/reearth-cms/server/pkg/version"
"github.com/reearth/reearthx/util"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -142,3 +143,43 @@ func TestVersionedList_Unwrap(t *testing.T) {

assert.Equal(t, List{i}, vl.Unwrap())
}

func TestVersionedList_IDs(t *testing.T) {
id1 := id.NewItemID()
id2 := id.NewItemID()
id3 := id.NewItemID()
id4 := id.NewItemID()
i1 := New().ID(id1).
Schema(id.NewSchemaID()).
Model(id.NewModelID()).
Project(id.NewProjectID()).
Thread(id.NewThreadID()).
MustBuild()
i2 := New().ID(id2).
Schema(id.NewSchemaID()).
Model(id.NewModelID()).
Project(id.NewProjectID()).
Thread(id.NewThreadID()).
MustBuild()
i3 := New().ID(id3).
Schema(id.NewSchemaID()).
Model(id.NewModelID()).
Project(id.NewProjectID()).
Thread(id.NewThreadID()).
MustBuild()
i4 := New().ID(id4).
Schema(id.NewSchemaID()).
Model(id.NewModelID()).
Project(id.NewProjectID()).
Thread(id.NewThreadID()).
MustBuild()
v := version.New()
vl := VersionedList{
version.MustBeValue(v, nil, version.NewRefs(version.Latest), util.Now(), i1),
version.MustBeValue(v, nil, version.NewRefs(version.Latest), util.Now(), i2),
version.MustBeValue(v, nil, version.NewRefs(version.Latest), util.Now(), i3),
version.MustBeValue(v, nil, version.NewRefs(version.Latest), util.Now(), i4),
}
assert.Equal(t, id.ItemIDList{id1, id2, id3, id4}, vl.IDs())
assert.Nil(t, new(VersionedList).IDs())
}