-
Notifications
You must be signed in to change notification settings - Fork 126
/
subscription.go
433 lines (359 loc) · 10.8 KB
/
subscription.go
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package mongo
import (
"context"
"errors"
"math"
"time"
"github.com/frain-dev/convoy/datastore"
"github.com/frain-dev/convoy/pkg/flatten"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type subscriptionRepo struct {
store datastore.Store
}
func NewSubscriptionRepo(store datastore.Store) datastore.SubscriptionRepository {
return &subscriptionRepo{
store: store,
}
}
func (s *subscriptionRepo) CreateSubscription(ctx context.Context, projectID string, subscription *datastore.Subscription) error {
ctx = s.setCollectionInContext(ctx)
if projectID != subscription.ProjectID {
return datastore.ErrNotAuthorisedToAccessDocument
}
subscription.ID = primitive.NewObjectID()
return s.store.Save(ctx, subscription, nil)
}
func (s *subscriptionRepo) UpdateSubscription(ctx context.Context, projectID string, subscription *datastore.Subscription) error {
ctx = s.setCollectionInContext(ctx)
if projectID != subscription.ProjectID {
return datastore.ErrNotAuthorisedToAccessDocument
}
subscription.UpdatedAt = primitive.NewDateTimeFromTime(time.Now())
filter := bson.M{
"uid": subscription.UID,
"project_id": projectID,
}
update := bson.M{
"$set": bson.M{
"name": subscription.Name,
"source_id": subscription.SourceID,
"endpoint_id": subscription.EndpointID,
"filter_config.event_types": subscription.FilterConfig.EventTypes,
"filter_config.filter": subscription.FilterConfig.Filter,
"alert_config": subscription.AlertConfig,
"retry_config": subscription.RetryConfig,
"rate_limit_config": subscription.RateLimitConfig,
},
}
err := s.store.UpdateOne(ctx, filter, update)
return err
}
func (s *subscriptionRepo) LoadSubscriptionsPaged(ctx context.Context, projectID string, f *datastore.FilterBy, pageable datastore.Pageable) ([]datastore.Subscription, datastore.PaginationData, error) {
ctx = s.setCollectionInContext(ctx)
var subscriptions []datastore.Subscription
matchStage := bson.D{
{
Key: "$match",
Value: bson.D{
{Key: "project_id", Value: projectID},
{Key: "deleted_at", Value: nil},
},
},
}
if len(f.EndpointIDs) > 0 {
matchStage = bson.D{
{
Key: "$match",
Value: bson.D{
{Key: "project_id", Value: projectID},
{Key: "endpoint_id", Value: bson.M{
"$in": f.EndpointIDs,
}},
{Key: "deleted_at", Value: nil},
},
},
}
}
endpointStage := bson.D{
{
Key: "$lookup",
Value: bson.D{
{Key: "from", Value: "endpoints"},
{Key: "localField", Value: "endpoint_id"},
{Key: "foreignField", Value: "uid"},
{
Key: "pipeline",
Value: bson.A{
bson.D{
{
Key: "$project",
Value: bson.D{
{Key: "uid", Value: 1},
{Key: "title", Value: 1},
{Key: "project_id", Value: 1},
{Key: "support_email", Value: 1},
{Key: "target_url", Value: 1},
{Key: "secrets", Value: 1},
},
},
},
},
},
{Key: "as", Value: "endpoint_metadata"},
},
},
}
sourceStage := bson.D{
{
Key: "$lookup",
Value: bson.D{
{Key: "from", Value: "sources"},
{Key: "localField", Value: "source_id"},
{Key: "foreignField", Value: "uid"},
{
Key: "pipeline",
Value: bson.A{
bson.D{
{
Key: "$project",
Value: bson.D{
{Key: "uid", Value: 1},
{Key: "name", Value: 1},
{Key: "type", Value: 1},
{Key: "mask_id", Value: 1},
{Key: "project_id", Value: 1},
{Key: "verifier", Value: 1},
{Key: "is_disabled", Value: 1},
},
},
},
},
},
{Key: "as", Value: "source_metadata"},
},
},
}
unwindSourceStage := bson.D{{Key: "$unwind", Value: bson.D{{Key: "path", Value: "$source_metadata"}, {Key: "preserveNullAndEmptyArrays", Value: true}}}}
unwindEndpointStage := bson.D{{Key: "$unwind", Value: bson.D{{Key: "path", Value: "$endpoint_metadata"}, {Key: "preserveNullAndEmptyArrays", Value: true}}}}
skipStage := bson.D{{Key: "$skip", Value: getSkip(pageable.Page, pageable.PerPage)}}
sortStage := bson.D{{Key: "$sort", Value: bson.D{{Key: "created_at", Value: -1}}}}
limitStage := bson.D{{Key: "$limit", Value: pageable.PerPage}}
// pipeline definition
pipeline := mongo.Pipeline{
matchStage,
sortStage,
skipStage,
limitStage,
sourceStage,
endpointStage,
unwindSourceStage,
unwindEndpointStage,
}
err := s.store.Aggregate(ctx, pipeline, &subscriptions, true)
if err != nil {
return nil, datastore.PaginationData{}, err
}
filter := bson.M{"project_id": projectID}
if len(f.EndpointIDs) > 0 {
filter["endpoint_id"] = bson.M{"$in": f.EndpointIDs}
}
count, err := s.store.Count(ctx, filter)
if err != nil {
return nil, datastore.PaginationData{}, err
}
pagination := datastore.PaginationData{
Total: count,
Page: int64(pageable.Page),
PerPage: int64(pageable.PerPage),
Prev: int64(getPrevPage(pageable.Page)),
Next: int64(pageable.Page + 1),
TotalPage: int64(math.Ceil(float64(count) / float64(pageable.PerPage))),
}
return subscriptions, pagination, nil
}
func (s *subscriptionRepo) DeleteSubscription(ctx context.Context, projectID string, subscription *datastore.Subscription) error {
ctx = s.setCollectionInContext(ctx)
if projectID != subscription.ProjectID {
return datastore.ErrNotAuthorisedToAccessDocument
}
filter := bson.M{
"uid": subscription.UID,
"project_id": projectID,
}
return s.store.DeleteOne(ctx, filter, false)
}
func (s *subscriptionRepo) FindSubscriptionByID(ctx context.Context, projectID string, uid string) (*datastore.Subscription, error) {
ctx = s.setCollectionInContext(ctx)
subscription := &datastore.Subscription{}
filter := bson.M{"uid": uid, "project_id": projectID}
err := s.store.FindOne(ctx, filter, nil, subscription)
if errors.Is(err, mongo.ErrNoDocuments) {
err = datastore.ErrSubscriptionNotFound
}
return subscription, err
}
func (s *subscriptionRepo) FindSubscriptionsByEventType(ctx context.Context, projectID string, endpointID string, eventType datastore.EventType) ([]datastore.Subscription, error) {
ctx = s.setCollectionInContext(ctx)
filter := bson.M{"project_id": projectID, "endpoint_id": endpointID, "filter_config.event_types": string(eventType)}
subscriptions := make([]datastore.Subscription, 0)
_, err := s.store.FindMany(ctx, filter, nil, nil, 0, 0, &subscriptions)
if err != nil {
return nil, err
}
return subscriptions, nil
}
func (s *subscriptionRepo) FindSubscriptionsByEndpointID(ctx context.Context, projectID string, endpointID string) ([]datastore.Subscription, error) {
ctx = s.setCollectionInContext(ctx)
filter := bson.M{
"endpoint_id": endpointID,
"project_id": projectID,
}
subscriptions := make([]datastore.Subscription, 0)
_, err := s.store.FindMany(ctx, filter, nil, nil, 0, 0, &subscriptions)
return subscriptions, err
}
func (s *subscriptionRepo) FindSubscriptionByDeviceID(ctx context.Context, projectID, deviceID string, subType datastore.SubscriptionType) (*datastore.Subscription, error) {
ctx = s.setCollectionInContext(ctx)
filter := bson.M{
"device_id": deviceID,
"project_id": projectID,
"type": subType,
}
subscription := &datastore.Subscription{}
err := s.store.FindOne(ctx, filter, nil, &subscription)
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, datastore.ErrSubscriptionNotFound
}
return subscription, nil
}
func (s *subscriptionRepo) FindSubscriptionsBySourceID(ctx context.Context, projectID string, sourceId string) ([]datastore.Subscription, error) {
ctx = s.setCollectionInContext(ctx)
filter := bson.M{"project_id": projectID, "source_id": sourceId}
subscriptions := make([]datastore.Subscription, 0)
_, err := s.store.FindMany(ctx, filter, nil, nil, 0, 0, &subscriptions)
if err != nil {
return nil, err
}
return subscriptions, nil
}
func (s *subscriptionRepo) FindCLISubscriptions(ctx context.Context, projectID string) ([]datastore.Subscription, error) {
ctx = s.setCollectionInContext(ctx)
filter := bson.M{"project_id": projectID, "type": datastore.SubscriptionTypeCLI}
subscriptions := make([]datastore.Subscription, 0)
_, err := s.store.FindMany(ctx, filter, nil, nil, 0, 0, &subscriptions)
if err != nil {
return nil, err
}
return subscriptions, nil
}
func (s *subscriptionRepo) TestSubscriptionFilter(ctx context.Context, payload map[string]interface{}, filter map[string]interface{}) (bool, error) {
ctx = context.WithValue(ctx, datastore.CollectionCtx, datastore.FilterCollection)
isValid := false
err := s.store.WithTransaction(ctx, func(sessCtx mongo.SessionContext) error {
f := datastore.SubscriptionFilter{
ID: primitive.NewObjectID(),
UID: uuid.NewString(),
Filter: payload,
DeletedAt: nil,
}
// insert the desired request payload
err := s.store.Save(sessCtx, f, nil)
if err != nil {
return err
}
// compare the filter with the test request payload
var q map[string]interface{}
if len(filter) == 0 {
filter = nil
}
if filter != nil {
q, err = flattenFilter(filter)
if err != nil {
return err
}
}
var filters []datastore.SubscriptionFilter
err = s.store.FindAll(sessCtx, q, nil, nil, &filters)
if err != nil {
return err
}
isValid = len(filters) > 0
err = s.store.DeleteByID(sessCtx, f.UID, true)
if err != nil {
return err
}
return nil
})
return isValid, err
}
func (s *subscriptionRepo) setCollectionInContext(ctx context.Context) context.Context {
return context.WithValue(ctx, datastore.CollectionCtx, datastore.SubscriptionCollection)
}
func flattenFilter(f map[string]interface{}) (map[string]interface{}, error) {
isAndOr := false
var operator string
for k := range f {
if k == "$or" {
if len(f) > 1 {
return nil, flatten.ErrTopLevelElementOr
}
operator = k
isAndOr = true
break
}
if k == "$and" {
if len(f) > 1 {
return nil, flatten.ErrTopLevelElementAnd
}
isAndOr = true
break
}
}
if isAndOr {
if a, ok := f[operator].([]interface{}); ok {
if !ok {
return nil, flatten.ErrOrAndMustBeArray
}
for i := range a {
t, err := flatten.FlattenWithPrefix("filter", a[i].(map[string]interface{}))
if err != nil {
return nil, err
}
a[i] = t
}
f[operator] = a
return f, nil
}
}
query := map[string]interface{}{"filter": f}
q, err := flatten.Flatten(query)
if err != nil {
return nil, err
}
return q, nil
}
// getSkip returns calculated skip value for the query
func getSkip(page, limit int) int {
skip := (page - 1) * limit
if skip <= 0 {
skip = 0
}
return skip
}
// getPrevPage returns calculated value for the prev page
func getPrevPage(page int) int {
if page == 0 {
return 1
}
prev := 0
if page-1 <= 0 {
prev = page
} else {
prev = page - 1
}
return prev
}