Skip to content

Commit a88c028

Browse files
committed
moderator
1 parent a7f6164 commit a88c028

File tree

4 files changed

+195
-24
lines changed

4 files changed

+195
-24
lines changed

app/controllers/ticket.js

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const UserModel = require('../models/User')
12
const HttpStatus = require('http-status-codes')
23
const TicketModel = require('../models/Ticket')
34
const TAGS = require('../utils/notificationTags')
@@ -21,7 +22,7 @@ module.exports = {
2122
notification.heading = 'New Support Ticket!'
2223
notification.content = `${req.user.name.firstName} ${req.user.name.lastName} Creted a new Support Ticket!`
2324
notification.tag = TAGS.NEW
24-
await ticketNotificationHelper.addToNotificationForAdmin(req, res, notification, next)
25+
await ticketNotificationHelper.addToNotificationForModerator(req, res, notification, next)
2526
await ticket.save()
2627
res.status(HttpStatus.CREATED).json({
2728
ticket: ticket
@@ -62,7 +63,7 @@ module.exports = {
6263
if (!ticket) {
6364
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No ticket exist' })
6465
}
65-
if (userId !== ticket.createdBy && !req.user.isAdmin) {
66+
if (userId !== ticket.createdBy && !req.user.isAdmin && !req.user.isTicketsModerator) {
6667
// Only user who created the ticket and admin can edit the ticket
6768
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Edit Forbidden by user' })
6869
}
@@ -100,7 +101,7 @@ module.exports = {
100101
if (!ticket) {
101102
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No ticket exist' })
102103
}
103-
if (userId !== ticket.createdBy && !req.user.isAdmin) {
104+
if (userId !== ticket.createdBy && !req.user.isAdmin && !req.user.isTicketsModerator) {
104105
// Only user who created the ticket and admin can delete the ticket
105106
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Bad delete request' })
106107
}
@@ -123,7 +124,7 @@ module.exports = {
123124
if (!ticket) {
124125
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No ticket exist' })
125126
}
126-
if (userId !== ticket.createdBy && !req.user.isAdmin) {
127+
if (userId !== ticket.createdBy && !req.user.isAdmin && !req.user.isTicketsModerator) {
127128
// Only user who created the ticket and admin can edit ticket tags
128129
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Edit Forbidden by user' })
129130
}
@@ -147,7 +148,7 @@ module.exports = {
147148
if (!ticket) {
148149
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No ticket exist' })
149150
}
150-
if (userId !== ticket.createdBy && !req.user.isAdmin) {
151+
if (userId !== ticket.createdBy && !req.user.isAdmin && !req.user.isTicketsModerator) {
151152
// Only user who created the ticket and admin can add tag to the ticket
152153
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Edit Forbidden by user' })
153154
}
@@ -220,7 +221,7 @@ module.exports = {
220221
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No ticket exist' })
221222
}
222223
const comment = ticket.comments.id(commentID)
223-
if (userId !== comment.createdBy && !req.user.isAdmin) {
224+
if (userId !== comment.createdBy && !req.user.isAdmin && !req.user.isTicketsModerator) {
224225
// Only user who created the comment and admin can edit the comment
225226
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Edit Forbidden by user' })
226227
}
@@ -324,7 +325,7 @@ module.exports = {
324325
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No ticket exist' })
325326
}
326327
const comment = ticket.comments.id(commentID)
327-
if (userId !== comment.createdBy && !req.user.isAdmin) {
328+
if (userId !== comment.createdBy && !req.user.isAdmin && !req.user.isTicketsModerator) {
328329
// Only user who created the comment and admin can edit the comment
329330
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Edit Forbidden by user' })
330331
}
@@ -340,6 +341,65 @@ module.exports = {
340341
}
341342
},
342343

344+
getModerators: async (req, res, next) => {
345+
try {
346+
const moderators = await UserModel.find({ isTicketsModerator: true })
347+
return res.status(HttpStatus.OK).json({ moderators: moderators })
348+
} catch (error) {
349+
console.log(error)
350+
HANDLER.handleError(res, {
351+
code: error.code || HttpStatus.BAD_REQUEST,
352+
...error
353+
})
354+
}
355+
},
356+
357+
addModerator: async (req, res, next) => {
358+
// id of User to add as moderator
359+
const { id } = req.params
360+
try {
361+
if (!req.user.isAdmin) {
362+
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Only Admin user can add moderator' })
363+
}
364+
const user = await UserModel.findById(id)
365+
if (!user) {
366+
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No user exist' })
367+
}
368+
user.isTicketsModerator = true
369+
await user.save()
370+
return res.status(HttpStatus.OK).json({ success: 'Add moderator successful' })
371+
} catch (error) {
372+
console.log(error)
373+
HANDLER.handleError(res, {
374+
code: error.code || HttpStatus.BAD_REQUEST,
375+
...error
376+
})
377+
}
378+
},
379+
380+
removeModerator: async (req, res, next) => {
381+
// id of User to add as moderator
382+
const { id } = req.params
383+
try {
384+
if (!req.user.isAdmin) {
385+
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Only Admin user can remove moderator' })
386+
}
387+
const user = await UserModel.findById(id)
388+
if (!user) {
389+
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No user exist' })
390+
}
391+
user.isTicketsModerator = false
392+
await user.save()
393+
return res.status(HttpStatus.OK).json({ success: 'Remove moderator successful' })
394+
} catch (error) {
395+
console.log(error)
396+
HANDLER.handleError(res, {
397+
code: error.code || HttpStatus.BAD_REQUEST,
398+
...error
399+
})
400+
}
401+
},
402+
343403
deleteTag: async (req, res, next) => {
344404
const { id, tag } = req.params
345405
const userId = req.user.id.toString()
@@ -348,7 +408,7 @@ module.exports = {
348408
if (!ticket) {
349409
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No ticket exist' })
350410
}
351-
if (userId !== ticket.createdBy && !req.user.isAdmin) {
411+
if (userId !== ticket.createdBy && !req.user.isAdmin && !req.user.isTicketsModerator) {
352412
// Only user who created the ticket and admin can delete tag from a ticket
353413
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Edit Forbidden by user' })
354414
}

app/models/User.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ const UserSchema = new mongoose.Schema({
223223
type: Boolean,
224224
default: false
225225
},
226+
isTicketsModerator: {
227+
type: Boolean,
228+
default: false
229+
},
226230
isActivated: {
227231
type: Boolean,
228232
default: false

app/routes/ticket.js

Lines changed: 115 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,147 @@ const ticketController = require('../controllers/ticket')
66
const isUnderMaintenance = require('../middleware/maintenance')
77

88
// CREATE A TICKET
9-
router.post('/', isUnderMaintenance, auth, ticketController.create)
9+
router.post(\
10+
'/',
11+
isUnderMaintenance,
12+
auth,
13+
ticketController.create
14+
)
1015

1116
// GET TICKETS - ALL OR FILTERED
12-
router.get('/', isUnderMaintenance, auth, ticketController.getTicket)
13-
14-
// GET ALL TICKETS OPENED BY LOGGED IN USER
15-
// router.get('/')
17+
router.get(
18+
'/',
19+
isUnderMaintenance,
20+
auth,
21+
ticketController.getTicket
22+
)
1623

1724
// EDIT A TICKET BY ID
18-
router.put('/:id', isUnderMaintenance, auth, isValidObjectId, ticketController.editTicket)
25+
router.put(
26+
'/:id',
27+
isUnderMaintenance,
28+
auth,
29+
isValidObjectId,
30+
ticketController.editTicket
31+
)
1932

2033
// EDIT TAG TO A TICKET
2134
// expects an array of tags and replaces the existing tags with that array
22-
router.put('/:id/tag', isUnderMaintenance, auth, isValidObjectId, ticketController.editTag)
35+
router.put(
36+
'/:id/tag',
37+
isUnderMaintenance,
38+
auth,
39+
isValidObjectId,
40+
ticketController.editTag
41+
)
2342

2443
// ADD A TAG TO A TICKET
2544
// adds a single tag to ticket
26-
router.post('/:id/tag/:tag', isUnderMaintenance, auth, isValidObjectId, ticketController.addTag)
45+
router.post(
46+
'/:id/tag/:tag',
47+
isUnderMaintenance,
48+
auth,
49+
isValidObjectId,
50+
ticketController.addTag
51+
)
2752

2853
// REMOVE TAG ON A TICKET
2954
// removes a single tag from a ticket
30-
router.delete('/:id/tag/:tag', isUnderMaintenance, auth, isValidObjectId, ticketController.deleteTag)
55+
router.delete(
56+
'/:id/tag/:tag',
57+
isUnderMaintenance,
58+
auth,
59+
isValidObjectId,
60+
ticketController.deleteTag
61+
)
3162

3263
// COMMENT ON A TICKET
33-
router.post('/:id/comment', isUnderMaintenance, auth, isValidObjectId, ticketController.createComment)
64+
router.post(
65+
'/:id/comment',
66+
isUnderMaintenance,
67+
auth,
68+
isValidObjectId,
69+
ticketController.createComment
70+
)
3471

3572
// GET TICKET COMMENTS BY TICKET ID
36-
router.get('/:id/comments', isUnderMaintenance, auth, isValidObjectId, ticketController.getComments)
73+
router.get(
74+
'/:id/comments',
75+
isUnderMaintenance,
76+
auth,
77+
isValidObjectId,
78+
ticketController.getComments
79+
)
3780

3881
// EDIT TICKET COMMENT BY ID
39-
router.put('/:id/comment/:commentID', isUnderMaintenance, auth, isValidObjectId, ticketController.editComment)
82+
router.put(
83+
'/:id/comment/:commentID',
84+
isUnderMaintenance,
85+
auth,
86+
isValidObjectId,
87+
ticketController.editComment
88+
)
4089

4190
// UPVOTE TICKET COMMENT
42-
router.put('/:id/comment/:commentID/upvote', isUnderMaintenance, auth, isValidObjectId, ticketController.upVoteComment)
91+
router.put(
92+
'/:id/comment/:commentID/upvote',
93+
isUnderMaintenance,
94+
auth,
95+
isValidObjectId,
96+
ticketController.upVoteComment
97+
)
4398

4499
// DOWNVOTE TICKET COMMENT
45-
router.put('/:id/comment/:commentID/downvote', isUnderMaintenance, auth, isValidObjectId, ticketController.downVoteComment)
100+
router.put(
101+
'/:id/comment/:commentID/downvote',
102+
isUnderMaintenance,
103+
auth,
104+
isValidObjectId,
105+
ticketController.downVoteComment)
106+
107+
// ADD TICKET MODERATOR
108+
router.post(
109+
'/moderator/:id',
110+
isUnderMaintenance,
111+
auth,
112+
isValidObjectId,
113+
ticketController.addModerator
114+
)
115+
116+
// GET ALL TICKET MODERATORS
117+
router.get(
118+
'/moderator/:id',
119+
isUnderMaintenance,
120+
auth,
121+
isValidObjectId,
122+
ticketController.getModerators
123+
)
124+
125+
// REMOVE TICKET MODERATOR
126+
router.delete(
127+
'/moderator/:id',
128+
isUnderMaintenance,
129+
auth,
130+
isValidObjectId,
131+
ticketController.removeModerator
132+
)
46133

47134
// DELETE TICKET COMMENT BY ID
48-
router.delete('/:id/comment/:commentID', isUnderMaintenance, auth, isValidObjectId, ticketController.deleteComment)
135+
router.delete(
136+
'/:id/comment/:commentID',
137+
isUnderMaintenance,
138+
auth,
139+
isValidObjectId,
140+
ticketController.deleteComment
141+
)
49142

50143
// DELETE TICKET BY ID
51-
router.delete('/:id', isUnderMaintenance, auth, isValidObjectId, ticketController.deleteTicket)
144+
router.delete(
145+
'/:id',
146+
isUnderMaintenance,
147+
auth,
148+
isValidObjectId,
149+
ticketController.deleteTicket
150+
)
52151

53152
module.exports = router

app/utils/ticket-notif-helper.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ module.exports = {
1010
console.log(error)
1111
}
1212
},
13+
addToNotificationForModerator: async (req, res, obj, next) => {
14+
try {
15+
console.log('adding to admin\'s notifications')
16+
await User.updateMany({ isTicketsModerator: true }, { $push: { ticketNotifications: { $each: [obj], $position: 0 } } })
17+
} catch (error) {
18+
console.log(error)
19+
}
20+
},
1321
addToNotificationForUser: async (userId, res, obj, next) => {
1422
try {
1523
console.log('adding to user\'s notifications')

0 commit comments

Comments
 (0)