Skip to content

Commit

Permalink
Merge pull request #133 from AuraOfDivinity/proposal-route
Browse files Browse the repository at this point in the history
Implementation of /proposal route and proposal test cases
  • Loading branch information
devesh-verma authored Jul 14, 2020
2 parents 90493a5 + 457dac8 commit 6a859e3
Show file tree
Hide file tree
Showing 19 changed files with 1,006 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ NODE_ENV="development"
JWT_SECRET="thisismysupersecrettokenjustkidding"
DATABASE_URL="mongodb://mongo:27017/donut-development"
SENDGRID_API_KEY='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'
SOCKET_PORT=8810
SOCKET_PORT=8810
16 changes: 15 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const cookieParser = require('cookie-parser')
const createError = require('http-errors')
const path = require('path')
const socket = require('socket.io')
const multer = require('multer')
const bodyParser = require('body-parser')
const cors = require('cors')
const fileConstants = require('./config/fileHandlingConstants')

const indexRouter = require('./app/routes/index')
const authRouter = require('./app/routes/auth')
Expand All @@ -16,10 +20,19 @@ const organizationRouter = require('./app/routes/organisation')
const commentRouter = require('./app/routes/comment')
const projectRouter = require('./app/routes/project')
const notificationRouter = require('./app/routes/notification')
const proposalRouter = require('./app/routes/proposal')

const app = express()
const server = require('http').Server(app)

app.use(cors())

app.use(bodyParser.json({ limit: '200mb' }))
app.use(bodyParser.urlencoded(fileConstants.fileParameters))

const memoryStorage = multer.memoryStorage()
app.use(multer({ storage: memoryStorage }).single('file'))

server.listen(process.env.SOCKET_PORT || 8810)
// WARNING: app.listen(80) will NOT work here!

Expand Down Expand Up @@ -54,10 +67,11 @@ app.use('/event', eventRouter)
app.use('/shortUrl', shortUrlRouter)
app.use('/comment', commentRouter)
app.use('/project', projectRouter)
app.use('/proposal', proposalRouter)

// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404, 'route doesn\'t exist'))
next(createError(404, "route doesn't exist"))
})

// error handler
Expand Down
21 changes: 19 additions & 2 deletions app/controllers/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ const HttpStatus = require('http-status-codes')
const Notifications = require('../models/Notifications')
const helper = require('../utils/paginate')
const User = require('../models/User')
const ProposalNotifications = require('../models/ProposalNotification')

