/
notification_service.go
418 lines (348 loc) · 11.8 KB
/
notification_service.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
package service
import (
"fmt"
uuid "github.com/gofrs/uuid"
"github.com/red-gold/telar-core/config"
"github.com/red-gold/telar-core/data"
coreData "github.com/red-gold/telar-core/data"
repo "github.com/red-gold/telar-core/data"
"github.com/red-gold/telar-core/data/mongodb"
mongoRepo "github.com/red-gold/telar-core/data/mongodb"
"github.com/red-gold/telar-core/utils"
dto "github.com/red-gold/telar-web/micros/notifications/dto"
)
// NotificationService handlers with injected dependencies
type NotificationServiceImpl struct {
NotificationRepo repo.Repository
}
// NewNotificationService initializes NotificationService's dependencies and create new NotificationService struct
func NewNotificationService(db interface{}) (NotificationService, error) {
notificationService := &NotificationServiceImpl{}
switch *config.AppConfig.DBType {
case config.DB_MONGO:
mongodb := db.(mongodb.MongoDatabase)
notificationService.NotificationRepo = mongoRepo.NewDataRepositoryMongo(mongodb)
}
return notificationService, nil
}
// SaveNotification save the notification
func (s NotificationServiceImpl) SaveNotification(notification *dto.Notification) error {
if notification.ObjectId == uuid.Nil {
var uuidErr error
notification.ObjectId, uuidErr = uuid.NewV4()
if uuidErr != nil {
return uuidErr
}
}
if notification.CreatedDate == 0 {
notification.CreatedDate = utils.UTCNowUnix()
}
result := <-s.NotificationRepo.Save(notificationCollectionName, notification)
return result.Error
}
// FindOneNotification get one notification
func (s NotificationServiceImpl) FindOneNotification(filter interface{}) (*dto.Notification, error) {
result := <-s.NotificationRepo.FindOne(notificationCollectionName, filter)
if result.Error() != nil {
return nil, result.Error()
}
var notificationResult dto.Notification
errDecode := result.Decode(¬ificationResult)
if errDecode != nil {
return nil, fmt.Errorf("Error docoding on dto.Notification")
}
return ¬ificationResult, nil
}
// FindNotificationList get all notifications by filter
func (s NotificationServiceImpl) FindNotificationList(filter interface{}, limit int64, skip int64, sort map[string]int) ([]dto.Notification, error) {
result := <-s.NotificationRepo.Find(notificationCollectionName, filter, limit, skip, sort)
defer result.Close()
if result.Error() != nil {
return nil, result.Error()
}
var notificationList []dto.Notification
for result.Next() {
var notification dto.Notification
errDecode := result.Decode(¬ification)
if errDecode != nil {
return nil, fmt.Errorf("Error docoding on dto.Notification")
}
notificationList = append(notificationList, notification)
}
return notificationList, nil
}
// FindNotificationsReceiver get all notifications by filter including receiver profile
func (s NotificationServiceImpl) FindNotificationsReceiver(filter interface{}, limit int64, skip int64, sort map[string]int) ([]dto.Notification, error) {
var pipeline []interface{}
matchOperator := make(map[string]interface{})
matchOperator["$match"] = filter
sortOperator := make(map[string]interface{})
sortOperator["$sort"] = sort
pipeline = append(pipeline, matchOperator, sortOperator)
if skip > 0 {
skipOperator := make(map[string]interface{})
skipOperator["$skip"] = skip
pipeline = append(pipeline, skipOperator)
}
if limit > 0 {
limitOperator := make(map[string]interface{})
limitOperator["$limit"] = limit
pipeline = append(pipeline, limitOperator)
}
lookupOperator := make(map[string]interface{})
lookupOperator["$lookup"] = map[string]string{
"localField": "notifyRecieverUserId",
"from": "userProfile",
"foreignField": "objectId",
"as": "userinfo",
}
unwindOperator := make(map[string]interface{})
unwindOperator["$unwind"] = "$userinfo"
projectOperator := make(map[string]interface{})
project := make(map[string]interface{})
project["objectId"] = 1
project["ownerUserId"] = 1
project["ownerDisplayName"] = 1
project["ownerAvatar"] = 1
project["created_date"] = 1
project["description"] = 1
project["url"] = 1
project["notifyRecieverUserId"] = 1
project["notifyRecieverEmail"] = "$userinfo.email"
project["targetId"] = 1
project["isSeen"] = 1
project["type"] = 1
project["emailNotification"] = 1
project["isEmailSent"] = 1
projectOperator["$project"] = project
pipeline = append(pipeline, lookupOperator, unwindOperator, projectOperator)
result := <-s.NotificationRepo.Aggregate(notificationCollectionName, pipeline)
defer result.Close()
if result.Error() != nil {
return nil, result.Error()
}
var commentList []dto.Notification
for result.Next() {
var comment dto.Notification
errDecode := result.Decode(&comment)
if errDecode != nil {
return nil, fmt.Errorf("Error docoding on dto.Comment")
}
commentList = append(commentList, comment)
}
return commentList, nil
}
// GetLastNotifications find by owner user id
func (s NotificationServiceImpl) GetLastNotifications() ([]dto.Notification, error) {
sortMap := make(map[string]int)
sortMap["created_date"] = -1
filter := make(map[string]interface{})
ne := make(map[string]interface{})
ne["$ne"] = true
filter["isEmailSent"] = ne
return s.FindNotificationsReceiver(filter, 10, 0, sortMap)
}
// FindByOwnerUserId find by owner user id
func (s NotificationServiceImpl) FindByOwnerUserId(ownerUserId uuid.UUID) ([]dto.Notification, error) {
sortMap := make(map[string]int)
sortMap["created_date"] = -1
filter := struct {
OwnerUserId uuid.UUID `json:"ownerUserId" bson:"ownerUserId"`
}{
OwnerUserId: ownerUserId,
}
return s.FindNotificationList(filter, 0, 0, sortMap)
}
// FindById find by notification id
func (s NotificationServiceImpl) FindById(objectId uuid.UUID) (*dto.Notification, error) {
filter := struct {
ObjectId uuid.UUID `json:"objectId" bson:"objectId"`
}{
ObjectId: objectId,
}
return s.FindOneNotification(filter)
}
// UpdateNotification update the notification
func (s NotificationServiceImpl) UpdateNotification(filter interface{}, data interface{}, opts ...*coreData.UpdateOptions) error {
result := <-s.NotificationRepo.Update(notificationCollectionName, filter, data, opts...)
if result.Error != nil {
return result.Error
}
return nil
}
// UpdateManyNotifications update many notifications
func (s NotificationServiceImpl) UpdateManyNotifications(filter interface{}, data interface{}, opts ...*coreData.UpdateOptions) error {
result := <-s.NotificationRepo.UpdateMany(notificationCollectionName, filter, data, opts...)
if result.Error != nil {
return result.Error
}
return nil
}
// UpdateBulkNotification update bulk notification
func (s NotificationServiceImpl) UpdateBulkNotification(bulk []data.BulkUpdateOne) error {
result := <-s.NotificationRepo.BulkUpdateOne(notificationCollectionName, bulk)
if result.Error != nil {
return result.Error
}
return nil
}
// UpdateBulkNotificationList update bulk notification list
func (s NotificationServiceImpl) UpdateBulkNotificationList(userNotification []dto.Notification) error {
var bulkList []repo.BulkUpdateOne
for _, notification := range userNotification {
filter := struct {
ObjectId uuid.UUID `json:"objectId" bson:"objectId"`
OwnerUserId uuid.UUID `json:"ownerUserId" bson:"ownerUserId"`
}{
ObjectId: notification.ObjectId,
OwnerUserId: notification.OwnerUserId,
}
setOperation := make(map[string]interface{})
setOperation["$set"] = notification
bulkItem := repo.BulkUpdateOne{
Filter: filter,
Data: setOperation,
}
bulkList = append(bulkList, bulkItem)
}
return s.UpdateBulkNotification(bulkList)
}
// UpdateEmailSent update bulk notification list
func (s NotificationServiceImpl) UpdateEmailSent(notifyIds []uuid.UUID) error {
include := make(map[string]interface{})
include["$in"] = notifyIds
filter := make(map[string]interface{})
filter["objectId"] = include
updateOperator := coreData.UpdateOperator{
Set: map[string]bool{
"isEmailSent": true,
},
}
err := s.UpdateManyNotifications(filter, updateOperator)
if err != nil {
return err
}
return nil
}
// UpdateNotificationById update the notification
func (s NotificationServiceImpl) UpdateNotificationById(data *dto.Notification) error {
filter := struct {
ObjectId uuid.UUID `json:"objectId" bson:"objectId"`
OwnerUserId uuid.UUID `json:"ownerUserId" bson:"ownerUserId"`
}{
ObjectId: data.ObjectId,
OwnerUserId: data.OwnerUserId,
}
updateOperator := coreData.UpdateOperator{
Set: data,
}
err := s.UpdateNotification(filter, updateOperator)
if err != nil {
return err
}
return nil
}
// DeleteNotification delete notification by filter
func (s NotificationServiceImpl) DeleteNotification(filter interface{}) error {
result := <-s.NotificationRepo.Delete(notificationCollectionName, filter, true)
if result.Error != nil {
return result.Error
}
return nil
}
// DeleteNotification delete notification by notificationReceiverId and notificationId
func (s NotificationServiceImpl) DeleteNotificationByOwner(notificationReceiverId uuid.UUID, notificationId uuid.UUID) error {
filter := struct {
ObjectId uuid.UUID `json:"objectId" bson:"objectId"`
NotifyRecieverUserId uuid.UUID `json:"notifyRecieverUserId" bson:"notifyRecieverUserId"`
}{
ObjectId: notificationId,
NotifyRecieverUserId: notificationReceiverId,
}
err := s.DeleteNotification(filter)
if err != nil {
return err
}
return nil
}
// DeleteManyNotifications delete many notifications by filter
func (s NotificationServiceImpl) DeleteManyNotifications(filter interface{}) error {
result := <-s.NotificationRepo.Delete(notificationCollectionName, filter, false)
if result.Error != nil {
return result.Error
}
return nil
}
// CreateNotificationIndex create index for notification search.
func (s NotificationServiceImpl) CreateNotificationIndex(indexes map[string]interface{}) error {
result := <-s.NotificationRepo.CreateIndex(notificationCollectionName, indexes)
return result
}
// GetNotificationByUserId get all notifications by userId who receive the notification
func (s NotificationServiceImpl) GetNotificationByUserId(userId *uuid.UUID, sortBy string, page int64, limit int64) ([]dto.Notification, error) {
sortMap := make(map[string]int)
sortMap[sortBy] = -1
skip := numberOfItems * (page - 1)
filter := struct {
NotifyRecieverUserId uuid.UUID `json:"notifyRecieverUserId" bson:"notifyRecieverUserId"`
}{
NotifyRecieverUserId: *userId,
}
result, err := s.FindNotificationList(filter, limit, skip, sortMap)
return result, err
}
// SeenNotification update the notification to seen
func (s NotificationServiceImpl) SeenNotification(objectId uuid.UUID, userId uuid.UUID) error {
filter := struct {
ObjectId uuid.UUID `json:"objectId" bson:"objectId"`
UserId uuid.UUID `json:"notifyRecieverUserId" bson:"notifyRecieverUserId"`
}{
ObjectId: objectId,
UserId: userId,
}
updateOperator := coreData.UpdateOperator{
Set: struct {
IsSeen bool `json:"isSeen" bson:"isSeen"`
}{
IsSeen: true,
},
}
err := s.UpdateNotification(filter, updateOperator)
if err != nil {
return err
}
return nil
}
// SeenNotification update all notifications to seen
func (s NotificationServiceImpl) SeenAllNotifications(userId uuid.UUID) error {
filter := struct {
UserId uuid.UUID `json:"notifyRecieverUserId" bson:"notifyRecieverUserId"`
}{
UserId: userId,
}
updateOperator := coreData.UpdateOperator{
Set: struct {
IsSeen bool `json:"isSeen" bson:"isSeen"`
}{
IsSeen: true,
},
}
err := s.UpdateManyNotifications(filter, updateOperator)
if err != nil {
return err
}
return nil
}
// DeleteNotificationsByUserId delete notifications by userId
func (s NotificationServiceImpl) DeleteNotificationsByUserId(userId uuid.UUID) error {
filter := struct {
UserId uuid.UUID `json:"notifyRecieverUserId" bson:"notifyRecieverUserId"`
}{
UserId: userId,
}
err := s.DeleteManyNotifications(filter)
if err != nil {
return err
}
return nil
}