@@ -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 ) => {
0 commit comments