Skip to content

Commit

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

import models from '../models/index';

/**
* @description Contains method to allow users opt in and out of notifications
Expand Down Expand Up @@ -31,5 +31,30 @@ 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) {
return res.status(404).json({ status: 404, message: 'You currently do not have notifications' });
}
return res.send({ notifications: userNotification });
} catch (error) {
return res.send({ error: error.message });
}
}
}
export default NotificationController;
3 changes: 2 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,10 @@ import validateToken from '../../../middlewares/auth';
import NotificationController from '../../../controllers/notifications.controller';

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

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

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 ee16e4f

Please sign in to comment.