5bcecd01f9
All skydive routes now fetch users via UserService (MySQL) instead of
mongoose.model('User'). Key changes:
- user._id.toString() → user.id (same UUID, different source)
- User.findOne({ username }) → UserService.getUserByUsername()
- /feed route migrated to async/await using UserService.getUserFollowed()
to resolve the following list from the MySQL Follower table
- jump/file/application author set as user.id (UUID string) instead of
the full Mongoose document
- jump.toJSONFor(null) — following field in responses is now always false
until Jump model is migrated away from MongoDB User dependency
- user.controller: licence/poids/bg_image routed to SkydiverProfileService
fixing the silent Sequelize save bug for those fields
156 lines
5.6 KiB
JavaScript
156 lines
5.6 KiB
JavaScript
var router = require('express').Router(),
|
|
mongoose = require('mongoose'),
|
|
Qcm = mongoose.model('Qcm'),
|
|
QcmCategory = mongoose.model('QcmCategory'),
|
|
QcmQuestion = mongoose.model('QcmQuestion'),
|
|
QcmChoice = mongoose.model('QcmChoice');
|
|
const auth = require('../../../middlewares/auth');
|
|
const { UserService } = require('../../../services');
|
|
|
|
// Preload qcm objects on routes with ':type'
|
|
router.param('type', function (req, res, next, type) {
|
|
Qcm.findOne({ name: type })
|
|
.populate('categories')
|
|
.then(qcm => {
|
|
if (!qcm) {
|
|
return res.status(404).json({ errors: { "Not Found": "Le QCM est introuvable ou inexistant." } });
|
|
}
|
|
req.qcm = qcm;
|
|
Promise.all(
|
|
req.qcm.categories.map(category => {
|
|
return category.populate('questions').then(() => {
|
|
return Promise.all(
|
|
category.questions.map(question => question.populate('choices'))
|
|
).then(() => {
|
|
return category;
|
|
}).catch(next);
|
|
});
|
|
})
|
|
).then(() => {
|
|
return next();
|
|
}).catch(next);
|
|
}).catch(next);
|
|
});
|
|
|
|
/**
|
|
* return all qcm
|
|
*/
|
|
router.get('/', auth.required, function (req, res, next) {
|
|
Promise.all([
|
|
req.payload ? UserService.getUserById(req.payload.id) : null
|
|
]).then(function (results) {
|
|
let user = results[0];
|
|
if (!user) {
|
|
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
|
}
|
|
return res.json({ qcm: [] });
|
|
}).catch(next);
|
|
});
|
|
|
|
/**
|
|
* return a qcm
|
|
*/
|
|
router.get('/type/:type', auth.required, function (req, res, next) {
|
|
Promise.all([
|
|
req.payload ? UserService.getUserById(req.payload.id) : null
|
|
]).then(function (results) {
|
|
let user = results[0];
|
|
if (!user) {
|
|
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
|
}
|
|
return res.json({ qcm: req.qcm.toJSONFor() });
|
|
}).catch(next);
|
|
});
|
|
|
|
/**
|
|
* save an array of qcm question choice
|
|
*/
|
|
router.post('/choices/', auth.required, function (req, res, next) {
|
|
Promise.all([
|
|
req.payload ? UserService.getUserById(req.payload.id) : null,
|
|
QcmQuestion
|
|
.findOne({num: req.body.question.num})
|
|
.populate('choices')
|
|
.exec()
|
|
]).then(async function (results) {
|
|
const user = results[0];
|
|
if (!user) {
|
|
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
|
}
|
|
if (user.role !== 'Admin') {
|
|
return res.status(403).json({ errors: { "Forbidden": "Vous ne disposez pas des autorisations suffisantes" } });
|
|
}
|
|
const question = results[1];
|
|
if (!question) {
|
|
return res.status(422).json({ errors: { question: "question introuvable" } });
|
|
}
|
|
let choices = [];
|
|
Promise.all(
|
|
req.body.question.choices.map(data => {
|
|
const choiceId = new mongoose.Types.ObjectId();
|
|
const choice = new QcmChoice({ _id: choiceId, index: data.index, libelle: data.libelle, correct: data.correct});
|
|
choices.push(choiceId);
|
|
return choice.save();
|
|
})
|
|
).then(function () {
|
|
question.choices = [...choices];
|
|
question.save().then(function () {
|
|
QcmQuestion
|
|
.findOne({num: req.body.question.num})
|
|
.populate('choices')
|
|
.exec()
|
|
.then(function (question) {
|
|
return res.json({ question: question.toJSONFor() });
|
|
}).catch(next);
|
|
}).catch(next);
|
|
}).catch(next);
|
|
}).catch(next);
|
|
});
|
|
|
|
/**
|
|
* save an array of qcm category questions
|
|
*/
|
|
router.post('/questions/', auth.required, function (req, res, next) {
|
|
Promise.all([
|
|
req.payload ? UserService.getUserById(req.payload.id) : null,
|
|
QcmCategory
|
|
.findOne({num: req.body.category.num})
|
|
.populate('questions')
|
|
.exec()
|
|
]).then(async function (results) {
|
|
const user = results[0];
|
|
if (!user) {
|
|
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
|
}
|
|
if (user.role !== 'Admin') {
|
|
return res.status(403).json({ errors: { "Forbidden": "Vous ne disposez pas des autorisations suffisantes" } });
|
|
}
|
|
const category = results[1];
|
|
if (!category) {
|
|
return res.status(422).json({ errors: { category: "catégorie introuvable" } });
|
|
}
|
|
let questions = [];
|
|
Promise.all(
|
|
req.body.category.questions.map(data => {
|
|
const questionId = new mongoose.Types.ObjectId();
|
|
const question = new QcmQuestion({ _id: questionId, num: data.num, libelle: data.libelle, choices: []});
|
|
questions.push(questionId);
|
|
return question.save();
|
|
})
|
|
).then(function () {
|
|
category.questions = [...questions];
|
|
category.save().then(function () {
|
|
QcmCategory
|
|
.findOne({num: req.body.category.num})
|
|
.populate('questions')
|
|
.exec()
|
|
.then(function (category) {
|
|
return res.json({ category: category.toJSONFor() });
|
|
}).catch(next);
|
|
}).catch(next);
|
|
}).catch(next);
|
|
}).catch(next);
|
|
});
|
|
|
|
module.exports = router;
|