2024-03-29 20:08:34 -04:00
|
|
|
const emailer = require('../config/email.js');
|
|
|
|
|
|
|
|
|
|
const AppError = require('../middleware/AppError.js');
|
2024-04-05 20:10:59 -04:00
|
|
|
const { MISSING_REQUIRED_PARAMETER, NOT_IMPLEMENTED, QUIZ_NOT_FOUND, FOLDER_NOT_FOUND, QUIZ_ALREADY_EXISTS, GETTING_QUIZ_ERROR, DELETE_QUIZ_ERROR, UPDATE_QUIZ_ERROR, MOVING_QUIZ_ERROR, DUPLICATE_QUIZ_ERROR, COPY_QUIZ_ERROR } = require('../constants/errorCodes');
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
class QuizController {
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
constructor(quizModel, foldersModel) {
|
|
|
|
|
this.folders = foldersModel;
|
|
|
|
|
console.log("QuizController constructor: folders", this.folders);
|
|
|
|
|
this.quizzes = quizModel;
|
|
|
|
|
console.log("QuizController constructor: quizzes", this.quizzes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
create = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { title, content, folderId } = req.body;
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!title || !content || !folderId) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
// Is this folder mine
|
2024-10-02 10:23:56 -04:00
|
|
|
const owner = await this.folders.getOwner(folderId);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (owner != req.user.userId) {
|
|
|
|
|
throw new AppError(FOLDER_NOT_FOUND);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
|
|
|
|
const result = await this.quizzes.create(title, content, folderId, req.user.userId);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!result) {
|
|
|
|
|
throw new AppError(QUIZ_ALREADY_EXISTS);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Quiz créé avec succès.'
|
|
|
|
|
});
|
2024-10-02 10:23:56 -04:00
|
|
|
|
|
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
get = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { quizId } = req.params;
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!quizId) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
|
|
|
|
const content = await this.quizzes.getContent(quizId);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!content) {
|
|
|
|
|
throw new AppError(GETTING_QUIZ_ERROR);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
// Is this quiz mine
|
|
|
|
|
if (content.userId != req.user.userId) {
|
|
|
|
|
throw new AppError(QUIZ_NOT_FOUND);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return res.status(200).json({
|
|
|
|
|
data: content
|
|
|
|
|
});
|
2024-10-02 10:23:56 -04:00
|
|
|
|
|
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
delete = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { quizId } = req.params;
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!quizId) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
// Is this quiz mine
|
2024-10-02 10:23:56 -04:00
|
|
|
const owner = await this.quizzes.getOwner(quizId);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (owner != req.user.userId) {
|
|
|
|
|
throw new AppError(QUIZ_NOT_FOUND);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
|
|
|
|
const result = await this.quizzes.delete(quizId);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!result) {
|
|
|
|
|
throw new AppError(DELETE_QUIZ_ERROR);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Quiz supprimé avec succès.'
|
|
|
|
|
});
|
2024-10-02 10:23:56 -04:00
|
|
|
|
|
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
update = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { quizId, newTitle, newContent } = req.body;
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!newTitle || !newContent || !quizId) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
// Is this quiz mine
|
2024-10-02 10:23:56 -04:00
|
|
|
const owner = await this.quizzes.getOwner(quizId);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (owner != req.user.userId) {
|
|
|
|
|
throw new AppError(QUIZ_NOT_FOUND);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
|
|
|
|
const result = await this.quizzes.update(quizId, newTitle, newContent);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!result) {
|
|
|
|
|
throw new AppError(UPDATE_QUIZ_ERROR);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Quiz mis à jours avec succès.'
|
|
|
|
|
});
|
2024-10-02 10:23:56 -04:00
|
|
|
|
|
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
move = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { quizId, newFolderId } = req.body;
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!quizId || !newFolderId) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
// Is this quiz mine
|
2024-10-02 10:23:56 -04:00
|
|
|
const quizOwner = await this.quizzes.getOwner(quizId);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (quizOwner != req.user.userId) {
|
|
|
|
|
throw new AppError(QUIZ_NOT_FOUND);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
// Is this folder mine
|
2024-10-02 10:23:56 -04:00
|
|
|
const folderOwner = await this.folders.getOwner(newFolderId);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (folderOwner != req.user.userId) {
|
|
|
|
|
throw new AppError(FOLDER_NOT_FOUND);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
|
|
|
|
const result = await this.quizzes.move(quizId, newFolderId);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!result) {
|
|
|
|
|
throw new AppError(MOVING_QUIZ_ERROR);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Utilisateur déplacé avec succès.'
|
|
|
|
|
});
|
2024-10-02 10:23:56 -04:00
|
|
|
|
|
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
};
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
copy = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
const { quizId, newTitle, folderId } = req.body;
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!quizId || !newTitle || !folderId) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
throw new AppError(NOT_IMPLEMENTED);
|
|
|
|
|
// const { quizId } = req.params;
|
|
|
|
|
// const { newUserId } = req.body;
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
// try {
|
|
|
|
|
// //Trouver le quiz a dupliquer
|
|
|
|
|
// const conn = db.getConnection();
|
2024-10-02 20:23:15 -04:00
|
|
|
// const quiztoduplicate = await conn.collection('quiz').findOne({ _id: ObjectId.createFromTime(quizId) });
|
2024-03-29 20:08:34 -04:00
|
|
|
// if (!quiztoduplicate) {
|
|
|
|
|
// throw new Error("Quiz non trouvé");
|
|
|
|
|
// }
|
|
|
|
|
// console.log(quiztoduplicate);
|
|
|
|
|
// //Suppression du id du quiz pour ne pas le répliquer
|
|
|
|
|
// delete quiztoduplicate._id;
|
|
|
|
|
// //Ajout du duplicata
|
2024-10-02 20:23:15 -04:00
|
|
|
// await conn.collection('quiz').insertOne({ ...quiztoduplicate, userId: ObjectId.createFromTime(newUserId) });
|
2024-03-29 20:08:34 -04:00
|
|
|
// res.json(Response.ok("Dossier dupliqué avec succès pour un autre utilisateur"));
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
// } catch (error) {
|
|
|
|
|
// if (error.message.startsWith("Quiz non trouvé")) {
|
|
|
|
|
// return res.status(404).json(Response.badRequest(error.message));
|
|
|
|
|
// }
|
|
|
|
|
// res.status(500).json(Response.serverError(error.message));
|
|
|
|
|
// }
|
2024-10-02 10:23:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
deleteQuizzesByFolderId = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { folderId } = req.body;
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!folderId) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
// Call the method from the Quiz model to delete quizzes by folder ID
|
|
|
|
|
await Quiz.deleteQuizzesByFolderId(folderId);
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Quizzes deleted successfully.'
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return next(error);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
duplicate = async (req, res, next) => {
|
|
|
|
|
const { quizId } = req.body;
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
2024-10-02 10:23:56 -04:00
|
|
|
const newQuizId = await this.quizzes.duplicate(quizId, req.user.userId);
|
2024-03-29 20:08:34 -04:00
|
|
|
res.status(200).json({ success: true, newQuizId });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return next(error);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
quizExists = async (title, userId) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
2024-10-02 10:23:56 -04:00
|
|
|
const existingFile = await this.quizzes.quizExists(title, userId);
|
2024-03-29 20:08:34 -04:00
|
|
|
return existingFile !== null;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new AppError(GETTING_QUIZ_ERROR);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
share = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { quizId, email } = req.body;
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
if (!quizId || !email) {
|
2024-03-29 20:08:34 -04:00
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
2024-10-02 10:23:56 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
const link = `${process.env.FRONTEND_URL}/teacher/Share/${quizId}`;
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
emailer.quizShare(email, link);
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Quiz partagé avec succès.'
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
};
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
getShare = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { quizId } = req.params;
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
if (!quizId) {
|
2024-03-29 20:08:34 -04:00
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
2024-10-02 10:23:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const content = await this.quizzes.getContent(quizId);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!content) {
|
|
|
|
|
throw new AppError(GETTING_QUIZ_ERROR);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return res.status(200).json({
|
|
|
|
|
data: content.title
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
receiveShare = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { quizId, folderId } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!quizId || !folderId) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
|
|
|
|
|
const folderOwner = await this.folders.getOwner(folderId);
|
2024-03-29 20:08:34 -04:00
|
|
|
if (folderOwner != req.user.userId) {
|
|
|
|
|
throw new AppError(FOLDER_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
const content = await this.quizzes.getContent(quizId);
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!content) {
|
|
|
|
|
throw new AppError(GETTING_QUIZ_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
const result = await this.quizzes.create(content.title, content.content, folderId, req.user.userId);
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!result) {
|
|
|
|
|
throw new AppError(QUIZ_ALREADY_EXISTS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Quiz partagé reçu.'
|
|
|
|
|
});
|
2024-10-02 10:23:56 -04:00
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
2024-10-02 10:23:56 -04:00
|
|
|
};
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
module.exports = QuizController;
|