Skip to content

Commit 48b32e6

Browse files
committed
Add middleware to routers and also add API for activity
Signed-off-by: K mehant <[email protected]>
1 parent e40df1e commit 48b32e6

20 files changed

+214
-71
lines changed

app/controllers/activity.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const User = require('../models/User')
2+
const HttpStatus = require('http-status-codes')
3+
module.exports = {
4+
getActivity: async (req, res, next) => {
5+
const { id } = req.params
6+
try {
7+
const user = await User.findById(req.user._id)
8+
if (!user) {
9+
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'Invalid request!' })
10+
}
11+
// check if not admin
12+
if (user.isAdmin !== true) {
13+
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'You don\'t have permission!' })
14+
}
15+
// else allowed
16+
const activityUser = await User.findById(id)
17+
if (!user) {
18+
return res.status(HttpStatus.NOT_FOUND).json({ message: 'No such user exists' })
19+
}
20+
res.status(HttpStatus.OK).json({ activity: activityUser.activity })
21+
} catch (error) {
22+
if (process.env.NODE_ENV !== 'production') {
23+
console.log(error.name, '-', error.message)
24+
}
25+
res.status(HttpStatus.BAD_REQUEST).json({ error: error.message })
26+
}
27+
}
28+
}

app/controllers/auth.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
},
1818
logout: (req, res, next) => {
1919
res.status(HttpStatus.OK).json({ success: 'ok' })
20+
next()
2021
},
2122
logoutAll: (req, res, next) => {
2223
res.status(HttpStatus.OK).json({ success: 'ok' })

app/controllers/comment.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ module.exports = {
1414
comment.userId = userId
1515
comment.postId = id // added postId
1616
await comment.save()
17+
res.locals.data = comment
1718
res.status(HttpStatus.CREATED).json({ comment: comment })
19+
next()
1820
} catch (error) {
1921
HANDLER.handleError(res, error)
2022
}
@@ -32,8 +34,10 @@ module.exports = {
3234
if (!permission.check(req, res, comment.userId)) {
3335
return res.status(HttpStatus.FORBIDDEN).json({ message: 'Bad delete request' })
3436
}
37+
res.locals.data = comment
3538
await CommentModel.findByIdAndRemove(id)
3639
res.status(HttpStatus.OK).json({ comment: comment })
40+
next()
3741
} catch (error) {
3842
HANDLER.handleError(res, error)
3943
}
@@ -63,7 +67,9 @@ module.exports = {
6367
comment[update] = req.body[update]
6468
})
6569
await comment.save()
70+
res.locals.data = comment
6671
res.status(HttpStatus.OK).json({ comment: comment })
72+
next()
6773
} catch (error) {
6874
HANDLER.handleError(res, error)
6975
}
@@ -112,7 +118,9 @@ module.exports = {
112118
})
113119
comment.votes.upVotes.user.unshift(userId)
114120
await comment.save()
115-
return res.status(HttpStatus.OK).json({ comment: comment })
121+
res.locals.data = comment
122+
res.status(HttpStatus.OK).json({ comment: comment })
123+
next()
116124
} catch (error) {
117125
HANDLER.handleError(res, error)
118126
}
@@ -143,7 +151,9 @@ module.exports = {
143151
})
144152
comment.votes.downVotes.user.unshift(userId)
145153
await comment.save()
146-
return res.status(HttpStatus.OK).json({ comment: comment })
154+
res.locals.data = comment
155+
res.status(HttpStatus.OK).json({ comment: comment })
156+
next()
147157
} catch (error) {
148158
HANDLER.handleError(res, error)
149159
}

