poll.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. let express = require('express')
  2. let router = express.Router()
  3. let { PollAnswer, PollQuestion, PollVote, User, Sequelize, Thread } = require('../models')
  4. const Errors = require('../lib/errors')
  5. router.get('/:id', async (req, res) => {
  6. try {
  7. let id = req.params.id
  8. let pollQuestion = await PollQuestion.findById(id, {
  9. include: [
  10. { model: User, attributes: { exclude: ['hash'] } },
  11. { model: PollAnswer, include: [PollVote] }
  12. ]
  13. })
  14. if(!pollQuestion) throw Errors.sequelizeValidation(Sequelize, {
  15. error: 'invalid poll id',
  16. value: id
  17. })
  18. let totalVotes = pollQuestion.PollAnswers.reduce((sum, answer) => {
  19. return sum + answer.PollVotes.length
  20. }, 0)
  21. let answersWithPercent = pollQuestion.PollAnswers.map(answer => {
  22. let jsonAnswer = answer.toJSON()
  23. let percent = answer.PollVotes.length / totalVotes
  24. jsonAnswer.percent = Math.round(percent*100 * 10) / 10
  25. return jsonAnswer
  26. })
  27. let jsonPollQuestion = pollQuestion.toJSON()
  28. jsonPollQuestion.totalVotes = totalVotes
  29. jsonPollQuestion.PollAnswers = answersWithPercent
  30. res.json(jsonPollQuestion)
  31. } catch (e) {
  32. if(e instanceof Sequelize.ValidationError) {
  33. res.status(400)
  34. res.json(e)
  35. } else {
  36. console.log(e)
  37. res.status(500)
  38. res.json({
  39. errors: [Errors.unknown]
  40. })
  41. }
  42. }
  43. })
  44. router.all('*', (req, res, next) => {
  45. if(req.session.loggedIn) {
  46. next()
  47. } else {
  48. res.status(401)
  49. res.json({
  50. errors: [Errors.requestNotAuthorized]
  51. })
  52. }
  53. })
  54. router.post('/', async (req, res) => {
  55. try {
  56. let threadId = req.body.threadId
  57. let thread = await Thread.findById(req.body.threadId)
  58. if(!thread) {
  59. throw Errors.sequelizeValidation(Sequelize, {
  60. error: 'invalid thread id',
  61. value: threadId
  62. })
  63. } else if(thread.UserId !== req.session.UserId) {
  64. throw Errors.requestNotAuthorized
  65. } else if(thread.PollQuestionId) {
  66. throw Errors.sequelizeValidation(Sequelize, {
  67. error: 'invalid thread id',
  68. value: threadId
  69. })
  70. }
  71. let answers = req.body.answers
  72. if(!answers || answers.length < 2) {
  73. throw Errors.sequelizeValidation(Sequelize, {
  74. error: 'You must provide at least 2 answers',
  75. value: answers
  76. })
  77. } else if(answers.length !== new Set(answers).size) {
  78. throw Errors.sequelizeValidation(Sequelize, {
  79. error: 'Answers cannot contain any duplicates',
  80. value: answers
  81. })
  82. }
  83. let pollQuestion = await PollQuestion.create({
  84. UserId: req.session.UserId,
  85. question: req.body.question
  86. })
  87. let pollAnswers = await Promise.all(
  88. answers.map(answer => {
  89. return PollAnswer.create({ answer })
  90. })
  91. )
  92. //Set associations
  93. await thread.setPollQuestion(pollQuestion)
  94. await Promise.all(
  95. pollAnswers.map(pollAnswer => {
  96. return pollQuestion.addPollAnswer(pollAnswer)
  97. })
  98. )
  99. res.json(pollQuestion.toJSON())
  100. } catch (e) {
  101. if(e instanceof Sequelize.ValidationError) {
  102. res.status(400)
  103. res.json(e)
  104. } else if(e === Errors.requestNotAuthorized) {
  105. res.status(401)
  106. res.json({
  107. errors: [e]
  108. })
  109. } else {
  110. console.log(e)
  111. res.status(500)
  112. res.json({
  113. errors: [Errors.unknown]
  114. })
  115. }
  116. }
  117. })
  118. router.post('/:id', async (req, res) => {
  119. try {
  120. let previousVote = await PollVote.findOne({
  121. where: { PollQuestionId: req.params.id, UserId: req.session.UserId }
  122. })
  123. if(previousVote) throw Errors.sequelizeValidation(Sequelize, {
  124. error: 'you cannot vote twice',
  125. value: req.params.id
  126. })
  127. let poll = await PollQuestion.findById(req.params.id, {
  128. include: [PollAnswer]
  129. })
  130. if(!poll) throw Errors.sequelizeValidation(Sequelize, {
  131. error: 'invalid poll id',
  132. value: req.params.id
  133. })
  134. let pollAnswer = poll.PollAnswers.find(a => a.answer === req.body.answer)
  135. if(!pollAnswer) throw Errors.sequelizeValidation(Sequelize, {
  136. error: 'invalid answer',
  137. value: req.body.answer
  138. })
  139. let pollVote = await PollVote.create({ UserId: req.session.UserId })
  140. await pollVote.setPollQuestion(poll)
  141. await pollVote.setPollAnswer(pollAnswer)
  142. res.json(pollVote.toJSON())
  143. } catch (e) {
  144. if(e instanceof Sequelize.ValidationError) {
  145. res.status(400)
  146. res.json(e)
  147. } else {
  148. console.log(e)
  149. res.status(500)
  150. res.json({
  151. errors: [Errors.unknown]
  152. })
  153. }
  154. }
  155. })
  156. module.exports = router