Skip to content

Commit

Permalink
Close GladysAssistant#450 : Add ability to create notification from R…
Browse files Browse the repository at this point in the history
…EST API
  • Loading branch information
Pierre-Gilles committed Nov 26, 2018
1 parent fea838e commit 57227c1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
22 changes: 22 additions & 0 deletions api/controllers/NotificationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ module.exports = {
.catch(next);
},

/**
* @api {post} /notification Create notification
* @apiName createNotification
* @apiGroup Notification
* @apiPermission authenticated
*
* @apiParam {string} title Title of the notification
* @apiParam {string} text Text of the notification
* @apiParam {string} icon font-awesome icon. Ex: 'fa fa-fire'
* @apiParam {string} iconColor bg-color, ex: 'bg-yellow',
* @apiParam {integer} priority priority from -1 to 2
*
*/
create: function(req, res, next){
req.body.user = req.session.User.id;
gladys.notification.create(req.body)
.then(function(notification){
return res.status(201).json(notification);
})
.catch(next);
},

/**
* Read notifications from a particular user
*/
Expand Down
1 change: 1 addition & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ module.exports.routes = {

// Notification
'get /notification': 'NotificationController.index',
'post /notification': 'NotificationController.create',
'patch /notification/read': 'NotificationController.read',

// NotificationType
Expand Down
27 changes: 27 additions & 0 deletions test/unit/api/controllers/Notification/notification.create.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var request = require('supertest');

describe('Notification', function() {
describe('create', function() {
it('should create a notification', function(done) {
var notification = {
title: 'Fire !',
text: 'Sir, there is a fire in the kitchen !',
icon: 'fa fa-fire',
iconColor: 'bg-yellow',
priority: 2
};

request(sails.hooks.http.app)
.post('/notification?token=test')
.send(notification)
.expect(201)
.end(function(err, res) {
if (err) {
return done(err);
}

done();
});
});
});
});

0 comments on commit 57227c1

Please sign in to comment.