@@ -6,12 +6,14 @@ const jwt = require("jsonwebtoken");
66const formidable = require ( "formidable" ) ;
77const fs = require ( "fs" ) ;
88const Books = require ( "../models/books.model" ) ;
9+ const moongose = require ( "mongoose" ) ;
910
1011const {
1112 sendVerificationMail,
1213 sendResetPasswordMail,
1314 sendEmailToAdminVerified,
1415} = require ( "../utils/sendVerificationMail" ) ;
16+ const { default : mongoose } = require ( "mongoose" ) ;
1517
1618async function getChatMaruti ( req , res ) {
1719 try {
@@ -251,6 +253,13 @@ async function userLogin(req, res) {
251253 . status ( 404 )
252254 . json ( { status : "failed" , message : "You are not registered" } ) ;
253255 }
256+ if ( user . isDisable ) {
257+ return res . status ( 403 ) . json ( {
258+ status : "failed" ,
259+ message : "Access denied. Your account has been disabled. Please contact support for further assistance."
260+ } ) ;
261+ }
262+
254263
255264 const isMatch = await bcrypt . compare ( password , user . password ) ;
256265 if ( ! isMatch ) {
@@ -433,6 +442,125 @@ async function userById(req, res) {
433442 . json ( { status : "failed" , message : "Unable to process request" } ) ;
434443 }
435444}
445+ async function userIsDisable ( req , res ) {
446+ try {
447+ const userId = req . params . id ; // Extract the user ID from params
448+ const ObjectId = new mongoose . Types . ObjectId ( userId ) ; // Ensure it's an ObjectId
449+
450+ // Find the user by ID
451+ const user = await User . findById ( ObjectId ) ;
452+ if ( ! user ) {
453+ return res
454+ . status ( 404 )
455+ . json ( { status : "failed" , message : "User not present" } ) ;
456+ }
457+ console . log ( req . body . isDisable )
458+ // Check if isDisable is provided in the request body
459+ if ( req . body . hasOwnProperty ( "isDisable" ) ) {
460+ user . isDisable = req . body . isDisable ;
461+ } else {
462+ user . isDisable = user . isDisable !== undefined ? user . isDisable : false ;
463+ }
464+
465+ await user . save ( ) ; // Save the updated user
466+
467+ res . status ( 200 ) . json ( {
468+ status : "success" ,
469+ data : user ,
470+ } ) ;
471+ } catch ( error ) {
472+ console . error ( error ) ;
473+ res
474+ . status ( 500 )
475+ . json ( { status : "failed" , message : "Unable to process request" } ) ;
476+ }
477+ }
478+
479+ async function userData ( req , res ) {
480+ try {
481+ const userId = req . userId ;
482+ const { page = 1 , limit = 10 } = req . query ;
483+
484+ const user = await User . findById ( userId ) ;
485+ if ( ! user ) {
486+ return res
487+ . status ( 404 )
488+ . json ( { status : "failed" , message : "User not present" } ) ;
489+ }
490+ if ( user ?. isAdmin === false ) {
491+ return res
492+ . status ( 401 )
493+ . json ( { status : "failed" , message : "Not Authorized for User" } ) ;
494+ }
495+
496+ const skip = ( page - 1 ) * limit ;
497+
498+ // Get the total number of users first
499+ const totalRecords = await User . countDocuments ( ) ;
500+
501+ // Fetch the paginated users
502+ const result = await User . aggregate ( [
503+ {
504+ $addFields : {
505+ stringUserId : { $toString : "$_id" } ,
506+ } ,
507+ } ,
508+ {
509+ $lookup : {
510+ from : "chatbot" ,
511+ localField : "stringUserId" ,
512+ foreignField : "userId" ,
513+ as : "chatbotData" ,
514+ } ,
515+ } ,
516+ {
517+ $addFields : {
518+ bookCount : { $size : "$chatbotData" } ,
519+ } ,
520+ } ,
521+ {
522+ $sort : { createdAt : - 1 } ,
523+ } ,
524+ {
525+ $skip : skip ,
526+ } ,
527+ {
528+ $limit : parseInt ( limit ) ,
529+ } ,
530+ {
531+ $project : {
532+ _id : 1 ,
533+ username : 1 ,
534+ email : 1 ,
535+ isVerified : 1 ,
536+ isAdmin : 1 ,
537+ institution : 1 ,
538+ avatar : 1 ,
539+ bookCount : 1 ,
540+ isDisable : 1 ,
541+ } ,
542+ } ,
543+ ] ) ;
544+
545+ // Calculate total pages
546+ const totalPages = Math . ceil ( totalRecords / limit ) ;
547+
548+ res . status ( 200 ) . json ( {
549+ status : "success" ,
550+ page : parseInt ( page ) ,
551+ limit : parseInt ( limit ) ,
552+ total : totalRecords , // total number of records
553+ totalPages : totalPages , // total pages
554+ data : result ,
555+ } ) ;
556+ } catch ( error ) {
557+ console . error ( error ) ;
558+ res . status ( 500 ) . json ( {
559+ status : "failed" ,
560+ message : "Unable to process request" ,
561+ } ) ;
562+ }
563+ }
436564
437565async function deleteBook ( req , res ) {
438566 try {
@@ -528,4 +656,6 @@ module.exports = {
528656 askChatBot,
529657 uploadBooks,
530658 deleteBook,
659+ userData,
660+ userIsDisable,
531661} ;
0 commit comments