11const HANDLER = require ( '../utils/response-helper' )
22const HttpStatus = require ( 'http-status-codes' )
33const TicketModel = require ( '../models/Ticket' )
4+ const Ticket = require ( '../models/Ticket' )
45
56module . exports = {
67 create : async ( req , res , next ) => {
@@ -13,10 +14,11 @@ module.exports = {
1314 ticket : ticket
1415 } )
1516 } catch ( error ) {
16- HANDLER . handleError ( res , error )
17+ HANDLER . handleError ( res , {
18+ code : error . code || HttpStatus . BAD_REQUEST ,
19+ ...error
20+ } )
1721 }
18- console . log ( userId )
19- console . log ( 'Create called' )
2022 } ,
2123 getTicket : async ( req , res , next ) => {
2224 const { user } = req . query
@@ -30,24 +32,23 @@ module.exports = {
3032 }
3133 res . status ( HttpStatus . OK ) . json ( { tickets : tickets } )
3234 } catch ( error ) {
33- HANDLER . handleError ( res , error )
35+ HANDLER . handleError ( res , {
36+ code : error . code || HttpStatus . BAD_REQUEST ,
37+ ...error
38+ } )
3439 }
3540 } ,
3641 editTicket : async ( req , res , next ) => {
37- const { title, content } = req . body
3842 const { id } = req . params
43+ const { title, content } = req . body
3944 const userId = req . user . id . toString ( )
40- console . log ( req . user )
4145 try {
42- if ( ! id . match ( / ^ [ 0 - 9 a - f A - F ] { 24 } $ / ) ) {
43- return res . status ( HttpStatus . BAD_REQUEST ) . json ( { error : 'Invalid ticket id' } )
44- }
4546 const ticket = await TicketModel . findById ( id )
46- console . log ( ticket )
4747 if ( ! ticket ) {
4848 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
4949 }
5050 if ( userId !== ticket . createdBy && ! req . user . isAdmin ) {
51+ // Only user who created the ticket and admin can edit the ticket
5152 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
5253 }
5354 ticket . title = title
@@ -62,28 +63,105 @@ module.exports = {
6263 ticket : ticket
6364 } )
6465 } catch ( error ) {
65- HANDLER . handleError ( res , error )
66+ HANDLER . handleError ( res , {
67+ code : error . code || HttpStatus . BAD_REQUEST ,
68+ ...error
69+ } )
6670 }
6771 } ,
6872 deleteTicket : async ( req , res , next ) => {
6973 const { id } = req . params
7074 const userId = req . user . id . toString ( )
7175 try {
72- if ( ! id . match ( / ^ [ 0 - 9 a - f A - F ] { 24 } $ / ) ) {
73- return res . status ( HttpStatus . BAD_REQUEST ) . json ( { error : 'Invalid ticket id' } )
74- }
7576 const ticket = await TicketModel . findById ( id )
76- console . log ( ticket )
7777 if ( ! ticket ) {
7878 return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
7979 }
8080 if ( userId !== ticket . createdBy && ! req . user . isAdmin ) {
81+ // Only user who created the ticket and admin can delete the ticket
8182 return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Bad delete request' } )
8283 }
8384 await TicketModel . findByIdAndRemove ( id )
8485 res . status ( HttpStatus . OK ) . json ( { ticket : ticket } )
8586 } catch ( error ) {
86- HANDLER . handleError ( res , error )
87+ HANDLER . handleError ( res , {
88+ code : error . code || HttpStatus . BAD_REQUEST ,
89+ ...error
90+ } )
91+ }
92+ } ,
93+ editTag : async ( req , res , next ) => {
94+ const { id } = req . params
95+ const { tags } = req . body // tags is the array of tags to add
96+ const userId = req . user . id . toString ( )
97+ try {
98+ const ticket = await TicketModel . findById ( id )
99+ if ( ! ticket ) {
100+ return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
101+ }
102+ if ( userId !== ticket . createdBy && ! req . user . isAdmin ) {
103+ // Only user who created the ticket and admin can edit ticket tags
104+ return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
105+ }
106+ ticket . tags = [ ...new Set ( tags ) ]
107+ await ticket . save ( )
108+ res . status ( HttpStatus . OK ) . json ( { ticket : ticket } )
109+ } catch ( error ) {
110+ console . log ( error )
111+ HANDLER . handleError ( res , {
112+ code : error . code || HttpStatus . BAD_REQUEST ,
113+ ...error
114+ } )
115+ }
116+ } ,
117+ addTag : async ( req , res , next ) => {
118+ const { id, tag } = req . params
119+ const userId = req . user . id . toString ( )
120+ try {
121+ const ticket = await TicketModel . findById ( id )
122+ if ( ! ticket ) {
123+ return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
124+ }
125+ if ( userId !== ticket . createdBy && ! req . user . isAdmin ) {
126+ // Only user who created the ticket and admin can add tag to the ticket
127+ return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
128+ }
129+ ticket . tags . addToSet ( tag )
130+ await ticket . save ( )
131+ res . status ( HttpStatus . OK ) . json ( { ticket : ticket } )
132+ } catch ( error ) {
133+ console . log ( error )
134+ HANDLER . handleError ( res , {
135+ code : error . code || HttpStatus . BAD_REQUEST ,
136+ ...error
137+ } )
138+ }
139+ } ,
140+ deleteTag : async ( req , res , next ) => {
141+ const { id, tag } = req . params
142+ const userId = req . user . id . toString ( )
143+ try {
144+ const ticket = await TicketModel . findById ( id )
145+ if ( ! ticket ) {
146+ return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
147+ }
148+ if ( userId !== ticket . createdBy && ! req . user . isAdmin ) {
149+ // Only user who created the ticket and admin can delete tag from a ticket
150+ return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
151+ }
152+ if ( ticket . tags . indexOf ( tag ) === - 1 ) {
153+ return res . status ( HttpStatus . BAD_REQUEST ) . json ( { error : 'Tag not found on ticket' } )
154+ }
155+ const tags = ticket . tags
156+ ticket . tags = [ ...tags . filter ( ele => ! ( ele === tag ) ) ]
157+ await ticket . save ( )
158+ res . status ( HttpStatus . OK ) . json ( { ticket : ticket } )
159+ } catch ( error ) {
160+ console . log ( error )
161+ HANDLER . handleError ( res , {
162+ code : error . code || HttpStatus . BAD_REQUEST ,
163+ ...error
164+ } )
87165 }
88166 }
89167}
0 commit comments