diff --git a/src/controllers/Auth/index.ts b/src/controllers/Auth/index.ts index 9782499..ab71d42 100644 --- a/src/controllers/Auth/index.ts +++ b/src/controllers/Auth/index.ts @@ -35,7 +35,13 @@ export const register = async (req: Request, res: Response, next: NextFunction) salt, password: hashedPassword, }); - + setCookie({ + user: newUser, + res, + next, + message: "Register Success", + statusCode: 200, + }); await newUser.save(); res.status(201).json({ message: 'User registered successfully' }); diff --git a/src/controllers/Mentor/index.ts b/src/controllers/Mentor/index.ts index e054568..ee7c6f1 100644 --- a/src/controllers/Mentor/index.ts +++ b/src/controllers/Mentor/index.ts @@ -136,7 +136,91 @@ export const getMentor = async (req: Request, res: Response, next: NextFunction) next(new CustomError(error.message)); } }; - - - + + export const getStudentsWithNullMentor = async (req: Request, res: Response, next: NextFunction) => { + try { + const { mentorId } = req.query as { mentorId?: string }; + const { query } = req.query as { query?: string }; + + if (!mentorId || !mongoose.Types.ObjectId.isValid(mentorId)) { + return next(new CustomError("Invalid mentor ID", 400)); + } + + const mentorObjectId = new mongoose.Types.ObjectId(mentorId); + + // Fetch the mentor preferences + const mentor = await db.collection('mentors').findOne({ _id: mentorObjectId }); + + if (!mentor) { + return next(new CustomError("Mentor not found", 404)); + } + + const { competitiveExam, standard } = mentor.preference; + const mentorGender = mentor.about.gender || ''; + + console.log('Mentor Preferences:', { standard, competitiveExam, mentorGender }); + + let filter: any = { + $or: [ + { "mentor._id": null }, + { "mentor._id": { $exists: false } }, + ], + }; + + if (standard.length > 0) { + filter["academic.standard"] = { $exists: true }; + } + if (competitiveExam.length > 0) { + filter["academic.competitiveExam"] = { $in: competitiveExam }; + } + + // Handle search query if provided + if (query) { + filter = { + ...filter, + $or: [ + { firstname: { $regex: query, $options: 'i' } }, + { lastname: { $regex: query, $options: 'i' } }, + { email: { $regex: query, $options: 'i' } }, + ], + }; + } + + // Find all users with a null mentor ID + const usersWithNullMentor = await db.collection('users').find(filter).project({ + _id: 1, + firstname: 1, + lastname: 1, + email: 1, + academic: 1, + mentor: 1, + about: 1, + }).toArray(); + + console.log('Users with null mentor ID:', usersWithNullMentor); + + // Separate users by gender + const usersWithSameGender = usersWithNullMentor.filter(user => user.about.gender === mentorGender); + const usersWithDifferentGender = usersWithNullMentor.filter(user => user.about.gender !== mentorGender && user.about.gender !== null); + const usersWithUnknownGender = usersWithNullMentor.filter(user => user.about.gender === null); + + // Combine the lists with same gender users first + const students = [...usersWithSameGender, ...usersWithDifferentGender, ...usersWithUnknownGender]; + + if (students.length === 0) { + return res.status(404).json({ + success: false, + message: "No students found matching the criteria", + }); + } + + res.status(200).json({ + success: true, + students: students, + }); + } catch (error: any) { + console.error(`Error: ${error.message}`); + next(new CustomError(error.message)); + } + }; \ No newline at end of file diff --git a/src/controllers/Student/index.ts b/src/controllers/Student/index.ts index b2e8e7a..3cd947b 100644 --- a/src/controllers/Student/index.ts +++ b/src/controllers/Student/index.ts @@ -98,78 +98,4 @@ export const deallocateStudents = async (req: Request, res: Response, next: Next } }; -export const getStudentsWithNullMentor = async (req: Request, res: Response, next: NextFunction) => { - try { - const { query } = req.query as { query?: string }; - - let filter: any = { "mentor._id": null }; - - if (query) { - // Exact match filter - const exactMatchFilter = { - $or: [ - { firstname: query }, - { lastname: query }, - { email: query }, - ], - }; - - // Search for exact matches first - const exactMatches = await db.collection('users').find({ - ...filter, - $or: [ - exactMatchFilter - ] - }).project({ - _id: 1, - firstname: 1, - lastname: 1, - email: 1, - academic: 1, - mentor: 1, - }).toArray(); - - if (exactMatches.length > 0) { - return res.status(200).json({ - success: true, - students: exactMatches, - }); - } - - // If no exact matches, search for partial matches - filter = { - ...filter, - $or: [ - { firstname: { $regex: query, $options: 'i' } }, - { lastname: { $regex: query, $options: 'i' } }, - { email: { $regex: query, $options: 'i' } }, - ], - }; - } - - const students = await db.collection('users').find(filter).project({ - _id: 1, - firstname: 1, - lastname: 1, - email: 1, - academic: 1, - mentor: 1, - }).toArray(); - - if (students.length === 0) { - return res.status(404).json({ - success: false, - message: "No students found with null mentor", - }); - } - - res.status(200).json({ - success: true, - students: students, - }); - } catch (error: any) { - console.error(`Error: ${error.message}`); - next(new CustomError(error.message)); - } - }; - \ No newline at end of file + diff --git a/src/routes/mentorRoutes.ts b/src/routes/mentorRoutes.ts index 459e81c..df940f6 100644 --- a/src/routes/mentorRoutes.ts +++ b/src/routes/mentorRoutes.ts @@ -1,12 +1,13 @@ import express from "express"; import { checkAuth } from "../middleware/checkAuth"; -import { getMentor, getMentorWithStudents, verifyMentor } from "../controllers/Mentor"; +import { getMentor, getMentorWithStudents, getStudentsWithNullMentor, verifyMentor } from "../controllers/Mentor"; const router = express.Router(); router.get("/getmentor", checkAuth, getMentor); router.put("/verify/:id", checkAuth, verifyMentor) router.get("/getstudent/:id", checkAuth, getMentorWithStudents) +router.get("/getmentorstudent", checkAuth, getStudentsWithNullMentor) export default router; diff --git a/src/routes/studentRoute.ts b/src/routes/studentRoute.ts index c9ad5ed..28900b7 100644 --- a/src/routes/studentRoute.ts +++ b/src/routes/studentRoute.ts @@ -1,12 +1,11 @@ import express from "express"; import { checkAuth } from "../middleware/checkAuth"; -import { getStudentsWithNullMentor, allocateStudents, deallocateStudents} from "../controllers/Student"; +import { allocateStudents, deallocateStudents} from "../controllers/Student"; const router = express.Router(); router.post("/allocate-student/:mentorId", checkAuth, allocateStudents) router.post("/deallocate-student", checkAuth, deallocateStudents) -router.get("/getmentorstudent", checkAuth, getStudentsWithNullMentor) export default router;