Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exports.findOneAndCurrent = (req, res) => {
- // First, get which student to base the query on
- const studentId = req.user.studentId
- ? req.user.studentId
- : req.query.student
- ? req.query.student
- : null;
- if (studentId === null) {
- res.status(404).json({
- success: false,
- msg: "No student_id provided!"
- });
- }
- Task.findOne({
- where: {
- student_id: studentId,
- unit_id: req.params.id,
- is_active: true
- },
- include: [{
- model: Unit,
- where: {
- id: req.params.id,
- is_active: true
- },
- include: [{
- model: Question,
- as: 'questions',
- where: {
- unit_id: req.params.id,
- is_active: true
- },
- order: [['id', 'ASC']],
- required: false
- }]
- }],
- required: true
- })
- .then(task => {
- if (task === null) {
- res.status(400).json({
- success: false,
- msg: "This student is not assigned a task with this unit!"
- });
- }
- const questionIds = task.Unit.questions.map(question => question.id);
- Activity.findAll({
- where: {
- question_id: { [Op.or]: [questionIds] },
- student_id: studentId,
- finish_time: {
- [Op.ne]: null
- }
- },
- order: [['question_id', 'ASC' ]]
- })
- .then(activities => {
- let questionIdsCopy = questionIds;
- activities.forEach(activity => {
- const index = questionIdsCopy.indexOf(activity.question_id);
- if (index > -1) questionIdsCopy.splice(index, 1);
- });
- const current_question = questionIdsCopy.length > 0
- ? task.Unit.questions[questionIds.indexOf(questionIdsCopy[0])]
- : null;
- res.json({...task.Unit.dataValues, current_question});
- })
- })
- .catch(err => {
- console.log(err);
- res.status(500).json({
- success: false,
- msg: "Something went wrong: " + err
- });
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement