Skip to content

Commit

Permalink
Add notifications for app
Browse files Browse the repository at this point in the history
  • Loading branch information
ravisuhag committed Jun 3, 2016
1 parent 57365ac commit 2e979cc
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
62 changes: 62 additions & 0 deletions app/controllers/api/notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

var Joi = require('joi');
var _ = require('lodash');
var Notifier = require('../../helpers/notifier');

exports.showUnreadNotifications = {
description: 'Show unread notifications',
handler: function(request, reply) {

var db = request.server.plugins.sequelize.db;
var notifications = request.server.plugins.sequelize.db.notifications;

notifications.findAll({
where: {
user_id: request.auth.credentials.id,
viewed: 0
}

}).then(function(unread) {

unread.forEach(function(n) {
n.dataValues.msg = Notifier.message(n, request);
});

notifications.update({
viewed: 1
}, {
where: {
user_id: request.auth.credentials.id,
viewed: 0
}
});
reply(unread);
});

}
};

exports.showReadNotifications = {
description: 'Show notifications',
handler: function(request, reply) {

var db = request.server.plugins.sequelize.db;
var notifications = request.server.plugins.sequelize.db.notifications;

notifications.findAll({
where: {
user_id: request.auth.credentials.id,
viewed: 1
}
}).then(function(read) {

read.forEach(function(n) {
n.dataValues.msg = Notifier.message(n, request);
});

reply(read);
});

}
};
21 changes: 15 additions & 6 deletions app/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ exports.register = function(plugin, options, next) {
logout: require('../controllers/api/logout'),
cards: require('../controllers/api/cards'),
profile: require('../controllers/api/profile'),
translate: require('../controllers/api/translate')
translate: require('../controllers/api/translate'),
notifications: require('../controllers/api/notifications')
}
};

plugin.route([
// All the routes all prefixed with /api check manifest
// Login api

// Api routes
{
method: 'POST',
path: '/api/login',
config: Controllers.api.login.postForm
},{
}, {
method: 'POST',
path: '/api/profile',
config: Controllers.api.profile.postEditProfile
Expand All @@ -33,11 +34,19 @@ exports.register = function(plugin, options, next) {
method: 'GET',
path: '/api/cards',
config: Controllers.api.cards.getData
},
{
}, {
method: 'GET',
path: '/api/translate',
config: Controllers.api.translate.getData
}, {
method: 'GET',
path: '/api/notifications/unread',
config: Controllers.api.notifications.showUnreadNotifications
},
{
method: 'GET',
path: '/api/notifications/read',
config: Controllers.api.notifications.showReadNotifications
}
]);

Expand Down

0 comments on commit 2e979cc

Please sign in to comment.