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
219 changes: 170 additions & 49 deletions controller/quesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,25 @@ export const createQuestion = async (req, res) => {

const imageUrls = await processImages(data.images);


const options = await Promise.all(data.options.map(async (option) => {

let optionImageUrls = [];
if (option.image && Array.isArray(option.image) && option.image.length > 0) {
optionImageUrls = await processImages(option.image);
}

return {
image: optionImageUrls,
optionDb: { name: option.name,
tag: option.isCorrect === true ? "Correct" : "Incorrect",
images: optionImageUrls.length > 0 ? optionImageUrls.map(image => ({ url: image?.getUrl, key: image?.key })) : null,}
image: optionImageUrls,
optionDb: {
name: option.name,
tag: option.isCorrect === true ? "Correct" : "Incorrect",
images: optionImageUrls.length > 0 ? optionImageUrls.map(image => ({ url: image?.getUrl, key: image?.key })) : null,
}
};
}));
const optionsSignedUrls = options.flatMap(option => (option.image ? option.image.map(image => image.putUrl) : []));

const hasCorrectOption = options.some(
(option) => option.optionDb.tag === 'Correct'
);
Expand All @@ -69,7 +71,7 @@ export const createQuestion = async (req, res) => {
message: 'At least one option must be correct',
});
}

const newQuestion = new Ques({
question: data.question,
options: options.map((option) => option.optionDb),
Expand Down Expand Up @@ -117,7 +119,7 @@ export const deleteQuestion = async (req, res) => {
.status(404)
.json({ success: false, message: "User not found" });

if(question.images && question.images.length > 0) await deleteImages(question.images);
if (question.images && question.images.length > 0) await deleteImages(question.images);

question.options.forEach(async (option) => {
if (option.images && option.images.length > 0) {
Expand All @@ -139,31 +141,55 @@ export const deleteQuestion = async (req, res) => {
}
};


export const getAllQuestion = async (req, res) => {
try {
const queryObject = {};

// Handle standard, subject, and createdBy queries
if (req.query.standard) queryObject.standard = req.query.standard;
if (req.query.subject) queryObject.subject = req.query.subject;
if (req.query.chapter) queryObject.chapter = req.query.chapter;
if (req.query.topic) queryObject.topics = req.query.topic;
if (req.query.createdBy) queryObject.createdBy = req.query.createdBy;

// Handle multiple chapters
if (req.query.chapter) {
const chapters = Array.isArray(req.query.chapter)
? req.query.chapter
: req.query.chapter.split(',').map(ch => ch.trim());
queryObject.chapter = { $in: chapters };
}

// Handle single or multiple topics
if (req.query.topic) {
const topics = Array.isArray(req.query.topic)
? req.query.topic
: req.query.topic.split(',').map(tp => tp.trim());
queryObject.topics = { $in: topics };
}

// Handle subtopics
if (req.query.subtopic) {
const subtopics = Array.isArray(req.query.subtopic)
? req.query.subtopic
: req.query.subtopic.split(',').map(st => st.trim());
queryObject.subtopics = { $in: subtopics }
}

// Handle search query
if (req.query.search) {
const searchTerms = req.query.search.split(' ').filter(term => term !== '');

const searchRegex = searchTerms.map(term => new RegExp(term, 'i'));

queryObject.$and = [
{ $or: searchRegex.map(regex => ({ question: regex })) }
];
}

console.log(queryObject);
console.log(queryObject); // For debugging purposes

let formattedQuestions = [];

let formattedQuestions = []
if (req.user.role === "admin") {
let questionsData = Ques.find(queryObject).sort({ createdAt: -1 });
let questionsData = Ques.find(queryObject).sort({ createdAt: -1 });

let page = req.query.page || 1;
let limit = req.query.limit || 50;
Expand All @@ -180,7 +206,7 @@ export const getAllQuestion = async (req, res) => {

formattedQuestions = questions.map(question => ({
...question.toObject(),
nestedSubTopic: question.nestedSubTopic || ""
nestedSubTopic: question.nestedSubTopic || ""
}));
}

Expand Down Expand Up @@ -251,6 +277,7 @@ export const getAllQuestion = async (req, res) => {
},
});
} catch (error) {
console.error("Error in getAllQuestion:", error);
return res.status(500).json({
success: false,
message: error.message || "Internal Server Error",
Expand All @@ -263,58 +290,82 @@ export const getTotalQuestions = async (req, res) => {
const queryObject = {};
const userId = req.user._id;

// Handle standard and subject queries
if (req.query.standard) queryObject.standard = req.query.standard;
if (req.query.subject) queryObject.subject = req.query.subject;
if (req.query.chapter) queryObject.chapter = req.query.chapter;
if (req.query.topic) queryObject.topics = req.query.topic;
if (req.query.createdBy) queryObject.createdBy = req.query.createdBy;

// const myquestion = req.query.createdBy ? true : false;
// Handle multiple chapters
if (req.query.chapter) {
const chapters = Array.isArray(req.query.chapter)
? req.query.chapter
: req.query.chapter.split(',').map(chapter => chapter.trim());
queryObject.chapter = { $in: chapters };
}

// Handle single or multiple topics
if (req.query.topic) {
const topics = Array.isArray(req.query.topic)
? req.query.topic
: req.query.topic.split(',').map(topic => topic.trim());
queryObject.topics = { $in: topics };
}

// Handle subtopics
if (req.query.subtopic) {
const subtopics = Array.isArray(req.query.subtopic)
? req.query.subtopic
: req.query.subtopic.split(',').map(subtopic => subtopic.trim());

queryObject.subtopics = { $in: subtopics };
}

// Handle createdBy query
if (req.query.createdBy) queryObject.createdBy = req.query.createdBy;

// Handle search query
if (req.query.search) {
const searchTerms = req.query.search.split(' ').filter(term => term !== '');
const searchRegex = searchTerms.map(term => new RegExp(term, 'i'));

queryObject.$and = [{ $or: searchRegex.map(regex => ({ question: regex })) }];
}

// Handle mySearch query to search within user's created questions
if (req.query.mySearch) {
const searchTerms = req.query.mySearch.split(' ').filter(term => term !== '');
const searchRegex = searchTerms.map(term => new RegExp(term, 'i'));

queryObject.$and = [
{ $or: searchRegex.map(regex => ({ question: regex })) },
{ createdBy: userId },
];
}

console.log(queryObject);
console.log(queryObject); // For debugging purposes

// Get the total count of questions matching the query
const totalQuestions = await Ques.countDocuments(queryObject);

let myQuestions, questionsLength, fixedTotalQuestions, totalMyQuestions, totalMyPages, totalPages;
// Get the count of questions created by the user
const myQuestionsQueryObject = { ...queryObject, createdBy: userId };
const totalMyQuestions = await Ques.countDocuments(myQuestionsQueryObject);

// if (myquestion) {
const queryObjects = { ...queryObject, createdBy: userId };
myQuestions = await Ques.find(queryObjects);
questionsLength = myQuestions.length;
totalMyQuestions = await Ques.countDocuments({ createdBy: userId });
totalMyPages = Math.ceil(totalMyQuestions / req.query.questionsPerPage);

// Calculate total pages
const questionsPerPage = parseInt(req.query.questionsPerPage) || 10; // Set a default value if not provided
const totalPages = Math.ceil(totalQuestions / questionsPerPage);
const totalMyPages = Math.ceil(totalMyQuestions / questionsPerPage);

// const totalMyQuestions = await Ques.countDocuments({ createdBy: userId });
fixedTotalQuestions = await Ques.countDocuments({});
totalPages = Math.ceil(totalQuestions / req.query.questionsPerPage);
// Get the total count of all questions in the collection
const fixedTotalQuestions = await Ques.countDocuments({});

return res.status(200).json({
success: true,
totalQuestions: totalQuestions,
questionsLength: questionsLength,
// totalSearchQuestions: totalSearchQuestions,
fixedTotalQuestions: fixedTotalQuestions,
totalMyQuestions: totalMyQuestions,
fixedTotalQuestions: fixedTotalQuestions,
totalPages: totalPages,
totalMyPages: totalMyPages,
});
} catch (error) {
console.error("Error in getTotalQuestions:", error); // Added logging for debugging
return res.status(500).json({
success: false,
message: error.message || "Internal Server Error",
Expand All @@ -324,15 +375,15 @@ export const getTotalQuestions = async (req, res) => {

export const getMyQuestions = async (req, res) => {
try {
const userId = req.user._id;
const userId = req.user._id;

const queryObject = { createdBy: userId };

if (req.query.standard) queryObject.standard = req.query.standard;
if (req.query.subject) queryObject.subject = req.query.subject;
if (req.query.chapter) queryObject.chapter = req.query.chapter;
if (req.query.topic) queryObject.topics = req.query.topic;
if (req.query.search) {
if (req.query.search) {
// Split search query by spaces to handle multiple words
const searchTerms = req.query.search.split(' ').filter(term => term !== '');

Expand All @@ -351,7 +402,7 @@ export const getMyQuestions = async (req, res) => {

let page = req.query.page || 1;
let limit = req.query.limit || 50;

let skip = (page - 1) * limit;

questionsData = questionsData.skip(skip).limit(limit);
Expand Down Expand Up @@ -392,14 +443,14 @@ export const getMyQuestions = async (req, res) => {
// Determine the rank of the current user
const userRank = userCounts.findIndex(user => user.userId.toString() === userId.toString()) + 1;

res.status(200).json({
success: true,
res.status(200).json({
success: true,
questions: paginatedQuestions,
todaysQuestionsCount: todaysQuestions.length,
userRank: userRank,
totalQuestions: totalQuestions
});

} catch (error) {
console.error(error);
res.status(500).json({ success: false, message: "Server error" });
Expand Down Expand Up @@ -474,13 +525,13 @@ export const updateOption = async (req, res) => {

export const allUser = async (req, res) => {
try {
const users = await User.find({})
if(!users) return res.status(404).json({ error: "Users not found" });
const users = await User.find({})
if (!users) return res.status(404).json({ error: "Users not found" });

res.status(200).json({
res.status(200).json({
success: true,
users
});
});
} catch (error) {
console.error('Error fetching questions:', error);
res.status(500).json({ error: error.message });
Expand Down Expand Up @@ -543,3 +594,73 @@ export const updateQuestionDetails = async (req, res) => {
}
};



export const updateQuestionTopics = async (req, res) => {
try {
const { questionIds, topic, selectedTopic } = req.body;

// Ensure questionIds is an array
const ids = Array.isArray(questionIds) ? questionIds : [questionIds];

if (ids.length === 0) {
return res.status(400).json({ message: 'No question IDs provided.' });
}

const existingQuestions = await Ques.find({ _id: { $in: ids } });
if (existingQuestions.length === 0) {
return res.status(404).json({ message: 'No questions found with the provided IDs.' });
}

const updateOperations = existingQuestions.map(async (question) => {
let updateFields = {};

// Ensure topics is an array
question.topics = question.topics || [];

// Case 1: Add the new topic if no selected topic exists
if (topic && !selectedTopic) {
if (!question.topics.includes(topic.trim())) {
question.topics.push(topic.trim());
updateFields.topics = question.topics;
}
}

// Case 2: Replace the selected topic with the new one
if (topic && selectedTopic) {
const index = question.topics.indexOf(selectedTopic.trim());
if (index !== -1) {
question.topics[index] = topic.trim();
} else {
question.topics.push(topic.trim());
}
updateFields.topics = question.topics;
}

// Only update if there are changes
if (Object.keys(updateFields).length > 0) {
await Ques.updateOne({ _id: question._id }, { $set: updateFields });
}

return { questionId: question._id, updatedFields: updateFields };
});

const results = await Promise.all(updateOperations);

res.status(200).json({ message: 'Questions updated successfully.', updateOperations: results });
} catch (error) {
console.error('Error updating questions:', error);
res.status(500).json({ message: 'An error occurred while updating questions.' });
}
};











Loading