poll.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 hasVoted = await PollVote.findOne({
  28. where: {
  29. UserId: req.session.UserId,
  30. PollQuestionId: id
  31. }
  32. })
  33. let jsonPollQuestion = pollQuestion.toJSON()
  34. jsonPollQuestion.totalVotes = totalVotes
  35. jsonPollQuestion.PollAnswers = answersWithPercent
  36. jsonPollQuestion.hasVoted = !!hasVoted
  37. res.json(jsonPollQuestion)
  38. } catch (e) {
  39. if(e instanceof Sequelize.ValidationError) {
  40. res.status(400)
  41. res.json(e)
  42. } else {
  43. console.log(e)
  44. res.status(500)
  45. res.json({
  46. errors: [Errors.unknown]
  47. })
  48. }
  49. }
  50. })
  51. router.all('*', (req, res, next) => {
  52. if(req.session.loggedIn) {
  53. next()
  54. } else {
  55. res.status(401)
  56. res.json({
  57. errors: [Errors.requestNotAuthorized]
  58. })
  59. }
  60. })
  61. router.post('/', async (req, res) => {
  62. try {
  63. let threadId = req.body.threadId
  64. let thread = await Thread.findById(req.body.threadId)
  65. if(!thread) {
  66. throw Errors.sequelizeValidation(Sequelize, {
  67. error: 'invalid thread id',
  68. value: threadId
  69. })
  70. } else if(thread.UserId !== req.session.UserId) {
  71. throw Errors.requestNotAuthorized
  72. } else if(thread.PollQuestionId) {
  73. throw Errors.sequelizeValidation(Sequelize, {
  74. error: 'invalid thread id',
  75. value: threadId
  76. })
  77. }
  78. let answers = req.body.answers
  79. if(!answers || answers.length < 2) {
  80. throw Errors.sequelizeValidation(Sequelize, {
  81. error: 'You must provide at least 2 answers',
  82. value: answers
  83. })
  84. } else if(answers.length !== new Set(answers).size) {
  85. throw Errors.sequelizeValidation(Sequelize, {
  86. error: 'Answers cannot contain any duplicates',
  87. value: answers
  88. })
  89. }
  90. let pollQuestion = await PollQuestion.create({
  91. UserId: req.session.UserId,
  92. question: req.body.question
  93. })
  94. let pollAnswers = await Promise.all(
  95. answers.map(answer => {
  96. return PollAnswer.create({ answer })
  97. })
  98. )
  99. //Set associations
  100. await thread.setPollQuestion(pollQuestion)
  101. await Promise.all(
  102. pollAnswers.map(pollAnswer => {
  103. return pollQuestion.addPollAnswer(pollAnswer)
  104. })
  105. )
  106. res.json(pollQuestion.toJSON())
  107. } catch (e) {
  108. if(e instanceof Sequelize.ValidationError) {
  109. res.status(400)
  110. res.json(e)
  111. } else if(e === Errors.requestNotAuthorized) {
  112. res.status(401)
  113. res.json({
  114. errors: [e]
  115. })
  116. } else {
  117. console.log(e)
  118. res.status(500)
  119. res.json({
  120. errors: [Errors.unknown]
  121. })
  122. }
  123. }
  124. })
  125. router.post('/:id', async (req, res) => {
  126. try {
  127. let previousVote = await PollVote.findOne({
  128. where: { PollQuestionId: req.params.id, UserId: req.session.UserId }
  129. })
  130. if(previousVote) throw Errors.sequelizeValidation(Sequelize, {
  131. error: 'you cannot vote twice',
  132. value: req.params.id
  133. })
  134. let poll = await PollQuestion.findById(req.params.id, {
  135. include: [PollAnswer]
  136. })
  137. if(!poll) throw Errors.sequelizeValidation(Sequelize, {
  138. error: 'invalid poll id',
  139. value: req.params.id
  140. })
  141. let pollAnswer = poll.PollAnswers.find(a => a.answer === req.body.answer)
  142. if(!pollAnswer) throw Errors.sequelizeValidation(Sequelize, {
  143. error: 'invalid answer',
  144. value: req.body.answer
  145. })
  146. let pollVote = await PollVote.create({ UserId: req.session.UserId })
  147. await pollVote.setPollQuestion(poll)
  148. await pollVote.setPollAnswer(pollAnswer)
  149. res.redirect('/api/v1/poll/' + req.params.id)
  150. } catch (e) {
  151. if(e instanceof Sequelize.ValidationError) {
  152. res.status(400)
  153. res.json(e)
  154. } else {
  155. console.log(e)
  156. res.status(500)
  157. res.json({
  158. errors: [Errors.unknown]
  159. })
  160. }
  161. }
  162. })
  163. module.exports = router