Skip to content

Commit

Permalink
Merge ac2a49f into fcb8c10
Browse files Browse the repository at this point in the history
  • Loading branch information
eliemugenzi authored Jul 9, 2019
2 parents fcb8c10 + ac2a49f commit f1eaf07
Show file tree
Hide file tree
Showing 50 changed files with 984 additions and 669 deletions.
6 changes: 6 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ exclude_patterns:
- "src/sequelize/seeders/"
- "**/node_modules/"
- "test/**/*.js"
- "src/middleware/search.js"
- "src/helpers/user/*.js"
- "src/helpers/article/*.js"
- "src/helpers/comment/*.js"
- "src/helpers/validationSchemas.js"
- "src/middleware/shareArticle.js"
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@
"src/sequelize/**/*.js",
"test/**/*.js",
"src/helpers/notifications/*.js",
"src/helpers/SocketIO.js"
"src/helpers/SocketIO.js",
"src/helpers/stats/*.js",
"src/helpers/article/*.js"
]
}
}
121 changes: 121 additions & 0 deletions src/api/controllers/articleBlocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import models from '../../sequelize/models';

import AuthorNotifier from '../../helpers/NotifyAuthorOnArticleBlock';

const {
notifyAuthorblock, notifyAuthorUnblock
} = AuthorNotifier;
const {
Article, BlockedArticles, ReportedArticles, User
} = models;

// eslint-disable-next-line no-array-constructor
const days = new Array(
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday'
);

/**
* @class
*/
class ArticleBlocks {
/**
* @param {object} req
* @param {object} res
* @return {object} returns a message with operation status
*/
static async reportArticle(req, res) {
const { username } = req.user;
const { comment } = req.body;
const { slug } = req.params;
ReportedArticles.create({
slug,
comment,
username,
createdAt: new Date(),
updatedAt: new Date()
}).then((out) => {
res.status(201).send({
status: 201,
data: out
});
});
}

/**
* @param {object} req
* @param {object} res
* @return {object} returns a message with operation status
*/
static async blockArticle(req, res) {
const { slug } = req.params;
const { user } = req;
const { description } = req.body;
const article = await Article.findOne({ where: { slug } });
const reporterUsername = await ReportedArticles.findOne({
where: { slug }
});
const { dataValues: { email, lastName } } = await User.findOne({
where: { id: article.authorId }
});
const username = !reporterUsername
? null
: reporterUsername.dataValues.username;
const repoterId = username === null ? null : await User.findOne({ where: { username } });
const id = repoterId === null ? null : repoterId.dataValues.id;
const object = {
reporterId: id,
articleId: article.id,
authorId: article.authorId,
moderatorId: user.id,
blockedDay: days[new Date().getDay() - 1] || 'Sunday',
description
};
BlockedArticles.create(object).then(async (responce) => {
await Article.update(
{ blocked: true },
{ where: { id: responce.articleId } }
);
await notifyAuthorblock({ email, lastName });
res.status(201).send({
status: 201,
data: {
message: 'Article blocked successfully',
responce
}
});
});
}

/**
* @param {object} req
* @param {object} res
* @return {object} returns a message with operation status
*/
static async unBlockArticle(req, res) {
const { slug } = req.params;
const { id, authorId } = await Article.findOne({ where: { slug } });

const { dataValues: { email, lastName } } = await User.findOne({
where: { id: authorId }
});

BlockedArticles.destroy({ where: { articleId: id } }).then(async () => {
await Article.update({ blocked: false }, { where: { slug } });
await notifyAuthorUnblock({ email, lastName, slug });
res.status(200).send({
status: 200,
data: {
message: 'Article unblocked successfully'
}
});
});
}
}

export default ArticleBlocks;
67 changes: 67 additions & 0 deletions src/api/controllers/articleDislikes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import db from '../../sequelize/models';
import updateDislike from '../../helpers/article/updateDislike';
import hasLiked from '../../helpers/article/hasLiked';
import dislikesCount from '../../helpers/article/countDislikes';

const { LikeDislike } = db;
/**
* @class
*/
class ArticleDislikes {
/**
* @param {object} req
* @param {object} res
* @return {object} returns a message with operation status
*/
static async dislikeArticle(req, res) {
const { id: currentUser } = req.user;
const { slug } = req.params;

try {
const foundArticle = req.article;

// If user has liked before, remove like, add dislike.
const { id } = hasLiked(foundArticle.id, currentUser);
if (id) {
updateDislike(id);
return res.status(200).json({
message: `User ${currentUser} has disliked article ${slug}`
});
}

// the user hasn't disliked before, create new dislike
await LikeDislike.create({
userId: currentUser,
articleId: foundArticle.id,
dislikes: 1,
likes: 0
});

return res
.status(200)
.json({ message: `User ${currentUser} has disliked article ${slug}` });
} catch (error) {
return res.status(500).json({ error: `${error}` });
}
}

/**
* @param {object} req
* @param {object} res
* @return {object} returns a message with operation status
*/
static async getDislikes(req, res) {
const { slug } = req.params;
const foundArticle = req.article;

return res.json({
status: 200,
data: {
slug,
dislikes: dislikesCount(foundArticle.id)
}
});
}
}

export default ArticleDislikes;
67 changes: 67 additions & 0 deletions src/api/controllers/articleLikes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import models from '../../sequelize/models';
import updateLike from '../../helpers/article/updateLike';
import hasDisliked from '../../helpers/article/hasDisliked';
import likesCount from '../../helpers/article/countLikes';

const { LikeDislike } = models;
/**
* @class
*/
class ArticleLikes {
/**
* @param {object} req
* @param {object} res
* @return {object} returns a message with operation status
*/
static async likeArticle(req, res) {
const { id: currentUser } = req.user;
const { slug } = req.params;

try {
const foundArticle = req.article;

// If user has disliked before, remove dislike, add like.
const { id } = hasDisliked(foundArticle.id, currentUser);
if (id) {
await updateLike(id);
return res
.status(200)
.json({ message: `User ${currentUser} has liked article ${slug}` });
}

// the user hasn't liked or disliked before, create new like
await LikeDislike.create({
userId: currentUser,
articleId: foundArticle.id,
dislikes: 0,
likes: 1
});

return res
.status(200)
.json({ message: `User ${currentUser} has liked article ${slug}` });
} catch (error) {
return res.status(500).json({ error: `${error}` });
}
}

/**
* @param {object} req
* @param {object} res
* @return {object} returns a message with operation status
*/
static async getLikes(req, res) {
const { slug } = req.params;
const foundArticle = req.article;

return res.status(200).json({
status: 200,
data: {
articleSlug: slug,
numberOfLikes: likesCount(foundArticle.id)
}
});
}
}

export default ArticleLikes;
Loading

0 comments on commit f1eaf07

Please sign in to comment.