app/controllers/event.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ module.exports = {
2222
notification.content = `${event.eventName} is added!`
2323
notification.tag = 'New!'
2424
notificationHelper.addToNotificationForAll(req, res, notification, next)
25+
res.locals.data = event
2526
res.status(HttpStatus.CREATED).json({ event: event })
27+
next()
2628
} catch (error) {
2729
res.status(HttpStatus.BAD_REQUEST).json({ error: error })
2830
}
@@ -53,7 +55,9 @@ module.exports = {
5355
notification.content = `${event.eventName} is updated!`
5456
notification.tag = 'Update'
5557
notificationHelper.addToNotificationForAll(req, res, notification, next)
58+
res.locals.data = event
5659
res.status(HttpStatus.OK).json({ event: event })
60+
next()
5761
} catch (error) {
5862
HANDLER.handleError(res, error)
5963
}
@@ -84,10 +88,12 @@ module.exports = {
8488
try {
8589
event.rsvpYes.push(req.user.id)
8690
await event.save()
91+
res.locals.data = event
8792
req.io.emit('rsvp done', { data: 'RSVP successfully done!' })
8893
notification.heading = 'RSVP done!'
8994
notification.content = 'RSVP successfully done!'
9095
notificationHelper.addToNotificationForUser(req.user._id, res, notification, next)
96+
next()
9197
res.status(HttpStatus.OK).json({ rsvpData: data })
9298
} catch (error) {
9399
return res.status(HttpStatus.BAD_REQUEST).json({ error: error })
@@ -97,10 +103,12 @@ module.exports = {
97103
try {
98104
event.rsvpNo.push(req.user.id)
99105
await event.save()
106+
res.locals.data = event
100107
req.io.emit('rsvp done', { data: 'RSVP successfully done!' })
101108
notification.heading = 'RSVP done!'
102109
notification.content = 'RSVP successfully done!'
103110
notificationHelper.addToNotificationForUser(req.user._id, res, notification, next)
111+
next()
104112
res.status(HttpStatus.OK).json({ rsvpData: data })
105113
} catch (error) {
106114
return res.status(HttpStatus.BAD_REQUEST).json({ error: error })
@@ -110,11 +118,13 @@ module.exports = {
110118
try {
111119
event.rsvpMaybe.push(req.user.id)
112120
await event.save()
121+
res.locals.data = event
113122
req.io.emit('rsvp done', { data: 'RSVP successfully done!' })
114123
notification.heading = 'RSVP done!'
115124
notification.content = 'RSVP successfully done!'
116125
notificationHelper.addToNotificationForUser(req.user._id, res, notification, next)
117126
res.status(HttpStatus.OK).json({ rsvpData: data })
127+
next()
118128
} catch (error) {
119129
return res.status(HttpStatus.BAD_REQUEST).json({ error: error })
120130
}
@@ -158,12 +168,14 @@ module.exports = {
158168
}
159169
if (permission.check(req, res, deleteEvent.createdBy)) {
160170
await Event.findByIdAndRemove(id)
171+
res.locals.data = event
161172
req.io.emit('event deleted', { data: deleteEvent.eventName })
162173
notification.heading = 'Event deleted!'
163174
notification.content = `Event ${deleteEvent.eventName} is deleted!`
164175
notification.tag = 'Deleted'
165176
notificationHelper.addToNotificationForAll(req, res, notification, next)
166-
return res.status(HttpStatus.OK).json({ deleteEvent: deleteEvent, message: 'Deleted the event' })
177+
res.status(HttpStatus.OK).json({ deleteEvent: deleteEvent, message: 'Deleted the event' })
178+
next()
167179
}
168180
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'Not permitted!' })
169181
} catch (error) {

app/controllers/organization.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ module.exports = {
2828
notification.content = `${org.name} is created!`
2929
notification.tag = TAGS.NEW
3030
notificationHelper.addToNotificationForAll(req, res, notification, next)
31-
return res.status(HttpStatus.CREATED).json({ org })
31+
res.locals.data = org
32+
res.status(HttpStatus.CREATED).json({ org })
33+
next()
3234
} catch (error) {
3335
HANDLER.handleError(res, error)
3436
}
@@ -70,7 +72,9 @@ module.exports = {
7072
helper.mapToDb(req, org)
7173
}
7274
await org.save()
73-
return res.status(HttpStatus.OK).json({ organization: org })
75+
res.locals.data = org
76+
res.status(HttpStatus.OK).json({ organization: org })
77+
next()
7478
} catch (error) {
7579
HANDLER.handleError(res, error)
7680
}
@@ -100,7 +104,9 @@ module.exports = {
100104
.status(HttpStatus.NOT_FOUND)
101105
.json({ error: 'No such organization exists!' })
102106
}
107+
res.locals.data = orgData
103108
res.status(HttpStatus.OK).json({ organization: orgData })
109+
next()
104110
} catch (error) {
105111
HANDLER.handleError(res, error)
106112
}
@@ -126,7 +132,9 @@ module.exports = {
126132
notification.content = `${org.name} is deleted!`
127133
notification.tag = TAGS.DELETE
128134
notificationHelper.addToNotificationForAll(req, res, notification, next)
129-
return res.status(HttpStatus.OK).json({ organization: org })
135+
res.locals.data = org
136+
res.status(HttpStatus.OK).json({ organization: org })
137+
next()
130138
} catch (error) {
131139
HANDLER.handleError(res, error)
132140
}
@@ -143,7 +151,9 @@ module.exports = {
143151
}
144152
org.isArchived = true
145153
await org.save()
146-
return res.status(HttpStatus.OK).json({ organization: org })
154+
res.locals.data = org
155+
res.status(HttpStatus.OK).json({ organization: org })
156+
next()
147157
} catch (error) {
148158
HANDLER.handleError(res, error)
149159
}
@@ -221,7 +231,9 @@ module.exports = {
221231
organization.options[update] = req.body[update]
222232
})
223233
await organization.save()
224-
return res.status(HttpStatus.OK).json({ organization })
234+
res.locals.data = organization
235+
res.status(HttpStatus.OK).json({ organization })
236+
next()
225237
}
226238
// invalid update
227239
return res
@@ -324,7 +336,9 @@ module.exports = {
324336
}
325337
user.isAdmin = false
326338
await user.save()
327-
return res.status(HttpStatus.OK).json({ org })
339+
res.locals.data = user
340+
res.status(HttpStatus.OK).json({ org })
341+
next()
328342
} catch (error) {
329343
HANDLER.handleError(res, error)
330344
}

