Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. exports.findOneAndCurrent = (req, res) => {
  2. Unit.findOne({
  3. where: {
  4. id: req.params.id,
  5. is_active: true
  6. },
  7. include: [{
  8. model: Question,
  9. as: 'questions',
  10. where: {
  11. unit_id: req.params.id,
  12. is_active: true
  13. },
  14. required: false,
  15. }]
  16. })
  17. .then(unit => {
  18. if (unit) {
  19. const questionIds = unit.questions.map(question => question.id);
  20. Activity.findAll({
  21. limit: 1,
  22. where: {
  23. question_id: { [Op.or]: [questionIds] },
  24. finish_time: null
  25. },
  26. order: [ [ 'id', 'ASC' ]]
  27. })
  28. .then(unfinishedActivity => {
  29. let current_question = unit.questions.length > 0 ? unit.questions[0] : null;
  30. if (unfinishedActivity !== null && unfinishedActivity.length > 0) {
  31. current_question = unit.questions.filter(question => question.id === unfinishedActivity[0].question_id)[0];
  32. }
  33. res.json({...unit.dataValues, current_question});
  34. })
  35. } else {
  36. res.status(404).json({
  37. success: false,
  38. msg: "No unit with this ID found!"
  39. });
  40. }
  41. })
  42. .catch(err => {
  43. console.log(err);
  44. res.status(500).json({
  45. success: false,
  46. msg: "Something went wrong: " + err
  47. });
  48. });
  49. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement