const express = require('express'); const crypto = require('crypto'); const path = require('path'); const Question = require('../../models/QuestionsModel'); const Room = require('../../models/RoomModel'); const RoomRouter = express.Router(); //JOIN or CREATE team button page RoomRouter .route('/enter') .get((req,res)=>{res.sendFile(path.join(__dirname,'../Public',"room.html"))}) .post(async (req,res)=>{ try { let data=req.body; let room=await Room.create(data) res.json({ room:room }); } catch (error) { res.status(500).json({ message:error.message }) } }) RoomRouter .route('/divide') .get((req,res)=>{res.sendFile(path.join(__dirname,'../Public',"divideQs.html"))}) .post(async(req,res)=>{ try { let round=req.body.Round;//current round let qsns=req.body.Questions; //maxm questions in each room //Round1 rooms const rooms=await Room.find({Round:round}) for(let i in rooms){ //for first round lets say limit = 8 ( 5 + 3 extra) const questions=await Question.find({Round:round,assigned:false}).limit(qsns) for(let j in questions){ questions[j].assigned=true; await questions[j].save(); rooms[i].Questions.push(questions[j]._id) } await rooms[i].save(); } res.send("done") } catch (error) { res.status(500).json({ message:error.message, status:false }) } } ) module.exports = RoomRouter;