app/controllers/post.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ module.exports = {
1919
try {
2020
await post.save()
2121
// req.io.emit('new post created', { data: post.content })
22-
return res.status(HttpStatus.CREATED).json({ post })
22+
res.locals.data = post
23+
res.status(HttpStatus.CREATED).json({ post })
24+
next()
2325
} catch (error) {
2426
HANDLER.handleError(res, error)
2527
}
@@ -41,7 +43,9 @@ module.exports = {
4143
.json({ message: 'Bad delete request' })
4244
}
4345
await PostModel.findByIdAndRemove(id)
46+
res.locals.data = post
4447
res.status(HttpStatus.OK).json({ post: post, msg: 'Deleted!' })
48+
next()
4549
} catch (error) {
4650
HANDLER.handleError(res, error)
4751
}
@@ -83,7 +87,9 @@ module.exports = {
8387
imgUploadHelper.mapToDb(req, post)
8488
}
8589
await post.save()
90+
res.locals.data = post
8691
res.status(HttpStatus.OK).json({ post: post })
92+
next()
8793
} catch (error) {
8894
HANDLER.handleError(res, error)
8995
}
@@ -158,7 +164,9 @@ module.exports = {
158164
})
159165
post.votes.upVotes.user.unshift(userId)
160166
await post.save()
167+
res.locals.data = post
161168
res.status(HttpStatus.OK).json({ post: post })
169+
next()
162170
} catch (error) {
163171
HANDLER.handleError(res, error)
164172
}
@@ -211,7 +219,9 @@ module.exports = {
211219
await user.save()
212220
}
213221
await post.save()
214-
return res.status(HttpStatus.OK).json({ post })
222+
res.locals.data = post
223+
res.status(HttpStatus.OK).json({ post })
224+
next()
215225
} catch (error) {
216226
HANDLER.handleError(res, error)
217227
}

app/controllers/project.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ module.exports = {
1111
const project = await new Project(req.body)
1212
project.createdBy = req.user._id
1313
await project.save()
14-
return res.status(HttpStatus.CREATED).json({ project })
14+
res.locals.data = project
15+
res.status(HttpStatus.CREATED).json({ project })
16+
next()
1517
} catch (error) {
1618
HANDLER.handleError(res, error)
1719
}
@@ -81,7 +83,9 @@ module.exports = {
8183
project[update] = req.body[update]
8284
})
8385
await project.save()
84-
return res.status(HttpStatus.OK).json({ project })
86+
res.locals.data = project
87+
res.status(HttpStatus.OK).json({ project })
88+
next()
8589
} catch (error) {
8690
HANDLER.handleError(res, error)
8791
}
@@ -96,7 +100,9 @@ module.exports = {
96100
// check if admin or user who created this project
97101
if (permission.check(req, res, project.createdBy)) {
98102
await Project.findByIdAndRemove(id)
99-
return res.status(HttpStatus.OK).json({ msg: 'Project deleted!' })
103+
res.locals.data = project
104+
res.status(HttpStatus.OK).json({ msg: 'Project deleted!' })
105+
next()
100106
}
101107
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'Not permitted!' })
102108
} catch (error) {

app/controllers/proposal.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ module.exports = {
3333
},
3434
next
3535
)
36-
36+
res.locals.data = proposal
3737
res.status(HttpStatus.CREATED).json({ proposal })
38+
next()
3839
} catch (error) {
3940
HANDLER.handleError(res, error)
4041
}
@@ -56,7 +57,9 @@ module.exports = {
5657
.status(HttpStatus.NOT_FOUND)
5758
.json({ message: 'No proposal exists under the provided ID' })
5859
}
60+
res.locals.data = proposal
5961
res.status(HttpStatus.OK).json({ proposal: proposal })
62+
next()
6063
} catch (error) {
6164
HANDLER.handleError(res, error)
6265
}
@@ -102,8 +105,9 @@ module.exports = {
102105
}
103106
}
104107
)
105-
108+
res.locals.data._id = proposalId
106109
res.send({ data })
110+
next()
107111
}
108112
})
109113
},
@@ -189,7 +193,9 @@ module.exports = {
189193
},
190194
next
191195
)
196+
res.locals.data = proposal
192197
res.status(HttpStatus.OK).json({ proposal: proposal })
198+
next()
193199
} catch (error) {
194200
HANDLER.handleError(res, error)
195201
}
@@ -264,8 +270,9 @@ module.exports = {
264270
next
265271
)
266272
}
267-
268-
return res.status(HttpStatus.OK).json({ proposal: proposal })
273+
res.locals.data = updatedProposal
274+
res.status(HttpStatus.OK).json({ proposal: proposal })
275+
next()
269276
} catch (error) {
270277
HANDLER.handleError(res, error)
271278
}

0 commit comments

Comments
 (0)