make it works

This commit is contained in:
Henrique Santos 2016-01-14 18:42:48 -02:00
parent 1e60327f2e
commit 2c81b978cf
4 changed files with 47 additions and 26 deletions

View file

@ -3,7 +3,7 @@ logger = require 'logger-sharelatex'
logger.initialize("notifications-sharelatex")
express = require('express')
app = express()
controller = require("./app/js/NotificationController")
controller = require("./app/js/NotificationsController")
mongojs = require('mongojs')
db = mongojs(Settings.mongo.url, ['notifications'])
Path = require("path")
@ -21,8 +21,8 @@ app.configure ()->
app.use express.errorHandler()
app.post '/user/:user_id', controller.addNotification
app.get '/user/:user_id', controller.getUsersNotifications
app.del '/user/:user_id/notification_id/:notification_id', controller.removeNotification
app.get '/user/:user_id', controller.getUserNotifications
app.del '/user/:user_id/notification/:notification_id', controller.removeNotification
app.get '/status', (req, res)->
res.send('notifications sharelatex up')

View file

@ -6,18 +6,20 @@ settings = require("settings-sharelatex")
port = settings.internal.notifications.port
logger = require "logger-sharelatex"
mongojs = require('mongojs')
Settings = require 'settings-sharelatex'
db = mongojs(Settings.mongo?.url, ['notifications'])
module.exports =
check : (callback)->
project_id = ObjectId()
user_id = ObjectId(settings.notifications.healthCheck.user_id)
notification_key = "smoke-test-notification"
notification_key = "smoke-test-notification-#{ObjectId()}"
getOpts = (endPath)-> {url:"http://localhost:#{port}/user/#{user_id}#{endPath}", timeout:3000}
logger.log user_id:user_id, opts:getOpts(), key:notification_key, user_id:user_id, "running health check"
jobs = [
(cb)->
opts = getOpts("/")
opts.json = {key: notification_key, messageOpts:'', templateKey:'', user_id:user_id}
opts.json = {key: notification_key, messageOpts:'', templateKey:'f4g5', user_id:user_id}
request.post(opts, cb)
(cb)->
opts = getOpts("/")
@ -27,14 +29,18 @@ module.exports =
return cb("status code not 200, its #{res.statusCode}")
hasNotification = _.some body, (notification)->
notification.key == notification_key and _.contains(notification.user_id, user_id.toString())
notification.key == notification_key and notification.user_id == user_id.toString()
if hasNotification
cb()
cb(null,body)
else
logger.log body:body, "what is in the body"
cb("notification not found in response")
]
async.series jobs, (err)->
async.series jobs, (err, body)->
if err?
callback(err)
opts = getOpts("/notification_id/#{notification_id}")
request.del opts, callback
else
notification_id = body[1][0]._id
opts = getOpts("/notification/#{notification_id}")
request.del opts, (err, res, body)->
db.notifications.remove {_id:ObjectId(notification_id)}, callback

View file

@ -2,25 +2,37 @@ Settings = require 'settings-sharelatex'
logger = require('logger-sharelatex')
mongojs = require('mongojs')
db = mongojs(Settings.mongo?.url, ['notifications'])
ObjectId = require("mongojs").ObjectId
module.exports =
getUserUnreadNotifications: (user_id, callback = (err, user)->)->
db.notifications.find {"user_id" : user_id}, (err, user)->
callback err, user
getUserNotifications: (user_id, callback = (err, notifications)->)->
query =
user_id: user_id
templateKey: {"$exists":true}
db.notifications.find query, (err, notifications)->
callback err, notifications
addNotification: (user_id, notification, callback)->
doc =
query =
user_id: user_id
key: notification.key
messageOpts: notification.messageOpts
templateKey: notification.templateKey
db.notifications.insert(doc, callback)
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 =
user_id: user_id
key: notification.key
messageOpts: notification.messageOpts
templateKey: notification.templateKey
db.notifications.insert(doc, callback)
removeNotification: (user_id, notification_key, callback)->
removeNotification: (user_id, notification_id, callback)->
searchOps =
user_id:user_id
key:notification_key
_id:ObjectId(notification_id)
updateOperation =
"$set": {read:true}
"$unset": {templateKey:true}
db.notifications.update searchOps, updateOperation, callback

View file

@ -5,15 +5,18 @@ module.exports =
getUserNotifications: (req, res)->
logger.log user_id: req.params.user_id, "getting user unread notifications"
Notifications.getUserUnreadNotifications req.params.user_id, (err, notifications)->
Notifications.getUserNotifications req.params.user_id, (err, notifications)->
res.json(notifications)
addNotification: (req, res)->
logger.log user_id: req.params.user_id, notification:req.body, "adding notification"
Notifications.addNotification req.params.user_id, req.body, (err, notifications)->
res.send()
if err?
res.send 500
else
res.send()
removeNotificacion: (req, res)->
logger.log user_id: req.params.user_id, notification_key:req.params.key "removing notification"
Notifications.removeNotification req.params.user_id, req.params.key, (err, notifications)->
removeNotification: (req, res)->
logger.log user_id: req.params.user_id, notification_id: req.params.notification_id, "removing notification"
Notifications.removeNotification req.params.user_id, req.params.notification_id, (err, notifications)->
res.send()