Skip to content

Commit

Permalink
Merge 6271a78 into 32f31e8
Browse files Browse the repository at this point in the history
  • Loading branch information
kevoese committed Jul 29, 2019
2 parents 32f31e8 + 6271a78 commit dc14c62
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 30 deletions.
15 changes: 12 additions & 3 deletions controllers/articles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ export default {
});
}

article.setDataValue('author', {
await article.setDataValue('author', {
username: user.username,
bio: user.bio,
image: user.image,
});

article.setDataValue('tagList', tagList);
await article.setDataValue('tagList', tagList);

await Notification.articleNotification({
userId: req.user.id,
Expand Down Expand Up @@ -316,7 +316,7 @@ export default {

let resStatus = 201;
let message = status ? 'You upvote this article' : 'You downvote this article';

let notify = status;

if (!vote) {
await db.ArticleVote.create(voteDetails);
Expand All @@ -325,11 +325,20 @@ export default {
if (status === vote.status) {
await vote.deleteArticleVote();
message = 'You have unvote this article';
notify = false;
} else {
await vote.updateArticleVote(status);
}
}

if (notify) {
await Notification.articleNotification({
articleId,
userId,
type: 'like'
});
}

const upvotes = await db.ArticleVote.getArticleVotes({ ...voteDetails, status: true });
const downvotes = await db.ArticleVote.getArticleVotes({ ...voteDetails, status: false });

Expand Down
9 changes: 8 additions & 1 deletion controllers/comments/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import db from '../../db/models';
import Notification from '../../utils/notifications';

export default {
addComment: async (req, res) => {
const { params: { slug }, body: { content }, user } = req;
let { highlightedTextObj } = req.body;
try {
const foundArticle = await db.Article.findOne({
where: { slug }
where: { slug },
});
if (!foundArticle) {
return res.status(404).send({
Expand All @@ -20,6 +21,12 @@ export default {
content,
highlightedText: highlightedTextObj
});

Notification.articleNotification({
articleId: foundArticle.id,
userId: user.id,
type: 'comment'
});
return res.status(201).json({
message: 'Comment added successfully',
comment
Expand Down
7 changes: 7 additions & 0 deletions controllers/membership/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import db from '../../db/models';
import { getMembers } from '../../utils';
import Notification from '../../utils/notifications';

export default {
createFollowing: async (req, res) => {
Expand Down Expand Up @@ -40,6 +41,12 @@ export default {
followerId,
followId
});

await Notification.followNotification({
userId: followerId,
followedUserId: followId
});

return res.status(200).send({
user: result
});
Expand Down
23 changes: 15 additions & 8 deletions tests/functions/notification.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ describe('Notification functions', () => {

after(async () => {
await db.User.destroy({ truncate: true, cascade: true });
await db.MemberShip.destroy({ truncate: true, cascade: true });
mockTransporter.restore();
mockPusher.restore();
});
beforeEach(async () => {
bulkCreateSpy = sinon.spy(db.Notification, 'bulkCreate');
Expand All @@ -55,7 +57,6 @@ describe('Notification functions', () => {
afterEach(() => {
createSpy.restore();
bulkCreateSpy.restore();
mockPusher.restore();
});

describe('Testing notification function', () => {
Expand Down Expand Up @@ -88,7 +89,7 @@ describe('Notification functions', () => {
});

const { message } = notificationContent.dataValues;
expect(message).to.equal(`${vincent.firstName} ${vincent.lastName} likes your article titled "${hamzaArticle.dataValues.title}"`);
expect(message).to.equal(`${vincent.firstName} ${vincent.lastName} likes your article "${hamzaArticle.dataValues.title}"`);
});

it('should create a new comment notification message', async () => {
Expand All @@ -106,7 +107,7 @@ describe('Notification functions', () => {
});

const { message } = notificationContent.dataValues;
expect(message).to.equal(`${vincent.firstName} ${vincent.lastName} commented on your article titled "${hamzaArticle.dataValues.title}"`);
expect(message).to.equal(`${vincent.firstName} ${vincent.lastName} commented on your article "${hamzaArticle.dataValues.title}"`);
});

it('should create a new dislike notification message', async () => {
Expand All @@ -125,25 +126,31 @@ describe('Notification functions', () => {
});

const { message } = notificationContent.dataValues;
expect(message).to.equal(`${vincent.firstName} ${vincent.lastName} dislikes your article titled "${hamzaArticle.dataValues.title}"`);
expect(message).to.equal(`${vincent.firstName} ${vincent.lastName} dislikes your article "${hamzaArticle.dataValues.title}"`);
});

it('should get a new publish notification message', async () => {
await db.MemberShip.create({
followId: hamza.id,
followerId: vincent.id
});

await Notifications.articleNotification({
userId: vincent.id,
userId: hamza.id,
articleId: hamzaArticle.dataValues.id,
type: 'publish'
});

expect(bulkCreateSpy.calledOnce).to.be.true;
const notificationContent = await db.Notification.findOne({
where: {
senderId: vincent.id,
receiverId: hamza.id
senderId: hamza.id,
receiverId: vincent.id
}
});

const { message } = notificationContent.dataValues;
expect(message).to.equal(`${vincent.firstName} ${vincent.lastName} published a new article titled "${hamzaArticle.dataValues.title}"`);
expect(message).to.equal(`${hamza.firstName} ${hamza.lastName} published a new article titled "${hamzaArticle.dataValues.title}"`);
});
});
});
4 changes: 4 additions & 0 deletions tests/routes/articles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../helpers';
import * as utils from '../../utils';
import { transporter } from '../../utils/mailer';
import Notifications from '../../utils/notifications';

const { expect } = chai;
let mockTransporter;
Expand All @@ -18,11 +19,13 @@ let register;
let mockUploadImage;
let mockDeleteImage;
let ratingUser;
let mockPusher;
let category;

describe('ARTICLES TEST', () => {
before(async () => {
mockTransporter = sinon.stub(transporter, 'sendMail').resolves({});
mockPusher = sinon.stub(Notifications.pusher, 'trigger').resolves({});
});
beforeEach(async () => {
category = await createCategory({ name: 'comms' });
Expand Down Expand Up @@ -50,6 +53,7 @@ describe('ARTICLES TEST', () => {

after(async () => {
mockTransporter.restore();
mockPusher.restore();
await db.Article.destroy({ truncate: true, cascade: true });
await db.User.destroy({ truncate: true, cascade: true });
await db.Ratings.destroy({ truncate: true, cascade: true });
Expand Down
4 changes: 4 additions & 0 deletions tests/routes/membership.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import sinon from 'sinon';
import { createUser } from '../helpers';
import { app, db } from '../../server';
import { transporter } from '../../utils/mailer';
import Notifications from '../../utils/notifications';


const { expect } = chai;

chai.use(chaiHttp);
let mockTransporter;
let mockPusher;
let user1;
let user2;
let user3;
Expand All @@ -18,6 +20,7 @@ let user4;
describe('FOLLOW TEST', () => {
beforeEach(async () => {
mockTransporter = sinon.stub(transporter, 'sendMail').resolves({});
mockPusher = sinon.stub(Notifications.pusher, 'trigger').resolves({});
await db.User.destroy({ truncate: true, cascade: true });
await db.MemberShip.destroy({ truncate: true, cascade: true });
user1 = await createUser({
Expand Down Expand Up @@ -52,6 +55,7 @@ describe('FOLLOW TEST', () => {

afterEach(async () => {
mockTransporter.restore();
mockPusher.restore();
await db.User.sync({ truncate: true, casade: true });
await db.MemberShip.sync({ truncate: true, casade: true });
});
Expand Down
8 changes: 7 additions & 1 deletion tests/routes/search.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import sinon from 'sinon';
import { app, db } from '../../server';
import {
createUser, createArticle, createTag, createArticleTag
} from '../helpers';
import { transporter } from '../../utils/mailer';

const { expect } = chai;

chai.use(chaiHttp);
let user, article, newUser, newArticle, newTag = {};
describe('USER PROFILE', () => {
let mockTransporter;

describe('SEARCH TEST', () => {
before(async () => {
await db.Article.destroy({ truncate: true, cascade: true });
mockTransporter = sinon.stub(transporter, 'sendMail').resolves({});
user = {
firstName: 'vincent',
lastName: 'hamza',
Expand All @@ -31,6 +36,7 @@ describe('USER PROFILE', () => {
});
after(async () => {
await db.Article.destroy({ truncate: true, cascade: true });
mockTransporter.restore();
});
describe('SEARCH', () => {
it('Should not get articles if no filter is entered', async () => {
Expand Down
47 changes: 30 additions & 17 deletions utils/notifications/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,44 @@ const followNotification = async ({ userId, followedUserId }) => {
const bulkNotify = async ({
senderId, message, emailMsg, link
}) => {
const userIds = await db.User.findAll({
const inApp = await db.MemberShip.findAll({
where: {
inAppNotify: true,
followId: senderId
},
attributes: ['id']
include: [{
model: db.User,
as: 'follower',
where: {
inAppNotify: true,
},
attributes: ['id']
}]
});

const users = await db.User.findAll({
const emailNotify = await db.MemberShip.findAll({
where: {
emailNotify: true,
followId: senderId
},
attributes: ['email']
include: [{
model: db.User,
as: 'follower',
where: {
emailNotify: true,
},
attributes: ['email']
}]
});
// TODO:: Fetch users following this user and send email
// to them when an article is created
const usersEmail = users.map(user => user.email);
const followersIds = inApp.map(user => user.follower.id);
const followersEmails = emailNotify.map(user => user.follower.email);

sendEmailNotification(usersEmail, emailMsg, link);
sendEmailNotification(followersEmails, emailMsg, link);

const data = userIds.map(user => ({
receiverId: user.id, senderId, message, link
const data = followersIds.map(receiverId => ({
receiverId, senderId, message, link
}));

await db.Notification.bulkCreate(data);
await pushNotification(userIds);
await pushNotification(followersIds);
};


Expand All @@ -122,8 +135,8 @@ const articleNotification = async ({ userId, articleId, type }) => {
const link = `/articles/${slug}`;

if (type === 'publish') {
const publishMsg = `${user.firstName} ${user.lastName} published a new article titled "${title}"`;
const publishEmailMsg = `${user.firstName} ${user.lastName} published a new article titled <strong>${title}</strong>`;
const publishMsg = `${author.firstName} ${author.lastName} published a new article titled "${title}"`;
const publishEmailMsg = `${author.firstName} ${author.lastName} published a new article titled <strong>${title}</strong>`;
await bulkNotify({
senderId: userId,
message: publishMsg,
Expand All @@ -136,8 +149,8 @@ const articleNotification = async ({ userId, articleId, type }) => {
else if (type === 'dislike') msgType = 'dislikes';
else msgType = 'commented on';

const message = `${user.firstName} ${user.lastName} ${msgType} your article titled "${title}"`;
const emailMsg = `${user.firstName} ${user.lastName} ${msgType} your article titled <strong>${title}</strong>`;
const message = `${user.firstName} ${user.lastName} ${msgType} your article "${title}"`;
const emailMsg = `${user.firstName} ${user.lastName} ${msgType} your article <strong>${title}</strong>`;

if (emailNotify) await sendEmailNotification(email, emailMsg, link);

Expand Down

0 comments on commit dc14c62

Please sign in to comment.