Skip to content

Commit

Permalink
Merge pull request #73 from andela/ch-improve-backend-167785593
Browse files Browse the repository at this point in the history
#167785593 Add filters
  • Loading branch information
Denis Niwemugisha authored Aug 9, 2019
2 parents ae64b52 + 4b602a3 commit 2b252ba
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 60 deletions.
9 changes: 8 additions & 1 deletion controllers/resetPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ class Password {
return res.status(400).send({ message: `no user with email ${email} found` });
}
const userDetails = {
id: user.id
id: user.id,
username: user.username,
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
image: user.image,
bio: user.bio,
notificationSettings: user.notificationSettings
};
const token = Helper.generateToken(userDetails);
const url = `${process.env.FRONTEND_URL}/completReset/${token}`;
Expand Down
103 changes: 44 additions & 59 deletions helpers/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ class ArticleHelper {
raw: true
}
],
order: [['createdAt', 'DESC']],
attributes: [
'id',
'authorId',
Expand Down Expand Up @@ -307,9 +308,7 @@ class ArticleHelper {
const { option } = req.params;
const result = await Article.findOne({ where: { slug } });
if (!result) {
return res
.status(404)
.send({ message: `article with slug ${slug} do not exist` });
return res.status(404).send({ message: `article with slug ${slug} do not exist` });
}
const Result = await Like.findOne({
where: { titleSlug: slug, userId: id }
Expand All @@ -319,30 +318,18 @@ class ArticleHelper {
return true;
}
if (option === 'dislike' && Result.status === 'like') {
await Like.update(
{ status: 'dislike' },
{ where: { titleSlug: slug, userId: id } }
);
await Like.update({ status: 'dislike' }, { where: { titleSlug: slug, userId: id } });
return res.status(200).send({ message: 'like is replaced by dislike' });
}
if (option === 'like' && Result.status === 'dislike') {
await Like.update(
{ status: 'like' },
{ where: { titleSlug: slug, userId: id } }
);
await Like.update({ status: 'like' }, { where: { titleSlug: slug, userId: id } });
return res.status(200).send({ message: 'dislike is replaced by like' });
}
if (Result.status === 'neutral') {
await Like.update(
{ status: option },
{ where: { titleSlug: slug, userId: id } }
);
await Like.update({ status: option }, { where: { titleSlug: slug, userId: id } });
return res.status(200).send({ message: 'reaction updated' });
}
await Like.update(
{ status: 'neutral' },
{ where: { titleSlug: slug, userId: id } }
);
await Like.update({ status: 'neutral' }, { where: { titleSlug: slug, userId: id } });
return res.status(200).send({ message: 'your reaction is now neutral' });
}

Expand All @@ -359,9 +346,7 @@ class ArticleHelper {
const { slug } = req.params;
const result = await Like.findAll({
where: { titleSlug: slug, status: 'like' },
include: [
{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] }
],
include: [{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] }],
attributes: ['id', 'titleSlug', 'status']
});
result.forEach(() => {
Expand All @@ -385,9 +370,7 @@ class ArticleHelper {
const { slug } = req.params;
const result = await Like.findAll({
where: { titleSlug: slug, status: 'dislike' },
include: [
{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] }
],
include: [{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] }],
attributes: ['id', 'titleSlug', 'status']
});
result.forEach(() => {
Expand Down Expand Up @@ -430,13 +413,7 @@ class ArticleHelper {
{
model: User,
as: 'author',
attributes: [
'username',
'bio',
'image',
'email',
'notificationSettings'
]
attributes: ['username', 'bio', 'image', 'email', 'notificationSettings']
}
],
attributes: ['id', 'titleSlug', 'status']
Expand All @@ -454,9 +431,7 @@ class ArticleHelper {
const { slug } = req.params;
const likesFetched = await Like.findAll({
where: { titleSlug: slug, status: 'dislike' },
include: [
{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] }
],
include: [{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] }],
attributes: ['id', 'titleSlug', 'status']
});
return likesFetched;
Expand Down Expand Up @@ -513,9 +488,7 @@ class ArticleHelper {
next();
return true;
}
return res
.status(400)
.send({ errors: { body: ['invalid platform in path'] } });
return res.status(400).send({ errors: { body: ['invalid platform in path'] } });
}

/**
Expand All @@ -537,10 +510,7 @@ class ArticleHelper {
const { platform } = share;
if (platform.includes(option)) {
const updatePlatforms = platform.filter(result => result !== option);
await Share.update(
{ platform: updatePlatforms },
{ where: { userId: id } }
);
await Share.update({ platform: updatePlatforms }, { where: { userId: id } });
return res.status(200).send({
message: `your ${option} share is removed, you can share again`
});
Expand Down Expand Up @@ -727,14 +697,7 @@ class ArticleHelper {
{
model: Article,
as: 'article',
attributes: [
'title',
'description',
'slug',
'image',
'createdAt',
'readingTime'
]
attributes: ['title', 'description', 'slug', 'image', 'createdAt', 'readingTime']
},
{
model: User,
Expand Down Expand Up @@ -778,6 +741,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -805,6 +769,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -832,6 +797,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -859,6 +825,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -887,6 +854,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -915,6 +883,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -942,6 +911,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -970,6 +940,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -998,6 +969,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -1026,6 +998,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -1055,6 +1028,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -1083,6 +1057,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -1111,6 +1086,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -1140,6 +1116,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -1169,6 +1146,7 @@ class ArticleHelper {
'slug',
'title',
'description',
'category',
'readingTime',
'body',
'createdAt'
Expand Down Expand Up @@ -1205,9 +1183,7 @@ class ArticleHelper {
const { slug } = req.params;
const article = await ArticleHelper.findArticleBySlug(slug);
if (!article) {
return res
.status(404)
.send({ status: 404, errors: { body: ['article not found'] } });
return res.status(404).send({ status: 404, errors: { body: ['article not found'] } });
}
const options = {
allowUnknown: true,
Expand Down Expand Up @@ -1331,9 +1307,20 @@ class ArticleHelper {
}
const result = await ArticleHighlight.findAll({
where: { articleSlug: slug },
include: [{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] },
{ model: HighlightComment, attributes: ['comment', 'createdAt'] }],
attributes: ['id', 'articleSlug', 'startIndex', 'endIndex', 'highlightedText', 'blockId', 'createdAt', 'updatedAt']
include: [
{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] },
{ model: HighlightComment, attributes: ['comment', 'createdAt'] }
],
attributes: [
'id',
'articleSlug',
'startIndex',
'endIndex',
'highlightedText',
'blockId',
'createdAt',
'updatedAt'
]
});
return result;
}
Expand Down Expand Up @@ -1433,9 +1420,7 @@ class ArticleHelper {
}
const highlights = await ArticleHighlight.findAll({
where: { articleSlug: slug },
include: [
{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] }
],
include: [{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] }],
attributes: [
'id',
'articleSlug',
Expand Down
1 change: 1 addition & 0 deletions helpers/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class NotificationHelper {
static async getByUser(req) {
const article = await Notification.findAll({
where: { userId: req.user.id },
order: [['createdAt', 'DESC']],
include: [{
model: User,
as: 'user',
Expand Down
1 change: 1 addition & 0 deletions helpers/tag.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TagHelper {
slug: currentArticle.slug,
title: currentArticle.title,
description: currentArticle.description,
category: currentArticle.category,
readingTime: currentArticle.readingTime,
body: currentArticle.body,
image: currentArticle.image,
Expand Down

0 comments on commit 2b252ba

Please sign in to comment.