154 lines
4.4 KiB
JavaScript
154 lines
4.4 KiB
JavaScript
import passport from 'passport'
|
|
import createError from 'http-errors'
|
|
import autobind from 'autobind-decorator'
|
|
|
|
@autobind
|
|
export class ActivityRoutes {
|
|
constructor(container) {
|
|
const app = container.app
|
|
|
|
this.log = container.log
|
|
this.db = container.db
|
|
this.mq = container.mq
|
|
this.ws = container.ws
|
|
|
|
app.route('/activities')
|
|
.get(passport.authenticate('bearer', { session: false }), this.listActivitys)
|
|
.post(passport.authenticate('bearer', { session: false }), this.createActivity)
|
|
.put(passport.authenticate('bearer', { session: false }), this.updateActivity)
|
|
|
|
app.route('/activities/:_id([a-f0-9]{24})')
|
|
.get(passport.authenticate('bearer', { session: false }), this.getActivity)
|
|
.delete(passport.authenticate('bearer', { session: false }), this.deleteActivity)
|
|
}
|
|
|
|
listActivitys(req, res, next) {
|
|
const Activity = this.db.Activity
|
|
const limit = req.query.limit || 20
|
|
const skip = req.query.skip || 0
|
|
const partial = !!req.query.partial
|
|
const branch = req.query.branch
|
|
const query = {}
|
|
|
|
if (branch) {
|
|
query.branch = branch
|
|
}
|
|
|
|
Activity.count({}).then((total) => {
|
|
let activities = []
|
|
let cursor = Activity.find(query).limit(limit).skip(skip).cursor().map((doc) => {
|
|
return doc.toClient(partial)
|
|
})
|
|
|
|
cursor.on('data', (doc) => {
|
|
activities.push(doc)
|
|
})
|
|
cursor.on('end', () => {
|
|
res.json({
|
|
total: total,
|
|
offset: skip,
|
|
count: activities.length,
|
|
items: activities
|
|
})
|
|
})
|
|
cursor.on('error', (err) => {
|
|
next(createError.InternalServerError(err.message))
|
|
})
|
|
}).catch((err) => {
|
|
next(createError.InternalServerError(err.message))
|
|
})
|
|
}
|
|
|
|
createActivity(req, res, next) {
|
|
const role = req.user.role
|
|
|
|
// If user's role is not Executive or Administrator, return an error
|
|
if (role !== 'executive' && role !== 'administrator') {
|
|
return next(new createError.Forbidden())
|
|
}
|
|
|
|
// Create a new Activity template then assign it to a value in the req.body
|
|
const Activity = this.db.Activity
|
|
let activity = new Activity(req.body)
|
|
|
|
// Save the activity (with promise) - If it doesnt, catch and throw error
|
|
activity.save().then((newActivity) => {
|
|
res.json(newActivity.toClient())
|
|
}).catch((err) => {
|
|
next(createError.InternalServerError(err.message))
|
|
})
|
|
}
|
|
|
|
updateActivity(req, res, next) {
|
|
const role = req.user.role
|
|
|
|
// If user's role is not Executive or Administrator, return an error
|
|
if (role !== 'executive' && role !== 'administrator') {
|
|
return new createError.Forbidden()
|
|
}
|
|
|
|
// Do this here because Mongoose will add it automatically otherwise
|
|
if (!req.body._id) {
|
|
return next(createError.BadRequest('No _id given in body'))
|
|
}
|
|
|
|
let Activity = this.db.Activity
|
|
let activityUpdates = null
|
|
|
|
try {
|
|
activityUpdates = new Activity(req.body)
|
|
} catch (err) {
|
|
return next(createError.BadRequest('Invalid data'))
|
|
}
|
|
|
|
Activity.findById(activityUpdates._id).then((foundActivity) => {
|
|
if (!foundActivity) {
|
|
return next(createError.NotFound(`Activity with _id ${_id} was not found`))
|
|
}
|
|
foundActivity.merge(activityUpdates)
|
|
return foundActivity.save()
|
|
}).then((savedActivity) => {
|
|
res.json(savedActivity.toClient())
|
|
}).catch((err) => {
|
|
next(createError.InternalServerError(err.message))
|
|
})
|
|
}
|
|
|
|
getActivity(req, res, next) {
|
|
const Activity = this.db.Activity
|
|
const _id = req.params._id
|
|
|
|
Activity.findById(_id).then((activity) => {
|
|
if (!activity) {
|
|
return next(createError.NotFound(`Activity with _id ${_id} not found`))
|
|
}
|
|
|
|
res.json(activity.toClient())
|
|
}).catch((err) => {
|
|
next(createError.InternalServerError(err.message))
|
|
})
|
|
}
|
|
|
|
deleteActivity(req, res, next) {
|
|
const role = req.user.role
|
|
|
|
// If user's role is not Executive or Administrator, return an error
|
|
if (role !== 'executive' && role !== 'administrator') {
|
|
return new createError.Forbidden()
|
|
}
|
|
|
|
const Activity = this.db.Activity
|
|
const _id = req.params._id
|
|
|
|
Activity.remove({ _id }).then((activity) => {
|
|
if (!activity) {
|
|
return next(createError.NotFound(`Activity with _id ${_id} not found`))
|
|
}
|
|
|
|
res.json({})
|
|
}).catch((err) => {
|
|
next(createError.InternalServerError(err.message))
|
|
})
|
|
}
|
|
}
|