Skip to content

Commit 46d1aaf

Browse files
committed
editing adding and deleting tags
1 parent 429c545 commit 46d1aaf

File tree

4 files changed

+135
-21
lines changed

4 files changed

+135
-21
lines changed

app/controllers/ticket.js

Lines changed: 94 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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')
45

56
module.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-9a-fA-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-9a-fA-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
}

app/models/Ticket.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,28 @@ const ticketSchema = new Schema({
4141
}
4242
}
4343
},
44+
tags: [
45+
{
46+
type: String,
47+
trim: true,
48+
validate: (value) => {
49+
if (!validator.isLength(value, { min: 0, max: 20 })) {
50+
throw new Error('Tags should have between 0 to 20 characters')
51+
}
52+
}
53+
}
54+
],
4455
comments: [
4556
{
4657
content: {
4758
type: String,
4859
trim: true,
4960
minlength: 10,
50-
validate (shortDescription) {
51-
if (validator.isEmpty(shortDescription)) {
61+
validate (content) {
62+
if (validator.isEmpty(content)) {
5263
throw new Error('Comment cannot be empty')
5364
}
54-
if (!validator.isLength(shortDescription, { min: 10 })) {
65+
if (!validator.isLength(content, { min: 10 })) {
5566
throw new Error('Comment should be alteast 10 characters long!')
5667
}
5768
}

app/routes/ticket.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express')
22
const router = express.Router()
33
const auth = require('../middleware/auth')
4+
const { isValidObjectId } = require('../utils/utils')
45
const ticketController = require('../controllers/ticket')
56
const isUnderMaintenance = require('../middleware/maintenance')
67

@@ -14,7 +15,19 @@ router.get('/', isUnderMaintenance, auth, ticketController.getTicket)
1415
// router.get('/')
1516

1617
// EDIT A TICKET BY ID
17-
router.put('/:id', isUnderMaintenance, auth, ticketController.editTicket)
18+
router.put('/:id', isUnderMaintenance, auth, isValidObjectId, ticketController.editTicket)
19+
20+
// EDIT TAG TO A TICKET
21+
// expects an array of tags and replaces the existing tags with that array
22+
router.put('/:id/tag', isUnderMaintenance, auth, isValidObjectId, ticketController.editTag)
23+
24+
// ADD A TAG TO A TICKET
25+
// adds a single tag to ticket
26+
router.post('/:id/tag/:tag', isUnderMaintenance, auth, isValidObjectId, ticketController.addTag)
27+
28+
// REMOVE TAG ON A TICKET
29+
// removes a single tag from a ticket
30+
router.delete('/:id/tag/:tag', isUnderMaintenance, auth, isValidObjectId, ticketController.deleteTag)
1831

1932
// COMMENT ON A TICKET
2033
// router.post('/:id/comment')
@@ -26,6 +39,6 @@ router.put('/:id', isUnderMaintenance, auth, ticketController.editTicket)
2639
// router.put('/:ticketID/comment/:commentID')
2740

2841
// DELETE TICKET BY ID
29-
router.delete('/:id', isUnderMaintenance, auth, ticketController.deleteTicket)
42+
router.delete('/:id', isUnderMaintenance, auth, isValidObjectId, ticketController.deleteTicket)
3043

3144
module.exports = router

app/utils/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const HttpStatus = require('http-status-codes')
2+
3+
module.exports = {
4+
isValidObjectId: (req, res, next) => {
5+
const { id } = req.params
6+
if (!id.match(/^[0-9a-fA-F]{24}$/)) {
7+
res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
8+
} else {
9+
next()
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)