Skip to content

Commit

Permalink
Merge 0627793 into e219388
Browse files Browse the repository at this point in the history
  • Loading branch information
nkalyesubula committed Nov 11, 2019
2 parents e219388 + 0627793 commit bb43c10
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
62 changes: 62 additions & 0 deletions src/controllers/notifications.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import notificationHelper from '../helpers/notification.helper';
import models from '../models/index';
import Util from '../helpers/util';

const util = new Util();

/**
* @description Contains method to allow users opt in and out of notifications
Expand Down Expand Up @@ -31,5 +34,64 @@ class NotificationController {
return res.status(404).json({ error: 'route does not exist' });
}
}

/**
*
* @description Method to opt in and out of email and push notication
* @static
* @param {object} req client request
* @param {object} res server response
* @returns {Object} server response object
* @param {Function} next passes control to the next middleware
* @memberof NotificationController
*/
static async getUserNotification(req, res) {
try {
const user = req.auth;
const userNotification = await models.AppNotification.findAll({
where: { receiverId: user.id },
});
if (userNotification.length === 0) {
util.setError(404, 'You currently do not have notifications');
return util.send(res);
}
util.setSuccess(200, 'notifications', userNotification);
return util.send(res);
} catch (error) {
util.setError(400, { error: error.message });
return util.send(res);
}
}

/**
*
* @description Method to change notification status to true
* @static
* @param {object} req client request
* @param {object} res server response
* @returns {Object} server response object
* @param {Function} next passes control to the next middleware
* @memberof NotificationController
*/
static async updateNotificationStatus(req, res) {
try {
const { id } = req.params;
const userNotification = await models.AppNotification.findOne({
where: { id }
});
if (userNotification) {
await models.AppNotification.update({
read: true,
}, { where: { id } });
util.setSuccess(200, 'Notification has been retrieved', null);
return util.send(res);
}
util.setError(404, { message: 'Notification Not Found' });
return util.send(res);
} catch (error) {
util.setError(400, { error: error.message });
return util.send(res);
}
}
}
export default NotificationController;
4 changes: 3 additions & 1 deletion src/routes/api/notifications/notification.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import validateToken from '../../../middlewares/auth';
import NotificationController from '../../../controllers/notifications.controller';

const router = express.Router();
const { notification } = NotificationController;
const { notification, getUserNotification, updateNotificationStatus } = NotificationController;

// req.params can only be 'email' or 'inApp''
router.patch('/:emailOrInApp', validateToken, notification);
router.get('/', validateToken, getUserNotification);
router.put('/:id', validateToken, updateNotificationStatus);

export default router;
12 changes: 12 additions & 0 deletions test/notifications.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,16 @@ describe('test for opting in and out of notifications', () => {
done();
});
});
it('test for getting user notifications', (done) => {
chai.request(server)
.get('/api/v1/notifications/')
.set('Authorization', usertoken)
.end((error, res) => {
expect(res.status).to.equal(404);
expect(res.body).to.be.an('object');
expect(res.body).to.have.property('message');
expect(res.body.message).to.contain('You currently do not have notifications');
done();
});
});
});

0 comments on commit bb43c10

Please sign in to comment.