Skip to content

Commit 429c545

Browse files
committed
Basic CRUD controllers and Model
1 parent 1e5a045 commit 429c545

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const notificationRouter = require('./app/routes/notification')
2828
const proposalRouter = require('./app/routes/proposal')
2929
const analyticsRouter = require('./app/routes/analytics')
3030
const activityRouter = require('./app/routes/activity')
31+
const ticketRouter = require('./app/routes/ticket')
3132

3233
const app = express()
3334
const server = require('http').Server(app)
@@ -107,6 +108,7 @@ app.use('/project', projectRouter)
107108
app.use('/proposal', proposalRouter)
108109
app.use('/analytics', analyticsRouter)
109110
app.use('/activity', activityRouter)
111+
app.use('/ticket', ticketRouter)
110112

111113
// catch 404 and forward to error handler
112114
app.use(function (req, res, next) {

app/controllers/ticket.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const HANDLER = require('../utils/response-helper')
2+
const HttpStatus = require('http-status-codes')
3+
const TicketModel = require('../models/Ticket')
4+
5+
module.exports = {
6+
create: async (req, res, next) => {
7+
const userId = req.user.id.toString()
8+
try {
9+
const ticket = new TicketModel(req.body)
10+
ticket.createdBy = userId
11+
await ticket.save()
12+
res.status(HttpStatus.CREATED).json({
13+
ticket: ticket
14+
})
15+
} catch (error) {
16+
HANDLER.handleError(res, error)
17+
}
18+
console.log(userId)
19+
console.log('Create called')
20+
},
21+
getTicket: async (req, res, next) => {
22+
const { user } = req.query
23+
try {
24+
let tickets
25+
if (user === 'me') {
26+
const userId = req.user.id.toString()
27+
tickets = await TicketModel.find({ createdBy: userId }).lean().exec()
28+
} else {
29+
tickets = await TicketModel.find({}).lean().exec()
30+
}
31+
res.status(HttpStatus.OK).json({ tickets: tickets })
32+
} catch (error) {
33+
HANDLER.handleError(res, error)
34+
}
35+
},
36+
editTicket: async (req, res, next) => {
37+
const { title, content } = req.body
38+
const { id } = req.params
39+
const userId = req.user.id.toString()
40+
console.log(req.user)
41+
try {
42+
if (!id.match(/^[0-9a-fA-F]{24}$/)) {
43+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
44+
}
45+
const ticket = await TicketModel.findById(id)
46+
console.log(ticket)
47+
if (!ticket) {
48+
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No ticket exist' })
49+
}
50+
if (userId !== ticket.createdBy && !req.user.isAdmin) {
51+
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Edit Forbidden by user' })
52+
}
53+
ticket.title = title
54+
if (content.shortDescription) {
55+
ticket.content.shortDescription = content.shortDescription
56+
}
57+
if (content.longDescription) {
58+
ticket.content.longDescription = content.longDescription
59+
}
60+
await ticket.save()
61+
res.status(HttpStatus.OK).json({
62+
ticket: ticket
63+
})
64+
} catch (error) {
65+
HANDLER.handleError(res, error)
66+
}
67+
},
68+
deleteTicket: async (req, res, next) => {
69+
const { id } = req.params
70+
const userId = req.user.id.toString()
71+
try {
72+
if (!id.match(/^[0-9a-fA-F]{24}$/)) {
73+
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'Invalid ticket id' })
74+
}
75+
const ticket = await TicketModel.findById(id)
76+
console.log(ticket)
77+
if (!ticket) {
78+
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No ticket exist' })
79+
}
80+
if (userId !== ticket.createdBy && !req.user.isAdmin) {
81+
return res.status(HttpStatus.FORBIDDEN).json({ error: 'Bad delete request' })
82+
}
83+
await TicketModel.findByIdAndRemove(id)
84+
res.status(HttpStatus.OK).json({ ticket: ticket })
85+
} catch (error) {
86+
HANDLER.handleError(res, error)
87+
}
88+
}
89+
}

app/models/Ticket.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const mongoose = require('mongoose')
2+
const validator = require('validator')
3+
const Schema = mongoose.Schema
4+
5+
const ticketSchema = new Schema({
6+
title: {
7+
type: String,
8+
trim: true,
9+
required: true
10+
},
11+
createdBy: {
12+
type: Schema.Types.ObjectId,
13+
ref: 'User'
14+
},
15+
content: {
16+
shortDescription: {
17+
type: String,
18+
required: true,
19+
trim: true,
20+
minlength: 5,
21+
validate (shortDescription) {
22+
if (validator.isEmpty(shortDescription)) {
23+
throw new Error('Short description is required!')
24+
}
25+
if (!validator.isLength(shortDescription, { min: 10 })) {
26+
throw new Error('Short description should be min 5 characters long!')
27+
}
28+
}
29+
},
30+
longDescription: {
31+
type: String,
32+
trim: true,
33+
minlength: 10,
34+
validate (longDescription) {
35+
if (validator.isEmpty(longDescription)) {
36+
throw new Error('Long description is required!')
37+
}
38+
if (!validator.isLength(longDescription, { min: 10 })) {
39+
throw new Error('Long description should be min 10 characters long!')
40+
}
41+
}
42+
}
43+
},
44+
comments: [
45+
{
46+
content: {
47+
type: String,
48+
trim: true,
49+
minlength: 10,
50+
validate (shortDescription) {
51+
if (validator.isEmpty(shortDescription)) {
52+
throw new Error('Comment cannot be empty')
53+
}
54+
if (!validator.isLength(shortDescription, { min: 10 })) {
55+
throw new Error('Comment should be alteast 10 characters long!')
56+
}
57+
}
58+
},
59+
createdBy: {
60+
type: Schema.Types.ObjectId,
61+
ref: 'User'
62+
},
63+
createdAt: {
64+
type: Date,
65+
required: true,
66+
default: Date.now()
67+
}
68+
}
69+
],
70+
createdAt: {
71+
type: Date,
72+
required: true,
73+
default: Date.now()
74+
},
75+
updatedAt: {
76+
type: Date,
77+
required: true,
78+
default: Date.now()
79+
}
80+
})
81+
82+
module.exports = mongoose.model('Ticket', ticketSchema)

app/routes/ticket.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const express = require('express')
2+
const router = express.Router()
3+
const auth = require('../middleware/auth')
4+
const ticketController = require('../controllers/ticket')
5+
const isUnderMaintenance = require('../middleware/maintenance')
6+
7+
// CREATE A TICKET
8+
router.post('/', isUnderMaintenance, auth, ticketController.create)
9+
10+
// GET TICKETS - ALL OR FILTERED
11+
router.get('/', isUnderMaintenance, auth, ticketController.getTicket)
12+
13+
// GET ALL TICKETS OPENED BY LOGGED IN USER
14+
// router.get('/')
15+
16+
// EDIT A TICKET BY ID
17+
router.put('/:id', isUnderMaintenance, auth, ticketController.editTicket)
18+
19+
// COMMENT ON A TICKET
20+
// router.post('/:id/comment')
21+
22+
// GET TICKET COMMENTS BY TICKET ID
23+
// router.get('/:id/comment')
24+
25+
// EDIT TICKET COMMENT BY ID
26+
// router.put('/:ticketID/comment/:commentID')
27+
28+
// DELETE TICKET BY ID
29+
router.delete('/:id', isUnderMaintenance, auth, ticketController.deleteTicket)
30+
31+
module.exports = router

0 commit comments

Comments
 (0)