Repository navigation
Expand file tree
/
Copy pathmetadata.go
More file actions
225 lines (199 loc) · 6.54 KB
/
Copy pathmetadata.go
File metadata and controls
225 lines (199 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package blnk
import (
"context"
"errors"
"fmt"
"strings"
"github.com/blnkfinance/blnk/internal/notification"
)
// getEntityTypeFromID determines the entity type from the ID prefix.
// It analyzes the prefix of the provided ID and returns the corresponding entity type.
//
// Parameters:
// - id: A string representing the entity ID to analyze.
//
// Returns:
// - string: The determined entity type ("transactions", "ledgers", "balances", or "identities").
// - error: An error if the ID format is invalid.
func getEntityTypeFromID(id string) (string, error) {
switch {
case strings.HasPrefix(id, "txn_"):
return "transactions", nil
case strings.HasPrefix(id, "bulk_"):
return "transactions", nil
case strings.HasPrefix(id, "ldg_"):
return "ledgers", nil
case strings.HasPrefix(id, "bln_"):
return "balances", nil
case strings.HasPrefix(id, "idt_"):
return "identities", nil
default:
return "", fmt.Errorf("invalid entity ID format: %s", id)
}
}
// UpdateMetadata updates the metadata for a given entity ID.
// It first determines the entity type, retrieves current metadata, merges it with new metadata,
// updates the entity with the merged metadata, and queues the updated entity for re-indexing in Typesense.
//
// Parameters:
// - ctx: The context for the operation.
// - entityID: A string representing the ID of the entity to update.
// - newMetadata: A map containing the new metadata to merge.
//
// Returns:
// - map[string]interface{}: The merged metadata after the update.
// - error: An error if the update operation fails.
func (l *Blnk) UpdateMetadata(ctx context.Context, entityID string, newMetadata map[string]interface{}) (map[string]interface{}, error) {
entityType, err := getEntityTypeFromID(entityID)
if err != nil {
return nil, err
}
// Check if entity exists first
switch entityType {
case "ledgers":
ledger, err := l.GetLedgerByID(entityID)
if err != nil {
return nil, errors.New("entity not found")
}
currentMetadata := ledger.MetaData
mergedMetadata := mergeMetadata(currentMetadata, newMetadata)
err = l.updateEntityMetadata(ctx, entityType, entityID, mergedMetadata)
if err != nil {
return nil, fmt.Errorf("failed to update metadata: %w", err)
}
// Queue the updated ledger for re-indexing in Typesense
if l.queue != nil {
go func() {
updatedLedger, err := l.GetLedgerByID(entityID)
if err == nil {
err = l.queue.queueIndexData(entityID, "ledgers", updatedLedger)
if err != nil {
notification.NotifyError(err)
}
}
}()
}
return mergedMetadata, nil
case "transactions":
// Check if transaction exists either by direct ID or as parent ID
exists, err := l.datasource.TransactionExistsByIDOrParentID(ctx, entityID)
if err != nil {
return nil, err
}
if !exists {
return nil, errors.New("entity not found")
}
// Apply metadata updates directly without trying to get current metadata
// This preserves existing metadata in child transactions
err = l.updateEntityMetadata(ctx, entityType, entityID, newMetadata)
if err != nil {
return nil, fmt.Errorf("failed to update metadata: %w", err)
}
// Queue the updated transaction for re-indexing in Typesense
if l.queue != nil {
go func() {
updatedTransaction, err := l.GetTransaction(context.Background(), entityID)
if err == nil {
err = l.queue.queueIndexData(entityID, "transactions", updatedTransaction)
if err != nil {
notification.NotifyError(err)
}
}
}()
}
return newMetadata, nil
case "balances":
balance, err := l.GetBalanceByID(ctx, entityID, nil, false)
if err != nil {
return nil, errors.New("entity not found")
}
currentMetadata := balance.MetaData
mergedMetadata := mergeMetadata(currentMetadata, newMetadata)
err = l.updateEntityMetadata(ctx, entityType, entityID, mergedMetadata)
if err != nil {
return nil, fmt.Errorf("failed to update metadata: %w", err)
}
if l.queue != nil {
// Queue the updated balance for re-indexing in Typesense
go func() {
updatedBalance, err := l.GetBalanceByID(context.Background(), entityID, nil, false)
if err == nil {
err = l.queue.queueIndexData(entityID, "balances", updatedBalance)
if err != nil {
notification.NotifyError(err)
}
}
}()
}
return mergedMetadata, nil
case "identities":
identity, err := l.GetIdentity(entityID)
if err != nil {
return nil, errors.New("entity not found")
}
currentMetadata := identity.MetaData
mergedMetadata := mergeMetadata(currentMetadata, newMetadata)
err = l.updateEntityMetadata(ctx, entityType, entityID, mergedMetadata)
if err != nil {
return nil, fmt.Errorf("failed to update metadata: %w", err)
}
// Queue the updated identity for re-indexing in Typesense
if l.queue != nil {
go func() {
updatedIdentity, err := l.GetIdentity(entityID)
if err == nil {
err = l.queue.queueIndexData(entityID, "identities", updatedIdentity)
if err != nil {
notification.NotifyError(err)
}
}
}()
}
return mergedMetadata, nil
default:
return nil, fmt.Errorf("unsupported entity type: %s", entityType)
}
}
// mergeMetadata merges new metadata with existing metadata.
// If the current metadata is nil, it initializes a new map.
//
// Parameters:
// - current: The existing metadata map.
// - new: The new metadata map to merge.
//
// Returns:
// - map[string]interface{}: The merged metadata map.
func mergeMetadata(current, new map[string]interface{}) map[string]interface{} {
if current == nil {
current = make(map[string]interface{})
}
for k, v := range new {
current[k] = v
}
return current
}
// updateEntityMetadata updates the metadata for a specific entity.
// It routes the update operation to the appropriate datasource method based on the entity type.
//
// Parameters:
// - ctx: The context for the operation.
// - entityType: The type of entity being updated.
// - entityID: The ID of the entity being updated.
// - metadata: The new metadata to set.
//
// Returns:
// - error: An error if the update operation fails.
func (l *Blnk) updateEntityMetadata(ctx context.Context, entityType, entityID string, metadata map[string]interface{}) error {
switch entityType {
case "ledgers":
return l.datasource.UpdateLedgerMetadata(entityID, metadata)
case "transactions":
return l.datasource.UpdateTransactionMetadata(ctx, entityID, metadata)
case "balances":
return l.datasource.UpdateBalanceMetadata(ctx, entityID, metadata)
case "identities":
return l.datasource.UpdateIdentityMetadata(entityID, metadata)
default:
return fmt.Errorf("unsupported entity type: %s", entityType)
}
}