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 WorkItemRoutes {
|
|
constructor(container) {
|
|
const app = container.app
|
|
|
|
this.log = container.log
|
|
this.db = container.db
|
|
this.mq = container.mq
|
|
this.ws = container.ws
|
|
|
|
app.route('/workitems')
|
|
.get(passport.authenticate('bearer', { session: false }), this.listWorkItems)
|
|
.post(passport.authenticate('bearer', { session: false }), this.createWorkItem)
|
|
.put(passport.authenticate('bearer', { session: false }), this.updateWorkItem)
|
|
|
|
app.route('/workitems/:_id([a-f0-9]{24})')
|
|
.get(passport.authenticate('bearer', { session: false }), this.getWorkItem)
|
|
.delete(passport.authenticate('bearer', { session: false }), this.deleteWorkItem)
|
|
}
|
|
|
|
listWorkItems(req, res, next) {
|
|
const WorkItem = this.db.WorkItem
|
|
let limit = req.params.limit || 20
|
|
let skip = req.params.skip || 0
|
|
let partial = !!req.params.partial
|
|
let branch = req.params.branch
|
|
let query = {}
|
|
|
|
if (branch) {
|
|
query.branch = branch
|
|
}
|
|
|
|
WorkItem.count({}).then((total) => {
|
|
let workItems = []
|
|
let cursor = WorkItem.find(query).limit(limit).skip(skip).cursor().map((doc) => {
|
|
return doc.toClient(partial)
|
|
})
|
|
|
|
cursor.on('data', (doc) => {
|
|
workItems.push(doc)
|
|
})
|
|
cursor.on('end', () => {
|
|
res.json({
|
|
total: total,
|
|
offset: skip,
|
|
count: workItems.length,
|
|
items: workItems
|
|
})
|
|
})
|
|
cursor.on('error', (err) => {
|
|
next(createError.InternalServerError(err.message))
|
|
})
|
|
}).catch((err) => {
|
|
next(createError.InternalServerError(err.message))
|
|
})
|
|
}
|
|
|
|
createWorkItem(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 WorkItem template then assign it to a value in the req.body
|
|
const WorkItem = this.db.WorkItem
|
|
let workItem = new WorkItem(req.body)
|
|
|
|
// Save the workItem (with promise) - If it doesnt, catch and throw error
|
|
workItem.save().then((newWorkItem) => {
|
|
res.json(newWorkItem.toClient())
|
|
}).catch((err) => {
|
|
next(createError.InternalServerError(err.message))
|
|
})
|
|
}
|
|
|
|
updateWorkItem(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 WorkItem = this.db.WorkItem
|
|
let workItemUpdates = null
|
|
|
|
try {
|
|
workItemUpdates = new WorkItem(req.body)
|
|
} catch (err) {
|
|
return next(createError.BadRequest('Invalid data'))
|
|
}
|
|
|
|
WorkItem.findById(workItemUpdates._id).then((foundWorkItem) => {
|
|
if (!foundWorkItem) {
|
|
return next(createError.NotFound(`WorkItem with _id ${_id} was not found`))
|
|
}
|
|
foundWorkItem.merge(workItemUpdates)
|
|
return foundWorkItem.save()
|
|
}).then((savedWorkItem) => {
|
|
res.json(savedWorkItem.toClient())
|
|
}).catch((err) => {
|
|
next(createError.InternalServerError(err.message))
|
|
})
|
|
}
|
|
|
|
getWorkItem(req, res, next) {
|
|
const WorkItem = this.db.WorkItem
|
|
const _id = req.params._id
|
|
|
|
WorkItem.findById(_id).then((workItem) => {
|
|
if (!workItem) {
|
|
return next(createError.NotFound(`WorkItem with _id ${_id} not found`))
|
|
}
|
|
|
|
res.json(workItem.toClient())
|
|
}).catch((err) => {
|
|
next(createError.InternalServerError(err.message))
|
|
})
|
|
}
|
|
|
|
deleteWorkItem(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 WorkItem = this.db.WorkItem
|
|
const _id = req.params._id
|
|
|
|
WorkItem.remove({ _id }).then((workItem) => {
|
|
if (!workItem) {
|
|
return next(createError.NotFound(`WorkItem with _id ${_id} not found`))
|
|
}
|
|
|
|
res.json({})
|
|
}).catch((err) => {
|
|
next(createError.InternalServerError(err.message))
|
|
})
|
|
}
|
|
}
|