Skip to content

Commit

Permalink
feature(user-notifications): User should be able to receive notificat…
Browse files Browse the repository at this point in the history
…ions [Finishes #169643311]
  • Loading branch information
nkalyesubula committed Nov 8, 2019
1 parent e219388 commit 22b95a0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/controllers/notifications.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import notificationHelper from '../helpers/notification.helper';
import models from '../models/index';


/**
Expand Down Expand Up @@ -31,5 +32,30 @@ class NotificationController {
return res.status(404).json({ error: 'route does not exist' });
}
}

/**
*
* @description Method to get user notifications
* @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 usersNotifications = await models.AppNotification.findAll({
where: { receiverId: user.id },
});
if (usersNotifications.length === 0) {
return res.status(404).json({ status: 404, message: 'You currently do not have notifications' });
}
return res.send({ notifications: usersNotifications });
} catch (error) {
return res.send({ error: error.message });
}
}
}
export default NotificationController;
1 change: 0 additions & 1 deletion src/helpers/notification.helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import models from '../models/index';


export default async (req, res, next, userColumn) => {
const userDetails = await models.user.findOne({ where: { email: req.auth.email } });
const { id } = userDetails;
Expand Down
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 22b95a0

Please sign in to comment.