Skip to content

Commit

Permalink
Fix bug: Social media
Browse files Browse the repository at this point in the history
- Login via facebook
- Login via goole
- login via twitter

[Delivers #164489888]
  • Loading branch information
Jaman-dedy committed Jun 4, 2019
1 parent bb080b4 commit b637fe2
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 118 deletions.
2 changes: 1 addition & 1 deletion controllers/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class ArticleController {
title: req.body.title,
body: req.body.body,
taglist: (req.body.tag ? req.body.tag.split(',') : req.findArticle.taglist),
image: (req.file ? req.file.url : null)
image: (req.file ? req.file.url : req.findArticle.image)
}, {
where: {
article_id: req.params.articleId
Expand Down
130 changes: 78 additions & 52 deletions controllers/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,39 @@ class CommentController {
create(req, res) {
// @comment
const newComment = {
body: req.body.content,
body: req.body.text,
articleId: req.params.articleId,
author: req.user.id
};
// @save comments and history
Comment.create(newComment)
.then((comment) => {
const newCommentHistory = {
commentId: comment.id,
userId: req.user.id,
body: req.body.content
};
EditedCommentHistory.create(newCommentHistory);
User.findOne({ where: { id: comment.author } })
.then(async (user) => {
const message = `${user.username} commented on an article you favorite`;
await notification.createNotificationForFavorite(comment.articleId,
message, comment.author);
await notification.sendNotificationToFavorites(comment.articleId,
message, comment.author);
res.status(201).json({ status: 201, comment });
});
Comment.create(newComment).then((comment) => {
const newCommentHistory = {
commentId: comment.id,
userId: req.user.id,
body: req.body.text
};
EditedCommentHistory.create(newCommentHistory);
User.findOne({ where: { id: comment.author } }).then(async (user) => {
const message = `${user.username} commented on an article you favorite`;
await notification.createNotificationForFavorite(
comment.articleId,
message,
comment.author
);
await notification.sendNotificationToFavorites(comment.articleId, message, comment.author);
Comment.findOne({
Where: { id: comment.dataValues.id },
order: [['id', 'DESC']],
include: [
{
model: User,
as: 'userfkey',
attributes: ['username', 'email', 'image', 'id', 'bio']
}
]
}).then(createdComment => res.status(201).json({ status: 201, createdComment }));
});
});
}

/**
Expand All @@ -51,13 +61,24 @@ class CommentController {
* @returns {Object} - will return all comment related to an article
*/
getAllComment(req, res) {
Comment.findAll({ where: { articleId: req.params.articleId } })
.then((comment) => {
if (comment.dataValues === {}) {
return res.status(404).json({ status: 404, error: 'No comment has been posted to that article' });
Comment.findAll({
where: { articleId: req.params.articleId },
order: [['createdAt', 'DESC']],
include: [
{
model: User,
as: 'userfkey',
attributes: ['username', 'email', 'image', 'id', 'bio']
}
return res.status(200).json({ status: 200, comment });
});
]
}).then((comment) => {
if (comment.dataValues === {}) {
return res
.status(404)
.json({ status: 404, error: 'No comment has been posted to that article' });
}
return res.status(200).json({ status: 200, comment });
});
}

/**
Expand All @@ -69,13 +90,12 @@ class CommentController {
getEditedComment(req, res) {
EditedCommentHistory.findAll({
where: { commentId: req.params.commentId }
})
.then((editedComment) => {
res.status(200).json({
status: 200,
editedComment
});
}).then((editedComment) => {
res.status(200).json({
status: 200,
editedComment
});
});
}

/**
Expand All @@ -85,25 +105,32 @@ class CommentController {
* @returns {Object} -will return an updated Comment
*/
updateComment(req, res) {
Comment.update({ body: req.body.content },
{ where: { id: req.params.commentId }, returning: true })
Comment.update(
{ body: req.body.text },
{ where: { id: req.params.commentId }, returning: true }
)
.then((comment) => {
User.findOne({ where: { id: comment[1][0].author } })
.then(async (user) => {
const message = `${user.username} updated his comment on an article you favorite`;
await notification.createNotificationForFavorite(comment[1][0].articleId,
message, comment[1][0].author);
await notification.sendNotificationToFavorites(comment[1][0].articleId,
message, comment[1][0].author);
const edited = {
commentId: req.params.commentId,
userId: req.user.id,
body: req.body.content
};
// @save the comment history
EditedCommentHistory.create(edited);
res.status(200).json({ status: 200, comment: comment[1] });
});
User.findOne({ where: { id: comment[1][0].author } }).then(async (user) => {
const message = `${user.username} updated his comment on an article you favorite`;
await notification.createNotificationForFavorite(
comment[1][0].articleId,
message,
comment[1][0].author
);
await notification.sendNotificationToFavorites(
comment[1][0].articleId,
message,
comment[1][0].author
);
const edited = {
commentId: req.params.commentId,
userId: req.user.id,
body: req.body.text
};
// @save the comment history
EditedCommentHistory.create(edited);
res.status(200).json({ status: 200, comment: comment[1] });
});
})
.catch((error) => {
res.status(500).json({ status: 500, message: error.message });
Expand All @@ -120,10 +147,9 @@ class CommentController {
Comment.destroy({
where: { id: req.params.commentId },
returning: true
})
.then(() => {
res.status(200).json({ status: 200, message: 'Comment deleted successfully' });
});
}).then(() => {
res.status(200).json({ status: 200, message: 'Comment deleted successfully' });
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion helpers/validateComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const validate = (error, res) => res.status(400).json({ status: 400, error });
* @returns {error} error
*/
const commentValidate = (req, res, next) => {
if (Validate.isEmpty(req.body.content)) {
if (Validate.isEmpty(req.body.text)) {
return validate('Please add the body of your comments', res);
}
next();
Expand Down
8 changes: 2 additions & 6 deletions middlewares/passport-facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import dotenv from 'dotenv';

// @dotenv configuration
dotenv.config();
const {
FACEBOOK_APP_ID,
FACEBOOK_APP_SECRET,
APP_URL
} = process.env;
const { FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, APP_URL } = process.env;

const fbStrategy = new FacebookTokenStrategy(
{
callbackURL: `${APP_URL}/api/users/login/facebook/redirect`,
callbackURL: `${APP_URL}api/users/login/facebook/redirect`,
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
profileFields: ['id', 'displayName', 'photos', 'email']
Expand Down
3 changes: 2 additions & 1 deletion middlewares/passport-google.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const googleStrategy = new GoogleStrategy(
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: `${APP_URL}/api/users/login/google/redirect`,
profileFields: ['id', 'displayName', 'photos', 'email']
}, (accessToken, refreshToken, profile, done) => {
},
(accessToken, refreshToken, profile, done) => {
const userGoogle = {
username: profile.displayName,
isActivated: true
Expand Down
2 changes: 1 addition & 1 deletion middlewares/passport-twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const twStrategy = new twitterStrategy(
{
consumerKey: TWITTER_CONSUMER_KEY,
consumerSecret: TWITTER_CONSUMER_SECRET,
callbackURL: `${APP_URL}/api/users/login/twitter/redirect`
callbackURL: `${APP_URL}api/users/login/twitter/redirect`
},
(token, tokenSecret, profile, done) => {
const userTwitter = {
Expand Down
Loading

0 comments on commit b637fe2

Please sign in to comment.