module.exports = {
// GET ALL THE NOTIFICATIONS FOR ALL
getOrgNotifications: async (req, res, next) => {
try {
const notifications = await Notifications.find({}, {}, helper.paginate(req))
const notifications = await Notifications.find(
{},
{},
helper.paginate(req)
)
.lean()
.sort({ createdAt: -1 })
.exec()
Expand All @@ -23,7 +28,9 @@ module.exports = {
try {
const user = await User.findById(userId)
if (!user) {
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'No such user exists!' })
return res
.status(HttpStatus.BAD_REQUEST)
.json({ msg: 'No such user exists!' })
}
// get all notifications of existing user
const notifications = user.notifications
Expand All @@ -34,5 +41,15 @@ module.exports = {
} catch (error) {
HANDLER.handleError(res, error)
}
},

getProposalNotifications: async (req, res, next) => {
try {
const notifications = await ProposalNotifications.find({})
console.log(notifications)
return res.status(HttpStatus.OK).json({ notifications })
} catch (error) {
HANDLER.handleError(res, error)
}
}
}
109 changes: 81 additions & 28 deletions app/controllers/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,33 @@ module.exports = {
updateOrgDetails: async (req, res, next) => {
const { id } = req.params
const updates = Object.keys(req.body)
const allowedUpdates = ['name', 'description', 'contactInfo', 'image', 'imgUrl', 'adminInfo', 'moderatorInfo']
const allowedUpdates = [
'name',
'description',
'contactInfo',
'image',
'imgUrl',
'adminInfo',
'moderatorInfo'
]
const isValidOperation = updates.every((update) => {
return allowedUpdates.includes(update)
})

if (!isValidOperation) {
return res.status(HttpStatus.BAD_REQUEST).json({ error: 'invalid update' })
return res
.status(HttpStatus.BAD_REQUEST)
.json({ error: 'invalid update' })
}
try {
const org = await Organization.findById(id)
// check for permission (ONLY ADMINS CAN UPDATE)
if (!permission.check(req, res)) {
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'You don\'t have the permission' })
return res
.status(HttpStatus.BAD_REQUEST)
.json({ msg: "You don't have the permission" })
}
updates.forEach(update => {
updates.forEach((update) => {
org[update] = req.body[update]
})
if (req.file) {
Expand All @@ -67,13 +79,25 @@ module.exports = {
const { id } = req.params
try {
const orgData = await Organization.findById(id)
.populate('adminInfo', ['name.firstName', 'name.lastName', 'email', 'isAdmin'])
.populate('moderatorInfo', ['name.firstName', 'name.lastName', 'email', 'isAdmin'])
.populate('adminInfo', [
'name.firstName',
'name.lastName',
'email',
'isAdmin'
])
.populate('moderatorInfo', [
'name.firstName',
'name.lastName',
'email',
'isAdmin'
])
.sort({ createdAt: -1 })
.lean()
.exec()
if (!orgData) {
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No such organization exists!' })
return res
.status(HttpStatus.NOT_FOUND)
.json({ error: 'No such organization exists!' })
}
res.status(HttpStatus.OK).json({ organization: orgData })
} catch (error) {
Expand All @@ -86,11 +110,15 @@ module.exports = {
try {
const org = await Organization.findByIdAndRemove(id)
if (!org) {
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No such organization exists!' })
return res
.status(HttpStatus.NOT_FOUND)
.json({ error: 'No such organization exists!' })
}
// check for permission
if (!permission.check(req, res)) {
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'You don\'t have the permission!' })
return res
.status(HttpStatus.BAD_REQUEST)
.json({ msg: "You don't have the permission!" })
}
req.io.emit('org deleted', { data: org.name })
notification.heading = 'Org deleted!'
Expand All @@ -108,7 +136,9 @@ module.exports = {
try {
const org = await Organization.findById(id)
if (!org) {
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No such organization exists!' })
return res
.status(HttpStatus.NOT_FOUND)
.json({ error: 'No such organization exists!' })
}
org.isArchived = true
await org.save()
Expand All @@ -124,7 +154,9 @@ module.exports = {
const organization = await Organization.findById(id)
// if org exists or not
if (!organization) {
return res.status(HttpStatus.NOT_FOUND).json({ error: 'No such organization exists!' })
return res
.status(HttpStatus.NOT_FOUND)
.json({ error: 'No such organization exists!' })
}
// if user is admin or not
const adminIds = organization.adminInfo.adminId
Expand All @@ -140,16 +172,27 @@ module.exports = {
req.io.emit('org under maintenance', { data: organization.name })
notification.heading = 'Maintenance mode on!'
notification.content = `${organization.name} is kept under maintenance!`
notificationHelper.addToNotificationForAll(req, res, notification, next)
return res.status(HttpStatus.OK).json({ msg: 'Organization is kept under the maintenance!!' })
notificationHelper.addToNotificationForAll(
req,
res,
notification,
next
)
return res
.status(HttpStatus.OK)
.json({ msg: 'Organization is kept under the maintenance!!' })
}

req.io.emit('org revoked maintenance', { data: organization.name })
notification.heading = 'Maintenance mode off!'
notification.content = `${organization.name} is revoked from maintenance!`
return res.status(HttpStatus.OK).json({ msg: 'Organization is recovered from maintenance!!' })
return res
.status(HttpStatus.OK)
.json({ msg: 'Organization is recovered from maintenance!!' })
} else {
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'You don\'t have access to triggerMaintenance!' })
return res
.status(HttpStatus.BAD_REQUEST)
.json({ msg: "You don't have access to triggerMaintenance!" })
}
} catch (error) {
HANDLER.handleError(res, error)
Expand All @@ -162,37 +205,41 @@ module.exports = {
// check if org exists
const organization = await Organization.findById(id)
if (!organization) {
return res.status(HttpStatus.NOT_FOUND).json({ msg: 'No Organization found!' })
return res
.status(HttpStatus.NOT_FOUND)
.json({ msg: 'No Organization found!' })
}
// check if user is admin or not
const adminIds = organization.adminInfo.adminId
const isAdmin = adminIds.indexOf(req.user.id)
const updates = Object.keys(req.body)
console.log('req.body ', req.body)
console.log('isAdmin ', isAdmin)
const allowedUpdates = [
'settings',
'permissions',
'authentication'
]
const allowedUpdates = ['settings', 'permissions', 'authentication']
// if admin then check if valid update
if (isAdmin !== -1) {
const isValidOperation = updates.every((update) => {
return allowedUpdates.includes(update)
})
// if valid update
if (isValidOperation) {
updates.forEach(update => {
updates.forEach((update) => {
organization.options[update] = req.body[update]
})
await organization.save()
return res.status(HttpStatus.OK).json({ msg: 'Successfully updated!' })
return res
.status(HttpStatus.OK)
.json({ msg: 'Successfully updated!' })
}
// invalid update
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'Invalid update' })
return res
.status(HttpStatus.BAD_REQUEST)
.json({ msg: 'Invalid update' })
}
// else not admin
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'You don\'t have access to perform this operation!' })
return res
.status(HttpStatus.BAD_REQUEST)
.json({ msg: "You don't have access to perform this operation!" })
} catch (error) {
HANDLER.handleError(res, error)
}
Expand Down Expand Up @@ -241,7 +288,9 @@ module.exports = {
.sort({ createdAt: -1 })
.exec()
if (members.length === 0) {
return res.status(HttpStatus.OK).json({ msg: 'No members joined yet!' })
return res
.status(HttpStatus.OK)
.json({ msg: 'No members joined yet!' })
}
return res.status(HttpStatus.OK).json({ members })
}
Expand All @@ -259,15 +308,19 @@ module.exports = {
}
// only permitted for admins
if (!req.user.isAdmin) {
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'You are not permitted!' })
return res
.status(HttpStatus.BAD_REQUEST)
.json({ msg: 'You are not permitted!' })
}
// console.log('Permitted to removeAdmin')
// REMOVE ADMINS FROM ADMINS LIST
const admins = org.adminInfo.adminId
console.log('adminIds ', admins)
const removableIndex = admins.indexOf(userId)
if (removableIndex === -1) {
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'User is not an admin!' })
return res
.status(HttpStatus.BAD_REQUEST)
.json({ msg: 'User is not an admin!' })
}
// user is admin so remove
org.adminInfo.adminId.splice(removableIndex, 1)
Expand Down
Loading

0 comments on commit 6a859e3

Please sign in to comment.