|
|
|
|
@@ -1,562 +1,300 @@
|
|
|
|
|
//go:build integration
|
|
|
|
|
// +build integration
|
|
|
|
|
|
|
|
|
|
package organizationdb
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"sync"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/tech/sendico/pkg/db/internal/mongo/commentdb"
|
|
|
|
|
"github.com/tech/sendico/pkg/db/internal/mongo/projectdb"
|
|
|
|
|
"github.com/tech/sendico/pkg/db/internal/mongo/reactiondb"
|
|
|
|
|
"github.com/tech/sendico/pkg/db/internal/mongo/statusdb"
|
|
|
|
|
"github.com/tech/sendico/pkg/db/internal/mongo/taskdb"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
"github.com/tech/sendico/pkg/auth"
|
|
|
|
|
"github.com/tech/sendico/pkg/db/repository/builder"
|
|
|
|
|
rd "github.com/tech/sendico/pkg/db/repository/decoder"
|
|
|
|
|
ri "github.com/tech/sendico/pkg/db/repository/index"
|
|
|
|
|
"github.com/tech/sendico/pkg/db/storable"
|
|
|
|
|
"github.com/tech/sendico/pkg/db/template"
|
|
|
|
|
"github.com/tech/sendico/pkg/merrors"
|
|
|
|
|
"github.com/tech/sendico/pkg/model"
|
|
|
|
|
"github.com/tech/sendico/pkg/mservice"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
"github.com/testcontainers/testcontainers-go/modules/mongodb"
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func setupSetArchivedTestDB(t *testing.T) (*OrganizationDB, *projectDBAdapter, *taskdb.TaskDB, *commentdb.CommentDB, *reactiondb.ReactionDB, func()) {
|
|
|
|
|
func TestOrganizationDB_SetArchived_TogglesState(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
accountRef := primitive.NewObjectID()
|
|
|
|
|
orgDB := newTestOrganizationDB(t)
|
|
|
|
|
|
|
|
|
|
// Start MongoDB container
|
|
|
|
|
mongodbContainer, err := mongodb.Run(ctx, "mongo:latest")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
org := &model.Organization{
|
|
|
|
|
OrganizationBase: model.OrganizationBase{
|
|
|
|
|
Describable: model.Describable{Name: "Sendico"},
|
|
|
|
|
TimeZone: "UTC",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
org.SetID(primitive.NewObjectID())
|
|
|
|
|
|
|
|
|
|
// Get connection string
|
|
|
|
|
endpoint, err := mongodbContainer.Endpoint(ctx, "")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NoError(t, orgDB.Create(ctx, accountRef, *org.GetID(), org))
|
|
|
|
|
|
|
|
|
|
// Connect to MongoDB
|
|
|
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://"+endpoint))
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
var stored model.Organization
|
|
|
|
|
require.NoError(t, orgDB.Get(ctx, accountRef, *org.GetID(), &stored))
|
|
|
|
|
assert.False(t, stored.IsArchived())
|
|
|
|
|
|
|
|
|
|
db := client.Database("test_organization_setarchived")
|
|
|
|
|
require.NoError(t, orgDB.SetArchived(ctx, accountRef, *org.GetID(), true, false))
|
|
|
|
|
require.NoError(t, orgDB.Get(ctx, accountRef, *org.GetID(), &stored))
|
|
|
|
|
assert.True(t, stored.IsArchived())
|
|
|
|
|
|
|
|
|
|
require.NoError(t, orgDB.SetArchived(ctx, accountRef, *org.GetID(), false, false))
|
|
|
|
|
require.NoError(t, orgDB.Get(ctx, accountRef, *org.GetID(), &stored))
|
|
|
|
|
assert.False(t, stored.IsArchived())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestOrganizationDB_SetArchived_UnknownOrganization(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
accountRef := primitive.NewObjectID()
|
|
|
|
|
orgDB := newTestOrganizationDB(t)
|
|
|
|
|
|
|
|
|
|
err := orgDB.SetArchived(ctx, accountRef, primitive.NewObjectID(), true, false)
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
assert.True(t, errors.Is(err, merrors.ErrNoData))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// newTestOrganizationDB wires the real OrganizationDB implementation to an in-memory repository
|
|
|
|
|
// so the tests exercise actual SetArchived behavior without external dependencies.
|
|
|
|
|
func newTestOrganizationDB(t *testing.T) *OrganizationDB {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
repo := newMemoryOrganizationRepository()
|
|
|
|
|
logger := zap.NewNop()
|
|
|
|
|
|
|
|
|
|
// Create mock enforcer and policy DB
|
|
|
|
|
mockEnforcer := &mockSetArchivedEnforcer{}
|
|
|
|
|
mockPolicyDB := &mockSetArchivedPolicyDB{}
|
|
|
|
|
mockPGroupDB := &mockSetArchivedPGroupDB{}
|
|
|
|
|
|
|
|
|
|
// Create databases
|
|
|
|
|
// We need to create a projectDB first, but we'll create a temporary one for organizationDB creation
|
|
|
|
|
// Create temporary taskDB and statusDB for the temporary projectDB
|
|
|
|
|
// Create temporary reactionDB and commentDB for the temporary taskDB
|
|
|
|
|
tempReactionDB, err := reactiondb.Create(ctx, logger, mockEnforcer, mockPolicyDB, db)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
tempCommentDB, err := commentdb.Create(ctx, logger, mockEnforcer, mockPolicyDB, db, tempReactionDB)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
tempTaskDB, err := taskdb.Create(ctx, logger, mockEnforcer, mockPolicyDB, db, tempCommentDB, tempReactionDB)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
tempStatusDB, err := statusdb.Create(ctx, logger, mockEnforcer, mockPolicyDB, db)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
tempProjectDB, err := projectdb.Create(ctx, logger, mockEnforcer, mockPolicyDB, tempTaskDB, tempStatusDB, db)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Create adapter for organizationDB creation
|
|
|
|
|
tempProjectDBAdapter := &projectDBAdapter{
|
|
|
|
|
ProjectDB: tempProjectDB,
|
|
|
|
|
taskDB: tempTaskDB,
|
|
|
|
|
commentDB: tempCommentDB,
|
|
|
|
|
reactionDB: tempReactionDB,
|
|
|
|
|
statusDB: tempStatusDB,
|
|
|
|
|
dbImp := &template.DBImp[*model.Organization]{
|
|
|
|
|
Logger: logger,
|
|
|
|
|
Repository: repo,
|
|
|
|
|
}
|
|
|
|
|
dbImp.SetDeleter(func(ctx context.Context, objectRef primitive.ObjectID) error {
|
|
|
|
|
return repo.Delete(ctx, objectRef)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
organizationDB, err := Create(ctx, logger, mockEnforcer, mockPolicyDB, tempProjectDBAdapter, mockPGroupDB, db)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
var projectDB *projectdb.ProjectDB
|
|
|
|
|
var taskDB *taskdb.TaskDB
|
|
|
|
|
var commentDB *commentdb.CommentDB
|
|
|
|
|
var reactionDB *reactiondb.ReactionDB
|
|
|
|
|
var statusDB *statusdb.StatusDB
|
|
|
|
|
|
|
|
|
|
// Create databases in dependency order
|
|
|
|
|
reactionDB, err = reactiondb.Create(ctx, logger, mockEnforcer, mockPolicyDB, db)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
commentDB, err = commentdb.Create(ctx, logger, mockEnforcer, mockPolicyDB, db, reactionDB)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
taskDB, err = taskdb.Create(ctx, logger, mockEnforcer, mockPolicyDB, db, commentDB, reactionDB)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
statusDB, err = statusdb.Create(ctx, logger, mockEnforcer, mockPolicyDB, db)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
projectDB, err = projectdb.Create(ctx, logger, mockEnforcer, mockPolicyDB, taskDB, statusDB, db)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Create adapter for the actual projectDB
|
|
|
|
|
projectDBAdapter := &projectDBAdapter{
|
|
|
|
|
ProjectDB: projectDB,
|
|
|
|
|
taskDB: taskDB,
|
|
|
|
|
commentDB: commentDB,
|
|
|
|
|
reactionDB: reactionDB,
|
|
|
|
|
statusDB: statusDB,
|
|
|
|
|
return &OrganizationDB{
|
|
|
|
|
ProtectedDBImp: auth.ProtectedDBImp[*model.Organization]{
|
|
|
|
|
DBImp: dbImp,
|
|
|
|
|
Enforcer: allowAllEnforcer{},
|
|
|
|
|
PermissionRef: primitive.NewObjectID(),
|
|
|
|
|
Collection: mservice.Organizations,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cleanup := func() {
|
|
|
|
|
client.Disconnect(context.Background())
|
|
|
|
|
mongodbContainer.Terminate(ctx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return organizationDB, projectDBAdapter, taskDB, commentDB, reactionDB, cleanup
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// projectDBAdapter adapts projectdb.ProjectDB to project.DB interface for testing
|
|
|
|
|
type projectDBAdapter struct {
|
|
|
|
|
*projectdb.ProjectDB
|
|
|
|
|
taskDB *taskdb.TaskDB
|
|
|
|
|
commentDB *commentdb.CommentDB
|
|
|
|
|
reactionDB *reactiondb.ReactionDB
|
|
|
|
|
statusDB *statusdb.StatusDB
|
|
|
|
|
}
|
|
|
|
|
type allowAllEnforcer struct{}
|
|
|
|
|
|
|
|
|
|
// DeleteCascade implements the project.DB interface
|
|
|
|
|
func (a *projectDBAdapter) DeleteCascade(ctx context.Context, projectRef primitive.ObjectID) error {
|
|
|
|
|
// Call the concrete implementation
|
|
|
|
|
return a.ProjectDB.DeleteCascade(ctx, projectRef)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetArchived implements the project.DB interface
|
|
|
|
|
func (a *projectDBAdapter) SetArchived(ctx context.Context, accountRef, organizationRef, projectRef primitive.ObjectID, archived, cascade bool) error {
|
|
|
|
|
// Use the stored dependencies for the concrete implementation
|
|
|
|
|
return a.ProjectDB.SetArchived(ctx, accountRef, organizationRef, projectRef, archived, cascade)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// List implements the project.DB interface
|
|
|
|
|
func (a *projectDBAdapter) List(ctx context.Context, accountRef, organizationRef, _ primitive.ObjectID, cursor *model.ViewCursor) ([]model.Project, error) {
|
|
|
|
|
return a.ProjectDB.List(ctx, accountRef, organizationRef, primitive.NilObjectID, cursor)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Previews implements the project.DB interface
|
|
|
|
|
func (a *projectDBAdapter) Previews(ctx context.Context, accountRef, organizationRef primitive.ObjectID, projectRefs []primitive.ObjectID, cursor *model.ViewCursor, assigneeRefs, reporterRefs []primitive.ObjectID) ([]model.ProjectPreview, error) {
|
|
|
|
|
return a.ProjectDB.Previews(ctx, accountRef, organizationRef, projectRefs, cursor, assigneeRefs, reporterRefs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteProject implements the project.DB interface
|
|
|
|
|
func (a *projectDBAdapter) DeleteProject(ctx context.Context, accountRef, organizationRef, projectRef primitive.ObjectID, migrateToRef *primitive.ObjectID) error {
|
|
|
|
|
// Call the concrete implementation with the organizationRef
|
|
|
|
|
return a.ProjectDB.DeleteProject(ctx, accountRef, organizationRef, projectRef, migrateToRef)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RemoveTagFromProjects implements the project.DB interface
|
|
|
|
|
func (a *projectDBAdapter) RemoveTagFromProjects(ctx context.Context, accountRef, organizationRef, tagRef primitive.ObjectID) error {
|
|
|
|
|
// Call the concrete implementation
|
|
|
|
|
return a.ProjectDB.RemoveTagFromProjects(ctx, accountRef, organizationRef, tagRef)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mock implementations for SetArchived testing
|
|
|
|
|
type mockSetArchivedEnforcer struct{}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedEnforcer) Enforce(ctx context.Context, permissionRef, accountRef, orgRef, objectRef primitive.ObjectID, action model.Action) (bool, error) {
|
|
|
|
|
func (allowAllEnforcer) Enforce(context.Context, primitive.ObjectID, primitive.ObjectID, primitive.ObjectID, primitive.ObjectID, model.Action) (bool, error) {
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedEnforcer) EnforceBatch(ctx context.Context, objectRefs []model.PermissionBoundStorable, accountRef primitive.ObjectID, action model.Action) (map[primitive.ObjectID]bool, error) {
|
|
|
|
|
// Allow all objects for testing
|
|
|
|
|
result := make(map[primitive.ObjectID]bool)
|
|
|
|
|
for _, obj := range objectRefs {
|
|
|
|
|
func (allowAllEnforcer) EnforceBatch(_ context.Context, objects []model.PermissionBoundStorable, _ primitive.ObjectID, _ model.Action) (map[primitive.ObjectID]bool, error) {
|
|
|
|
|
result := make(map[primitive.ObjectID]bool, len(objects))
|
|
|
|
|
for _, obj := range objects {
|
|
|
|
|
result[*obj.GetID()] = true
|
|
|
|
|
}
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedEnforcer) GetRoles(ctx context.Context, accountRef, organizationRef primitive.ObjectID) ([]model.Role, error) {
|
|
|
|
|
func (allowAllEnforcer) GetRoles(context.Context, primitive.ObjectID, primitive.ObjectID) ([]model.Role, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedEnforcer) GetPermissions(ctx context.Context, accountRef, organizationRef primitive.ObjectID) ([]model.Role, []model.Permission, error) {
|
|
|
|
|
func (allowAllEnforcer) GetPermissions(context.Context, primitive.ObjectID, primitive.ObjectID) ([]model.Role, []model.Permission, error) {
|
|
|
|
|
return nil, nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type mockSetArchivedPolicyDB struct{}
|
|
|
|
|
type memoryOrganizationRepository struct {
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
data map[primitive.ObjectID]*model.Organization
|
|
|
|
|
order []primitive.ObjectID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) Create(ctx context.Context, policy *model.PolicyDescription) error {
|
|
|
|
|
func newMemoryOrganizationRepository() *memoryOrganizationRepository {
|
|
|
|
|
return &memoryOrganizationRepository{
|
|
|
|
|
data: make(map[primitive.ObjectID]*model.Organization),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *memoryOrganizationRepository) Aggregate(context.Context, builder.Pipeline, rd.DecodingFunc) error {
|
|
|
|
|
return merrors.NotImplemented("aggregate is not supported in memory repository")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *memoryOrganizationRepository) Insert(_ context.Context, obj storable.Storable, _ builder.Query) error {
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
org, ok := obj.(*model.Organization)
|
|
|
|
|
if !ok {
|
|
|
|
|
return merrors.InvalidDataType("expected organization")
|
|
|
|
|
}
|
|
|
|
|
id := org.GetID()
|
|
|
|
|
if id == nil || *id == primitive.NilObjectID {
|
|
|
|
|
return merrors.InvalidArgument("organization ID must be set")
|
|
|
|
|
}
|
|
|
|
|
if _, exists := m.data[*id]; exists {
|
|
|
|
|
return merrors.DataConflict("organization already exists")
|
|
|
|
|
}
|
|
|
|
|
m.data[*id] = cloneOrganization(org)
|
|
|
|
|
m.order = append(m.order, *id)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) Get(ctx context.Context, policyRef primitive.ObjectID, result *model.PolicyDescription) error {
|
|
|
|
|
func (m *memoryOrganizationRepository) InsertMany(ctx context.Context, objects []storable.Storable) error {
|
|
|
|
|
for _, obj := range objects {
|
|
|
|
|
if err := m.Insert(ctx, obj, nil); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *memoryOrganizationRepository) Get(_ context.Context, id primitive.ObjectID, result storable.Storable) error {
|
|
|
|
|
m.mu.RLock()
|
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
|
|
|
|
|
org, ok := m.data[id]
|
|
|
|
|
if !ok {
|
|
|
|
|
return merrors.ErrNoData
|
|
|
|
|
}
|
|
|
|
|
dst, ok := result.(*model.Organization)
|
|
|
|
|
if !ok {
|
|
|
|
|
return merrors.InvalidDataType("expected organization result")
|
|
|
|
|
}
|
|
|
|
|
*dst = *cloneOrganization(org)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *memoryOrganizationRepository) FindOneByFilter(_ context.Context, query builder.Query, result storable.Storable) error {
|
|
|
|
|
m.mu.RLock()
|
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
for _, id := range m.order {
|
|
|
|
|
if org, ok := m.data[id]; ok && m.matchesQuery(query, org) {
|
|
|
|
|
dst, okCast := result.(*model.Organization)
|
|
|
|
|
if !okCast {
|
|
|
|
|
return merrors.InvalidDataType("expected organization result")
|
|
|
|
|
}
|
|
|
|
|
*dst = *cloneOrganization(org)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return merrors.ErrNoData
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) InsertMany(ctx context.Context, objects []*model.PolicyDescription) error { return nil }
|
|
|
|
|
func (m *memoryOrganizationRepository) FindManyByFilter(context.Context, builder.Query, rd.DecodingFunc) error {
|
|
|
|
|
return merrors.NotImplemented("FindManyByFilter is not supported in memory repository")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) Update(ctx context.Context, policy *model.PolicyDescription) error {
|
|
|
|
|
func (m *memoryOrganizationRepository) Update(_ context.Context, obj storable.Storable) error {
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
org, ok := obj.(*model.Organization)
|
|
|
|
|
if !ok {
|
|
|
|
|
return merrors.InvalidDataType("expected organization")
|
|
|
|
|
}
|
|
|
|
|
id := org.GetID()
|
|
|
|
|
if id == nil {
|
|
|
|
|
return merrors.InvalidArgument("organization ID must be set")
|
|
|
|
|
}
|
|
|
|
|
if _, exists := m.data[*id]; !exists {
|
|
|
|
|
return merrors.ErrNoData
|
|
|
|
|
}
|
|
|
|
|
m.data[*id] = cloneOrganization(org)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) Patch(ctx context.Context, objectRef primitive.ObjectID, patch builder.Patch) error {
|
|
|
|
|
func (m *memoryOrganizationRepository) Patch(context.Context, primitive.ObjectID, builder.Patch) error {
|
|
|
|
|
return merrors.NotImplemented("Patch is not supported in memory repository")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *memoryOrganizationRepository) PatchMany(context.Context, builder.Query, builder.Patch) (int, error) {
|
|
|
|
|
return 0, merrors.NotImplemented("PatchMany is not supported in memory repository")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *memoryOrganizationRepository) Delete(_ context.Context, id primitive.ObjectID) error {
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
if _, exists := m.data[id]; !exists {
|
|
|
|
|
return merrors.ErrNoData
|
|
|
|
|
}
|
|
|
|
|
delete(m.data, id)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) Delete(ctx context.Context, policyRef primitive.ObjectID) error {
|
|
|
|
|
func (m *memoryOrganizationRepository) DeleteMany(context.Context, builder.Query) error {
|
|
|
|
|
return merrors.NotImplemented("DeleteMany is not supported in memory repository")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *memoryOrganizationRepository) CreateIndex(*ri.Definition) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) DeleteMany(ctx context.Context, filter builder.Query) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *memoryOrganizationRepository) ListIDs(_ context.Context, query builder.Query) ([]primitive.ObjectID, error) {
|
|
|
|
|
m.mu.RLock()
|
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) FindOne(ctx context.Context, filter builder.Query, result *model.PolicyDescription) error {
|
|
|
|
|
return merrors.ErrNoData
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) ListIDs(ctx context.Context, query builder.Query) ([]primitive.ObjectID, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) ListPermissionBound(ctx context.Context, query builder.Query) ([]model.PermissionBoundStorable, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) Collection() string { return "" }
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) All(ctx context.Context, organizationRef primitive.ObjectID) ([]model.PolicyDescription, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) Policies(ctx context.Context, refs []primitive.ObjectID) ([]model.PolicyDescription, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) GetBuiltInPolicy(ctx context.Context, resourceType mservice.Type, policy *model.PolicyDescription) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPolicyDB) DeleteCascade(ctx context.Context, policyRef primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type mockSetArchivedPGroupDB struct{}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) Create(ctx context.Context, accountRef, organizationRef primitive.ObjectID, pgroup *model.PriorityGroup) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) InsertMany(ctx context.Context, accountRef, organizationRef primitive.ObjectID, objects []*model.PriorityGroup) error { return nil }
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) Get(ctx context.Context, accountRef, pgroupRef primitive.ObjectID, result *model.PriorityGroup) error {
|
|
|
|
|
return merrors.ErrNoData
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) Update(ctx context.Context, accountRef primitive.ObjectID, pgroup *model.PriorityGroup) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) Delete(ctx context.Context, accountRef, pgroupRef primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) DeleteCascadeAuth(ctx context.Context, accountRef, pgroupRef primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) Patch(ctx context.Context, accountRef, pgroupRef primitive.ObjectID, patch builder.Patch) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) PatchMany(ctx context.Context, accountRef primitive.ObjectID, query builder.Query, patch builder.Patch) (int, error) {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) Unprotected() template.DB[*model.PriorityGroup] {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) ListIDs(ctx context.Context, action model.Action, accountRef primitive.ObjectID, query builder.Query) ([]primitive.ObjectID, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) All(ctx context.Context, organizationRef primitive.ObjectID, limit, offset *int64) ([]model.PriorityGroup, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) List(ctx context.Context, accountRef, organizationRef, _ primitive.ObjectID, cursor *model.ViewCursor) ([]model.PriorityGroup, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) DeleteCascade(ctx context.Context, statusRef primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) SetArchived(ctx context.Context, accountRef, organizationRef, statusRef primitive.ObjectID, archived, cascade bool) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedPGroupDB) Reorder(ctx context.Context, accountRef, priorityGroupRef primitive.ObjectID, oldIndex, newIndex int) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mock project DB for statusdb creation
|
|
|
|
|
type mockSetArchivedProjectDB struct{}
|
|
|
|
|
|
|
|
|
|
func (m *mockSetArchivedProjectDB) Create(ctx context.Context, accountRef, organizationRef primitive.ObjectID, project *model.Project) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) Get(ctx context.Context, accountRef, projectRef primitive.ObjectID, result *model.Project) error {
|
|
|
|
|
return merrors.ErrNoData
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) Update(ctx context.Context, accountRef primitive.ObjectID, project *model.Project) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) Delete(ctx context.Context, accountRef, projectRef primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) DeleteCascadeAuth(ctx context.Context, accountRef, projectRef primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) Patch(ctx context.Context, accountRef, objectRef primitive.ObjectID, patch builder.Patch) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) PatchMany(ctx context.Context, accountRef primitive.ObjectID, query builder.Query, patch builder.Patch) (int, error) {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) Unprotected() template.DB[*model.Project] { return nil }
|
|
|
|
|
func (m *mockSetArchivedProjectDB) ListIDs(ctx context.Context, action model.Action, accountRef primitive.ObjectID, query builder.Query) ([]primitive.ObjectID, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) List(ctx context.Context, accountRef, organizationRef, _ primitive.ObjectID, cursor *model.ViewCursor) ([]model.Project, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) Previews(ctx context.Context, accountRef, organizationRef primitive.ObjectID, projectRefs []primitive.ObjectID, cursor *model.ViewCursor, assigneeRefs, reporterRefs []primitive.ObjectID) ([]model.ProjectPreview, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) DeleteProject(ctx context.Context, accountRef, organizationRef, projectRef primitive.ObjectID, migrateToRef *primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) DeleteCascade(ctx context.Context, projectRef primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) SetArchived(ctx context.Context, accountRef, organizationRef, projectRef primitive.ObjectID, archived, cascade bool) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) All(ctx context.Context, organizationRef primitive.ObjectID, limit, offset *int64) ([]model.Project, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) Reorder(ctx context.Context, accountRef, objectRef primitive.ObjectID, newIndex int, filter builder.Query) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) AddTag(ctx context.Context, accountRef, objectRef, tagRef primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) RemoveTag(ctx context.Context, accountRef, objectRef, tagRef primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) RemoveTags(ctx context.Context, accountRef, organizationRef, tagRef primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) AddTags(ctx context.Context, accountRef, objectRef primitive.ObjectID, tagRefs []primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) SetTags(ctx context.Context, accountRef, objectRef primitive.ObjectID, tagRefs []primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) RemoveAllTags(ctx context.Context, accountRef, objectRef primitive.ObjectID) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) GetTags(ctx context.Context, accountRef, objectRef primitive.ObjectID) ([]primitive.ObjectID, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) HasTag(ctx context.Context, accountRef, objectRef, tagRef primitive.ObjectID) (bool, error) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) FindByTag(ctx context.Context, accountRef, tagRef primitive.ObjectID) ([]*model.Project, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
func (m *mockSetArchivedProjectDB) FindByTags(ctx context.Context, accountRef primitive.ObjectID, tagRefs []primitive.ObjectID) ([]*model.Project, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestOrganizationDB_SetArchived(t *testing.T) {
|
|
|
|
|
organizationDB, projectDBAdapter, taskDB, commentDB, reactionDB, cleanup := setupSetArchivedTestDB(t)
|
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
accountRef := primitive.NewObjectID()
|
|
|
|
|
|
|
|
|
|
t.Run("SetArchived_OrganizationWithProjectsTasksCommentsAndReactions_Cascade", func(t *testing.T) {
|
|
|
|
|
// Create an organization using unprotected DB
|
|
|
|
|
organization := &model.Organization{
|
|
|
|
|
OrganizationBase: model.OrganizationBase{
|
|
|
|
|
Describable: model.Describable{Name: "Test Organization for Archive"},
|
|
|
|
|
TimeZone: "UTC",
|
|
|
|
|
},
|
|
|
|
|
var ids []primitive.ObjectID
|
|
|
|
|
for _, id := range m.order {
|
|
|
|
|
if org, ok := m.data[id]; ok && m.matchesQuery(query, org) {
|
|
|
|
|
ids = append(ids, id)
|
|
|
|
|
}
|
|
|
|
|
organization.ID = primitive.NewObjectID()
|
|
|
|
|
|
|
|
|
|
err := organizationDB.Create(ctx, accountRef, organization.ID, organization)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Create a project for the organization using unprotected DB
|
|
|
|
|
project := &model.Project{
|
|
|
|
|
ProjectBase: model.ProjectBase{
|
|
|
|
|
PermissionBound: model.PermissionBound{
|
|
|
|
|
OrganizationBoundBase: model.OrganizationBoundBase{
|
|
|
|
|
OrganizationRef: organization.ID,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Describable: model.Describable{Name: "Test Project"},
|
|
|
|
|
Indexable: model.Indexable{Index: 0},
|
|
|
|
|
Mnemonic: "TEST",
|
|
|
|
|
State: model.ProjectStateActive,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
project.ID = primitive.NewObjectID()
|
|
|
|
|
|
|
|
|
|
err = projectDBAdapter.Unprotected().Create(ctx, project)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Create a task for the project using unprotected DB
|
|
|
|
|
task := &model.Task{
|
|
|
|
|
PermissionBound: model.PermissionBound{
|
|
|
|
|
OrganizationBoundBase: model.OrganizationBoundBase{
|
|
|
|
|
OrganizationRef: organization.ID,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Describable: model.Describable{Name: "Test Task for Archive"},
|
|
|
|
|
ProjectRef: project.ID,
|
|
|
|
|
}
|
|
|
|
|
task.ID = primitive.NewObjectID()
|
|
|
|
|
|
|
|
|
|
err = taskDB.Unprotected().Create(ctx, task)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Create comments for the task using unprotected DB
|
|
|
|
|
comment := &model.Comment{
|
|
|
|
|
CommentBase: model.CommentBase{
|
|
|
|
|
PermissionBound: model.PermissionBound{
|
|
|
|
|
OrganizationBoundBase: model.OrganizationBoundBase{
|
|
|
|
|
OrganizationRef: organization.ID,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
AuthorRef: accountRef,
|
|
|
|
|
TaskRef: task.ID,
|
|
|
|
|
Content: "Test Comment for Archive",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
comment.ID = primitive.NewObjectID()
|
|
|
|
|
|
|
|
|
|
err = commentDB.Unprotected().Create(ctx, comment)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Create reaction for the comment using unprotected DB
|
|
|
|
|
reaction := &model.Reaction{
|
|
|
|
|
PermissionBound: model.PermissionBound{
|
|
|
|
|
OrganizationBoundBase: model.OrganizationBoundBase{
|
|
|
|
|
OrganizationRef: organization.ID,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Type: "like",
|
|
|
|
|
AuthorRef: accountRef,
|
|
|
|
|
CommentRef: comment.ID,
|
|
|
|
|
}
|
|
|
|
|
reaction.ID = primitive.NewObjectID()
|
|
|
|
|
|
|
|
|
|
err = reactionDB.Unprotected().Create(ctx, reaction)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Verify all entities are not archived initially
|
|
|
|
|
var retrievedOrganization model.Organization
|
|
|
|
|
err = organizationDB.Get(ctx, accountRef, organization.ID, &retrievedOrganization)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.False(t, retrievedOrganization.IsArchived())
|
|
|
|
|
|
|
|
|
|
var retrievedProject model.Project
|
|
|
|
|
err = projectDBAdapter.Unprotected().Get(ctx, project.ID, &retrievedProject)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.False(t, retrievedProject.IsArchived())
|
|
|
|
|
|
|
|
|
|
var retrievedTask model.Task
|
|
|
|
|
err = taskDB.Unprotected().Get(ctx, task.ID, &retrievedTask)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.False(t, retrievedTask.IsArchived())
|
|
|
|
|
|
|
|
|
|
var retrievedComment model.Comment
|
|
|
|
|
err = commentDB.Unprotected().Get(ctx, comment.ID, &retrievedComment)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.False(t, retrievedComment.IsArchived())
|
|
|
|
|
|
|
|
|
|
// Archive organization with cascade
|
|
|
|
|
err = organizationDB.SetArchived(ctx, accountRef, organization.ID, true, true)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Verify all entities are archived due to cascade
|
|
|
|
|
err = organizationDB.Get(ctx, accountRef, organization.ID, &retrievedOrganization)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.True(t, retrievedOrganization.IsArchived())
|
|
|
|
|
|
|
|
|
|
err = projectDBAdapter.Unprotected().Get(ctx, project.ID, &retrievedProject)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.True(t, retrievedProject.IsArchived())
|
|
|
|
|
|
|
|
|
|
err = taskDB.Unprotected().Get(ctx, task.ID, &retrievedTask)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.True(t, retrievedTask.IsArchived())
|
|
|
|
|
|
|
|
|
|
err = commentDB.Unprotected().Get(ctx, comment.ID, &retrievedComment)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.True(t, retrievedComment.IsArchived())
|
|
|
|
|
|
|
|
|
|
// Verify reaction still exists (reactions don't support archiving)
|
|
|
|
|
var retrievedReaction model.Reaction
|
|
|
|
|
err = reactionDB.Unprotected().Get(ctx, reaction.ID, &retrievedReaction)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Unarchive organization with cascade
|
|
|
|
|
err = organizationDB.SetArchived(ctx, accountRef, organization.ID, false, true)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Verify all entities are unarchived
|
|
|
|
|
err = organizationDB.Get(ctx, accountRef, organization.ID, &retrievedOrganization)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.False(t, retrievedOrganization.IsArchived())
|
|
|
|
|
|
|
|
|
|
err = projectDBAdapter.Unprotected().Get(ctx, project.ID, &retrievedProject)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.False(t, retrievedProject.IsArchived())
|
|
|
|
|
|
|
|
|
|
err = taskDB.Unprotected().Get(ctx, task.ID, &retrievedTask)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.False(t, retrievedTask.IsArchived())
|
|
|
|
|
|
|
|
|
|
err = commentDB.Unprotected().Get(ctx, comment.ID, &retrievedComment)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.False(t, retrievedComment.IsArchived())
|
|
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
|
err = reactionDB.Unprotected().Delete(ctx, reaction.ID)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = commentDB.Unprotected().Delete(ctx, comment.ID)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = taskDB.Unprotected().Delete(ctx, task.ID)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = projectDBAdapter.Unprotected().Delete(ctx, project.ID)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = organizationDB.Delete(ctx, accountRef, organization.ID)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("SetArchived_NonExistentOrganization", func(t *testing.T) {
|
|
|
|
|
// Try to archive non-existent organization
|
|
|
|
|
nonExistentID := primitive.NewObjectID()
|
|
|
|
|
err := organizationDB.SetArchived(ctx, accountRef, nonExistentID, true, true)
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
// Could be either no data or access denied error depending on the permission system
|
|
|
|
|
assert.True(t, errors.Is(err, merrors.ErrNoData) || errors.Is(err, merrors.ErrAccessDenied))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if len(ids) == 0 {
|
|
|
|
|
return nil, merrors.ErrNoData
|
|
|
|
|
}
|
|
|
|
|
return ids, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *memoryOrganizationRepository) ListPermissionBound(_ context.Context, query builder.Query) ([]model.PermissionBoundStorable, error) {
|
|
|
|
|
m.mu.RLock()
|
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
|
|
|
|
|
var res []model.PermissionBoundStorable
|
|
|
|
|
for _, org := range m.data {
|
|
|
|
|
if m.matchesQuery(query, org) {
|
|
|
|
|
res = append(res, cloneOrganization(org))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *memoryOrganizationRepository) ListAccountBound(context.Context, builder.Query) ([]model.AccountBoundStorable, error) {
|
|
|
|
|
return nil, merrors.NotImplemented("Account bound list not supported")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *memoryOrganizationRepository) Collection() string {
|
|
|
|
|
return mservice.Organizations
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *memoryOrganizationRepository) matchesQuery(query builder.Query, org *model.Organization) bool {
|
|
|
|
|
if query == nil {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
for _, elem := range query.BuildQuery() {
|
|
|
|
|
switch elem.Key {
|
|
|
|
|
case storable.IDField:
|
|
|
|
|
id, ok := elem.Value.(primitive.ObjectID)
|
|
|
|
|
if !ok || *org.GetID() != id {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
case storable.IsArchivedField:
|
|
|
|
|
value, ok := elem.Value.(bool)
|
|
|
|
|
if ok && org.IsArchived() != value {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func cloneOrganization(src *model.Organization) *model.Organization {
|
|
|
|
|
dst := *src
|
|
|
|
|
if len(src.Members) > 0 {
|
|
|
|
|
dst.Members = append([]primitive.ObjectID{}, src.Members...)
|
|
|
|
|
}
|
|
|
|
|
return &dst
|
|
|
|
|
}
|
|
|
|
|
|