diff --git a/src/controllers/notifications.controller.js b/src/controllers/notifications.controller.js index a1c6c5b..e351e17 100644 --- a/src/controllers/notifications.controller.js +++ b/src/controllers/notifications.controller.js @@ -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 @@ -49,11 +52,45 @@ class NotificationController { where: { receiverId: user.id }, }); if (userNotification.length === 0) { - return res.status(404).json({ status: 404, message: 'You currently do not have notifications' }); + 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); } - return res.send({ notifications: userNotification }); + util.setError(404, { message: 'Notification Not Found' }); + return util.send(res); } catch (error) { - return res.send({ error: error.message }); + util.setError(400, { error: error.message }); + return util.send(res); } } } diff --git a/src/routes/api/notifications/notification.route.js b/src/routes/api/notifications/notification.route.js index e730059..b244299 100644 --- a/src/routes/api/notifications/notification.route.js +++ b/src/routes/api/notifications/notification.route.js @@ -3,10 +3,11 @@ import validateToken from '../../../middlewares/auth'; import NotificationController from '../../../controllers/notifications.controller'; const router = express.Router(); -const { notification, getUserNotification } = 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;