Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ckend into ft-user-get-notifications-169643311
  • Loading branch information
nkalyesubula committed Nov 11, 2019
2 parents e0bbabf + e219388 commit 0627793
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
43 changes: 40 additions & 3 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 @@ -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);
}
}
}
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,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;

0 comments on commit 0627793

Please sign in to comment.