Skip to content

Commit

Permalink
Merge pull request #53 from andela/bg-notification-168191815
Browse files Browse the repository at this point in the history
#168191815 bug fix to notify users when article is published
  • Loading branch information
devPinheiro committed Aug 29, 2019
2 parents 0a491f3 + 7af1082 commit e092620
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 11 deletions.
28 changes: 26 additions & 2 deletions src/services/article.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import Helper from './helper';
import { paginationQueryMetadata, pageMetadata } from '../helpers/pagination';
import readtime from '../helpers/read-time';
import averageRatings from '../helpers/average-ratings';
import {
commentArticleNotification,
likeCommentNotification,
sendNotificationOnArticlePublish
} from './notification.service';

const {
Comment,
Expand Down Expand Up @@ -394,6 +399,11 @@ export const publishArticleService = async data => {
isPublished: true,
publishedAt: date.toISOString()
});
const details = {
publisherUserId: data.user.id,
articleSlug
};
sendNotificationOnArticlePublish(details);
return article;
};

Expand Down Expand Up @@ -442,7 +452,7 @@ export const createCommentService = async (userId, slug, commentDetails) => {
{
model: User,
as: 'author',
attributes: ['firstName', 'lastName'],
attributes: ['id', 'firstName', 'lastName'],
include: [
{ model: Follow, as: 'followersfriend', attributes: ['isFollowing'] }
]
Expand Down Expand Up @@ -471,6 +481,14 @@ export const createCommentService = async (userId, slug, commentDetails) => {
highlightedText: highlightedText || null
});

const details = {
userId: article['author.id'],
commentUserId: userId,
articleSlug: slug,
commentId: result.dataValues.id
};
commentArticleNotification(details);

const comment = {
id: result.id,
createdAt: result.createdAt,
Expand Down Expand Up @@ -665,13 +683,19 @@ export const likeCommentService = async (slug, commentId, userId, email) => {
});

const likeCount = await getCommentLikesCount(commentId);

const result = {
likeCount,
message: 'you have successfully liked this comment',
commentId,
userId
};
const details = {
userId: commentExist.dataValues.userId,
likeUserId: userId,
articleSlug: slug,
commentId
};
likeCommentNotification(details);
return result;
}
await CommentReaction.destroy({
Expand Down
53 changes: 49 additions & 4 deletions src/services/notification.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import Pusher from 'pusher';
import dotenv from 'dotenv';
import models from '../db/models';
import { findUserById } from './auth.service';
import { getUserFollowersService } from './user.service';

dotenv.config();

const { Notification } = models;
const { Notification, Follow } = models;

/**
* @method pusher
Expand Down Expand Up @@ -86,7 +87,7 @@ export const followNotification = async (userId, friendUserId) => {
const sender = await findUserById(userId);
const receiver = await findUserById(friendUserId);
if (receiver.isNotified) {
const message = `${sender.userName} is now following you`;
const message = `${sender.firstName} is now following you`;
const link = `/profiles/${sender.userName}`;
const messageDetails = {
senderUserId: sender.id,
Expand Down Expand Up @@ -158,7 +159,7 @@ export const likeArticleNotification = async details => {
const receiver = await findUserById(userId);

if (receiver.isNotified) {
const message = `${sender.userName} liked your article`;
const message = `${sender.firstName} liked your article`;
const link = `/articles/${articleSlug}`;
const messageDetails = {
senderUserId: sender.dataValues.id,
Expand Down Expand Up @@ -194,7 +195,7 @@ export const likeCommentNotification = async details => {
const receiver = await findUserById(userId);

if (receiver.isNotified) {
const message = `${sender.userName} liked your comment`;
const message = `${sender.firstName} liked your comment`;
const link = `/articles/${articleSlug}/comments/${commentId}`;
const messageDetails = {
senderUserId: sender.id,
Expand All @@ -214,3 +215,47 @@ export const likeCommentNotification = async details => {
return response;
}
};

/**
* @method likeCommentNotification
* @description create a notification when a user likes a comment
*
* @param {Object} details details of the notification to be sent
*
* @returns {Boolean} true or false
*/
export const sendNotificationOnArticlePublish = async details => {
try {
const { publisherUserId, articleSlug } = details;
const sender = await findUserById(publisherUserId);
const followers = await getUserFollowersService(publisherUserId);

const followersUserId = followers.map(fol => {
return fol.dataValues.friendUserId;
});
await followersUserId.forEach(async userId => {
const receiver = await findUserById(userId);
if (receiver.isNotified) {
const message = `${sender.firstName} published an article`;
const link = `/articles/${articleSlug}`;
const messageDetails = {
senderUserId: sender.id,
receiverUserId: receiver.id,
notificationMessage: message,
link
};
pushNotification(receiver, message);

const savedNotification = await createNotificationMessage(
messageDetails
);
return !!savedNotification;
}
return false;
});
return false;
} catch (error) {
const response = { message: 'something went wrong' };
return response;
}
};
16 changes: 15 additions & 1 deletion src/services/reaction.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable import/prefer-default-export */
import models from '../db/models';
import { likeArticleNotification } from './notification.service';

const { ArticleReaction } = models;
const { ArticleReaction, Article } = models;
/**
* @method reactionService
* - user can like or dislike article
Expand All @@ -23,11 +24,24 @@ export const reactionService = async data => {
articleId
}
});
const article = await Article.findOne({
where: {
id: articleId
}
});

if (findReaction == null) {
await ArticleReaction.create({
userId,
articleId
});

const details = {
userId: article.dataValues.userId,
likeUserId: userId,
articleSlug: article.dataValues.slug
};
likeArticleNotification(details);
return { message: 'You have liked this article successfully' };
}
await findReaction.destroy({ where: { userId } });
Expand Down
3 changes: 3 additions & 0 deletions src/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Helper from './helper';
import model from '../db/models';
import { paginationQueryMetadata, pageMetadata } from '../helpers/pagination';
import { sendWelcomeEmail } from '../helpers/mail.helper';
import { followNotification } from './notification.service';

const { User, Follow } = model;

Expand Down Expand Up @@ -174,12 +175,14 @@ export const followUserService = async (userId, friendUserId) => {
}

// if isFollwing is false, it's updated to true
followNotification(friendUserId, userId);
return 'You have followed this user';
}
await Follow.create({
userId,
friendUserId
});
followNotification(friendUserId, userId);
return 'You have followed this user';
};

Expand Down
8 changes: 4 additions & 4 deletions src/tests/services/notification.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Notification service', () => {
userId: userIdA,
commentUserId: userIdB,
articleSlug:
'/articles/temporibus-labore-laborum-repudiandae-vitae-rerum-debitis-ut-quidem-quisquam/comments/2',
'temporibus-labore-laborum-repudiandae-vitae-rerum-debitis-ut-quidem-quisquam',
commentId: 5
};
const response = await commentArticleNotification(details);
Expand All @@ -80,7 +80,7 @@ describe('Notification service', () => {
userId: userIdA,
likeUserId: userIdB,
articleSlug:
'/articles/temporibus-labore-laborum-repudiandae-vitae-rerum-debitis-ut-quidem-quisquam/comments/2'
'temporibus-labore-laborum-repudiandae-vitae-rerum-debitis-ut-quidem-quisquam'
};
const response = await likeArticleNotification(details);
expect(response).to.equal(true);
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('Notification service', () => {
userId: userIdC,
commentUserId: userIdB,
articleSlug:
'/articles/temporibus-labore-laborum-repudiandae-vitae-rerum-debitis-ut-quidem-quisquam/comments/2',
'temporibus-labore-laborum-repudiandae-vitae-rerum-debitis-ut-quidem-quisquam',
commentId: 5
};
const response = await commentArticleNotification(details);
Expand All @@ -163,7 +163,7 @@ describe('Notification service', () => {
userId: userIdC,
likeUserId: userIdA,
articleSlug:
'/articles/temporibus-labore-laborum-repudiandae-vitae-rerum-debitis-ut-quidem-quisquam/comments/2'
'temporibus-labore-laborum-repudiandae-vitae-rerum-debitis-ut-quidem-quisquam'
};
const response = await likeArticleNotification(details);
expect(response).to.equal(false);
Expand Down
Binary file added uploads/2019-08-29T12:54:12.299Zimage.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added uploads/2019-08-29T12:54:12.695Zimage.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added uploads/2019-08-29T14:23:18.454Zimage.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added uploads/2019-08-29T14:23:18.827Zimage.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added uploads/2019-08-29T14:24:01.159Zimage.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added uploads/2019-08-29T14:24:01.492Zimage.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e092620

Please sign in to comment.