@@ -3,8 +3,12 @@ const HttpStatus = require('http-status-codes')
33const TicketModel = require ( '../models/Ticket' )
44const TAGS = require ( '../utils/notificationTags' )
55const HANDLER = require ( '../utils/response-helper' )
6- const { isValidObjectId } = require ( '../utils/ticket-helper' )
7- const ticketNotificationHelper = require ( '../utils/ticket-helper' )
6+ const {
7+ isValidObjectId,
8+ isCreatorModeratorAdmin,
9+ addToNotificationForUser,
10+ addToNotificationForModerator
11+ } = require ( '../utils/ticket-helper' )
812
913const notification = {
1014 heading : '' ,
@@ -22,21 +26,19 @@ module.exports = {
2226 ticket . createdBy = {
2327 id : userId ,
2428 name : `${ req . user . name . firstName } ${ req . user . name . lastName } ` ,
25- eaill : req . user . email ,
2629 shortDescription : req . user . info . about . shortDescription ,
2730 designation : req . user . info . about . designation ,
28- location : req . user . info . about . location
31+ location : req . user . info . about . location ,
32+ email : req . user . email
2933 }
30- ticket . number = allTickets . length ? ( allTickets [ allTickets . length - 1 ] . number + 1 ) : 1
3134 ticket . createdAt = Date . now ( )
3235 ticket . updatedAt = Date . now ( )
36+ ticket . number = allTickets . length ? ( allTickets [ allTickets . length - 1 ] . number + 1 ) : 1
37+ notification . tag = TAGS . NEW
3338 notification . heading = 'New Support Ticket!'
3439 notification . content = `${ req . user . name . firstName } ${ req . user . name . lastName } Creted a new Support Ticket!`
35- notification . tag = TAGS . NEW
36- notification . createdAt = Date . now ( )
37- await ticketNotificationHelper . addToNotificationForModerator ( req , res , notification , next )
3840 await ticket . save ( )
39- req . io . emit ( 'New Ticket Notification' , { ... notification , for : 'moderator' } )
41+ await addToNotificationForModerator ( req , notification , next )
4042 res . status ( HttpStatus . CREATED ) . json ( {
4143 ticket : ticket
4244 } )
@@ -50,7 +52,8 @@ module.exports = {
5052
5153 getTicket : async ( req , res , next ) => {
5254 try {
53- const tickets = await TicketModel . find ( { } ) . lean ( ) . select ( 'shortDescription number createdAt createdBy status title comments tags' ) . exec ( )
55+ const filteredProperties = 'shortDescription number createdAt createdBy status title comments tags'
56+ const tickets = await TicketModel . find ( { } ) . lean ( filteredProperties ) . select ( ) . exec ( )
5457 tickets . forEach ( ticket => {
5558 ticket . comments = ticket . comments . length
5659 ticket . createdBy = {
@@ -100,8 +103,7 @@ module.exports = {
100103 if ( ! ticket ) {
101104 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
102105 }
103- if ( userId !== ticket . createdBy . id . toString ( ) && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
104- // Only user who created the ticket and admin can edit the ticket
106+ if ( isCreatorModeratorAdmin ( ticket , req . user ) ) {
105107 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
106108 }
107109 const historyItem = { }
@@ -140,7 +142,6 @@ module.exports = {
140142
141143 deleteTicket : async ( req , res , next ) => {
142144 const { id } = req . params
143- const userId = req . user . id . toString ( )
144145 if ( ! isValidObjectId ( id ) ) {
145146 return res . status ( HttpStatus . BAD_REQUEST ) . json ( { error : 'Invalid ticket id' } )
146147 }
@@ -149,8 +150,7 @@ module.exports = {
149150 if ( ! ticket ) {
150151 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
151152 }
152- if ( userId !== ticket . createdBy . id . toString ( ) && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
153- // Only user who created the ticket and admin can delete the ticket
153+ if ( isCreatorModeratorAdmin ( ticket , req . user ) ) {
154154 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Bad delete request' } )
155155 }
156156 await TicketModel . findByIdAndRemove ( id )
@@ -165,8 +165,7 @@ module.exports = {
165165
166166 editTag : async ( req , res , next ) => {
167167 const { id } = req . params
168- const { tags } = req . body // tags is the array of tags to add
169- const userId = req . user . id . toString ( )
168+ const { tags } = req . body
170169 if ( ! isValidObjectId ( id ) ) {
171170 return res . status ( HttpStatus . BAD_REQUEST ) . json ( { error : 'Invalid ticket id' } )
172171 }
@@ -175,8 +174,7 @@ module.exports = {
175174 if ( ! ticket ) {
176175 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
177176 }
178- if ( userId !== ticket . createdBy . id . toString ( ) && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
179- // Only user who created the ticket and admin can edit ticket tags
177+ if ( ! isCreatorModeratorAdmin ( ticket , req . user ) ) {
180178 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
181179 }
182180 ticket . tags = [ ...new Set ( tags ) ]
@@ -202,8 +200,7 @@ module.exports = {
202200 if ( ! ticket ) {
203201 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
204202 }
205- if ( userId !== ticket . createdBy . id . toString ( ) && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
206- // Only user who created the ticket and admin can add tag to the ticket
203+ if ( isCreatorModeratorAdmin ( ticket , req . user ) ) {
207204 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
208205 }
209206 ticket . tags . addToSet ( tag )
@@ -238,21 +235,19 @@ module.exports = {
238235 ticket . comments . push ( {
239236 ...req . body ,
240237 createdBy : {
241- userId,
242- eaill : req . user . email ,
243238 name : `${ req . user . name . firstName } ${ req . user . name . lastName } ` ,
244239 shortDescription : req . user . info . about . shortDescription ,
245240 designation : req . user . info . about . designation ,
246- location : req . user . info . about . location
241+ location : req . user . info . about . location ,
242+ eaill : req . user . email ,
243+ userId
247244 }
248245 } )
246+ notification . tag = TAGS . NEW
249247 notification . heading = 'New Comment on Ticket!'
250248 notification . content = `${ req . user . name . firstName } ${ req . user . name . lastName } commented on your Ticket!`
251- notification . tag = TAGS . NEW
252- notification . createdAt = Date . now ( )
253- await ticketNotificationHelper . addToNotificationForUser ( ticket . createdBy . id , res , notification , next )
254249 await ticket . save ( )
255- req . io . emit ( 'New Ticket Notification' , { ... notification , for : ticket . createdBy . id } )
250+ await addToNotificationForUser ( ticket . createdBy . id , req , notification )
256251 res . status ( HttpStatus . OK ) . json ( { ticket : ticket } )
257252 } catch ( error ) {
258253 console . log ( error )
@@ -298,7 +293,6 @@ module.exports = {
298293 }
299294 const comment = ticket . comments . id ( commentID )
300295 if ( userId !== comment . createdBy . userId && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
301- // Only user who created the comment and admin can edit the comment
302296 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
303297 }
304298 comment . content = content
@@ -409,7 +403,6 @@ module.exports = {
409403 }
410404 const comment = ticket . comments . id ( commentID )
411405 if ( userId !== comment . createdBy . userId && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
412- // Only user who created the comment and admin can edit the comment
413406 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
414407 }
415408 comment . remove ( )
@@ -513,8 +506,7 @@ module.exports = {
513506 if ( ! ticket ) {
514507 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
515508 }
516- if ( userId !== ticket . createdBy . id . toString ( ) && ! req . user . isAdmin && ! req . user . isTicketsModerator ) {
517- // Only user who created the ticket and admin can delete tag from a ticket
509+ if ( isCreatorModeratorAdmin ( ticket , req . user ) ) {
518510 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
519511 }
520512 if ( ticket . tags . indexOf ( tag ) === - 1 ) {
0 commit comments