Skip to content

Commit 05a60a5

Browse files
update vote APIs
1 parent fcd4ec9 commit 05a60a5

File tree

2 files changed

+33
-50
lines changed

2 files changed

+33
-50
lines changed

controllers/Temp.js

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -599,68 +599,47 @@ class TempController {
599599
return resolve(HTTPWTHandler.badInput('userId must be an ObjectId.'))
600600
}
601601

602-
if (typeof optionSelected !== 'string') {
603-
return resolve(HTTPWTHandler.badInput(`optionSelected must be a string. Provided type: ${typeof optionSelected}`))
602+
if (typeof optionSelected !== 'number') {
603+
return resolve(HTTPWTHandler.badInput(`optionSelected must be a number. Provided type: ${typeof optionSelected}`))
604604
}
605605

606606
if (typeof pollId !== 'string') {
607607
return resolve(HTTPWTHandler.badInput(`pollId must be a string. Provided type: ${typeof pollId}`))
608608
}
609609

610-
const allowedOptionsToSelect = ['One', 'Two', 'Three', 'Four', 'Five', 'Six']
611-
if (!allowedOptionsToSelect.includes(optionSelected)) {
612-
return resolve(HTTPWTHandler.badInput(`optionSelected must be either ${allowedOptionsToSelect.join(', ')}`))
613-
}
614-
615-
if (pollId.length == 0) {
616-
return resolve(HTTPWTHandler.badInput('pollId cannot be an empty string'))
610+
if (!mongoose.isObjectIdOrHexString(pollId)) {
611+
return resolve(HTTPWTHandler.badInput('pollId must be an ObjectId.'))
617612
}
618613

619614
User.findOne({_id: {$eq: userId}}).lean().then(result => {
620-
if (result) {
621-
//User exists
622-
Poll.findOne({_id: {$eq: pollId}}).lean().then(data => {
623-
if (data) {
624-
if (data.creatorId == userId) {
625-
return resolve(HTTPWTHandler.forbidden('You cannot vote on your own poll'))
626-
}
627-
628-
if (data.totalNumberOfOptions === "Two" && (allowedOptionsToSelect.slice(2).includes(optionSelected))) {
629-
//There are only two options and the optionSelected is Three or more
630-
return resolve(HTTPWTHandler.badInput('Invalid vote'))
631-
}
632-
633-
if (data.totalNumberOfOptions === "Three" && (allowedOptionsToSelect.slice(3).includes(optionSelected))) {
634-
//There are only two options and the optionSelected is Three or more
635-
return resolve(HTTPWTHandler.badInput('Invalid vote'))
636-
}
637-
638-
if (data.totalNumberOfOptions === "Four" && (allowedOptionsToSelect.slice(4).includes(optionSelected))) {
639-
//There are only two options and the optionSelected is Three or more
640-
return resolve(HTTPWTHandler.badInput('Invalid vote'))
641-
}
615+
if (!result) {
616+
return resolve(HTTPWTHandler.notFound('Could not find user with provided userId'))
617+
}
618+
619+
//User exists
620+
Poll.findOne({_id: {$eq: pollId}}).lean().then(data => {
621+
if (!data) {
622+
return resolve(HTTPWTHandler.notFound('Could not find poll'))
623+
}
624+
625+
if (data.creatorId == userId) {
626+
return resolve(HTTPWTHandler.forbidden('You cannot vote on your own poll'))
627+
}
642628

643-
if (data.totalNumberOfOptions === "Five" && (allowedOptionsToSelect.slice(5).includes(optionSelected))) {
644-
//There are only two options and the optionSelected is Three or more
645-
return resolve(HTTPWTHandler.badInput('Invalid vote'))
646-
}
629+
if (optionSelected > data.options.length - 1) {
630+
return resolve(HTTPWTHandler.badInput('Invalid vote'))
631+
}
647632

648-
PollVote.findOneAndUpdate({userId: {$eq: userId}, pollId: {$eq: pollId}}, {dateVoted: Date.now(), vote: optionSelected}, {upsert: true}).then(() => {
649-
return resolve(HTTPWTHandler.OK('Added poll vote'))
650-
}).catch(error => {
651-
console.error('An error occurred while finding one and updating PollVote with filter filtering by userId:', userId, 'and pollId:', pollId, 'and update query updating dateVoted to Date.now() and vote to:', optionSelected, 'and upserts are enabled. The error was:', error)
652-
return resolve(HTTPWTHandler.serverError('An error occurred while adding vote to the poll. Please try again.'))
653-
})
654-
} else {
655-
return resolve(HTTPWTHandler.notFound('Could not find poll'))
656-
}
633+
PollVote.findOneAndUpdate({userId: {$eq: userId}, pollId: {$eq: pollId}}, {dateVoted: Date.now(), vote: optionSelected}, {upsert: true}).then(() => {
634+
return resolve(HTTPWTHandler.OK('Added poll vote'))
657635
}).catch(error => {
658-
console.error('An error occured while finding poll with id:', pollId, '. The error was:', error)
659-
return resolve(HTTPWTHandler.serverError('An error occurred while finding poll. Please try again.'))
636+
console.error('An error occurred while finding one and updating PollVote with filter filtering by userId:', userId, 'and pollId:', pollId, 'and update query updating dateVoted to Date.now() and vote to:', optionSelected, 'and upserts are enabled. The error was:', error)
637+
return resolve(HTTPWTHandler.serverError('An error occurred while adding vote to the poll. Please try again.'))
660638
})
661-
} else {
662-
return resolve(HTTPWTHandler.notFound('Could not find user with provided userId'))
663-
}
639+
}).catch(error => {
640+
console.error('An error occured while finding poll with id:', pollId, '. The error was:', error)
641+
return resolve(HTTPWTHandler.serverError('An error occurred while finding poll. Please try again.'))
642+
})
664643
}).catch(error => {
665644
console.error('An error occured while finding user with id:', userId, '. The error was:', error)
666645
return resolve(HTTPWTHandler.serverError('An error occurred while finding user with your id. Please try again.'))
@@ -682,6 +661,10 @@ class TempController {
682661
return resolve(HTTPWTHandler.badInput(`pollId must be a string. Provided type: ${typeof pollId}`))
683662
}
684663

664+
if (!mongoose.isObjectIdOrHexString(pollId)) {
665+
return resolve(HTTPWTHandler.badInput('pollId must be an ObjectId.'))
666+
}
667+
685668
User.findOne({_id: {$eq: userId}}).lean().then(userFound => {
686669
if (!userFound) {
687670
return resolve(HTTPWTHandler.notFound('Could not find user with provided userId'))

models/PollVote.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Schema = mongoose.Schema;
44
const PollVoteSchema = new Schema({
55
pollId: mongoose.Schema.Types.ObjectId,
66
userId: mongoose.Schema.Types.ObjectId,
7-
vote: String,
7+
vote: Number, // 0-based vote index
88
dateVoted: Number
99
})
1010

0 commit comments

Comments
 (0)