@@ -2,6 +2,7 @@ const User = require('../models/User')
22const jwt = require ( 'jsonwebtoken' )
33const HttpStatus = require ( 'http-status-codes' )
44const emailController = require ( './email' )
5+ const HANDLER = require ( '../utils/response-helper' )
56
67module . exports = {
78 createUser : async ( req , res , next ) => {
@@ -150,5 +151,139 @@ module.exports = {
150151 return res . status ( HttpStatus . OK ) . json ( { success : true , msg : 'Redirect user to register in client side!' } )
151152 }
152153 return res . status ( HttpStatus . BAD_REQUEST ) . json ( { msg : 'Invalid token!' } )
154+ } ,
155+
156+ // ADD TO THE FOLLOWINGS LIST
157+ addFollowing : async ( req , res , next ) => {
158+ const { followId } = req . body
159+ try {
160+ if ( followId === req . user . _id ) {
161+ return res . status ( HttpStatus . OK ) . json ( { msg : 'You can not follow yourself!' } )
162+ }
163+ const user = await User . findById ( req . user . id )
164+ if ( ! user ) {
165+ return res . status ( HttpStatus . BAD_REQUEST ) . json ( { msg : 'No such user exists!' } )
166+ }
167+ user . followings . unshift ( followId )
168+ await user . save ( )
169+ next ( )
170+ } catch ( error ) {
171+ HANDLER . handleError ( res , error )
172+ }
173+ } ,
174+
175+ // ADD TO FOLLOWERS LIST
176+ addFollower : async ( req , res , next ) => {
177+ const { followId } = req . body
178+ try {
179+ const user = await User . findById ( followId )
180+ . populate ( 'followings' , [ 'name.firstName' , 'name.lastName' , 'email' ] )
181+ . populate ( 'followers' , [ 'name.firstName' , 'name.lastName' , 'email' ] )
182+ . exec ( )
183+ if ( ! user ) {
184+ return res . status ( HttpStatus . BAD_REQUEST ) . json ( { msg : 'No such user exists!' } )
185+ }
186+ // add to the followers list
187+ user . followers . unshift ( req . user . id )
188+ await user . save ( )
189+ return res . status ( HttpStatus . OK ) . json ( { user } )
190+ } catch ( error ) {
191+ HANDLER . handleError ( res , error )
192+ }
193+ } ,
194+
195+ // REMOVE FROM FOLLOWINGS LIST
196+ removeFollowing : async ( req , res , next ) => {
197+ const { followId } = req . body
198+ try {
199+ const user = await User . findById ( req . user . _id )
200+ if ( ! user ) {
201+ return res . status ( HttpStatus . OK ) . json ( { msg : 'No such user exists!' } )
202+ }
203+ // check if followId is in following list or not
204+ const followingIdArray = user . followings . map ( followingId => followingId . _id )
205+ const isFollowingIdIndex = followingIdArray . indexOf ( followId )
206+ if ( isFollowingIdIndex === - 1 ) {
207+ return res . status ( HttpStatus . OK ) . json ( { msg : 'You haven\'t followed the user!' } )
208+ } else {
209+ // remove from followings list
210+ user . followings . splice ( isFollowingIdIndex , 1 )
211+ await user . save ( )
212+ }
213+ next ( )
214+ } catch ( error ) {
215+ HANDLER . handleError ( res , error )
216+ }
217+ } ,
218+
219+ // REMOVE FROM FOLLOWERS LIST
220+ removeFollower : async ( req , res , next ) => {
221+ const { followId } = req . body
222+ try {
223+ const user = await User . findById ( followId )
224+ . populate ( 'followings' , [ 'name.firstName' , 'name.lastName' , 'email' ] )
225+ . populate ( 'followers' , [ 'name.firstName' , 'name.lastName' , 'email' ] )
226+ . exec ( )
227+ if ( ! user ) {
228+ return res . status ( HttpStatus . NOT_FOUND ) . json ( { msg : 'No such user exists!' } )
229+ }
230+ const followersIdArray = user . followers . map ( ( follower ) => follower . _id )
231+ const isFollowingIndex = followersIdArray . indexOf ( req . user . _id )
232+ if ( isFollowingIndex === - 1 ) {
233+ return res . status ( HttpStatus . OK ) . json ( { msg : 'User is not following!' } )
234+ }
235+ user . followers . splice ( isFollowingIndex , 1 )
236+ await user . save ( )
237+ return res . status ( HttpStatus . OK ) . json ( { user } )
238+ } catch ( error ) {
239+ HANDLER . handleError ( res , error )
240+ }
241+ } ,
242+ blockUser : async ( req , res , next ) => {
243+ const { id } = req . params
244+ try {
245+ const user = await User . findById ( req . user . _id )
246+ . populate ( 'blocked' , [ 'name.firstName' , 'name.lastName' , 'email' ] )
247+ . exec ( )
248+ if ( ! user ) {
249+ return res . status ( HttpStatus . BAD_REQUEST ) . json ( { msg : 'Invalid request!' } )
250+ }
251+ // check if admin
252+ if ( user . isAdmin === true ) {
253+ user . blocked . unshift ( id )
254+ await user . save ( )
255+ return res . status ( HttpStatus . OK ) . json ( { user } )
256+ }
257+ // else not permitted
258+ return res . status ( HttpStatus . BAD_REQUEST ) . json ( { msg : 'You don\'t have permission!' } )
259+ } catch ( error ) {
260+ HANDLER . handleError ( res , error )
261+ }
262+ } ,
263+ unBlockUser : async ( req , res , next ) => {
264+ const { id } = req . params
265+ try {
266+ const user = await User . findById ( req . user . _id )
267+ . populate ( 'blocked' , [ 'name.firstName' , 'name.lastName' , 'email' ] )
268+ . exec ( )
269+ if ( ! user ) {
270+ return res . status ( HttpStatus . NOT_FOUND ) . json ( { msg : 'No such user exists!' } )
271+ }
272+ // if admin
273+ if ( user . isAdmin === true ) {
274+ const blockedIds = user . blocked . map ( item => item . _id )
275+ const unblockIndex = blockedIds . indexOf ( id )
276+ console . log ( 'UnblockIndex ' , unblockIndex )
277+ if ( unblockIndex !== - 1 ) {
278+ user . blocked . splice ( unblockIndex , 1 )
279+ await user . save ( )
280+ return res . status ( HttpStatus . OK ) . json ( { user } )
281+ }
282+ return res . status ( HttpStatus . NOT_FOUND ) . json ( { msg : 'No such user exist!' } )
283+ }
284+ return res . status ( HttpStatus . BAD_REQUEST ) . json ( { msg : 'You don\'t have permission!' } )
285+ } catch ( error ) {
286+ HANDLER . handleError ( res , error )
287+ }
153288 }
154289}
0 commit comments