overleaf/services/notifications/app/coffee/Notifications.coffee

39 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2016-01-14 12:35:16 +00:00
Settings = require 'settings-sharelatex'
logger = require('logger-sharelatex')
mongojs = require('mongojs')
db = mongojs(Settings.mongo?.url, ['notifications'])
2016-01-14 20:42:48 +00:00
ObjectId = require("mongojs").ObjectId
2016-01-14 12:35:16 +00:00
module.exports =
2016-01-14 20:42:48 +00:00
getUserNotifications: (user_id, callback = (err, notifications)->)->
query =
2016-01-21 16:34:12 +00:00
user_id: ObjectId(user_id)
2016-01-14 20:42:48 +00:00
templateKey: {"$exists":true}
db.notifications.find query, (err, notifications)->
callback err, notifications
2016-01-14 12:35:16 +00:00
addNotification: (user_id, notification, callback)->
2016-01-14 20:42:48 +00:00
query =
2016-01-21 16:34:12 +00:00
user_id: ObjectId(user_id)
2016-01-14 12:35:16 +00:00
key: notification.key
2016-01-14 20:42:48 +00:00
db.notifications.count query, (err, number)->
if number > 0
logger.log number:number, user_id:user_id, key:notification.key, "alredy has notification key for user"
callback number
else
doc =
2016-01-21 16:34:12 +00:00
user_id: ObjectId(user_id)
2016-01-14 20:42:48 +00:00
key: notification.key
messageOpts: notification.messageOpts
templateKey: notification.templateKey
db.notifications.insert(doc, callback)
2016-01-14 12:35:16 +00:00
2016-01-14 20:42:48 +00:00
removeNotification: (user_id, notification_id, callback)->
2016-01-14 12:35:16 +00:00
searchOps =
2016-01-21 16:34:12 +00:00
user_id:ObjectId(user_id)
2016-01-14 20:42:48 +00:00
_id:ObjectId(notification_id)
2016-01-14 12:35:16 +00:00
updateOperation =
2016-01-21 13:40:24 +00:00
"$unset": {templateKey:true, messageOpts: true}
2016-01-14 12:35:16 +00:00
db.notifications.update searchOps, updateOperation, callback