SHARE
TWEET

Untitled

a guest Mar 14th, 2019 240 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* jslint node: true */
  2. 'use strict'
  3.  
  4. const sequelizeInit = require('./sequelize.js')
  5. const sequelize = sequelizeInit.initSequelize()
  6. const bcrypt = require('bcrypt')
  7.  
  8. const User = require('./models/userModel.js')
  9. const Role = require('./models/roleModel.js')
  10. const Course = require('./models/courseModel.js')
  11. const Queue = require('./models/queueModel.js')
  12.  
  13. var Users
  14. var Roles
  15. var StudentStatistics
  16. var CourseStatistics
  17. var Courses
  18. var Queues
  19.  
  20. const courses = []
  21. const users = []
  22. const roles = []
  23. const queues = []
  24.  
  25. // creating sequelize models
  26. exports.initDatabase = () => {
  27.   // sequelizeInit.autoSequelize()
  28.   initConstants()
  29. }
  30. function initConstants () {
  31.   Users = sequelize.import('./seq_models/users1.js')
  32.   Roles = sequelize.import('./seq_models/roles1.js')
  33.   StudentStatistics = sequelize.import('./seq_models/studentStatistics1.js')
  34.   CourseStatistics = sequelize.import('./seq_models/courseStatistics1.js')
  35.   Courses = sequelize.import('./seq_models/courses1.js')
  36.   Queues = sequelize.import('./seq_models/queues1.js')
  37.  
  38.   linkModels()
  39.   fillCache()
  40. }
  41.  
  42. function linkModels () {
  43.   Queues.belongsTo(Users, { foreignKey: 'student' })
  44.   Queues.belongsTo(Courses, { foreignKey: 'courseID' })
  45.   Roles.belongsTo(Users, { foreignKey: 'ID' })
  46.   Roles.belongsTo(Courses, { foreignKey: 'courseID' })
  47.   console.log('linked')
  48. }
  49.  
  50. function fillCache () {
  51.   fillUsers()
  52.   fillRoles()
  53.   fillCourses()
  54.   fillQueues()
  55. }
  56.  
  57. // fill User fillCache
  58. // works
  59. function fillUsers () {
  60.   Users.findAll().then(res => {
  61.     for (let user of res) {
  62.       users.push(new User({
  63.         ID: user.ID,
  64.         name: user.name,
  65.         email: user.email,
  66.         password: user.password
  67.       })
  68.       )
  69.     }
  70.   })
  71. }
  72.  
  73. // fill roles cache
  74. // works
  75. function fillRoles () {
  76.   Roles.findAll({
  77.     include: Users
  78.   }).then(res => {
  79.     for (let role of res) {
  80.       roles.push(new Role(
  81.         {
  82.           ID: role.ID,
  83.           courseID: role.courseID,
  84.           role: role.role
  85.  
  86.         })
  87.       )
  88.     }
  89.   })
  90. }
  91.  
  92. function fillCourses () {
  93.   Courses.findAll().then(res => {
  94.     for (let course of res) {
  95.       courses.push(new Course({
  96.         ID: course.ID,
  97.         name: course.name,
  98.         open: course.dataValues.open === 1
  99.       }))
  100.  
  101.       queues.push(new Queue({
  102.         ID: course.ID,
  103.         name: course.name,
  104.         queue: [],
  105.         open: false
  106.       }))
  107.     }
  108.   })
  109. }
  110.  
  111. function fillQueues () {
  112.   Queues.findAll({
  113.     include: [
  114.       { model: Users },
  115.       { model: Courses }
  116.     ]
  117.  
  118.   }).then(res => {
  119.     for (let queue of res) {
  120.       // adds students in queue to given course
  121.       var queueItem = {
  122.         studentID: queue.student,
  123.         studentName: queue.users1.name,
  124.         task: queue.task,
  125.         location: queue.location,
  126.         comment: queue.comment,
  127.         queueNo: queue.queueNo,
  128.         enterTime: new Date(queue.enterTime), // √§ndrad
  129.         receivingHelp: false
  130.       }
  131.  
  132.       appendToQueue(queue.courses1.ID, queueItem)
  133.     }
  134.   }).catch(error => {
  135.     console.error(error)
  136.   })
  137. }
  138.  
  139. function appendToQueue (ID, queueItem) {
  140.   for (let i = 0; i < queues.length; i++) {
  141.     if (queues[i].ID === ID) {
  142.       queues[i].queue.push(queueItem)
  143.     }
  144.   }
  145. }
  146. // works
  147. exports.queueLockSwitch = (ID) => {
  148.   let open = this.getCourse(ID).open
  149.  
  150.   if (open === false) {
  151.     open = 1
  152.   } else {
  153.     open = 0
  154.   }
  155.  
  156.   this.getCourse(ID).open = !this.getCourse(ID).open
  157.  
  158.   Courses.update(
  159.     { open: open },
  160.     { where: { ID: ID } }
  161.   ).catch(err => console.error(err))
  162.  
  163.   return this.getCourse(ID).open
  164. }
  165.  
  166. // works
  167. exports.deleteCourse = (ID) => {
  168.   courses.splice(this.getCourseIndex(ID), 1)
  169.  
  170.   Courses.destroy({
  171.     where: {
  172.       ID: ID
  173.     }
  174.   }).catch(err => { console.error(err) })
  175. }
  176. // works
  177. exports.getCourseIndex = (ID) => {
  178.   for (var i = 0; i < courses.length; i++) {
  179.     if (courses[i].ID === ID) {
  180.       return i
  181.     }
  182.   }
  183.   return null
  184. }
  185. // works
  186. exports.getCourse = (ID) => {
  187.   for (let course of courses) {
  188.     if (course.ID === ID) {
  189.       return course
  190.     }
  191.   }
  192.   return null
  193. }
  194.  
  195. exports.getCoursesBelongingTo = (email) => {
  196.   console.log('In Model: getcoursesbelonging to: ', email)
  197.   let myCourses = []
  198.   let userID = this.findIDfromEmail(email)
  199.  
  200.   for (let r of roles) {
  201.     if (userID === r.ID) {
  202.       if (myCourses.some(e => e.courseID === r.courseID)) {
  203.  
  204.       } else {
  205.         let currentCourse = this.getCourse(r.courseID)
  206.         console.log('currentCourse in model is ', this.getCourse(r.courseID))
  207.  
  208.         /*
  209.          * bug hotfix. Should only be temporary. currentCourse is currently null
  210.          * because it wasn't added properly
  211.          */
  212.         if (currentCourse != null) {
  213.           myCourses.push({
  214.             CourseID: currentCourse.ID,
  215.             key: currentCourse.name,
  216.             open: currentCourse.open,
  217.             role: r.role
  218.           })
  219.         }
  220.       }
  221.     }
  222.   }
  223.  
  224.   return myCourses
  225. }
  226.  
  227. exports.getQueueByCourseID = (ID) => {
  228.   for (let q of queues) {
  229.     if (q.ID === ID) {
  230.       return q
  231.     }
  232.   }
  233.   return null
  234. }
  235.  
  236. exports.getQueue = (courseID) => {
  237.   return this.getQueueByCourseID(courseID)
  238. }
  239.  
  240. exports.getCourses = () => {
  241.   return courses
  242. }
  243.  
  244. exports.check = (req, emit) => {
  245.   // req = JSON.parse(req)
  246.   console.log('checking from model', req.username)
  247.  
  248.   for (let user of users) {
  249.     if (user.email === req.username) {
  250.       console.log('User found')
  251.  
  252.       let userID = this.findIDfromEmail(req.username)
  253.  
  254.       if (this.getHighestRole(userID) < req.role) {
  255.         console.log('Too low authority')
  256.         return null
  257.       } else {
  258.         verifyPassword(req.password, user.password, emit)
  259.       }
  260.     }
  261.   }
  262. }
  263.  
  264. // works
  265. exports.findIDfromEmail = (email) => {
  266.   for (let user of users) {
  267.     if (user.email === email) {
  268.       return user.ID
  269.     }
  270.   }
  271. }
  272.  
  273. // works
  274. exports.getHighestRole = (userID) => {
  275.   let highest = 0
  276.   for (let role of roles) {
  277.     if (role.ID === userID && role.role > highest) {
  278.       highest = role.role
  279.       if (highest === 2) {
  280.         return highest
  281.       }
  282.     }
  283.   }
  284.  
  285.   return highest
  286. }
  287.  
  288. // wroks
  289. exports.updateQueueItem = (courseID, queueItemID) => {
  290.   console.log('in model updateQueueItem: ', courseID, queueItemID)
  291.   for (let queue of queues) {
  292.     if (queue.ID === courseID) {
  293.       for (let student of queue.queue) {
  294.         if (student.queueNo === queueItemID) {
  295.           student.receivingHelp = !student.receivingHelp
  296.           incrementTimesHelped(student.studentID)
  297.           break
  298.         }
  299.       }
  300.     }
  301.   }
  302. }
  303.  
  304. // wokrs
  305. exports.removeQueueItem = (courseID, queueItemID, studentID, callback) => {
  306.   for (let i = 0; i < queues.length; i++) {
  307.     if (queues[i].ID === courseID) {
  308.       for (let j = 0; j < queues[i].queue.length; j++) {
  309.         if (queues[i].queue[j].queueNo === queueItemID) {
  310.           incrementTimeSpentInQueues(queues[i].queue[j].studentID, queues[i].ID, queues[i].queue[j].enterTime)
  311.           queues[i].queue.splice(j, 1)
  312.  
  313.           Queues.destroy({
  314.             where: { courseID: courseID, student: studentID }
  315.           }).catch(err => { console.error(err) })
  316.           break
  317.         }
  318.       }
  319.     }
  320.   }
  321.   callback(true)
  322. }
  323.  
  324. // OLD belonging to alex
  325.  
  326. exports.createNewCourse = (courseObject, callback) => {
  327.   let errorMessage = ''
  328.   let success = true
  329.   let managers = courseObject.managers.split(',')
  330.   for (let i = 0; i < managers.length; i++) {
  331.     managers[i] = managers[i].trim()
  332.   }
  333.  
  334.   let assistants = courseObject.assistants.split(',')
  335.   for (let i = 0; i < assistants.length; i++) {
  336.     assistants[i] = assistants[i].trim()
  337.   }
  338.  
  339.   if (assistants.length === 1 && assistants[0] === '') {
  340.     assistants = []
  341.     console.log('Assistants is empty.')
  342.   }
  343.   let confirmedAssistants = []
  344.   let confirmedManagers = []
  345.  
  346.   for (let i = 0; i < users.length; i++) {
  347.     let managerIndex = managers.indexOf(users[i].name)
  348.     let assistantIndex = assistants.indexOf(users[i].name)
  349.  
  350.     console.log('usersName:', users[i].name)
  351.     if (managerIndex !== -1) {
  352.       confirmedManagers.push(users[i].name)
  353.       managers.splice(managerIndex, 1)
  354.     }
  355.     if (assistantIndex !== -1) {
  356.       confirmedAssistants.push(users[i].name)
  357.       assistants.splice(assistantIndex)
  358.     }
  359.   }
  360.  
  361.   for (let i = 0; i < courses.length; i++) {
  362.     if (courses[i].name === courseObject.courseName) {
  363.       errorMessage += '\nCourse with name ' + courseObject.courseName + ' already exists.'
  364.       success = false
  365.     }
  366.     if (courses[i].ID === courseObject.courseID) {
  367.       errorMessage += '\nCourse with ID ' + courseObject.courseID + ' already exists.'
  368.       success = false
  369.     }
  370.   }
  371.  
  372.   if (assistants.length > 0) {
  373.     success = false
  374.     errorMessage += '\nThe following assistants were not found: ' + assistants
  375.   }
  376.  
  377.   if (managers.length > 0) {
  378.     success = false
  379.     errorMessage += '\nThe following managers were not found: ' + managers
  380.   }
  381.   console.log(errorMessage)
  382.   console.log('Success? ', success)
  383.  
  384.   if (success) {
  385.     courses.push(
  386.       {
  387.         name: courseObject.courseName,
  388.         ID: courseObject.courseID,
  389.         open: false
  390.       }
  391.     )
  392.  
  393.     Courses.create({
  394.       ID: courseObject.courseID,
  395.       name: courseObject.courseName,
  396.       open: 0
  397.     }).then(res => {
  398.       CourseStatistics.create({
  399.         courseID: res.ID
  400.       })
  401.     }).catch(err => { console.error(err) })
  402.       .catch(err => { console.error(err) })
  403.  
  404.     for (let asse of confirmedAssistants) {
  405.       let ID = findIDofUser(asse)
  406.  
  407.       roles.push({
  408.         ID: ID,
  409.         courseID: courseObject.courseID,
  410.         role: 1
  411.       })
  412.  
  413.       Roles.create({
  414.         ID: ID,
  415.         courseID: courseObject.courseID,
  416.         role: 1 }).catch(err => { console.error(err) })
  417.     }
  418.  
  419.     for (let manager of confirmedManagers) {
  420.       let ID = findIDofUser(manager)
  421.       roles.push({
  422.         ID: ID,
  423.         courseID: courseObject.courseID,
  424.         role: 1
  425.       })
  426.  
  427.       Roles.create({
  428.         ID: ID,
  429.         courseID: courseObject.courseID,
  430.         role: 2
  431.       }).catch(err => { console.error(err) })
  432.     }
  433.   }
  434.  
  435.   callback({ success: success, err: errorMessage })
  436. }
  437.  
  438. exports.sortQueueByTime = (courseID) => {
  439.   let q = this.getQueueByCourseID(courseID)
  440.   let sortedQueue = q.queue
  441.   let result = q.queue.sort(function (a, b) {
  442.     new Date(a.time) - new Date(b.time)
  443.   })
  444. }
  445.  
  446. function findIDofUser (name) {
  447.   for (let i = 0; i < users.length; i++) {
  448.     if (name === users[i].name) {
  449.       return users[i].ID
  450.     }
  451.   }
  452.   return -1
  453. }
  454.  
  455. exports.addQueueItem = (req, callback) => {
  456.   console.log('in model, addQueueItem, received ', req)
  457.  
  458.   // find right queue
  459.   for (let queue of queues) {
  460.     if (queue.ID === req.CourseID) {
  461.       let time = new Date()
  462.  
  463.       let studentID = req.queueObject.studentID
  464.       console.log(studentID)
  465.  
  466.       // alex saknar
  467.  
  468.       Queues.create({
  469.         courseID: req.CourseID,
  470.         student: studentID,
  471.         task: req.queueObject.request,
  472.         location: req.queueObject.location,
  473.         comment: req.queueObject.comment,
  474.         queueNo: null,
  475.         enterTime: time
  476.       }).then(res => {
  477.         let queueItem = {
  478.           studentID: studentID,
  479.           studentName: req.queueObject.name,
  480.           task: res.task, // √§ndrad
  481.           location: res.location,
  482.           comment: res.comment,
  483.           queueNo: res.queueNo,
  484.           enterTime: res.enterTime,
  485.           receivingHelp: false
  486.         }
  487.         appendToQueue(queue.ID, queueItem)
  488.         callback({ success: true, queueObject: queueItem })
  489.         incrementTimesQueued(res.student, res.courseID)
  490.       }).catch(err => console.error(err))
  491.       break
  492.     }
  493.   }
  494. }
  495.  
  496. exports.registerNewUser = (userObject, emit) => {
  497.   checkAccounts(userObject.username, (success) => {
  498.     if (success) {
  499.       createPasswordHash(userObject.password, userObject.username.length, (hashPassword) => {
  500.         Users.create({
  501.  
  502.           ID: NaN,
  503.           name: userObject.username,
  504.           email: userObject.email,
  505.           password: hashPassword
  506.  
  507.         }).then(res => {
  508.           users.push(new User({
  509.  
  510.             ID: res.ID,
  511.             name: res.name,
  512.             email: r.users1.email,
  513.             password: res.password
  514.  
  515.           })
  516.           )
  517.           StudentStatistics.create({
  518.             ID: res.ID,
  519.             totTimeSpentInQueue: new Date('1000-01-01T00:00:00Z')
  520.           })
  521.         })
  522.  
  523.         console.log('Registered new user', users)
  524.         emit('Your account is successfully registered')
  525.       })
  526.     } else {
  527.       emit('Failure to register user: Username already taken')
  528.     }
  529.   })
  530. }
  531.  
  532. function checkAccounts (username, cb) {
  533.   let success = true
  534.  
  535.   for (let account of users) {
  536.     if (account != null) {
  537.       if (account.name === username) {
  538.         console.log('username already exists')
  539.         success = false
  540.       }
  541.     }
  542.   }
  543.   console.log('username available')
  544.   cb(success)
  545. }
  546.  
  547. function createPasswordHash (plain, saltRound, store) {
  548.   bcrypt.genSalt(saltRound).then(salt => {
  549.     return bcrypt.hash(plain, salt)
  550.   }).then(hash => {
  551.     store(hash)
  552.   }).catch(err => { console.error(err) })
  553. }
  554.  
  555. function verifyPassword (test, hash, callback) {
  556.   bcrypt.compare(test, hash).then(res => {
  557.     console.log('verified:', res)
  558.     callback(res)
  559.   }).catch(err => { console.error(err) })
  560. }
  561.  
  562. exports.editCourse = (request, callback) => {
  563.   let errorMessage = ''
  564.   let success = true
  565.  
  566.   let courseID = request.CourseID
  567.  
  568.   if (this.getCourse(courseID) === null) {
  569.     success = false
  570.     errorMessage = 'Course with id ' + courseID + ' does not exist.'
  571.   }
  572.  
  573.   let assistants = request.assistants.split(',')
  574.   let managers = request.managers.split(',')
  575.   for (let i = 0; i < managers.length; i++) {
  576.     managers[i] = managers[i].trim()
  577.   }
  578.   for (let i = 0; i < assistants.length; i++) {
  579.     assistants[i] = assistants[i].trim()
  580.   }
  581.  
  582.   if (managers.length === 1 && managers[0] === '') {
  583.     managers = []
  584.     success = false
  585.     errorMessage += '\nTried removing all managers. Each course needs a manager.'
  586.   }
  587.   if (assistants.length === 1 && assistants[0] === '') {
  588.     assistants = []
  589.   }
  590.  
  591.   console.log('received managers: ', managers)
  592.   console.log('received assistants: ', assistants)
  593.  
  594.   if (assistants.length === 1 && assistants[0] === '') {
  595.     assistants = []
  596.     console.log('Assistants is empty.')
  597.   }
  598.   let confirmedAssistants = []
  599.   let confirmedManagers = []
  600.  
  601.   for (let i = 0; i < users.length; i++) {
  602.     let managerIndex = managers.indexOf(users[i].email)
  603.     let assistantIndex = assistants.indexOf(users[i].email)
  604.  
  605.     console.log('userEmail:', users[i].email)
  606.     if (managerIndex !== -1) {
  607.       confirmedManagers.push(users[i].email)
  608.       managers.splice(managerIndex, 1)
  609.     }
  610.     if (assistantIndex !== -1) {
  611.       confirmedAssistants.push(users[i].email)
  612.       assistants.splice(assistantIndex)
  613.     }
  614.   }
  615.  
  616.   if (assistants.length > 0) {
  617.     success = false
  618.     errorMessage += '\nThe following assistants were not found: ' + assistants
  619.   }
  620.  
  621.   if (managers.length > 0) {
  622.     success = false
  623.     errorMessage += '\nThe following managers were not found: ' + managers
  624.   }
  625.   console.log(errorMessage)
  626.   console.log('Success? ', success)
  627.   let resultObject = {
  628.     success: success,
  629.     err: errorMessage
  630.   }
  631.   callback(resultObject)
  632.  
  633.   if (success) {
  634.     console.log('COURSEID FROM REQUEST', request.CourseID)
  635.     Roles.findAll({
  636.       // attributes: ['users1.ID', 'users1.email', 'roles1.role'],
  637.       where: { courseID: request.CourseID },
  638.       include: {
  639.         model: Users
  640.       }
  641.  
  642.     }).then(res => {
  643.       // for (let r of res) {
  644.       //   console.log(r.role, r.users1.email)
  645.       //   let assoLoop=1
  646.       //   let mngrLoop=1
  647.       //   for (let role of roles) {
  648.       //     for (let email of confirmedAssistants) {
  649.       //
  650.       //       console.log('ASS email', email, assoLoop)
  651.       //       if (email === r.users1.email) {
  652.       //         console.log('ASSE, email=email')
  653.       //         if (r.role !== 1) {
  654.       //           console.log('ASSE, role!=role')
  655.       //           Roles.update({
  656.       //             role: 1,
  657.       //             where: { ID: res.users1.ID }
  658.       //           }).then(() => {
  659.       //             role.role = 1
  660.       //           })
  661.       //         }
  662.       //       } else {
  663.       //         console.log('ASSE, inget lika')
  664.       //         Roles.create({
  665.       //           ID: r.users1.ID,
  666.       //           courseID: request.CourseID,
  667.       //           role: 1
  668.       //         }).then(res2 => {
  669.       //
  670.       //           roles.push(new Role({
  671.       //             ID: res2.users1.ID,
  672.       //             courseID: request.CourseID,
  673.       //             role: 1
  674.       //           }))
  675.       //         })
  676.       //       }
  677.       //       assoLoop +=1
  678.       //     }
  679.       //
  680.       //     for (let email of confirmedManagers) {
  681.       //       console.log('MNGR email', email, mngrLoop)
  682.       //       if (email === r.users1.email) {
  683.       //         console.log('MNGR, email=email')
  684.       //         if (r.role !== 2) {
  685.       //           console.log('MNGR, role!=role')
  686.       //           Roles.update({
  687.       //             role: 2,
  688.       //             where: { ID: res.users1.ID }
  689.       //           }).then(() => {
  690.       //             role.role = 2
  691.       //           })
  692.       //         } else {
  693.       //           console.log('MNGR, inget lika')
  694.       //           Roles.create({
  695.       //             ID: r.users1.ID,
  696.       //             courseID: request.CourseID,
  697.       //             role: 2
  698.       //           }).then(res2 => {
  699.       //             roles.push(new Role({
  700.       //               ID: res2.users1.ID,
  701.       //               courseID: request.CourseID,
  702.       //               role: 2
  703.       //             }))
  704.       //           })
  705.       //         }
  706.       //       }
  707.       //       mngrLoop += 1
  708.       //     }
  709.       //   }
  710.       // }
  711.  
  712.       let rolesToCourse = []
  713.       for (let r of res) {
  714.         console.log(r.ID, r.role, r.users1.email)
  715.         rolesToCourse.push({ ID: r.ID, role: r.role, email: r.users1.email })
  716.       }
  717.       for (let asse of confirmedAssistants) {
  718.         if (!(rolesToCourse.some(e => e.email === asse))) {
  719.           Roles.create({
  720.             ID: findIDfromEmail(asse),
  721.             courseID: request.CourseID,
  722.             role: 1
  723.           }).then(res2 => {
  724.             roles.push(new Role({
  725.               ID: res2.ID,
  726.               courseID: res2.CourseID,
  727.               role: 1
  728.             }))
  729.           })
  730.         } else if {
  731.           if (rolesToCourse.some(e => e.email === asse)) {
  732.            
  733.           }
  734.         }
  735.       }
  736.  
  737.       // console.log(r.)
  738.       // for (let asse of confirmedAssistants){
  739.       //   if (!(roles.some(e => e.ID === this.findIDfromEmail(asse)))) {
  740.       //     console.log('INGET ID MATCHAR')
  741.       //     Roles.create({
  742.       //       ID: r.users1.ID,
  743.       //       courseID: request.CourseID,
  744.       //       role:
  745.       //     }).then(res2 => {
  746.       //       roles.push(new Role({
  747.       //         ID: res2.users1.ID,
  748.       //         courseID: request.CourseID,
  749.       //         role: 1
  750.       //       }))
  751.       //     })
  752.       //   }
  753.       // }
  754.     }
  755.     )
  756.   }
  757. }
  758.  
  759. exports.getUserInfoByEmail = (email, callback) => {
  760.   for (let user of users) {
  761.     if (email.trim() === user.email) {
  762.       console.log('found user!', user)
  763.       let userObject = {
  764.         ID: user.ID,
  765.         name: user.name,
  766.         email: user.email
  767.       }
  768.       callback(userObject)
  769.     }
  770.   }
  771. }
  772.  
  773. exports.checkIfUserInQueue = (courseID, studentID) => {
  774.   console.log('check if user in queue in model: ', courseID, studentID)
  775.   // let queue = this.getQueueByCourseID(courseID).queue
  776.   let queue = this.getQueueByCourseID(courseID).queue
  777.   console.log('Checking still: ', queue)
  778.   for (let qItem of queue) {
  779.     console.log(qItem.studentID, studentID)
  780.  
  781.     if (qItem.studentID === studentID) {
  782.       console.log('in model checkIfUserInQueue: returning true')
  783.       return true
  784.     }
  785.   }
  786.   console.log('in model checkIfUserInQueue: returning false')
  787.   return false
  788. }
  789.  
  790. function incrementTimesQueued (studentID, courseID) {
  791.   console.log('-----incrementTimesQueued')
  792.   StudentStatistics.findOne(
  793.     { where: { ID: studentID } }
  794.  
  795.   ).then(res => {
  796.     StudentStatistics.update(
  797.       { noTimesQueued: res.noTimesQueued + 1 },
  798.       { where: { ID: studentID } }
  799.     ).catch(err => { console.error(err) })
  800.   })
  801.  
  802.   CourseStatistics.findOne(
  803.     { where: { courseID: courseID },
  804.       attributes: ['courseID', 'totalQueued']
  805.     }
  806.  
  807.   ).then(res => {
  808.     CourseStatistics.update(
  809.       { totalQueued: res.totalQueued + 1 },
  810.       { where: { courseID: courseID } }
  811.     ).catch(err => { console.error(err) })
  812.   })
  813. }
  814.  
  815. function incrementTimesHelped (studentID) {
  816.   StudentStatistics.findOne(
  817.     { where: { ID: studentID } }
  818.  
  819.   ).then(res => {
  820.     StudentStatistics.update(
  821.       { noTimesHelpRecieved: res.noTimesHelpRecieved + 1 },
  822.       { where: { ID: studentID } }
  823.     ).catch(err => { console.error(err) })
  824.   })
  825. }
  826.  
  827. function incrementTimeSpentInQueues (studentID, courseID, enterTime) {
  828.   console.log('----in timeIncrement')
  829.   let now = new Date()
  830.  
  831.   StudentStatistics.findOne(
  832.     { where: { ID: studentID } }
  833.  
  834.   ).then(res => {
  835.     console.log('STUDENT old time in queue', res.totTimeSpentInQueue)
  836.     res.totTimeSpentInQueue.setMilliseconds((now - enterTime))
  837.     console.log('STUDENT new time in queue', res.totTimeSpentInQueue)
  838.  
  839.     StudentStatistics.update(
  840.       { totTimeSpentInQueue: res.totTimeSpentInQueue },
  841.       { where: { ID: studentID } }
  842.     ).catch(err => { console.error(err) })
  843.   }).catch(err => { console.error(err) })
  844.  
  845.   CourseStatistics.findOne(
  846.     {
  847.       where: { courseID: courseID },
  848.       attributes: ['TotalQueueTime']
  849.     }
  850.   ).then(res => {
  851.     res.TotalQueueTime.setMilliseconds((now - enterTime))
  852.  
  853.     CourseStatistics.update(
  854.       { TotalQueueTime: res.TotalQueueTime },
  855.       { where: { courseID: courseID } }
  856.     )
  857.   })
  858. }
  859. exports.addStudentToStatistics = (i) => {
  860.   StudentStatistics.create({
  861.     ID: i
  862.   })
  863. }
  864.  
  865. exports.addRecievedHelp = (studentID) => {
  866.   StudentStatistics.findOne({
  867.     where: { ID: studentID }
  868.   }).then(res => {
  869.     StudentStatistics.update(
  870.       { noTimesHelpRecieved: res.noTimesHelpRecieved + 1 }
  871.     )
  872.   })
  873. }
  874. exports.getStudentStatistics = (studentID) => {
  875.   StudentStatistics.findOne({
  876.     where: { ID: studentID }
  877.   }).then(res => {
  878.     let avgTime = convertTime(((res.totTimeSpentInQueue.getTime() - (new Date('1000-01-01T00:00:00Z')).getTime()) / (res.noTimesQueued)))
  879.  
  880.     let stats = new Map()
  881.     stats.set('totQueued', res.noTimesQueued)
  882.     stats.set('noTimesHelpRecieved', res.noTimesHelpRecieved)
  883.     stats.set('avgTime', avgTime)
  884.  
  885.     return stats
  886.   })
  887. }
  888.  
  889. exports.getCourseStatistics = (courseID) => {
  890.   CourseStatistics.findOne({
  891.     where: { courseID: courseID }
  892.   }).then(res => {
  893.     let avgTime = convertTime(((res.TotalQueueTime.getTime() - (new Date('1000-01-01T00:00:00Z')).getTime()) / (res.totalQueued)))
  894.  
  895.     let stats = new Map()
  896.     stats.set('totQueued', res.totalQueued)
  897.     stats.set('avgTime', avgTime)
  898.  
  899.     return stats
  900.   })
  901. }
  902.  
  903. function convertTime (milliseconds) {
  904.   var h, m, s
  905.   s = Math.floor(milliseconds / 1000)
  906.   m = Math.floor(s / 60)
  907.   s = s % 60
  908.   h = Math.floor(m / 60)
  909.   m = m % 60
  910.  
  911.   console.log('STUDENT AVGTIME', [h, m, s])
  912.   return [h, m, s]
  913. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top