From 2a5f026c2767aa611e0694a0867042d1da66793f Mon Sep 17 00:00:00 2001 From: Roman-Ojha Date: Fri, 24 Jun 2022 09:10:03 +0545 Subject: [PATCH] fix(eslint): updated user route fixing #108 --- controllers/user.controller.ts | 703 +++++++++++++++------------------ 1 file changed, 328 insertions(+), 375 deletions(-) diff --git a/controllers/user.controller.ts b/controllers/user.controller.ts index 37d8a6b1..2dd6a6be 100644 --- a/controllers/user.controller.ts +++ b/controllers/user.controller.ts @@ -1,21 +1,112 @@ -import userDetail from "../models/userDetail_model.js"; -import fs from "fs"; +/* eslint-disable import/no-unresolved */ import { Request, Response } from "express"; +import fs from "fs"; +import userDetail from "../models/userDetail_model.js"; import ResponseObject from "../interface/responseObject.js"; import ResPonseUserPost from "../interface/resUserPost.js"; import { UserDocumentPosts } from "../interface/userDocument.js"; import SchemaMethodInstance from "../interface/userSchemaMethods.js"; -var botUser = []; +let botUser = []; fs.readFile("./db/botUser.json", "utf-8", (err, user) => { botUser = JSON.parse(user); }); +function mergeRootUserData( + userPosts: UserDocumentPosts[], + commentedUser: (SchemaMethodInstance & { + _id: any; + })[] +) { + return userPosts.map((post) => { + const lastCommented = post.comments.by[post.comments.by.length - 1]; + if (lastCommented) { + const filteredUser = commentedUser.filter((user) => user.id === lastCommented.user); + if (!filteredUser.length) { + // post.phone = filteredUser; + return post; + } + const newUser = filteredUser.map((user) => ({ + picture: user.picture, + userID: user.userID + })); + const newObj: ResPonseUserPost = { + // ...post, + picture: { + url: post.picture.url + }, + caption: post.caption, + date: post.date, + id: post.id, + likes: post.likes, + comments: { + No: post.comments.No, + by: [ + { + user: lastCommented.user, + comment: lastCommented.comment, + picture: newUser[0].picture, + userID: newUser[0].userID + } + ] + } + }; + return newObj; + } + return post; + }); +} + +function mergeRootUserProfile( + userPosts: UserDocumentPosts[], + commentedUser: (SchemaMethodInstance & { + _id: any; + })[] +) { + return userPosts.map((post) => { + const lastCommented = post.comments.by[post.comments.by.length - 1]; + if (lastCommented) { + const filteredUser = commentedUser.filter((user) => user.id === lastCommented.user); + if (!filteredUser.length) { + // post.phone = filteredUser; + return post; + } + const newUser = filteredUser.map((user) => ({ + picture: user.picture, + userID: user.userID + })); + const newObj: ResPonseUserPost = { + // ...post, + picture: { + url: post.picture.url + }, + caption: post.caption, + date: post.date, + id: post.id, + likes: post.likes, + comments: { + No: post.comments.No, + by: [ + { + user: lastCommented.user, + comment: lastCommented.comment, + picture: newUser[0].picture, + userID: newUser[0].userID + } + ] + } + }; + return newObj; + } + return post; + }); +} + export default { main: async (req: Request, res: Response): Promise => { // writing logic to get all rootUser and rootUser follow user post try { - const rootUser = req.rootUser; + const { rootUser } = req; const currentDate = new Date(); const getRootUserData = async () => { @@ -39,7 +130,7 @@ export default { // finding those user which i follow and get the posts of them // and finding post which is {getPastDate} days early { - id: rootUser.id, + id: rootUser.id // posts: { // $elemMatch: { // date: { $gt: dateGivenDaysAgo }, @@ -56,19 +147,18 @@ export default { stories: 1, followersNo: 1, followingNo: 1, - postNo: 1, + postNo: 1 } ); if (!resRootUser) { return; } - let commentedUserId: string[] = []; - const posts: object = resRootUser.posts; + const commentedUserId: string[] = []; + const { posts } = resRootUser; // let userIdFromSameUserPostsComment: string[] = []; - for (let i = 0; i < resRootUser.posts.length; i++) { - const comment: { user: string } | undefined = - posts[i].comments.by[posts[i].comments.by.length - 1]; + for (let i = 0; i < resRootUser.posts.length; i += 1) { + const comment: { user: string } | undefined = posts[i].comments.by[posts[i].comments.by.length - 1]; if (comment) { commentedUserId.push(comment.user); } @@ -80,56 +170,9 @@ export default { _id: 0, userID: 1, picture: 1, - id: 1, + id: 1 } ); - - const mergeArrays = ( - userPosts: UserDocumentPosts[], - commentedUser: (SchemaMethodInstance & { - _id: any; - })[] - ) => { - return userPosts.map((post) => { - const lastCommented = post.comments.by[post.comments.by.length - 1]; - if (lastCommented) { - const filteredUser = commentedUser.filter( - (user) => user.id === lastCommented.user - ); - if (!filteredUser.length) { - // post.phone = filteredUser; - return post; - } - const newUser = filteredUser.map((user) => ({ - picture: user.picture, - userID: user.userID, - })); - const newObj: ResPonseUserPost = { - // ...post, - picture: { - url: post.picture.url, - }, - caption: post.caption, - date: post.date, - id: post.id, - likes: post.likes, - comments: { - No: post.comments.No, - by: [ - { - user: lastCommented.user, - comment: lastCommented.comment, - picture: newUser[0].picture, - userID: newUser[0].userID, - }, - ], - }, - }; - return newObj; - } - return post; - }); - }; const finalRootUserData: RootUserResponseData = { userID: resRootUser.userID, name: resRootUser.name, @@ -137,11 +180,12 @@ export default { email: resRootUser.email, id: resRootUser.id, stories: resRootUser.stories, - posts: mergeArrays(resRootUser.posts, resAllCommentedUser), + posts: mergeRootUserData(resRootUser.posts, resAllCommentedUser), followersNo: resRootUser.followersNo, followingNo: resRootUser.followingNo, - postNo: resRootUser.postNo, + postNo: resRootUser.postNo }; + // eslint-disable-next-line consistent-return return finalRootUserData; }; const rootUserData = await getRootUserData(); @@ -149,9 +193,7 @@ export default { const getRootUserFollowingUserPostData = async (getPastDate: number) => { // getPastDate will get those date from which we want to user post filed const dateCurrentDateEarly = new Date(currentDate); - dateCurrentDateEarly.setDate( - dateCurrentDateEarly.getDate() - getPastDate - ); + dateCurrentDateEarly.setDate(dateCurrentDateEarly.getDate() - getPastDate); // resRootUserFollowingUserPostData = await userDetail // .find( // // finding those user which i follow and get the posts of them @@ -182,11 +224,11 @@ export default { // .skip(skip); // const resRootUserFollowingUserPostData = await userDetail.aggregate([ - //getting the document that is not rootUser & and the user which is not friend of rootUser + // getting the document that is not rootUser & and the user which is not friend of rootUser { $match: { - "followers.id": rootUser.id, - }, + "followers.id": rootUser.id + } }, { // getting only required field @@ -197,24 +239,19 @@ export default { name: 1, userID: 1, email: 1, - id: 1, - }, + id: 1 + } }, - { $sample: { size: 10 } }, + { $sample: { size: 10 } } ]); // Getting Dynamic Data of the user who comment on the user post - let commentedUserId: string[] = []; - for (let i = 0; i < resRootUserFollowingUserPostData.length; i++) { - const posts: object = resRootUserFollowingUserPostData[i].posts; + const commentedUserId: string[] = []; + for (let i = 0; i < resRootUserFollowingUserPostData.length; i += 1) { + const { posts } = resRootUserFollowingUserPostData[i]; // let userIdFromSameUserPostsComment: string[] = []; - for ( - let j = 0; - j < resRootUserFollowingUserPostData[i].posts.length; - j++ - ) { - const comment: { user: string } | undefined = - posts[j].comments.by[posts[j].comments.by.length - 1]; + for (let j = 0; j < resRootUserFollowingUserPostData[i].posts.length; j += 1) { + const comment: { user: string } | undefined = posts[j].comments.by[posts[j].comments.by.length - 1]; if (comment) { commentedUserId.push(comment.user); } @@ -228,29 +265,27 @@ export default { _id: 0, userID: 1, picture: 1, - id: 1, + id: 1 } ); - let finalPostData: object[] = []; - const mergeArrays = ( + const finalPostData: object[] = []; + function mergeRootUserFollowingUserPost( userPosts: any[], commentedUser: (SchemaMethodInstance & { _id: any; })[] - ) => { + ) { return userPosts.map((post) => { const lastCommented = post.comments.by[post.comments.by.length - 1]; if (lastCommented) { - const filteredUser = commentedUser.filter( - (user) => user.id === lastCommented.user - ); + const filteredUser = commentedUser.filter((user) => user.id === lastCommented.user); if (!filteredUser.length) { // post.phone = filteredUser; return post; } const newUser = filteredUser.map((user) => ({ picture: user.picture, - userID: user.userID, + userID: user.userID })); const newObj = { ...post, @@ -261,37 +296,33 @@ export default { user: lastCommented.user, comment: lastCommented.comment, picture: newUser[0].picture, - userID: newUser[0].userID, - }, - ], - }, + userID: newUser[0].userID + } + ] + } }; return newObj; } return post; }); - }; + } - for (let i = 0; i < resRootUserFollowingUserPostData.length; i++) { + for (let i = 0; i < resRootUserFollowingUserPostData.length; i += 1) { finalPostData.push({ - posts: mergeArrays( - resRootUserFollowingUserPostData[i].posts, - resAllCommentedUser - ), + posts: mergeRootUserFollowingUserPost(resRootUserFollowingUserPostData[i].posts, resAllCommentedUser), // [<>,,] picture: resRootUserFollowingUserPostData[i].picture, name: resRootUserFollowingUserPostData[i].name, userID: resRootUserFollowingUserPostData[i].userID, email: resRootUserFollowingUserPostData[i].email, - id: resRootUserFollowingUserPostData[i].id, + id: resRootUserFollowingUserPostData[i].id }); } // ======================================================== return finalPostData; }; - const rootUserFollowingUserPostData = - await getRootUserFollowingUserPostData(5); + const rootUserFollowingUserPostData = await getRootUserFollowingUserPostData(5); // if (rootUserFollowingUserPostData.length === 0) { // // if there is not any post which is fivedays early // rootUserFollowingUserPostData = await getRootUserFollowingUserPostData(30); @@ -317,16 +348,12 @@ export default { // } // } // getting/creating data for Suggestion for You and followed By block - let userSuggestion: any[] = await userDetail.aggregate([ - //getting the document that is not rootUser & and the user which is not friend of rootUser + const userSuggestion: any[] = await userDetail.aggregate([ + // getting the document that is not rootUser & and the user which is not friend of rootUser { $match: { - $and: [ - { "friends.id": { $not: { $eq: req.rootUser.id } } }, - { "followers.id": { $not: { $eq: req.rootUser.id } } }, - { id: { $not: { $eq: req.rootUser.id } } }, - ], - }, + $and: [{ "friends.id": { $not: { $eq: req.rootUser.id } } }, { "followers.id": { $not: { $eq: req.rootUser.id } } }, { id: { $not: { $eq: req.rootUser.id } } }] + } }, { // getting only required field @@ -335,25 +362,22 @@ export default { name: 1, userID: 1, email: 1, - id: 1, - }, + id: 1 + } }, - { $sample: { size: 5 } }, + { $sample: { size: 5 } } ]); const lengthOfuserSuggestion: number = userSuggestion.length; - for (let i = 0; i < 5 - lengthOfuserSuggestion; i++) { + for (let i = 0; i < 5 - lengthOfuserSuggestion; i += 1) { // pushing but user according to the user that are avilable in original userSuggestion data userSuggestion.push(botUser[i]); } // getting/create data for Followed by user block in client site - let followedBy: any[] = await userDetail.aggregate([ + const followedBy: any[] = await userDetail.aggregate([ { $match: { - $and: [ - { "friends.id": { $not: { $eq: req.rootUser.id } } }, - { "following.id": req.rootUser.id }, - ], - }, + $and: [{ "friends.id": { $not: { $eq: req.rootUser.id } } }, { "following.id": req.rootUser.id }] + } }, { // getting only required field @@ -362,39 +386,36 @@ export default { picture: 1, name: 1, userID: 1, - id: 1, - }, + id: 1 + } }, - { $sample: { size: 5 } }, + { $sample: { size: 5 } } ]); const lengthOfFollowedBy: number = followedBy.length; - for (let i = botUser.length - 1; i >= lengthOfFollowedBy; i--) { + for (let i = botUser.length - 1; i >= lengthOfFollowedBy; i -= 1) { followedBy.push(botUser[i]); } - let userStories: any[] = await userDetail.aggregate([ + const userStories: any[] = await userDetail.aggregate([ { $match: { - $and: [ - { "followers.id": req.rootUser.id }, - { stories: { $not: { $eq: null } } }, - ], - }, + $and: [{ "followers.id": req.rootUser.id }, { stories: { $not: { $eq: null } } }] + } }, { $project: { picture: 1, userID: 1, name: 1, - stories: 1, - }, + stories: 1 + } }, { - $sample: { size: 10 }, - }, + $sample: { size: 10 } + } ]); const lengthOfUserStories: number = userStories.length; - for (let i = botUser.length - 1; i >= lengthOfUserStories; i--) { + for (let i = botUser.length - 1; i >= lengthOfUserStories; i -= 1) { userStories.push(botUser[i]); } const resData: any = { @@ -402,34 +423,34 @@ export default { followedUserPost: rootUserFollowingUserPostData, userSuggestion, followedBy, - userStories, + userStories }; return res.status(200).json({ success: true, msg: "Welcome to Social", - data: resData, + data: resData }); } catch (err) { return res.status(500).json({ success: false, - msg: "Server Error, Please Try again later", + msg: "Server Error, Please Try again later" }); } }, homeUser: (req: Request, res: Response): object => { try { - const rootUser = req.rootUser; + const { rootUser } = req; return res.status(200).json(rootUser); } catch (err) { return res.status(500).json({ success: false, - error: "Server Error!!, Please Try again later", + error: "Server Error!!, Please Try again later" }); } }, getUserProfile: async (req: Request, res: Response): Promise => { try { - const rootUser = req.rootUser; + const { rootUser } = req; const userID = req.params.userid; type SearchedUserResponseData = { posts: [] | {}; @@ -443,9 +464,7 @@ export default { followingNo: number; postNo: number; }; - const searchedUser = await userDetail.findOne({ userID: userID }, < - SearchedUserResponseData - >{ + const searchedUser = await userDetail.findOne({ userID }, { postNo: 1, posts: { $slice: -20 }, name: 1, @@ -455,129 +474,78 @@ export default { id: 1, stories: 1, followersNo: 1, - followingNo: 1, + followingNo: 1 }); if (!searchedUser) { - return res - .status(401) - .json({ success: false, msg: "User doesn't exist" }); - } else { - const isRootUserFollowed = await userDetail.findOne( - { - id: rootUser.id, - following: { - $elemMatch: { - id: searchedUser.id, - }, - }, - }, - { - id: 1, - _id: 0, - userID: 1, - } - ); - - let commentedUserId: string[] = []; - const posts: object = searchedUser.posts; - // let userIdFromSameUserPostsComment: string[] = []; - for (let i = 0; i < searchedUser.posts.length; i++) { - const comment: { user: string } | undefined = - posts[i].comments.by[posts[i].comments.by.length - 1]; - if (comment) { - commentedUserId.push(comment.user); + return res.status(401).json({ success: false, msg: "User doesn't exist" }); + } + const isRootUserFollowed = await userDetail.findOne( + { + id: rootUser.id, + following: { + $elemMatch: { + id: searchedUser.id + } } + }, + { + id: 1, + _id: 0, + userID: 1 } + ); - const resAllCommentedUser = await userDetail.find( - { id: { $in: commentedUserId } }, - { - _id: 0, - userID: 1, - picture: 1, - id: 1, - } - ); + const commentedUserId: string[] = []; + const { posts } = searchedUser; + // let userIdFromSameUserPostsComment: string[] = []; + for (let i = 0; i < searchedUser.posts.length; i += 1) { + const comment: { user: string } | undefined = posts[i].comments.by[posts[i].comments.by.length - 1]; + if (comment) { + commentedUserId.push(comment.user); + } + } - const mergeArrays = ( - userPosts: UserDocumentPosts[], - commentedUser: (SchemaMethodInstance & { - _id: any; - })[] - ) => { - return userPosts.map((post) => { - const lastCommented = post.comments.by[post.comments.by.length - 1]; - if (lastCommented) { - const filteredUser = commentedUser.filter( - (user) => user.id === lastCommented.user - ); - if (!filteredUser.length) { - // post.phone = filteredUser; - return post; - } - const newUser = filteredUser.map((user) => ({ - picture: user.picture, - userID: user.userID, - })); - const newObj: ResPonseUserPost = { - // ...post, - picture: { - url: post.picture.url, - }, - caption: post.caption, - date: post.date, - id: post.id, - likes: post.likes, - comments: { - No: post.comments.No, - by: [ - { - user: lastCommented.user, - comment: lastCommented.comment, - picture: newUser[0].picture, - userID: newUser[0].userID, - }, - ], - }, - }; - return newObj; - } - return post; - }); - }; - const finalSearchedUserData: SearchedUserResponseData = { - userID: searchedUser.userID, - name: searchedUser.name, - picture: searchedUser.picture, - email: searchedUser.email, - id: searchedUser.id, - stories: searchedUser.stories, - posts: mergeArrays(searchedUser.posts, resAllCommentedUser), - followersNo: searchedUser.followersNo, - followingNo: searchedUser.followingNo, - postNo: searchedUser.postNo, - }; - if (!isRootUserFollowed) { - return res.status(200).json({ - success: true, - msg: "Found User", - searchedUser: finalSearchedUserData, - isRootUserFollowed: false, - }); - } else { - return res.status(200).json({ - success: true, - msg: "Found User", - searchedUser: finalSearchedUserData, - isRootUserFollowed: true, - }); + const resAllCommentedUser = await userDetail.find( + { id: { $in: commentedUserId } }, + { + _id: 0, + userID: 1, + picture: 1, + id: 1 } + ); + + const finalSearchedUserData: SearchedUserResponseData = { + userID: searchedUser.userID, + name: searchedUser.name, + picture: searchedUser.picture, + email: searchedUser.email, + id: searchedUser.id, + stories: searchedUser.stories, + posts: mergeRootUserProfile(searchedUser.posts, resAllCommentedUser), + followersNo: searchedUser.followersNo, + followingNo: searchedUser.followingNo, + postNo: searchedUser.postNo + }; + if (!isRootUserFollowed) { + return res.status(200).json({ + success: true, + msg: "Found User", + searchedUser: finalSearchedUserData, + isRootUserFollowed: false + }); } + return res.status(200).json({ + success: true, + msg: "Found User", + searchedUser: finalSearchedUserData, + isRootUserFollowed: true + }); } catch (err) { return res.status(500).json({ success: false, - msg: "Server Error!!, Please Try again later", + msg: "Server Error!!, Please Try again later" }); } }, @@ -589,8 +557,9 @@ export default { const resUser = await userDetail .find( { - userID: { $regex: "^" + req.body.userID, $options: "i" }, + userID: { $regex: `^${req.body.userID}`, $options: "i" } }, + // eslint-disable-next-line object-curly-newline { name: 1, picture: 1, userID: 1, email: 1, _id: 0 } ) .limit(10); @@ -598,19 +567,17 @@ export default { } catch (err) { return res.status(500).json({ success: false, - msg: "Server Error!!, Please Try again later", + msg: "Server Error!!, Please Try again later" }); } }, followUser: async (req: Request, res: Response): Promise => { try { - const rootUser = req.rootUser; + const { rootUser } = req; const { userID, id } = req.body; // these are the followed to user id and email if (!userID || !id) { - return res - .status(401) - .json({ success: false, msg: "UnAuthorized" }); + return res.status(401).json({ success: false, msg: "UnAuthorized" }); } const followUserExist = await userDetail.findOne( { @@ -619,64 +586,60 @@ export default { id: rootUser.id, following: { $elemMatch: { - id: id, - }, - }, + id + } + } }, { name: 1, picture: 1, email: 1, - id: 1, + id: 1 } ); if (followUserExist) { return res.status(400).json({ success: false, - msg: "you had already followed this user", + msg: "you had already followed this user" }); } const followedToUser = await userDetail.findOne( { - id: id, + id }, { email: 1, name: 1, userID: 1, picture: 1, - id: 1, + id: 1 } ); if (!followedToUser) { - return res - .status(401) - .json({ success: false, msg: "User doesn't exist" }); + return res.status(401).json({ success: false, msg: "User doesn't exist" }); } const followRes = await rootUser.followUser({ id: followedToUser.id }); if (!followRes) { - return res - .status(500) - .json({ success: false, msg: "Server error" }); + return res.status(500).json({ success: false, msg: "Server error" }); } // logic to store as a friend if both of them had followed // we had already check for rootUser that that does rootUser followed the other user // now we just have to check does other user follow rootUser if then then save it as a friend const rootUserExistInFollowUser = await userDetail.findOne( { - id: id, + id, following: { $elemMatch: { - id: rootUser.id, - }, - }, + id: rootUser.id + } + } }, { name: 1, picture: 1, userID: 1, email: 1, - id: 1, + id: 1 } ); if (rootUserExistInFollowUser) { @@ -686,16 +649,16 @@ export default { id: rootUser.id, following: { $elemMatch: { - id: id, - }, - }, + id + } + } }, { name: 1, picture: 1, userID: 1, email: 1, - id: 1, + id: 1 } ); if (followUserExistInRootUser) { @@ -703,7 +666,7 @@ export default { // storing as a friend to rootuser await userDetail.updateOne( { - id: rootUser.id, + id: rootUser.id }, { // pushing the new followers into followed to user database @@ -713,18 +676,18 @@ export default { // email: followedToUser.email, // userID: followedToUser.userID, // picture: followedToUser.picture, - id: followedToUser.id, - }, + id: followedToUser.id + } }, $inc: { - friendsNo: 1, - }, + friendsNo: 1 + } } ); // storing as a friend to followedToUser await userDetail.updateOne( { - id: followedToUser.id, + id: followedToUser.id }, { // pushing the new followers into followed to user database @@ -734,115 +697,109 @@ export default { // email: rootUser.email, // userID: rootUser.userID, // picture: rootUser.picture, - id: rootUser.id, - }, + id: rootUser.id + } }, $inc: { - friendsNo: 1, - }, + friendsNo: 1 + } } ); } } - return res - .status(200) - .json({ success: true, msg: "Follow successfully" }); + return res.status(200).json({ success: true, msg: "Follow successfully" }); } catch (err) { return res.status(500).json({ success: false, - msg: "Server Error!!, Please Try again later", + msg: "Server Error!!, Please Try again later" }); } }, unFollowUser: async (req: Request, res: Response): Promise => { try { - const rootUser = req.rootUser; + const { rootUser } = req; const { userID, id } = req.body; // NOTE userID = user that rootUser is trying to search or query if (!userID || !id) { - return res - .status(401) - .json({ success: false, msg: "UnAuthorized" }); + return res.status(401).json({ success: false, msg: "UnAuthorized" }); } const unFollowUserExistOnRootUser = await userDetail.findOne( { id: rootUser.id, following: { $elemMatch: { - id: id, - }, - }, + id + } + } }, { name: 1, picture: 1, userID: 1, email: 1, - id: 1, + id: 1 } ); if (!unFollowUserExistOnRootUser) { return res.status(400).json({ success: false, - msg: "you hadn't followed this user yet", + msg: "you hadn't followed this user yet" }); } const unFollowedToUserExist = await userDetail.findOne( { - id: id, + id }, { email: 1, name: 1, userID: 1, picture: 1, - id: 1, + id: 1 } ); if (!unFollowedToUserExist) { - return res - .status(401) - .json({ success: false, msg: "User doesn't exist" }); + return res.status(401).json({ success: false, msg: "User doesn't exist" }); } // const followRes = await rootUser.unFollowUser(unFollowedToUserExist); let unFollowRes = await userDetail.updateOne( { - id: rootUser.id, + id: rootUser.id }, { - $pull: { following: { id: id } }, + $pull: { following: { id } }, $inc: { - followingNo: -1, - }, + followingNo: -1 + } } ); if (!unFollowRes) { return res.status(500).json({ success: false, - msg: "Server error!!, Please try again later", + msg: "Server error!!, Please try again later" }); } unFollowRes = await userDetail.updateOne( { - id: id, + id }, { $pull: { followers: { id: rootUser.id }, notification: { - user: rootUser.id, - }, + user: rootUser.id + } }, $inc: { - followersNo: -1, - }, + followersNo: -1 + } } ); if (!unFollowRes) { return res.status(500).json({ success: false, - msg: "Server error!!, Please Try again later", + msg: "Server error!!, Please Try again later" }); } const friendExist = await userDetail.findOne( @@ -850,75 +807,75 @@ export default { id: rootUser.id, friends: { $elemMatch: { - id: id, - }, - }, + id + } + } }, { name: 1, picture: 1, userID: 1, email: 1, - id: 1, + id: 1 } ); if (!friendExist) { return res.status(200).json({ success: true, - msg: "UnFollowed User Successfully", + msg: "UnFollowed User Successfully" }); } let unfriendRes = await userDetail.updateOne( { - id: rootUser.id, + id: rootUser.id }, { - $pull: { friends: { id: id } }, + $pull: { friends: { id } }, $inc: { - friendsNo: -1, - }, + friendsNo: -1 + } } ); if (!unfriendRes) { return res.status(500).json({ success: false, - msg: "Server error!!, Please Try again later", + msg: "Server error!!, Please Try again later" }); } unfriendRes = await userDetail.updateOne( { - id: id, + id }, { $pull: { friends: { id: rootUser.id } }, $inc: { - friendsNo: -1, - }, + friendsNo: -1 + } } ); if (!unfriendRes) { return res.status(500).json({ success: false, - msg: "Server error!!, Please Try again later", + msg: "Server error!!, Please Try again later" }); } return res.status(200).json({ success: true, - msg: "UnFollowed User Successfully", + msg: "UnFollowed User Successfully" }); } catch (err) { return res.status(500).json({ success: false, - msg: "Server Error!!, Please Try again later", + msg: "Server Error!!, Please Try again later" }); } }, getNotificationData: async (req: Request, res: Response) => { try { - const userID = req.rootUser.userID; + const { userID } = req.rootUser; const getNotificationRes = await userDetail.findOne( { - userID: userID, + userID }, { notification: { $slice: -10 }, @@ -944,13 +901,11 @@ export default { messages: 0, picture: 0, userID: 0, - _id: 0, + _id: 0 } ); if (!getNotificationRes) { - return res - .status(404) - .json({ success: false, msg: "User not Found" }); + return res.status(404).json({ success: false, msg: "User not Found" }); } const users = getNotificationRes.notification.map((el) => el.user); const resUser = await userDetail.find( @@ -958,136 +913,134 @@ export default { { _id: 0, userID: 1, - picture: 1, + picture: 1 } ); // At least for right now i will not again add topic field in notification if (!resUser) { res.status(400).json({ success: false, - msg: "Some problem occur please try again later...", + msg: "Some problem occur please try again later..." }); } - res.status(200).json({ + return res.status(200).json({ success: true, msg: "Successful", - data: resUser, + data: resUser }); } catch (err) { - return res - .status(500) - .json({ error: "Server Error!!, Please Try again later" }); + return res.status(500).json({ error: "Server Error!!, Please Try again later" }); } }, getFriends: async (req: Request, res: Response): Promise => { try { - const id = req.params.id; + const { id } = req.params; const resFriends: any[] = await userDetail.find( { friends: { $elemMatch: { - id: id, - }, - }, + id + } + } }, { userID: 1, name: 1, picture: 1, email: 1, - id: 1, + id: 1 } ); if (!resFriends) { return res.status(500).json({ success: false, - msg: "Error Occur while fetching friends data", + msg: "Error Occur while fetching friends data" }); } return res.status(200).json({ success: true, msg: "Successful", - friends: resFriends, + friends: resFriends }); } catch (err) { return res.status(500).json({ success: false, - msg: "Server Error!!, while fetching friends data", + msg: "Server Error!!, while fetching friends data" }); } }, getFollowers: async (req: Request, res: Response): Promise => { try { - const id = req.params.id; + const { id } = req.params; const resFriends: any[] = await userDetail.find( { following: { $elemMatch: { - id: id, - }, - }, + id + } + } }, { userID: 1, name: 1, picture: 1, email: 1, - id: 1, + id: 1 } ); if (!resFriends) { return res.status(500).json({ success: false, - msg: "Error Occur while fetching friends data", + msg: "Error Occur while fetching friends data" }); } return res.status(200).json({ success: true, msg: "Successful", - friends: resFriends, + friends: resFriends }); } catch (err) { return res.status(500).json({ success: false, - msg: "Server Error!!, while fetching friends data", + msg: "Server Error!!, while fetching friends data" }); } }, getFollowings: async (req: Request, res: Response): Promise => { try { - const id = req.params.id; + const { id } = req.params; const resFriends: any[] = await userDetail.find( { followers: { $elemMatch: { - id: id, - }, - }, + id + } + } }, { userID: 1, name: 1, picture: 1, email: 1, - id: 1, + id: 1 } ); if (!resFriends) { return res.status(500).json({ success: false, - msg: "Error Occur while fetching friends data", + msg: "Error Occur while fetching friends data" }); } return res.status(200).json({ success: true, msg: "Successful", - friends: resFriends, + friends: resFriends }); } catch (err) { return res.status(500).json({ success: false, - msg: "Server Error!!, while fetching friends data", + msg: "Server Error!!, while fetching friends data" }); } - }, + } };