Skip to content

Commit 72fd852

Browse files
committed
comments
1 parent 370c571 commit 72fd852

File tree

5 files changed

+133
-32
lines changed

5 files changed

+133
-32
lines changed

app/controllers/comment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const HANDLER = require('../utils/response-helper')
22
const HttpStatus = require('http-status-codes')
3-
const CommentModel = require('../models/Comment')
3+
const CommentModel = require('../models/Comment').model
44
const permission = require('../utils/permission')
55
const helper = require('../utils/paginate')
66
const activityTracker = require('../utils/activity-helper')

app/controllers/ticket.js

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const HANDLER = require('../utils/response-helper')
22
const HttpStatus = require('http-status-codes')
33
const TicketModel = require('../models/Ticket')
4-
const Ticket = require('../models/Ticket')
54

65
module.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()

app/models/Comment.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ const commentSchema = new Schema({
4747
}
4848
})
4949

50-
module.exports = mongoose.model('Comment', commentSchema)
50+
module.exports = {
51+
model: mongoose.model('Comment', commentSchema),
52+
schema: commentSchema
53+
}

app/models/Ticket.js

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const mongoose = require('mongoose')
22
const validator = require('validator')
33
const Schema = mongoose.Schema
4+
const commentSchema = require('./Comment').schema
45

56
const ticketSchema = new Schema({
67
title: {
@@ -79,32 +80,7 @@ const ticketSchema = new Schema({
7980
}
8081
}
8182
],
82-
comments: [
83-
{
84-
content: {
85-
type: String,
86-
trim: true,
87-
minlength: 10,
88-
validate (content) {
89-
if (validator.isEmpty(content)) {
90-
throw new Error('Comment cannot be empty')
91-
}
92-
if (!validator.isLength(content, { min: 10 })) {
93-
throw new Error('Comment should be alteast 10 characters long!')
94-
}
95-
}
96-
},
97-
createdBy: {
98-
type: Schema.Types.ObjectId,
99-
ref: 'User'
100-
},
101-
createdAt: {
102-
type: Date,
103-
required: true,
104-
default: Date.now()
105-
}
106-
}
107-
],
83+
comments: [commentSchema], // mongoose subdocument
10884
createdAt: {
10985
type: Date,
11086
required: true,

app/routes/ticket.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,22 @@ router.post('/:id/tag/:tag', isUnderMaintenance, auth, isValidObjectId, ticketCo
3030
router.delete('/:id/tag/:tag', isUnderMaintenance, auth, isValidObjectId, ticketController.deleteTag)
3131

3232
// COMMENT ON A TICKET
33-
// router.post('/:id/comment')
33+
router.post('/:id/comment', isUnderMaintenance, auth, isValidObjectId, ticketController.createComment)
3434

3535
// GET TICKET COMMENTS BY TICKET ID
36-
// router.get('/:id/comment')
36+
router.get('/:id/comments', isUnderMaintenance, auth, isValidObjectId, ticketController.getComments)
3737

3838
// EDIT TICKET COMMENT BY ID
39-
// router.put('/:ticketID/comment/:commentID')
39+
router.put('/:id/comment/:commentID', isUnderMaintenance, auth, isValidObjectId, ticketController.editComment)
40+
41+
// UPVOTE TICKET COMMENT
42+
router.put(':id/comment/:commentID/upvote', isUnderMaintenance, auth, isValidObjectId, ticketController.upVoteComment)
43+
44+
// DOWNVOTE TICKET COMMENT
45+
router.put(':id/comment/:commentID/downvote', isUnderMaintenance, auth, isValidObjectId, ticketController.downVoteComment)
46+
47+
// DELETE TICKET COMMENT BY ID
48+
router.delete('/:id/comment/:commentID', isUnderMaintenance, auth, isValidObjectId, ticketController.deleteComment)
4049

4150
// DELETE TICKET BY ID
4251
router.delete('/:id', isUnderMaintenance, auth, isValidObjectId, ticketController.deleteTicket)

0 commit comments

Comments
 (0)