Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exports.findOneAndCurrent = (req, res) => {
- Unit.findOne({
- where: {
- id: req.params.id,
- is_active: true
- },
- include: [{
- model: Question,
- as: 'questions',
- where: {
- unit_id: req.params.id,
- is_active: true
- },
- required: false,
- }]
- })
- .then(unit => {
- if (unit) {
- const questionIds = unit.questions.map(question => question.id);
- Activity.findAll({
- limit: 1,
- where: {
- question_id: { [Op.or]: [questionIds] },
- finish_time: null
- },
- order: [ [ 'id', 'ASC' ]]
- })
- .then(unfinishedActivity => {
- let current_question = unit.questions.length > 0 ? unit.questions[0] : null;
- if (unfinishedActivity !== null && unfinishedActivity.length > 0) {
- current_question = unit.questions.filter(question => question.id === unfinishedActivity[0].question_id)[0];
- }
- res.json({...unit.dataValues, current_question});
- })
- } else {
- res.status(404).json({
- success: false,
- msg: "No unit with this ID found!"
- });
- }
- })
- .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