Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/controllers/Auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
90 changes: 87 additions & 3 deletions src/controllers/Mentor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
76 changes: 1 addition & 75 deletions src/controllers/Student/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};


3 changes: 2 additions & 1 deletion src/routes/mentorRoutes.ts
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 1 addition & 2 deletions src/routes/studentRoute.ts
Original file line number Diff line number Diff line change
@@ -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;