From b8c1749cc9e24e1f97a14f7a6ab8ac28d7f1aac7 Mon Sep 17 00:00:00 2001 From: MassiGy Date: Tue, 24 May 2022 21:48:27 +0200 Subject: [PATCH 1/3] Block System fixed --- src/controllers/User/Block/blockUser.js | 13 ++++++++----- src/controllers/User/Block/listBlocked.js | 9 +++++---- src/controllers/User/Block/queryBlock.js | 6 ++++-- src/controllers/User/Block/unblockUser.js | 6 ++++-- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/controllers/User/Block/blockUser.js b/src/controllers/User/Block/blockUser.js index 7a1b8619..e2c2555e 100644 --- a/src/controllers/User/Block/blockUser.js +++ b/src/controllers/User/Block/blockUser.js @@ -1,13 +1,13 @@ /** * @module Block User Controller - * + * * @param {Request} req - HTTP Request from the client * @param {Response} res - HTTP Response for the client - * + * * @description * This controller will allow the user to block a specific user, if all parameters are correct. - * + * * @todo * Nothing for now. */ @@ -40,11 +40,14 @@ module.exports.blockUser = async (req, res) => { if (!user) { return res.status(404).send({ msg: 'Current account not found.' }) } - if (user.hasBlocked(userToBlock)) { + + const alreadyBlocked = await user.hasBlockedUser(userToBlock); + + if (alreadyBlocked) { return res.status(400).send({ msg: 'You have already blocked this person.' }); } - user.addBlocked(userToBlock); + user.addBlockedUser(userToBlock); return res.status(200).send({ msg: 'User blocked.' }); diff --git a/src/controllers/User/Block/listBlocked.js b/src/controllers/User/Block/listBlocked.js index 9f59bb21..3fd593cf 100644 --- a/src/controllers/User/Block/listBlocked.js +++ b/src/controllers/User/Block/listBlocked.js @@ -19,18 +19,19 @@ module.exports.listBlocked = async (req, res) => { try { - const user = await User.findOne({ + const userBlockedList = await User.findOne({ where: { id: req.user.id, }, - }) + include: 'BlockedUser', + }); if (!user) { return res.status(404).send({ msg: 'User not found.' }); } - const blockedUsers = await user.getBlocked(); - return res.status(200).json(blockedUsers); + + return res.status(200).json(userBlockedList.BlockedUser); } catch (err) { console.log(err.message); diff --git a/src/controllers/User/Block/queryBlock.js b/src/controllers/User/Block/queryBlock.js index 371285b1..673099ff 100644 --- a/src/controllers/User/Block/queryBlock.js +++ b/src/controllers/User/Block/queryBlock.js @@ -23,7 +23,7 @@ module.exports.queryBlock = async (req, res) => { const [blockedUser, user] = await Promise.all([ User.findOne({ where: { - id: req.params.blockId, + id: req.params.blockedId, }, }) , @@ -41,7 +41,9 @@ module.exports.queryBlock = async (req, res) => { return res.status(404).send({ msg: 'Current account not found. Try loggin in.' }) } - if (!user.hasBlocked(blockedUser)) { + const alreadyBlocked = await user.hasBlockedUser(blockedUser); + + if (!alreadyBlocked) { return res.status(401).send({ msg: "You have not blocked this person." }); } diff --git a/src/controllers/User/Block/unblockUser.js b/src/controllers/User/Block/unblockUser.js index d28fbc1a..f17b94a8 100644 --- a/src/controllers/User/Block/unblockUser.js +++ b/src/controllers/User/Block/unblockUser.js @@ -43,11 +43,13 @@ module.exports.unblockUser = async (req, res) => { return res.status(404).send({ msg: 'Current account not found.' }); } - if (!user.hasBlocked(blockedUser)) { + const alreadyBlocked = await user.hasBlockedUser(blockedUser); + + if (!alreadyBlocked) { return res.status(401).send({ msg: "You have not blocked this person." }); } - user.removeBlocked(blockedUser); + user.removeBlockedUser(blockedUser); return res.status(200).send({ msg: 'User unblocked.' }); From 3ad2f7932d4d72d6adb6a07db5171ec8b9696778 Mon Sep 17 00:00:00 2001 From: MassiGy Date: Tue, 24 May 2022 22:14:58 +0200 Subject: [PATCH 2/3] Following System Fixed --- src/controllers/User/Followers/followUser.js | 5 ++++- src/controllers/User/Followers/listFollowers.js | 10 +++++----- src/controllers/User/Followers/queryFollower.js | 8 ++++---- src/controllers/User/Followers/unfollowUser.js | 9 +++------ 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/controllers/User/Followers/followUser.js b/src/controllers/User/Followers/followUser.js index 6067bbca..07ab6f4e 100644 --- a/src/controllers/User/Followers/followUser.js +++ b/src/controllers/User/Followers/followUser.js @@ -45,7 +45,10 @@ module.exports.followUser = async (req, res) => { return res.status(404).send({ msg: 'Current account not found.' }); } - if (userToFollow.hasFollower(user)) { + + const alreadyFollower = await userToFollow.hasFollower(user); + + if (alreadyFollower) { return res.status(401).send({ msg: "You are already following this person." }); } diff --git a/src/controllers/User/Followers/listFollowers.js b/src/controllers/User/Followers/listFollowers.js index 8f638b05..3979c4cc 100644 --- a/src/controllers/User/Followers/listFollowers.js +++ b/src/controllers/User/Followers/listFollowers.js @@ -13,25 +13,25 @@ */ module.exports.listFollowers = async (req, res) => { - + if (!req.params.id) { return res.status(400).send({ msg: "Not All Parameters Provided." }); } try { - const user = await User.findOne({ + const userFollowersList = await User.findOne({ where: { id: req.params.id, }, + include: 'Follower', }); - if (!user) { + if (!userFollowersList) { return res.status(404).send({ msg: "User not found." }); } - const followers = await user.getFollowers(); - return res.status(200).json(followers); + return res.status(200).json(userFollowersList.Follower); } catch (err) { console.log(err.message); diff --git a/src/controllers/User/Followers/queryFollower.js b/src/controllers/User/Followers/queryFollower.js index eedcff99..fb94b5cf 100644 --- a/src/controllers/User/Followers/queryFollower.js +++ b/src/controllers/User/Followers/queryFollower.js @@ -37,8 +37,6 @@ module.exports.queryFollower = async (req, res) => { ]) - - if (!follower) { return res.status(404).send({ msg: 'Follower user not found.' }); } @@ -47,8 +45,10 @@ module.exports.queryFollower = async (req, res) => { return res.status(404).send({ msg: 'Followee user not found.' }); } - if (!followee.hasFollower(follower)) { - return res.status(401).send({ + const alreadyFollower = await followee.hasFollower(follower); + + if (!alreadyFollower) { + return res.status(400).send({ msg: `User with id=${req.params.id} is not followed by user with id=${req.params.followerId}` }); } diff --git a/src/controllers/User/Followers/unfollowUser.js b/src/controllers/User/Followers/unfollowUser.js index db56de4b..0027a9f7 100644 --- a/src/controllers/User/Followers/unfollowUser.js +++ b/src/controllers/User/Followers/unfollowUser.js @@ -19,9 +19,7 @@ module.exports.unfollowUser = async (req, res) => { return res.status(400).send({ msg: "Not All Parameters Provided." }); } - try { - const [followee, user] = await Promise.all([ User.findOne({ @@ -47,13 +45,12 @@ module.exports.unfollowUser = async (req, res) => { return res.status(404).send({ msg: 'Current user not found.' }); } - if (!followee.hasFollower(user)) { + const alreadyFollower = await followee.hasFollower(user); + + if (!alreadyFollower) { return res.status(401).send({ msg: 'Your are not following this person.' }); } - - - followee.removeFollower(user); return res.status(200).send({ msg: 'Your are not longer following this person.' }); From bd57b8a7503068526c854c2061b7cec45e9a3374 Mon Sep 17 00:00:00 2001 From: MassiGy Date: Tue, 24 May 2022 22:18:27 +0200 Subject: [PATCH 3/3] Fixed The API route Config --- src/routes/userRoutes.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/routes/userRoutes.js b/src/routes/userRoutes.js index 3b183d17..f28c9bbc 100644 --- a/src/routes/userRoutes.js +++ b/src/routes/userRoutes.js @@ -7,8 +7,7 @@ const { verifyEmail } = require('../middelwares/verifyEmail'); // User Endpoints router.get('/', userControllers.listUsers); router.get('/count', userControllers.countUsers); -router.get('/:id/', userControllers.queryUser); -router.get('/:id/status', userControllers.getStatus); + router.post('/signup', userControllers.signup); router.post('/login', userControllers.login); @@ -21,6 +20,14 @@ router.post('/delete', verifyLogin, userControllers.deleteAccount); router.get('/blockedUsers', verifyLogin, userControllers.listBlocked); router.get('/blockedUsers/:blockedId', verifyLogin, userControllers.queryBlock); + +// User endpoints - +// Note: since id can be anything the get by id route sould be last to be invoked. + +router.get('/:id/', userControllers.queryUser); +router.get('/:id/status', userControllers.getStatus); + + router.post('/:toBlockId/block', verifyLogin, verifyEmail, userControllers.blockUser); router.post('/:toUnblockId/unblock', verifyLogin, verifyEmail, userControllers.unblockUser);