Skip to content

Commit 0db2a26

Browse files
committed
upvoting downvoting status updates
1 parent 72fd852 commit 0db2a26

File tree

3 files changed

+85
-9
lines changed

3 files changed

+85
-9
lines changed

app/controllers/ticket.js

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,18 @@ module.exports = {
5656
}
5757
if (ticket.title === title &&
5858
ticket.content.shortDescription === content.shortDescription &&
59-
ticket.content.longDescription === content.longDescription) {
59+
ticket.content.longDescription === content.longDescription &&
60+
ticket.status === content.status) {
6061
return res.status(HttpStatus.NOT_MODIFIED).json({ error: 'No changes to ticket' })
6162
}
6263
ticket.title = title
6364
ticket.content.shortDescription = content.shortDescription
6465
ticket.content.longDescription = content.longDescription
6566
ticket.history.push({ title, content, editedBy: userId, editedAt: Date.now() })
6667
ticket.updatedAt = Date.now()
68+
if (content.status) {
69+
ticket.status = content.status
70+
}
6771
await ticket.save()
6872
res.status(HttpStatus.OK).json({
6973
ticket: ticket
@@ -218,15 +222,81 @@ module.exports = {
218222
},
219223

220224
upVoteComment: async (req, res, next) => {
221-
res.status(HttpStatus.OK).json({
222-
error: 'under development'
223-
})
225+
const { id, commentID } = req.params
226+
const userId = req.user.id.toString()
227+
try {
228+
const ticket = await TicketModel.findById(id)
229+
if (!ticket) {
230+
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No ticket exist' })
231+
}
232+
const comment = ticket.comments.id(commentID)
233+
if (!comment) {
234+
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No comment exist' })
235+
}
236+
// CHECKS IF THE USER HAS ALREADY UPVOTED THE COMMENT
237+
comment.votes.upVotes.user.filter(user => {
238+
if (JSON.stringify(user) === JSON.stringify(userId)) {
239+
const error = new Error()
240+
error.message = 'Bad request - User has already upvoted'
241+
error.code = HttpStatus.BAD_REQUEST
242+
throw error
243+
}
244+
})
245+
// CHECKS IF THE USER HAS ALREADY DOWNVOTED THE COMMENT
246+
comment.votes.downVotes.user.filter(user => {
247+
if (JSON.stringify(user) === JSON.stringify(userId)) {
248+
comment.votes.downVotes.user.remove(user)
249+
}
250+
})
251+
comment.votes.upVotes.user.unshift(userId)
252+
await ticket.save()
253+
res.status(HttpStatus.OK).json({ comment: comment })
254+
} catch (error) {
255+
console.log(error)
256+
HANDLER.handleError(res, {
257+
code: error.code || HttpStatus.BAD_REQUEST,
258+
...error
259+
})
260+
}
224261
},
225262

226263
downVoteComment: async (req, res, next) => {
227-
res.status(HttpStatus.OK).json({
228-
error: 'under development'
229-
})
264+
const { id, commentID } = req.params
265+
const userId = req.user.id.toString()
266+
try {
267+
const ticket = await TicketModel.findById(id)
268+
if (!ticket) {
269+
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No ticket exist' })
270+
}
271+
const comment = ticket.comments.id(commentID)
272+
if (!comment) {
273+
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No comment exist' })
274+
}
275+
// CHECKS IF THE USER HAS ALREADY DOWNVOTED THE COMMENT
276+
comment.votes.downVotes.user.filter(user => {
277+
if (JSON.stringify(user) === JSON.stringify(userId)) {
278+
const error = new Error()
279+
error.message = 'Bad request - User has already downvoted'
280+
error.code = HttpStatus.BAD_REQUEST
281+
throw error
282+
}
283+
})
284+
// CHECKS IF THE USER HAS ALREADY UPVOTED THE COMMENT
285+
comment.votes.upVotes.user.filter(user => {
286+
if (JSON.stringify(user) === JSON.stringify(userId)) {
287+
comment.votes.upVotes.user.remove(user)
288+
}
289+
})
290+
comment.votes.downVotes.user.unshift(userId)
291+
await ticket.save()
292+
res.status(HttpStatus.OK).json({ comment: comment })
293+
} catch (error) {
294+
console.log(error)
295+
HANDLER.handleError(res, {
296+
code: error.code || HttpStatus.BAD_REQUEST,
297+
...error
298+
})
299+
}
230300
},
231301

232302
deleteComment: async (req, res, next) => {

app/models/Ticket.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ const ticketSchema = new Schema({
1313
type: Schema.Types.ObjectId,
1414
ref: 'User'
1515
},
16+
status: {
17+
type: String,
18+
enum: ['Open', 'Closed', 'Pending', 'Closed'],
19+
default: 'Open',
20+
required: true
21+
},
1622
content: {
1723
shortDescription: {
1824
type: String,

app/routes/ticket.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ router.get('/:id/comments', isUnderMaintenance, auth, isValidObjectId, ticketCon
3939
router.put('/:id/comment/:commentID', isUnderMaintenance, auth, isValidObjectId, ticketController.editComment)
4040

4141
// UPVOTE TICKET COMMENT
42-
router.put(':id/comment/:commentID/upvote', isUnderMaintenance, auth, isValidObjectId, ticketController.upVoteComment)
42+
router.put('/:id/comment/:commentID/upvote', isUnderMaintenance, auth, isValidObjectId, ticketController.upVoteComment)
4343

4444
// DOWNVOTE TICKET COMMENT
45-
router.put(':id/comment/:commentID/downvote', isUnderMaintenance, auth, isValidObjectId, ticketController.downVoteComment)
45+
router.put('/:id/comment/:commentID/downvote', isUnderMaintenance, auth, isValidObjectId, ticketController.downVoteComment)
4646

4747
// DELETE TICKET COMMENT BY ID
4848
router.delete('/:id/comment/:commentID', isUnderMaintenance, auth, isValidObjectId, ticketController.deleteComment)

0 commit comments

Comments
 (0)