Skip to content

Commit

Permalink
Merge 0873a02 into e937525
Browse files Browse the repository at this point in the history
  • Loading branch information
blaisebakundukize committed Jun 4, 2019
2 parents e937525 + 0873a02 commit 5df9867
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
47 changes: 47 additions & 0 deletions controllers/rate.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,53 @@ class Rate {
}
});
}

/**
* Fetch average rating on a particular article
* @param {object} req - user's request
* @param {object} res - response
* @return {object} response
*/
async getArticleAverageRating(req, res) {
const { articleId } = req.params;
const ratings = await rate.findAll({
where: { articleId },
attributes: ['userId', 'rating'],
});

if (ratings.length === 0) throw new httpError(404, 'Not found: Article has not rated');

const totalRating = ratings.map(rating => rating.rating)
.reduce((sum, rating) => sum + rating, 0);

const average = (totalRating / ratings.length).toFixed(1);

res.status(200).send({
rating: {
average,
rates: totalRating,
raters: ratings.length
}
});
}

/**
* Fetch user rating on a particular article
* @param {object} req - user's request
* @param {object} res - response
* @return {object} response
*/
async getUserArticleRating(req, res) {
const userId = req.user.id;
const { articleId } = req.params;
const rating = await rate.findOne({
where: { articleId, userId }
});
if (!rating) throw new httpError(404, 'Not found: You have not rated this article');
res.status(200).send({
rating,
});
}
}

export default Rate;
3 changes: 1 addition & 2 deletions helpers/sendEmail/emailTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import dotenv from 'dotenv';
dotenv.config();
const url = (process.env.NODE_ENV === 'production') ? 'https://badass-ah-backend-staging.herokuapp.com' : `http://127.0.0.1:${process.env.LOCALHOST_PORT}`;
const clientUrl = (process.env.NODE_ENV === 'production') ? 'https://authorsheaven.herokuapp.com' : 'http://127.0.0.1:3000';
const clientVerifytUrl = (process.env.NODE_ENV === 'production') ? 'https://authorsheaven.herokuapp.com' : 'http://127.0.0.1:3000';

/**
* An object module that holds mails' templates
Expand All @@ -17,7 +16,7 @@ const emailTemplates = {
subject: 'Email Verification',
html: `<h1 style="color: #444; margin-left: 20px;">Welcome to Author's Haven</h1>
<p style="color: #555; margin-left: 20px; font-size: 14px">Thank you for signing to the Author's Haven. Please click on the button below to activate your account.</p><br>
<a style="background-color: #61a46e; padding: 12px 15px 12px 15px; color: #eee; font-size: 16px; text-decoration: none; margin-left: 20px; cursor: pointer;" href="${clientVerifytUrl}/verify/$token">Activate account</a>`
<a style="background-color: #61a46e; padding: 12px 15px 12px 15px; color: #eee; font-size: 16px; text-decoration: none; margin-left: 20px; cursor: pointer;" href="${url}/api/users/verify/$token">Activate account</a>`
},

resetPassword: {
Expand Down
3 changes: 2 additions & 1 deletion routes/api/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ router.delete('/:articleId', auth, checkingArticle, article.deleteArticle);
router.post('/:articleId/record-reading', auth, asyncHandler(UserAccount.isUserAccountActivated), asyncHandler(checkArticle), asyncHandler(articleStats.recordReading));

router.post('/:articleId/rate', auth, asyncHandler(UserAccount.isUserAccountActivated), asyncHandler(checkArticle), asyncHandler(rate.rateArticle));

router.get('/:articleId/rate', asyncHandler(checkArticle), asyncHandler(rate.getArticleRatings));
router.get('/:articleId/average-rating', asyncHandler(checkArticle), asyncHandler(rate.getArticleAverageRating));
router.get('/:articleId/user-article-rating', auth, asyncHandler(checkArticle), asyncHandler(rate.getUserArticleRating));

router.post('/:articleId/report/type/:reportTypeId', auth, asyncHandler(UserAccount.isUserAccountActivated), asyncHandler(checkArticle), asyncHandler(articleReport.reportArticle));

Expand Down
46 changes: 45 additions & 1 deletion test/7-rate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ describe('Article ratings and reading stats', () => {
done();
});
});
it('Should return error article has not rated', (done) => {
chai.request(app)
.get(`/api/articles/${articleId}/average-rating`)
.end((err, res) => {
if (err) done(err);
res.should.have.status(404);
res.body.should.have.property('errors');
res.body.errors.should.have.property('body');
done();
});
});
it('Should return error user have not rated this article rating', (done) => {
chai.request(app)
.get(`/api/articles/${articleId}/user-article-rating`)
.set('Authorization', userToken)
.end((err, res) => {
if (err) done(err);
res.should.have.status(404);
res.body.should.have.property('errors');
done();
});
});
it('Should send article rating', (done) => {
chai.request(app)
.post(`/api/articles/${articleId}/rate`)
Expand All @@ -78,6 +100,18 @@ describe('Article ratings and reading stats', () => {
done();
});
});
it('Should return user article rating', (done) => {
chai.request(app)
.get(`/api/articles/${articleId}/user-article-rating`)
.set('Authorization', userToken)
.end((err, res) => {
if (err) done(err);
res.should.have.status(200);
res.body.should.have.property('rating');
res.body.rating.should.have.property('rating');
done();
});
});
it('Should return all article ratings', (done) => {
chai.request(app)
.get(`/api/articles/${articleId}/rate`)
Expand All @@ -90,7 +124,17 @@ describe('Article ratings and reading stats', () => {
done();
});
});

it('Should return average rating of an article', (done) => {
chai.request(app)
.get(`/api/articles/${articleId}/average-rating`)
.end((err, res) => {
if (err) done(err);
res.should.have.status(200);
res.body.should.have.property('rating');
res.body.rating.should.have.property('average');
done();
});
});
it('Should return all article ratings on a single page', (done) => {
chai.request(app)
.get(`/api/articles/${articleId}/rate?page=1`)
Expand Down

0 comments on commit 5df9867

Please sign in to comment.