Hooked up Work Item screen to API

This commit is contained in:
John Lyon-Smith
2018-04-05 15:28:57 -07:00
parent e6bd1f8fed
commit aec39ae17d
7 changed files with 195 additions and 103 deletions

View File

@@ -22,14 +22,16 @@ export class WorkItemRoutes {
.delete(passport.authenticate('bearer', { session: false }), this.deleteWorkItem)
}
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 = {}
async listWorkItems(req, res, next) {
try {
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({})
WorkItem.count({}).then((total) => {
let workItems = []
let cursor = WorkItem.find(query).limit(limit).skip(skip).cursor().map((doc) => {
return doc.toClient(partial)
@@ -47,102 +49,128 @@ export class WorkItemRoutes {
})
})
cursor.on('error', (err) => {
next(createError.InternalServerError(err.message))
throw createError.InternalServerError(err.message)
})
}).catch((err) => {
next(createError.InternalServerError(err.message))
})
} catch(err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
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
async createWorkItem(req, res, next) {
try {
workItemUpdates = new WorkItem(req.body)
} catch (err) {
return next(createError.BadRequest('Invalid data'))
}
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())
} catch(err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
}
}
async updateWorkItem(req, res, next) {
try {
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 workItemUpdates = null
try {
workItemUpdates = new WorkItem(req.body)
} catch (err) {
throw createError.BadRequest('Invalid data')
}
const foundWorkItem = await WorkItem.findById(workItemUpdates._id)
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) => {
const savedWorkItem = await foundWorkItem.save()
res.json(savedWorkItem.toClient())
}).catch((err) => {
next(createError.InternalServerError(err.message))
})
} catch(err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
}
}
getWorkItem(req, res, next) {
const WorkItem = this.db.WorkItem
const _id = req.params._id
async getWorkItem(req, res, next) {
try {
const WorkItem = this.db.WorkItem
const _id = req.params._id
const workItem = await WorkItem.findById(_id)
WorkItem.findById(_id).then((workItem) => {
if (!workItem) {
return next(createError.NotFound(`WorkItem with _id ${_id} not found`))
throw createError.NotFound(`WorkItem with _id ${_id} not found`)
}
res.json(workItem.toClient())
}).catch((err) => {
next(createError.InternalServerError(err.message))
})
} catch(err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
}
}
deleteWorkItem(req, res, next) {
const role = req.user.role
async deleteWorkItem(req, res, next) {
try {
const isAdmin = req.user.administrator
// If user's role is not Executive or Administrator, return an error
if (role !== 'executive' && role !== 'administrator') {
return new createError.Forbidden()
}
if (!isAdmin) {
return new createError.Forbidden()
}
const WorkItem = this.db.WorkItem
const _id = req.params._id
const WorkItem = this.db.WorkItem
const _id = req.params._id
const workItem = await WorkItem.remove({ _id })
WorkItem.remove({ _id }).then((workItem) => {
if (!workItem) {
return next(createError.NotFound(`WorkItem with _id ${_id} not found`))
throw createError.NotFound(`WorkItem with _id ${_id} not found`)
}
res.json({})
}).catch((err) => {
next(createError.InternalServerError(err.message))
})
} catch(err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
}
}
}

View File

@@ -8,7 +8,7 @@ export let workItemSchema = new Schema({
message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
}},
number: Number,
loc: {
location: {
type: { type: String },
coordinates: [Number],
},