Integrated master/detail, refactor Icon, add base router
This commit is contained in:
@@ -2,33 +2,14 @@ import passport from "passport"
|
||||
import createError from "http-errors"
|
||||
import autobind from "autobind-decorator"
|
||||
import merge from "deepmerge"
|
||||
import { catchAll } from "."
|
||||
import { catchAll, BaseRoutes } from "."
|
||||
|
||||
@autobind
|
||||
export class WorkItemRoutes {
|
||||
export class WorkItemRoutes extends BaseRoutes {
|
||||
constructor(container) {
|
||||
super(container, container.db.WorkItem)
|
||||
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 }),
|
||||
catchAll(this.listWorkItems)
|
||||
)
|
||||
.post(
|
||||
passport.authenticate("bearer", { session: false }),
|
||||
catchAll(this.createWorkItem)
|
||||
)
|
||||
.put(
|
||||
passport.authenticate("bearer", { session: false }),
|
||||
catchAll(this.updateWorkItem)
|
||||
)
|
||||
|
||||
app
|
||||
.route("/workitems/activities")
|
||||
.get(
|
||||
@@ -36,17 +17,6 @@ export class WorkItemRoutes {
|
||||
catchAll(this.listWorkItemActivities)
|
||||
)
|
||||
|
||||
app
|
||||
.route("/workitems/:_id([a-f0-9]{24})")
|
||||
.get(
|
||||
passport.authenticate("bearer", { session: false }),
|
||||
catchAll(this.getWorkItem)
|
||||
)
|
||||
.delete(
|
||||
passport.authenticate("bearer", { session: false }),
|
||||
catchAll(this.deleteWorkItem)
|
||||
)
|
||||
|
||||
app
|
||||
.route("/workitems/all")
|
||||
.delete(
|
||||
@@ -55,40 +25,6 @@ export class WorkItemRoutes {
|
||||
)
|
||||
}
|
||||
|
||||
async listWorkItems(req, res, next) {
|
||||
const WorkItem = this.db.WorkItem
|
||||
const limit = req.query.limit || 20
|
||||
const skip = req.query.skip || 0
|
||||
const partial = !!req.query.partial
|
||||
let query = {}
|
||||
|
||||
const total = await WorkItem.count({})
|
||||
|
||||
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) => {
|
||||
throw createError.InternalServerError(err.message)
|
||||
})
|
||||
}
|
||||
|
||||
async listWorkItemActivities(req, res, next) {
|
||||
const WorkItem = this.db.WorkItem
|
||||
const aggregate = WorkItem.aggregate()
|
||||
@@ -117,83 +53,6 @@ export class WorkItemRoutes {
|
||||
res.json({ items })
|
||||
}
|
||||
|
||||
async createWorkItem(req, res, next) {
|
||||
const isAdmin = req.user.administrator
|
||||
|
||||
if (!isAdmin) {
|
||||
return 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
|
||||
const newWorkItem = await workItem.save()
|
||||
|
||||
res.json(newWorkItem.toClient())
|
||||
}
|
||||
|
||||
async updateWorkItem(req, res, next) {
|
||||
const isAdmin = req.user.administrator
|
||||
|
||||
if (!isAdmin) {
|
||||
return new createError.Forbidden()
|
||||
}
|
||||
|
||||
// Do this here because Mongoose will add it automatically otherwise
|
||||
if (!req.body._id) {
|
||||
throw createError.BadRequest("No _id given in body")
|
||||
}
|
||||
|
||||
let WorkItem = this.db.WorkItem
|
||||
let workItem = await WorkItem.findById(req.body._id)
|
||||
|
||||
if (!workItem) {
|
||||
return next(
|
||||
createError.NotFound(`WorkItem with _id ${req.body_id} was not found`)
|
||||
)
|
||||
}
|
||||
|
||||
const workItemUpdates = new WorkItem(req.body)
|
||||
|
||||
workItem.merge(workItemUpdates)
|
||||
|
||||
const savedWorkItem = await workItem.save()
|
||||
|
||||
res.json(savedWorkItem.toClient())
|
||||
}
|
||||
|
||||
async getWorkItem(req, res, next) {
|
||||
const WorkItem = this.db.WorkItem
|
||||
const _id = req.params._id
|
||||
const workItem = await WorkItem.findById(_id)
|
||||
|
||||
if (!workItem) {
|
||||
throw createError.NotFound(`WorkItem with _id ${_id} not found`)
|
||||
}
|
||||
|
||||
res.json(workItem.toClient())
|
||||
}
|
||||
|
||||
async deleteWorkItem(req, res, next) {
|
||||
const isAdmin = req.user.administrator
|
||||
|
||||
if (!isAdmin) {
|
||||
return new createError.Forbidden()
|
||||
}
|
||||
|
||||
const WorkItem = this.db.WorkItem
|
||||
const _id = req.params._id
|
||||
const workItem = await WorkItem.remove({ _id })
|
||||
|
||||
if (!workItem) {
|
||||
throw createError.NotFound(`WorkItem with _id ${_id} not found`)
|
||||
}
|
||||
|
||||
res.json({})
|
||||
}
|
||||
|
||||
async deleteAllWorkItems(req, res, next) {
|
||||
const Activity = this.db.Activity
|
||||
const WorkItem = this.db.WorkItem
|
||||
|
||||
Reference in New Issue
Block a user