diff --git a/server/controllers/UserController.js b/server/controllers/UserController.js index a37405fb..fd98b774 100755 --- a/server/controllers/UserController.js +++ b/server/controllers/UserController.js @@ -7,7 +7,7 @@ import response from '../helpers/response'; import passwordResetEmailTemplate from '../helpers/emailTemplates/resetPasswordTemplate'; -const { User, Sequelize } = db; +const { User, Article, Follow, Sequelize } = db; const { Op } = Sequelize; /** @@ -133,12 +133,16 @@ class UserController { } }); } catch (error) { - if (error.name === 'TokenExpiredError' || error.name === 'JsonWebTokenError') { + if ( + error.name === 'TokenExpiredError' || + error.name === 'JsonWebTokenError' + ) { return res.status(401).send({ status: 'failure', data: { statusCode: '401', - message: 'Sorry! Link has expired. Kindly re-initiate password reset.' + message: + 'Sorry! Link has expired. Kindly re-initiate password reset.' } }); } @@ -161,9 +165,7 @@ class UserController { */ static async signUp(req, res) { try { - const { - fullName, userName, email, password - } = req.body; + const { fullName, userName, email, password } = req.body; const foundUser = await User.findOne({ where: { @@ -436,9 +438,7 @@ class UserController { } }; - const { - id, displayName, emails, photos, provider - } = profile; + const { id, displayName, emails, photos, provider } = profile; if (!emails) { const userWithNoEmail = { hasNoEmail: true }; @@ -478,7 +478,9 @@ class UserController { */ static handleSocialAuth(req, res) { if (req.user.hasNoEmail) { - return res.redirect(`${process.env.FRONTEND_URL}/auth/social?error=${400}`); + return res.redirect( + `${process.env.FRONTEND_URL}/auth/social?error=${400}` + ); } const payload = { userId: req.user.id, @@ -487,7 +489,9 @@ class UserController { roleId: req.user.roleId }; const token = TokenManager.sign(payload); - return res.redirect(`${process.env.FRONTEND_URL}/auth/social?token=${token}`); + return res.redirect( + `${process.env.FRONTEND_URL}/auth/social?token=${token}` + ); } /** @@ -503,15 +507,41 @@ class UserController { const { userName } = req.params; const userProfile = await User.findOne({ where: { userName: userName.toLowerCase() }, - attributes: ['id', 'fullName', 'userName', 'img', 'bio'] + attributes: ['id', 'fullName', 'userName', 'img', 'bio', 'email'], + include: [ + { + model: Article, + as: 'articles' + }, + { + model: User, + through: 'Follow', + as: 'following', + attributes: ['id', 'fullName', 'userName', 'img', 'bio'] + }, + { + model: User, + through: 'Follow', + as: 'followers', + attributes: ['id', 'fullName', 'userName', 'img', 'bio'] + } + ] }); if (!userProfile) { response(res, 404, 'failure', 'User not found'); return; } - response(res, 200, 'success', 'User retrieved successfully', null, userProfile); + response( + res, + 200, + 'success', + 'User retrieved successfully', + null, + userProfile + ); } catch (error) { + console.log(error); response(res, 500, 'failure', 'An error occured on the server'); } } diff --git a/server/models/user.js b/server/models/user.js index 961b8d6c..852a1ed0 100755 --- a/server/models/user.js +++ b/server/models/user.js @@ -70,7 +70,7 @@ export default (sequelize, DataTypes) => { }, {} ); - User.associate = (models) => { + User.associate = models => { const { Article, AuthType, @@ -149,12 +149,12 @@ export default (sequelize, DataTypes) => { }); User.belongsToMany(User, { through: 'Follow', - as: 'followers', + as: 'following', foreignKey: 'followersId' }); User.belongsToMany(User, { through: 'Follow', - as: 'following', + as: 'followers', foreignKey: 'userId' }); User.hasMany(ReadingStats, {