Skip to content

Commit

Permalink
Bug(notification): fix user notifications
Browse files Browse the repository at this point in the history
- adding the condition to check if the user allows notifications before saving them

[Delivers #166756952]
  • Loading branch information
P.C. Ndayisenga authored and P.C. Ndayisenga committed Jun 18, 2019
1 parent 92b69ab commit 18db76c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion controllers/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
})
Expand Down
38 changes: 21 additions & 17 deletions controllers/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -59,34 +61,36 @@ 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) {
return error;
}
}


/**
* Send email notifications
* @param {Integer} articleId id of the article
Expand Down Expand Up @@ -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' });
Expand Down

0 comments on commit 18db76c

Please sign in to comment.