|
1
|
|
|
package memory |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"errors" |
|
6
|
|
|
"time" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/Permify/permify/internal/storage" |
|
9
|
|
|
"github.com/Permify/permify/internal/storage/memory/constants" |
|
10
|
|
|
db "github.com/Permify/permify/pkg/database/memory" |
|
11
|
|
|
base "github.com/Permify/permify/pkg/pb/base/v1" |
|
12
|
|
|
) |
|
13
|
|
|
|
|
14
|
|
|
// TenantWriter - Structure for Tenant Writer |
|
15
|
|
|
type TenantWriter struct { |
|
16
|
|
|
database *db.Memory |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
// NewTenantWriter creates a new TenantWriter |
|
20
|
|
|
func NewTenantWriter(database *db.Memory) *TenantWriter { |
|
21
|
|
|
return &TenantWriter{ |
|
22
|
|
|
database: database, |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
// CreateTenant - |
|
27
|
|
|
func (w *TenantWriter) CreateTenant(_ context.Context, id, name string) (result *base.Tenant, err error) { |
|
28
|
|
|
tenant := storage.Tenant{ |
|
29
|
|
|
ID: id, |
|
30
|
|
|
Name: name, |
|
31
|
|
|
CreatedAt: time.Now(), |
|
32
|
|
|
} |
|
33
|
|
|
txn := w.database.DB.Txn(true) |
|
34
|
|
|
defer txn.Abort() |
|
35
|
|
|
if err = txn.Insert(constants.TenantsTable, tenant); err != nil { |
|
36
|
|
|
return nil, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) |
|
37
|
|
|
} |
|
38
|
|
|
txn.Commit() |
|
39
|
|
|
return tenant.ToTenant(), nil |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// DeleteTenant - |
|
43
|
|
|
func (w *TenantWriter) DeleteTenant(_ context.Context, tenantID string) (err error) { |
|
44
|
|
|
txn := w.database.DB.Txn(true) |
|
45
|
|
|
defer txn.Abort() |
|
46
|
|
|
|
|
47
|
|
|
// Check if tenant exists first |
|
48
|
|
|
raw, err := txn.First(constants.TenantsTable, "id", tenantID) |
|
49
|
|
|
if err != nil || raw == nil { |
|
50
|
|
|
return errors.New(base.ErrorCode_ERROR_CODE_NOT_FOUND.String()) |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// Define a slice of tables to delete associated records |
|
54
|
|
|
tables := []string{ |
|
55
|
|
|
constants.AttributesTable, |
|
56
|
|
|
constants.BundlesTable, |
|
57
|
|
|
constants.RelationTuplesTable, |
|
58
|
|
|
constants.SchemaDefinitionsTable, |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Iterate through each table and delete records associated with the tenant |
|
62
|
|
|
for _, table := range tables { |
|
63
|
|
|
if _, err := txn.First(table, "tenant_id", tenantID); err == nil { |
|
64
|
|
|
_, deleteErr := txn.DeleteAll(table, "tenant_id", tenantID) |
|
65
|
|
|
if deleteErr != nil { |
|
66
|
|
|
return errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Finally, delete the tenant record |
|
72
|
|
|
if _, err = txn.DeleteAll(constants.TenantsTable, "id", tenantID); err != nil { |
|
73
|
|
|
return errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
txn.Commit() |
|
77
|
|
|
return nil |
|
78
|
|
|
} |
|
79
|
|
|
|