Skip to content

Commit f6a3767

Browse files
authored
Merge branch 'Project-Developers-2k24:dev' into dev
2 parents 1ae88d8 + d6d953e commit f6a3767

File tree

5 files changed

+152
-5
lines changed

5 files changed

+152
-5
lines changed

src/config/swaggerConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const swaggerDefinition = {
99
},
1010
servers: [
1111
{
12-
url: 'http://localhost:8000/api',
12+
url: 'http://localhost:8080/api',
1313
description: 'Development server',
1414
},
1515
{

src/controllers/googleAuthController.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,29 @@ passport.use(
99
{
1010
clientID: process.env.GOOGLE_CLIENT_ID,
1111
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
12-
callbackURL: "https://projectdev2114.azurewebsites.net/api/user/google/callback", // Use your actual redirect URL
12+
callbackURL:
13+
"https://projectdev2114.azurewebsites.net/api/user/google/callback", // Use your actual redirect URL
1314
},
1415
async (accessToken, refreshToken, profile, done) => {
1516
// Find or create user logic
1617
try {
1718
let user = await User.findOne({ email: profile.emails[0].value });
18-
console.log(user)
19+
console.log(user);
20+
if (user.isDisable) {
21+
return res.status(403).json({
22+
status: "failed",
23+
message:
24+
"Access denied. Your account has been disabled. Please contact support for further assistance.",
25+
});
26+
}
1927
if (!user) {
2028
user = new User({
2129
googleId: profile.id,
2230
username: profile.displayName,
2331
email: profile.emails[0].value,
2432
avatar: profile.photos[0].value,
25-
isVerified:true
33+
// password:,
34+
isVerified: true,
2635
});
2736
await user.save();
2837
}

src/controllers/userController.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ const jwt = require("jsonwebtoken");
66
const formidable = require("formidable");
77
const fs = require("fs");
88
const Books = require("../models/books.model");
9+
const moongose = require("mongoose");
910

1011
const {
1112
sendVerificationMail,
1213
sendResetPasswordMail,
1314
sendEmailToAdminVerified,
1415
} = require("../utils/sendVerificationMail");
16+
const { default: mongoose } = require("mongoose");
1517

1618
async 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

437565
async function deleteBook(req, res) {
438566
try {
@@ -528,4 +656,6 @@ module.exports = {
528656
askChatBot,
529657
uploadBooks,
530658
deleteBook,
659+
userData,
660+
userIsDisable,
531661
};

src/models/user.model.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ const userSchema = new mongoose.Schema(
6868
type: Boolean,
6969
default:false
7070
},
71+
isDisable: {
72+
type: Boolean,
73+
default:false
74+
},
7175
},
7276
{
7377
timestamps: true,

src/routes/userRoutes.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ router.get(
276276
passport.authenticate("google", { scope: ["profile", "email"] })
277277
);
278278

279+
//admin get data
280+
router.get("/userData", authMiddleware, UserController.userData);
281+
router.patch("/userDisable/:id", authMiddleware, UserController.userIsDisable);
282+
279283
router.get(
280284
"/google/callback",
281285
passport.authenticate("google", { failureRedirect: "/login" }),
@@ -293,7 +297,7 @@ router.get(
293297
message: "Login successfully",
294298
status: true,
295299
token,
296-
user: req.user
300+
user: req.user,
297301
})}, '*');
298302
window.close();
299303
</script>

0 commit comments

Comments
 (0)