Skip to content

Commit 8f3e369

Browse files
committed
notify admins on new ticket creation
1 parent 0db2a26 commit 8f3e369

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

app/controllers/notification.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,26 @@ module.exports = {
5050
} catch (error) {
5151
HANDLER.handleError(res, error)
5252
}
53+
},
54+
55+
// GET LOGGED IN USER TICKET NOTIFICATIONS
56+
getTicketNotifications: async (req, res, next) => {
57+
const userId = req.user._id
58+
try {
59+
const user = await User.findById(userId)
60+
if (!user) {
61+
return res
62+
.status(HttpStatus.BAD_REQUEST)
63+
.json({ msg: 'No such user exists!' })
64+
}
65+
// get ticket notifications of existing user
66+
const notifications = user.ticketNotifications
67+
if (notifications.length === 0) {
68+
return res.status(HttpStatus.OK).json({ msg: 'No ticket notifications!' })
69+
}
70+
return res.status(HttpStatus.OK).json({ notifications })
71+
} catch (error) {
72+
HANDLER.handleError(res, error)
73+
}
5374
}
5475
}

app/controllers/ticket.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
const HANDLER = require('../utils/response-helper')
21
const HttpStatus = require('http-status-codes')
32
const TicketModel = require('../models/Ticket')
3+
const TAGS = require('../utils/notificationTags')
4+
const HANDLER = require('../utils/response-helper')
5+
const ticketNotificationHelper = require('../utils/ticket-notif-helper')
6+
7+
const notification = {
8+
heading: '',
9+
content: '',
10+
tag: ''
11+
}
412

513
module.exports = {
614

@@ -10,6 +18,10 @@ module.exports = {
1018
const ticket = new TicketModel(req.body)
1119
ticket.createdBy = userId
1220
ticket.history.push({ ...req.body, editedBy: userId })
21+
notification.heading = 'New Support Ticket!'
22+
notification.content = `${req.user.name.firstName} ${req.user.name.lastName} Creted a new Support Ticket!`
23+
notification.tag = TAGS.NEW
24+
await ticketNotificationHelper.addToNotificationForAdmin(req, res, notification, next)
1325
await ticket.save()
1426
res.status(HttpStatus.CREATED).json({
1527
ticket: ticket

app/models/User.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,24 @@ const UserSchema = new mongoose.Schema({
170170
}
171171
}
172172
],
173+
ticketNotifications: [
174+
{
175+
heading: {
176+
type: String
177+
},
178+
content: {
179+
type: String
180+
},
181+
tag: {
182+
type: String
183+
},
184+
createdAt: {
185+
type: Date,
186+
required: true,
187+
default: Date.now()
188+
}
189+
}
190+
],
173191
followers: [
174192
{
175193
type: mongoose.Schema.Types.ObjectId,

app/routes/notification.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ router.get(
2323
// GET NOTICATIONS FOR PROPOSALS
2424
router.get('/proposal/all', notificationController.getProposalNotifications)
2525

26+
// GET TICKET NOTIFICATIONS FOR LOGGED IN USER
27+
router.get('/ticket/user/all', isUnderMaintenance, auth, notificationController.getTicketNotifications)
28+
2629
module.exports = router

app/utils/ticket-notif-helper.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const User = require('../models/User')
2+
3+
module.exports = {
4+
// Notification for Admins
5+
addToNotificationForAdmin: async (req, res, obj, next) => {
6+
try {
7+
console.log('adding to admin\'s notifications')
8+
await User.updateMany({ isAdmin: true }, { $push: { ticketNotifications: { $each: [obj], $position: 0 } } })
9+
} catch (error) {
10+
console.log(error)
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)