Debug issues with work item CRUD

This commit is contained in:
John Lyon-Smith
2018-04-06 14:59:18 -07:00
parent d646b9477b
commit 57f98ad398
14 changed files with 684 additions and 418 deletions

View File

@@ -1,6 +1,6 @@
import passport from 'passport'
import createError from 'http-errors'
import autobind from 'autobind-decorator'
import passport from "passport"
import createError from "http-errors"
import autobind from "autobind-decorator"
@autobind
export class WorkItemRoutes {
@@ -12,14 +12,31 @@ export class WorkItemRoutes {
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")
.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)
app
.route("/workitems/:_id([a-f0-9]{24})")
.get(
passport.authenticate("bearer", { session: false }),
this.getWorkItem
)
.delete(
passport.authenticate("bearer", { session: false }),
this.deleteWorkItem
)
}
async listWorkItems(req, res, next) {
@@ -33,25 +50,29 @@ export class WorkItemRoutes {
const total = await WorkItem.count({})
let workItems = []
let cursor = WorkItem.find(query).limit(limit).skip(skip).cursor().map((doc) => {
return doc.toClient(partial)
})
let cursor = WorkItem.find(query)
.limit(limit)
.skip(skip)
.cursor()
.map((doc) => {
return doc.toClient(partial)
})
cursor.on('data', (doc) => {
cursor.on("data", (doc) => {
workItems.push(doc)
})
cursor.on('end', () => {
cursor.on("end", () => {
res.json({
total: total,
offset: skip,
count: workItems.length,
items: workItems
items: workItems,
})
})
cursor.on('error', (err) => {
cursor.on("error", (err) => {
throw createError.InternalServerError(err.message)
})
} catch(err) {
} catch (err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
@@ -61,7 +82,6 @@ export class WorkItemRoutes {
}
async createWorkItem(req, res, next) {
try {
const isAdmin = req.user.administrator
@@ -77,7 +97,7 @@ export class WorkItemRoutes {
const newWorkItem = await workItem.save()
res.json(newWorkItem.toClient())
} catch(err) {
} catch (err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
@@ -96,7 +116,7 @@ export class WorkItemRoutes {
// Do this here because Mongoose will add it automatically otherwise
if (!req.body._id) {
throw createError.BadRequest('No _id given in body')
throw createError.BadRequest("No _id given in body")
}
let WorkItem = this.db.WorkItem
@@ -105,13 +125,15 @@ export class WorkItemRoutes {
try {
workItemUpdates = new WorkItem(req.body)
} catch (err) {
throw createError.BadRequest('Invalid data')
throw createError.BadRequest("Invalid data")
}
const foundWorkItem = await WorkItem.findById(workItemUpdates._id)
if (!foundWorkItem) {
return next(createError.NotFound(`WorkItem with _id ${_id} was not found`))
return next(
createError.NotFound(`WorkItem with _id ${_id} was not found`)
)
}
foundWorkItem.merge(workItemUpdates)
@@ -119,7 +141,7 @@ export class WorkItemRoutes {
const savedWorkItem = await foundWorkItem.save()
res.json(savedWorkItem.toClient())
} catch(err) {
} catch (err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
@@ -139,7 +161,7 @@ export class WorkItemRoutes {
}
res.json(workItem.toClient())
} catch(err) {
} catch (err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
@@ -165,7 +187,7 @@ export class WorkItemRoutes {
}
res.json({})
} catch(err) {
} catch (err) {
if (err instanceof createError.HttpError) {
next(err)
} else {