-
Notifications
You must be signed in to change notification settings - Fork 1
/
notifications.go
256 lines (216 loc) · 7.58 KB
/
notifications.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
package notifications
import (
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"github.com/mdaxf/iac/com"
"github.com/mdaxf/iac/logger"
"github.com/mdaxf/iac/documents"
)
// GetNotificationsbyUser retrieves notifications for a specific user.
// It takes a user string as input and returns a list of notifications and an error.
// The function first logs the start time and defer logs the performance duration.
// It also recovers from any panics and logs the error if any.
// The function constructs a filter using the user parameter and other conditions.
// It queries the "Notifications" collection using the constructed filter.
// If there is an error in querying the collection, it logs the error and returns nil and the error.
// Otherwise, it logs the retrieved notification list and returns it along with nil error.
func GetNotificationsbyUser(user string) (interface{}, error) {
iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "Notifications"}
startTime := time.Now()
defer func() {
elapsed := time.Since(startTime)
iLog.PerformanceWithDuration("notification.GetNotificationsbyUser", elapsed)
}()
/* defer func() {
err := recover()
if err != nil {
iLog.Error(fmt.Sprintf("Error: %v", err))
}
}()
*/
var filter bson.M
filter = bson.M{
"$or": []bson.M{
{"receipts." + user: bson.M{"$in": []int{1, 2, 3}}},
{
"$and": []bson.M{
{"receipts." + user: bson.M{"$exists": false}},
{"receipts.all": bson.M{"$exists": true}},
},
},
{"sender": user},
},
"status": bson.M{"$in": []int{1, 2}},
}
collectionitems, err := documents.DocDBCon.QueryCollection("Notifications", filter, nil)
if err != nil {
iLog.Error(fmt.Sprintf("failed to retrieve the list from notification: %v", err))
return nil, err
}
iLog.Debug(fmt.Sprintf("Get notification list from respository with data: %s", logger.ConvertJson(collectionitems)))
return collectionitems, nil
}
// SaveNotification saves a notification for a specific user.
// It takes ndata, which is the data of the notification, and user, which is the username of the user.
// It returns an error if there was a problem saving the notification.
func SaveNotification(ndata interface{}, user string) error {
iLog := logger.Log{ModuleName: logger.Framework, User: user, ControllerName: "Notifications"}
startTime := time.Now()
defer func() {
elapsed := time.Since(startTime)
iLog.PerformanceWithDuration("notification.GetNotificationsbyUser", elapsed)
}()
/* defer func() {
err := recover()
if err != nil {
iLog.Error(fmt.Sprintf("Error: %v", err))
}
}()
*/
_, err := documents.DocDBCon.InsertCollection("Notifications", ndata)
if err != nil {
iLog.Error(fmt.Sprintf("failed to save notification: %v", err))
return err
}
return nil
}
func UpdateNotification(ndata interface{}, user string, comments string, status int) error {
iLog := logger.Log{ModuleName: logger.Framework, User: user, ControllerName: "Notifications"}
startTime := time.Now()
defer func() {
elapsed := time.Since(startTime)
iLog.PerformanceWithDuration("notification.UpdateNotification", elapsed)
}()
defer func() {
err := recover()
if err != nil {
iLog.Error(fmt.Sprintf("Error: %v", err))
}
}()
newdata := ndata.(map[string]interface{})
newdata["system.updatedby"] = user
newdata["system.updatedon"] = time.Now()
if newdata["sender"] != user && newdata["receipts.all"] == 1 && (newdata["receipts."+user] == nil || newdata["receipts."+user] == 1) {
newdata["receipts."+user] = 2
}
userhisitem := make(map[string]interface{})
userhisitem["status"] = newdata["receipts."+user]
userhisitem["updatedby"] = user
userhisitem["updatedon"] = time.Now()
userhisitem["comments"] = comments
userhistory := newdata["histories"]
if userhistory == nil {
userhistory = make([]map[string]interface{}, 1)
userhistory.([]map[string]interface{})[0] = userhisitem
newdata["histories"] = userhistory
} else {
userhistory = append(userhistory.([]interface{}), userhisitem)
newdata["histories"] = userhistory
}
if status != 0 {
newdata["status"] = status
}
var filter bson.M
filter = bson.M{"uuid": newdata["uuid"]}
/* objectid, err := primitive.ObjectIDFromHex(newdata["_id"])
if err != nil {
doc.iLog.Error(fmt.Sprintf("failed to convert id to objectid with error: %s", err))
return err
}
filter := bson.M{"_id": objectid} */
delete(newdata, "_id")
err := documents.DocDBCon.UpdateCollection("Notifications", filter, nil, newdata)
if err != nil {
iLog.Error(fmt.Sprintf("failed to update notification: %v", err))
return err
}
go com.IACMessageBusClient.Invoke("send", "IAC_SERVER_NOTIICATION_UPDATE", newdata, "")
return nil
}
func UpdateNotificationbyUUID(uuid string, user string, comments string) error {
iLog := logger.Log{ModuleName: logger.Framework, User: user, ControllerName: "Notifications"}
startTime := time.Now()
defer func() {
elapsed := time.Since(startTime)
iLog.PerformanceWithDuration("notification.UpdateNotificationbyUUID", elapsed)
}()
defer func() {
err := recover()
if err != nil {
iLog.Error(fmt.Sprintf("Error: %v", err))
}
}()
var filter bson.M
filter = bson.M{"uuid": uuid}
update := bson.M{
"$set": bson.M{
"system.updatedby": user,
"system.updatedon": time.Now(),
"receipts." + user: 2,
"histories": bson.M{
"$push": bson.M{
"status": 1,
"updatedby": user,
"updatedon": time.Now(),
"comments": comments,
},
},
},
}
err := documents.DocDBCon.UpdateCollection("Notifications", filter, nil, update)
if err != nil {
iLog.Error(fmt.Sprintf("failed to update notification: %v", err))
return err
}
data := make(map[string]interface{})
data["uuid"] = uuid
data["comments"] = comments
data["user"] = user
go com.IACMessageBusClient.Invoke("send", "IAC_SERVER_NOTIICATION_REPLY", data, "")
return nil
}
// CreateNewNotification creates a new notification with the given notification data and user.
// It sets various system fields such as created by, created on, updated by, updated on, and status.
// If receipts are not provided, it sets the receipts to "all".
// It also adds a history entry with status, updated by, updated on, and comments.
// The notification data is then inserted into the "Notifications" collection in the database.
// Returns an error if there was a failure in saving the notification.
func CreateNewNotification(notificationdata interface{}, user string) error {
iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "Notifications"}
startTime := time.Now()
defer func() {
elapsed := time.Since(startTime)
iLog.PerformanceWithDuration("notification.CreateNewNotification", elapsed)
}()
/* defer func() {
err := recover()
if err != nil {
iLog.Error(fmt.Sprintf("Error: %v", err))
}
}() */
ndata := notificationdata.(map[string]interface{})
ndata["system.createdby"] = user
ndata["system.createdon"] = time.Now()
ndata["system.updatedby"] = user
ndata["system.updatedon"] = time.Now()
ndata["status"] = 1
ndata["sender"] = user
if ndata["receipts"] == nil {
ndata["receipts"] = map[string]interface{}{"all": 1}
}
history := make([]map[string]interface{}, 1)
history[0] = make(map[string]interface{})
history[0]["status"] = 1
history[0]["updatedby"] = user
history[0]["updatedon"] = time.Now()
history[0]["comments"] = "New Notification"
ndata["histories"] = history
_, err := documents.DocDBCon.InsertCollection("Notifications", ndata)
if err != nil {
iLog.Error(fmt.Sprintf("failed to save notification: %v", err))
return err
}
go com.IACMessageBusClient.Invoke("send", "IAC_SERVER_NOTIICATION", ndata, "")
return nil
}