Browse Source

Add hasVoted parameter

sbkwgh 8 years ago
parent
commit
07a56dbce8
2 changed files with 23 additions and 0 deletions
  1. 8 0
      routes/poll.js
  2. 15 0
      test/poll.js

+ 8 - 0
routes/poll.js

@@ -30,9 +30,17 @@ router.get('/:id', async (req, res) => {
 			return jsonAnswer
 		})
 
+		let hasVoted = await PollVote.findOne({
+			where: {
+				UserId: req.session.UserId,
+				PollQuestionId: id
+			}
+		})
+
 		let jsonPollQuestion = pollQuestion.toJSON()
 		jsonPollQuestion.totalVotes = totalVotes
 		jsonPollQuestion.PollAnswers = answersWithPercent
+		jsonPollQuestion.hasVoted = !!hasVoted
 
 		res.json(jsonPollQuestion)
 	} catch (e) {

+ 15 - 0
test/poll.js

@@ -449,6 +449,7 @@ describe('Poll', () => {
 				res.body.should.have.property('question', 'Do you like polls?')
 				res.body.PollAnswers.should.have.property('length', 3)
 				res.body.should.have.property('totalVotes', 3)
+				res.body.should.have.property('hasVoted', true)
 
 				res.body.should.have.deep.property('PollAnswers.0.answer', 'yes')
 				res.body.should.have.deep.property('PollAnswers.0.PollVotes.length', 2)
@@ -462,6 +463,20 @@ describe('Poll', () => {
 				res.body.should.have.deep.property('PollAnswers.2.PollVotes.length', 0)
 				res.body.should.have.deep.property('PollAnswers.2.percent', 0)
 			})
+			it('should set hasVoted to false if user not voted', async () => {
+				let res = await admin.get('/api/v1/poll/' + pollId)
+			
+				res.should.be.json
+				res.should.have.status(200)	
+				res.body.should.have.property('hasVoted', false)
+			})
+			it('should set hasVoted to false if user not logged in', async () => {
+				let res = await chai.request(server).get('/api/v1/poll/' + pollId)
+			
+				res.should.be.json
+				res.should.have.status(200)	
+				res.body.should.have.property('hasVoted', false)
+			})
 			it('should return an error if invalid id', done => {
 				chai.request(server)
 					.get('/api/v1/poll/' + pollId)