From 18db76cc3b2bc494d8bf63a986d5eef2aaa2c48d Mon Sep 17 00:00:00 2001 From: "P.C. Ndayisenga" Date: Tue, 18 Jun 2019 11:47:40 +0200 Subject: [PATCH] Bug(notification): fix user notifications - adding the condition to check if the user allows notifications before saving them [Delivers #166756952] --- controllers/article.js | 2 +- controllers/notification.js | 38 ++++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/controllers/article.js b/controllers/article.js index c5072d5..2e45490 100644 --- a/controllers/article.js +++ b/controllers/article.js @@ -54,7 +54,7 @@ class ArticleController { } }); const message = `${user.username} published a new article`; - await notification.createNotificationForFavorite(article.author, message); + await notification.createNotificationForFollower(article.author, message); await notification.sendNotificationToFollower(article.author, message); res.status(201).json({ status: 201, message: 'Article created successfully', article }); }) diff --git a/controllers/notification.js b/controllers/notification.js index a78df37..73b933b 100644 --- a/controllers/notification.js +++ b/controllers/notification.js @@ -38,17 +38,19 @@ class NotificationController { if (usersWhoFavoritedArticle.length > 0) { // eslint-disable-next-line array-callback-return usersWhoFavoritedArticle.map((user) => { - const { id } = user.userfkey.dataValues; - if (id !== notifier) { + const { id, allowNotifications } = user.userfkey.dataValues; + if (id !== notifier && allowNotifications === true) { const notification = { userId: id, message }; notifications.push(notification); } }); if (notifications.length > 0) { - notifications = Object.values(notifications - .reduce((acc, cur) => Object.assign(acc, { [cur.userId]: cur }), {})); - const createdNotifications = await Notification - .bulkCreate(notifications, { returning: true }); + notifications = Object.values( + notifications.reduce((acc, cur) => Object.assign(acc, { [cur.userId]: cur }), {}) + ); + const createdNotifications = await Notification.bulkCreate(notifications, { + returning: true + }); return createdNotifications; } } @@ -59,26 +61,29 @@ class NotificationController { /** * Create app notifications - * @param {Integer} folloewerId id of the article + * @param {Integer} followerId id of the article * @param {Text} message -body of notification * @param {Integer} notifier -notifier id * @returns {Object} Create notifications */ - async createNotificationForFollower(folloewerId, message) { + async createNotificationForFollower(followerId, message) { try { const notifications = []; const notification = { userId: '', message: '' }; - const followers = await Check.checkFollowers(folloewerId); + const followers = await Check.checkFollowers(followerId); if (followers.length > 0) { // eslint-disable-next-line array-callback-return followers.map((follower) => { - const { id } = follower.followedFkey.dataValues; - notification.userId = id; - notification.message = message; - notifications.push(notification); + const { id, allowNotifications } = follower.followedFkey.dataValues; + if (allowNotifications === true) { + notification.userId = id; + notification.message = message; + notifications.push(notification); + } + }); + const createdNotifications = await Notification.bulkCreate(notifications, { + returning: true }); - const createdNotifications = await Notification - .bulkCreate(notifications, { returning: true }); return createdNotifications; } } catch (error) { @@ -86,7 +91,6 @@ class NotificationController { } } - /** * Send email notifications * @param {Integer} articleId id of the article @@ -172,7 +176,7 @@ class NotificationController { try { const id = parseInt(req.params.id, 10); const notification = await Notification.findOne({ - where: { id, userId: req.user.id, status: 'unread' }, + where: { id, userId: req.user.id, status: 'unread' } }); if (!notification) { return res.status(404).json({ status: 404, error: 'Notification not found' });