Skip to content

Commit

Permalink
Merge 683a7d3 into 6d10d56
Browse files Browse the repository at this point in the history
  • Loading branch information
mcaleb808 committed Apr 23, 2019
2 parents 6d10d56 + 683a7d3 commit 9670d59
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 27 deletions.
4 changes: 2 additions & 2 deletions __tests__/routes/profile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('Profile', () => {

expect(res.status).toBe(200);
expect(res.body.user.firstName).toBe('Peter');
expect(res.body.message).toBe('The information was updated successful');
expect(res.body.message).toBe('The information was updated successfully');
done();
});

Expand Down Expand Up @@ -136,7 +136,7 @@ describe('Profile', () => {
.set('Authorization', loginUser1.token)
.send({ user: { username: 'claudine' } });
expect(res.status).toBe(200);
expect(res.body.message).toBe('The information was updated successful');
expect(res.body.message).toBe('The information was updated successfully');
done();
});

Expand Down
8 changes: 4 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ app.use(
secret: 'MYSECRETISVERYSECRET',
store,
resave: true,
saveUninitialized: true,
}),
saveUninitialized: true
})
);
app.use(passport.initialize());
app.use(passport.session());
Expand Down Expand Up @@ -66,8 +66,8 @@ app.use((err, req, res) => {
res.status(err.status || 500).json({
errors: {
message: err.message,
error: err,
},
error: err
}
});
});

Expand Down
9 changes: 5 additions & 4 deletions controllers/ArticleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ class ArticleController {
* @returns {Object} Returns the response
*/
static async createArticle(req, res) {
const { file, currentUser } = req;
const { currentUser } = req;
const { article } = req.body;
const cover = file ? file.url : undefined;
const slug = slugString(article.title);
const readingTime = getReadingTime(article.body);
const newArticle = await Article.create(
{
...article,
userId: currentUser.id,
slug,
cover,
readingTime
},
{
Expand Down Expand Up @@ -100,12 +98,15 @@ class ArticleController {
await Reader.create({ articleId: article.id });
}
const views = await Reader.count({ where: { articleId: article.id } });

const rated = await Favorite.findOne({
where: { articleId: article.id, userId: currentUser ? currentUser.id : null }
});
return res.status(200).json({
status: 200,
article: {
...article.get(),
rating: await calculateRating(article.get().id),
rated: rated ? rated.rating : 0,
author: { ...article.get().author.get(), following },
favorited,
favoritesCount,
Expand Down
4 changes: 2 additions & 2 deletions controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import moment from 'moment';
import { User, ResetPassword, Token } from '../database/models';
import { sendEmailConfirmationLink, resetPasswordEmail, newPasswordEmail } from './MailController';

const { JWT_SECRET } = process.env;
const { JWT_SECRET, FRONTEND_URL } = process.env;

/**
* @description Authentication class
Expand Down Expand Up @@ -192,7 +192,7 @@ class AuthController {
* @returns {Object} Returns the response
*/
static async socialLogin(req, res) {
return res.json({ status: 200, user: req.user });
res.redirect(`${FRONTEND_URL}?token=${req.user.token}`);
}
}

Expand Down
11 changes: 7 additions & 4 deletions controllers/ProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ class ProfileController {
await sendEmailConfirmationLink({ ...profile.get() });
message = 'Your email has changed. Please check your email for confirmation';
} else {
message = 'The information was updated successful';
message = 'The information was updated successfully';
}
const { confirmationCode, ...userData } = profile.get();
return res.status(200).send({
statu: 200,
status: 200,
message,
user: userData
});
Expand Down Expand Up @@ -198,12 +198,15 @@ class ProfileController {
*/
static async getCurrentUser(req, res) {
const { currentUser } = req;

const articles = await Article.findAll({
where: { userId: currentUser.id, status: { [Op.not]: 'deleted' } }
});
return res.status(200).json({
status: 200,
user: {
...currentUser,
password: undefined
password: undefined,
articles
}
});
}
Expand Down
8 changes: 7 additions & 1 deletion controllers/RatingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class RatingController {
if (rating) {
await rating.update({ rating: rate, updatedAt: new Date() });
const averageRate = await calculateRating(articleId);

return res.status(200).send({
status: 200,
message: 'Rating updated successfully',
rate: { ...rating.get() },
averageRate
});
}
const averageRate = await calculateRating(articleId);
const newRate = await Favorite.create(
{
userId: currentUser.id,
Expand All @@ -44,6 +44,8 @@ class RatingController {
},
{ include: [{ model: User, as: 'author' }, { model: Article, as: 'favorites' }] }
);
const averageRate = await calculateRating(articleId);

return res.status(201).send({
status: 201,
message: 'article has been rated successfully',
Expand Down Expand Up @@ -105,6 +107,9 @@ class RatingController {
articleId: article.id,
rating: { [Op.ne]: null }
},
include: [
{ model: User, as: 'author', attributes: ['username', 'firstName', 'lastName', 'image'] }
],
limit,
offset: (queryPage - 1) * limit
});
Expand All @@ -116,6 +121,7 @@ class RatingController {
}
return res.status(200).send({
status: 200,
article: article.get(),
averageRate: await calculateRating(null, article.slug),
ratings: allRating.rows,
page: Number(queryPage),
Expand Down
2 changes: 1 addition & 1 deletion controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UserController {
await user.update({ confirmed: 'confirmed', confirmationCode: null });

await sendEmailVerified(user.get());
return res.json({ message: `${user.email} has been confirmed` });
return res.status(200).json({ status: 200, message: `${user.email} has been confirmed` });
}

/**
Expand Down
Loading

0 comments on commit 9670d59

Please sign in to comment.