Skip to content

Commit 5f130b4

Browse files
committed
suggested changes
1 parent 8f9fd47 commit 5f130b4

File tree

8 files changed

+69
-75
lines changed

8 files changed

+69
-75
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').model
3+
const CommentModel = require('../models/Comment')
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: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const HttpStatus = require('http-status-codes')
33
const TicketModel = require('../models/Ticket')
44
const TAGS = require('../utils/notificationTags')
55
const HANDLER = require('../utils/response-helper')
6-
const ticketNotificationHelper = require('../utils/ticket-notif-helper')
6+
const { isValidObjectId } = require('../utils/ticket-helper')
7+
const ticketNotificationHelper = require('../utils/ticket-helper')
78

89
const notification = {
910
heading: '',
@@ -14,7 +15,6 @@ const notification = {
1415
module.exports = {
1516

1617
create: async (req, res, next) => {
17-
await (new Promise(resolve => setTimeout(resolve, 2000)))
1818
const userId = req.user.id.toString()
1919
try {
2020
const allTickets = (await TicketModel.find({}))
@@ -49,17 +49,8 @@ module.exports = {
4949
},
5050

5151
getTicket: async (req, res, next) => {
52-
const { user } = req.query
5352
try {
54-
let tickets
55-
if (user === 'me') {
56-
const userId = req.user.id.toString()
57-
console.log(userId)
58-
console.log(`${req.user.name.firstName} ${req.user.name.lastName}`)
59-
tickets = await TicketModel.find({ 'createdBy.id': userId }).lean().select('shortDescription number createdAt createdBy status title shortDescription comments tags').exec()
60-
} else {
61-
tickets = await TicketModel.find({}).lean().select('shortDescription number createdAt createdBy status title comments tags').exec()
62-
}
53+
const tickets = await TicketModel.find({}).lean().select('shortDescription number createdAt createdBy status title comments tags').exec()
6354
tickets.forEach(ticket => {
6455
ticket.comments = ticket.comments.length
6556
ticket.createdBy = {
@@ -77,8 +68,10 @@ module.exports = {
7768
},
7869

7970
getTicketFull: async (req, res, next) => {
80-
await (new Promise(resolve => setTimeout(resolve, 2000)))
8171
const { id } = req.params
72+
if (!isValidObjectId(id)) {
73+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
74+
}
8275
try {
8376
const ticket = await TicketModel.findById(id)
8477
if (!ticket) {
@@ -98,10 +91,10 @@ module.exports = {
9891
editTicket: async (req, res, next) => {
9992
const { id } = req.params
10093
const { type } = req.body
101-
await (new Promise(resolve => setTimeout(resolve, 2000)))
102-
// const allowedUpdates = ['title', 'shortDescription', 'content', 'status']
103-
// const updates = Object.keys(req.body)
10494
const userId = req.user.id.toString()
95+
if (!isValidObjectId(id)) {
96+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
97+
}
10598
try {
10699
const ticket = await TicketModel.findById(id)
107100
if (!ticket) {
@@ -146,9 +139,11 @@ module.exports = {
146139
},
147140

148141
deleteTicket: async (req, res, next) => {
149-
await (new Promise(resolve => setTimeout(resolve, 2000)))
150142
const { id } = req.params
151143
const userId = req.user.id.toString()
144+
if (!isValidObjectId(id)) {
145+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
146+
}
152147
try {
153148
const ticket = await TicketModel.findById(id)
154149
if (!ticket) {
@@ -172,6 +167,9 @@ module.exports = {
172167
const { id } = req.params
173168
const { tags } = req.body // tags is the array of tags to add
174169
const userId = req.user.id.toString()
170+
if (!isValidObjectId(id)) {
171+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
172+
}
175173
try {
176174
const ticket = await TicketModel.findById(id)
177175
if (!ticket) {
@@ -194,9 +192,11 @@ module.exports = {
194192
},
195193

196194
addTag: async (req, res, next) => {
197-
await (new Promise(resolve => setTimeout(resolve, 2000)))
198195
const { id, tag } = req.params
199196
const userId = req.user.id.toString()
197+
if (!isValidObjectId(id)) {
198+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
199+
}
200200
try {
201201
const ticket = await TicketModel.findById(id)
202202
if (!ticket) {
@@ -225,9 +225,11 @@ module.exports = {
225225

226226
// Create Comment of a Ticket
227227
createComment: async (req, res, next) => {
228-
await (new Promise(resolve => setTimeout(resolve, 2000)))
229228
const { id } = req.params
230229
const userId = req.user.id.toString()
230+
if (!isValidObjectId(id)) {
231+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
232+
}
231233
try {
232234
const ticket = await TicketModel.findById(id)
233235
if (!ticket) {
@@ -264,6 +266,9 @@ module.exports = {
264266
// Get Comments on a Ticket
265267
getComments: async (req, res, next) => {
266268
const { id } = req.params
269+
if (!isValidObjectId(id)) {
270+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
271+
}
267272
try {
268273
const ticket = await TicketModel.findById(id)
269274
if (!ticket) {
@@ -283,6 +288,9 @@ module.exports = {
283288
const { id, commentID } = req.params
284289
const { content } = req.body
285290
const userId = req.user.id.toString()
291+
if (!isValidObjectId(id)) {
292+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid comment id' })
293+
}
286294
try {
287295
const ticket = await TicketModel.findById(id)
288296
if (!ticket) {
@@ -307,9 +315,11 @@ module.exports = {
307315
},
308316

309317
upVoteComment: async (req, res, next) => {
310-
await (new Promise(resolve => setTimeout(resolve, 2000)))
311318
const { id, commentID } = req.params
312319
const userId = req.user.id.toString()
320+
if (!isValidObjectId(id)) {
321+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
322+
}
313323
try {
314324
const ticket = await TicketModel.findById(id)
315325
if (!ticket) {
@@ -346,9 +356,11 @@ module.exports = {
346356
},
347357

348358
downVoteComment: async (req, res, next) => {
349-
await (new Promise(resolve => setTimeout(resolve, 2000)))
350359
const { id, commentID } = req.params
351360
const userId = req.user.id.toString()
361+
if (!isValidObjectId(id)) {
362+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
363+
}
352364
try {
353365
const ticket = await TicketModel.findById(id)
354366
if (!ticket) {
@@ -385,8 +397,10 @@ module.exports = {
385397
},
386398

387399
deleteComment: async (req, res, next) => {
388-
await (new Promise(resolve => setTimeout(resolve, 2000)))
389400
const { id, commentID } = req.params
401+
if (!isValidObjectId(id)) {
402+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
403+
}
390404
const userId = req.user.id.toString()
391405
try {
392406
const ticket = await TicketModel.findById(id)
@@ -411,7 +425,6 @@ module.exports = {
411425
},
412426

413427
getUsers: async (req, res, next) => {
414-
await (new Promise(resolve => setTimeout(resolve, 2000)))
415428
try {
416429
const users = await UserModel.find({isAdmin: false}).lean().select('name email info isTicketsModerator').exec()
417430
return res.status(HttpStatus.OK).json({ users: users })
@@ -438,10 +451,10 @@ module.exports = {
438451
},
439452

440453
addModerator: async (req, res, next) => {
441-
await (new Promise(resolve => setTimeout(resolve, 2000)))
442-
// return res.status(HttpStatus.BAD_REQUEST).json({ error: 'No ticket exist' })
443-
// id of User to add as moderator
444454
const { id } = req.params
455+
if (!isValidObjectId(id)) {
456+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
457+
}
445458
try {
446459
if (!req.user.isAdmin) {
447460
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Only Admin user can add moderator' })
@@ -464,10 +477,10 @@ module.exports = {
464477
},
465478

466479
removeModerator: async (req, res, next) => {
467-
await (new Promise(resolve => setTimeout(resolve, 2000)))
468-
// return res.status(HttpStatus.BAD_REQUEST).json({ error: 'No ticket exist' })
469-
// id of User to add as moderator
470480
const { id } = req.params
481+
if (!isValidObjectId(id)) {
482+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid user id' })
483+
}
471484
try {
472485
if (!req.user.isAdmin) {
473486
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Only Admin user can remove moderator' })
@@ -490,9 +503,11 @@ module.exports = {
490503
},
491504

492505
deleteTag: async (req, res, next) => {
493-
await (new Promise(resolve => setTimeout(resolve, 2000)))
494506
const { id, tag } = req.params
495507
const userId = req.user.id.toString()
508+
if (!isValidObjectId(id)) {
509+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
510+
}
496511
try {
497512
const ticket = await TicketModel.findById(id)
498513
if (!ticket) {

app/models/Comment.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,12 @@ const commentSchema = new Schema({
3737
},
3838
createdAt: {
3939
type: Date,
40-
required: true,
4140
default: Date.now()
4241
},
4342
updatedAt: {
4443
type: Date,
45-
required: true,
4644
default: Date.now()
4745
}
4846
})
4947

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

app/models/User.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ const UserSchema = new mongoose.Schema({
183183
},
184184
createdAt: {
185185
type: Date,
186-
required: true,
187186
default: Date.now()
188187
}
189188
}

app/routes/notification.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,19 @@ router.get(
2121
)
2222

2323
// GET NOTICATIONS FOR PROPOSALS
24-
router.get('/proposal/all', notificationController.getProposalNotifications)
24+
router.get(
25+
'/proposal/all',
26+
isUnderMaintenance,
27+
auth,
28+
notificationController.getProposalNotifications
29+
)
2530

2631
// GET TICKET NOTIFICATIONS FOR LOGGED IN USER
27-
router.get('/ticket/user/all', isUnderMaintenance, auth, notificationController.getTicketNotifications)
32+
router.get(
33+
'/ticket/user/all',
34+
isUnderMaintenance,
35+
auth,
36+
notificationController.getTicketNotifications
37+
)
2838

2939
module.exports = router

0 commit comments

Comments
 (0)