1+ const UserModel = require ( '../models/User' )
12const HttpStatus = require ( 'http-status-codes' )
23const TicketModel = require ( '../models/Ticket' )
34const TAGS = require ( '../utils/notificationTags' )
@@ -21,7 +22,7 @@ module.exports = {
2122 notification . heading = 'New Support Ticket!'
2223 notification . content = `${ req . user . name . firstName } ${ req . user . name . lastName } Creted a new Support Ticket!`
2324 notification . tag = TAGS . NEW
24- await ticketNotificationHelper . addToNotificationForAdmin ( req , res , notification , next )
25+ await ticketNotificationHelper . addToNotificationForModerator ( req , res , notification , next )
2526 await ticket . save ( )
2627 res . status ( HttpStatus . CREATED ) . json ( {
2728 ticket : ticket
@@ -62,7 +63,7 @@ module.exports = {
6263 if ( ! ticket ) {
6364 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
6465 }
65- if ( userId !== ticket . createdBy && ! req . user . isAdmin ) {
66+ if ( userId !== ticket . createdBy && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
6667 // Only user who created the ticket and admin can edit the ticket
6768 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
6869 }
@@ -100,7 +101,7 @@ module.exports = {
100101 if ( ! ticket ) {
101102 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
102103 }
103- if ( userId !== ticket . createdBy && ! req . user . isAdmin ) {
104+ if ( userId !== ticket . createdBy && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
104105 // Only user who created the ticket and admin can delete the ticket
105106 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Bad delete request' } )
106107 }
@@ -123,7 +124,7 @@ module.exports = {
123124 if ( ! ticket ) {
124125 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
125126 }
126- if ( userId !== ticket . createdBy && ! req . user . isAdmin ) {
127+ if ( userId !== ticket . createdBy && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
127128 // Only user who created the ticket and admin can edit ticket tags
128129 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
129130 }
@@ -147,7 +148,7 @@ module.exports = {
147148 if ( ! ticket ) {
148149 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
149150 }
150- if ( userId !== ticket . createdBy && ! req . user . isAdmin ) {
151+ if ( userId !== ticket . createdBy && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
151152 // Only user who created the ticket and admin can add tag to the ticket
152153 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
153154 }
@@ -220,7 +221,7 @@ module.exports = {
220221 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
221222 }
222223 const comment = ticket . comments . id ( commentID )
223- if ( userId !== comment . createdBy && ! req . user . isAdmin ) {
224+ if ( userId !== comment . createdBy && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
224225 // Only user who created the comment and admin can edit the comment
225226 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
226227 }
@@ -324,7 +325,7 @@ module.exports = {
324325 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
325326 }
326327 const comment = ticket . comments . id ( commentID )
327- if ( userId !== comment . createdBy && ! req . user . isAdmin ) {
328+ if ( userId !== comment . createdBy && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
328329 // Only user who created the comment and admin can edit the comment
329330 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
330331 }
@@ -340,6 +341,65 @@ module.exports = {
340341 }
341342 } ,
342343
344+ getModerators : async ( req , res , next ) => {
345+ try {
346+ const moderators = await UserModel . find ( { isTicketsModerator : true } )
347+ return res . status ( HttpStatus . OK ) . json ( { moderators : moderators } )
348+ } catch ( error ) {
349+ console . log ( error )
350+ HANDLER . handleError ( res , {
351+ code : error . code || HttpStatus . BAD_REQUEST ,
352+ ...error
353+ } )
354+ }
355+ } ,
356+
357+ addModerator : async ( req , res , next ) => {
358+ // id of User to add as moderator
359+ const { id } = req . params
360+ try {
361+ if ( ! req . user . isAdmin ) {
362+ return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Only Admin user can add moderator' } )
363+ }
364+ const user = await UserModel . findById ( id )
365+ if ( ! user ) {
366+ return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No user exist' } )
367+ }
368+ user . isTicketsModerator = true
369+ await user . save ( )
370+ return res . status ( HttpStatus . OK ) . json ( { success : 'Add moderator successful' } )
371+ } catch ( error ) {
372+ console . log ( error )
373+ HANDLER . handleError ( res , {
374+ code : error . code || HttpStatus . BAD_REQUEST ,
375+ ...error
376+ } )
377+ }
378+ } ,
379+
380+ removeModerator : async ( req , res , next ) => {
381+ // id of User to add as moderator
382+ const { id } = req . params
383+ try {
384+ if ( ! req . user . isAdmin ) {
385+ return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Only Admin user can remove moderator' } )
386+ }
387+ const user = await UserModel . findById ( id )
388+ if ( ! user ) {
389+ return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No user exist' } )
390+ }
391+ user . isTicketsModerator = false
392+ await user . save ( )
393+ return res . status ( HttpStatus . OK ) . json ( { success : 'Remove moderator successful' } )
394+ } catch ( error ) {
395+ console . log ( error )
396+ HANDLER . handleError ( res , {
397+ code : error . code || HttpStatus . BAD_REQUEST ,
398+ ...error
399+ } )
400+ }
401+ } ,
402+
343403 deleteTag : async ( req , res , next ) => {
344404 const { id, tag } = req . params
345405 const userId = req . user . id . toString ( )
@@ -348,7 +408,7 @@ module.exports = {
348408 if ( ! ticket ) {
349409 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
350410 }
351- if ( userId !== ticket . createdBy && ! req . user . isAdmin ) {
411+ if ( userId !== ticket . createdBy && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
352412 // Only user who created the ticket and admin can delete tag from a ticket
353413 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
354414 }
0 commit comments