11const HANDLER = require ( '../utils/response-helper' )
22const HttpStatus = require ( 'http-status-codes' )
33const TicketModel = require ( '../models/Ticket' )
4- const Ticket = require ( '../models/Ticket' )
54
65module . exports = {
6+
77 create : async ( req , res , next ) => {
88 const userId = req . user . id . toString ( )
99 try {
@@ -21,6 +21,7 @@ module.exports = {
2121 } )
2222 }
2323 } ,
24+
2425 getTicket : async ( req , res , next ) => {
2526 const { user } = req . query
2627 try {
@@ -39,6 +40,7 @@ module.exports = {
3940 } )
4041 }
4142 } ,
43+
4244 editTicket : async ( req , res , next ) => {
4345 const { id } = req . params
4446 const { title, content } = req . body
@@ -73,6 +75,7 @@ module.exports = {
7375 } )
7476 }
7577 } ,
78+
7679 deleteTicket : async ( req , res , next ) => {
7780 const { id } = req . params
7881 const userId = req . user . id . toString ( )
@@ -94,6 +97,7 @@ module.exports = {
9497 } )
9598 }
9699 } ,
100+
97101 editTag : async ( req , res , next ) => {
98102 const { id } = req . params
99103 const { tags } = req . body // tags is the array of tags to add
@@ -118,6 +122,7 @@ module.exports = {
118122 } )
119123 }
120124 } ,
125+
121126 addTag : async ( req , res , next ) => {
122127 const { id, tag } = req . params
123128 const userId = req . user . id . toString ( )
@@ -141,6 +146,114 @@ module.exports = {
141146 } )
142147 }
143148 } ,
149+
150+ // Create Comment of a Ticket
151+ createComment : async ( req , res , next ) => {
152+ const { id } = req . params
153+ const userId = req . user . id . toString ( )
154+ try {
155+ const ticket = await TicketModel . findById ( id )
156+ if ( ! ticket ) {
157+ return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
158+ }
159+ ticket . comments . push ( {
160+ ...req . body ,
161+ userId,
162+ postId : id
163+ } )
164+ await ticket . save ( )
165+ res . status ( HttpStatus . OK ) . json ( { ticket : ticket } )
166+ } catch ( error ) {
167+ console . log ( error )
168+ HANDLER . handleError ( res , {
169+ code : error . code || HttpStatus . BAD_REQUEST ,
170+ ...error
171+ } )
172+ }
173+ } ,
174+
175+ // Get Comments on a Ticket
176+ getComments : async ( req , res , next ) => {
177+ const { id } = req . params
178+ try {
179+ const ticket = await TicketModel . findById ( id )
180+ if ( ! ticket ) {
181+ return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
182+ }
183+ res . status ( HttpStatus . OK ) . json ( { comments : ticket . comments } )
184+ } catch ( error ) {
185+ console . log ( error )
186+ HANDLER . handleError ( res , {
187+ code : error . code || HttpStatus . BAD_REQUEST ,
188+ ...error
189+ } )
190+ }
191+ } ,
192+
193+ editComment : async ( req , res , next ) => {
194+ const { id, commentID } = req . params
195+ const { content } = req . body
196+ const userId = req . user . id . toString ( )
197+ try {
198+ const ticket = await TicketModel . findById ( id )
199+ if ( ! ticket ) {
200+ return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
201+ }
202+ const comment = ticket . comments . id ( commentID )
203+ if ( userId !== comment . createdBy && ! req . user . isAdmin ) {
204+ // Only user who created the comment and admin can edit the comment
205+ return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
206+ }
207+ comment . content = content
208+ comment . updatedAt = Date . now ( )
209+ await ticket . save ( )
210+ res . status ( HttpStatus . OK ) . json ( { comment : comment } )
211+ } catch ( error ) {
212+ console . log ( error )
213+ HANDLER . handleError ( res , {
214+ code : error . code || HttpStatus . BAD_REQUEST ,
215+ ...error
216+ } )
217+ }
218+ } ,
219+
220+ upVoteComment : async ( req , res , next ) => {
221+ res . status ( HttpStatus . OK ) . json ( {
222+ error : 'under development'
223+ } )
224+ } ,
225+
226+ downVoteComment : async ( req , res , next ) => {
227+ res . status ( HttpStatus . OK ) . json ( {
228+ error : 'under development'
229+ } )
230+ } ,
231+
232+ deleteComment : async ( req , res , next ) => {
233+ const { id, commentID } = req . params
234+ const userId = req . user . id . toString ( )
235+ try {
236+ const ticket = await TicketModel . findById ( id )
237+ if ( ! ticket ) {
238+ return res . status ( HttpStatus . NOT_FOUND ) . json ( { error : 'No ticket exist' } )
239+ }
240+ const comment = ticket . comments . id ( commentID )
241+ if ( userId !== comment . createdBy && ! req . user . isAdmin ) {
242+ // Only user who created the comment and admin can edit the comment
243+ return res . status ( HttpStatus . FORBIDDEN ) . json ( { error : 'Edit Forbidden by user' } )
244+ }
245+ comment . remove ( )
246+ await ticket . save ( )
247+ res . status ( HttpStatus . OK ) . json ( { comment : comment } )
248+ } catch ( error ) {
249+ console . log ( error )
250+ HANDLER . handleError ( res , {
251+ code : error . code || HttpStatus . BAD_REQUEST ,
252+ ...error
253+ } )
254+ }
255+ } ,
256+
144257 deleteTag : async ( req , res , next ) => {
145258 const { id, tag } = req . params
146259 const userId = req . user . id . toString ( )
0 commit comments