Skip to content

Commit

Permalink
Merge 70e8b77 into 03e8b40
Browse files Browse the repository at this point in the history
  • Loading branch information
sojida committed Apr 11, 2019
2 parents 03e8b40 + 70e8b77 commit dc150b4
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 22 deletions.
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"prettier:fix": "prettier --write \"./server/**/*.js\" && prettier --write \"./tests/**/*.js\"",
"check:lint-prettier": "npm run eslint:check && npm run prettier:check",
"pretest": "export NODE_ENV=test && npm run check:lint-prettier && npm run db:reset",
"coverage": "nyc report --reporter=text-lcov | coveralls"
"coverage": "nyc report --reporter=text-lcov | coveralls",
"check:coverage": "nyc check-coverage --lines 90",
"posttest": "npm run check:coverage"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -87,5 +89,11 @@
"prettier --write",
"git add"
]
},
"nyc": {
"exclude": [
"server/config/*.js",
"tests"
]
}
}
5 changes: 5 additions & 0 deletions server/controllers/article.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import tagsHelpers from '../helpers/tags-helpers';
import serverError from '../helpers/server-error';
import serchDatabase from '../helpers/search-database';
import readingTime from '../helpers/reading-time';
import notifications from '../helpers/notifications';

const { findArticle } = serchDatabase;
const { Article, User, Reported_articles: ReportedArticle } = model;
Expand Down Expand Up @@ -89,6 +90,10 @@ const createArticle = async (req, res) => {
});
}

if (!req.body.is_draft) {
notifications.sendEmailNotificationArticle(article.title, userObj.id);
}

return res.status(201).json({
message: validations.draftPublishMessage(article.is_draft),
article,
Expand Down
43 changes: 27 additions & 16 deletions server/helpers/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,39 @@ const sendEmailNotificationArticle = async (articleTitle, userId) => {
include: [
{
model: User,
required: false,
where: { id: Follower.followee_id, isNotified: true },
as: 'follower',
where: { is_notified: true },
attributes: ['email', 'first_name'],
},
{
model: User,
as: 'followee',
attributes: ['first_name'],
},
],
});
const templateSubject = 'New Notification on Authors Haven';
const templateFollowersEmail = followers.User.email;
const templateMessage = `<h1>${articleTitle}</h1>`;

const message = template(
templateSubject,
templateMessage,
templateFollowersEmail
);
sendEmail(templateFollowersEmail, templateSubject, message);
followers.forEach(user => {
const templateSubject = 'New Notification on Authors Haven';
const templateFollowersEmail = user.follower.email;
const templateMessage = `
<p>${
user.followee.first_name
} just published a new article <br> <b>${articleTitle}</b></p>`;

// in-app notification
pusher.trigger('channel', 'event', {
message: `${
followers.User.first_name
} has published a new article ${articleTitle}`,
const message = template(
templateSubject,
templateMessage,
templateFollowersEmail
);
sendEmail(templateFollowersEmail, templateSubject, message);

// in-app notification
pusher.trigger('channel', 'event', {
message: `${
user.follower.first_name
} has published a new article ${articleTitle}`,
});
});
};

Expand Down
8 changes: 4 additions & 4 deletions server/seeders/20190331205705-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
is_admin: true,
is_reviewer: false,
is_reported: false,
is_notified: true,
is_notified: false,
research_field: 'Chemistry',
},
{
Expand All @@ -52,7 +52,7 @@ module.exports = {
is_admin: false,
is_reviewer: false,
is_reported: false,
is_notified: true,
is_notified: false,
research_field: 'Physics',
},
{
Expand All @@ -79,7 +79,7 @@ module.exports = {
last_name: 'Daniel',
title: 'Dr',
phone_number: '(934) 975-1648',
email: 'peter@gmail.com',
email: 'adesojitest22@gmail.com',
bio: 'Worked as a lecturer...',
password:
'$2a$08$FaLCM57LR8X4apZYpKeVb.1XC082FTmkhWp3//j3TVr2XHYg.fuDK',
Expand All @@ -106,7 +106,7 @@ module.exports = {
is_admin: false,
is_reviewer: false,
is_reported: false,
is_notified: true,
is_notified: false,
research_field: 'Physics',
},
]),
Expand Down
2 changes: 1 addition & 1 deletion tests/article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('ARTICLE', () => {
chai
.request(app)
.post(`/api/v1/article`)
.set('Authorization', userAToken)
.set('Authorization', userBToken)
.send({
body: 'Lorem ipsum dolor sit amet consectetur adipiscing elit',
is_draft: false,
Expand Down
10 changes: 10 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ describe('HOMEPAGE', () => {
done();
});
});

it('should respond with documentation route', done => {
chai
.request(app)
.get('/api/v1/doc')
.end((err, res) => {
expect(res).to.have.status(200);
done();
});
});
});

0 comments on commit dc150b4

Please sign in to comment.