SectionList on home screen with API

This commit is contained in:
John Lyon-Smith
2018-04-08 18:33:21 -07:00
parent 5634acb967
commit 7891bb71c9
19 changed files with 1278 additions and 1201 deletions

View File

@@ -1,6 +1,7 @@
import passport from "passport"
import createError from "http-errors"
import autobind from "autobind-decorator"
import { catchAll } from "."
@autobind
export class WorkItemRoutes {
@@ -16,183 +17,176 @@ export class WorkItemRoutes {
.route("/workitems")
.get(
passport.authenticate("bearer", { session: false }),
this.listWorkItems
catchAll(this.listWorkItems)
)
.post(
passport.authenticate("bearer", { session: false }),
this.createWorkItem
catchAll(this.createWorkItem)
)
.put(
passport.authenticate("bearer", { session: false }),
this.updateWorkItem
catchAll(this.updateWorkItem)
)
app
.route("/workitems/activities")
.get(
passport.authenticate("bearer", { session: false }),
catchAll(this.listWorkItemActivities)
)
app
.route("/workitems/:_id([a-f0-9]{24})")
.get(
passport.authenticate("bearer", { session: false }),
this.getWorkItem
catchAll(this.getWorkItem)
)
.delete(
passport.authenticate("bearer", { session: false }),
this.deleteWorkItem
catchAll(this.deleteWorkItem)
)
}
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 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({})
const total = await WorkItem.count({})
let workItems = []
let cursor = WorkItem.find(query)
.limit(limit)
.skip(skip)
.cursor()
.map((doc) => {
return doc.toClient(partial)
})
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("data", (doc) => {
workItems.push(doc)
})
cursor.on("end", () => {
res.json({
total: total,
offset: skip,
count: workItems.length,
items: workItems,
})
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()
.sort({ ticketNumber: 1 })
.lookup({
from: "activities",
localField: "_id",
foreignField: "workItem",
as: "data",
})
cursor.on("error", (err) => {
throw createError.InternalServerError(err.message)
.project({
workItemType: 1,
ticketNumber: 1,
address: 1,
"coordinate.longitude": { $arrayElemAt: ["$location.coordinates", 0] },
"coordinate.latitude": { $arrayElemAt: ["$location.coordinates", 1] },
"data._id": 1,
"data.resolution": 1,
"data.status": 1,
})
} catch (err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
}
const items = await aggregate.exec()
res.json({ items })
}
async createWorkItem(req, res, next) {
try {
const isAdmin = req.user.administrator
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))
}
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) {
try {
const isAdmin = req.user.administrator
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)
if (!foundWorkItem) {
return next(
createError.NotFound(`WorkItem with _id ${_id} was not found`)
)
}
foundWorkItem.merge(workItemUpdates)
const savedWorkItem = await foundWorkItem.save()
res.json(savedWorkItem.toClient())
} catch (err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
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)
if (!foundWorkItem) {
return next(
createError.NotFound(`WorkItem with _id ${_id} was not found`)
)
}
foundWorkItem.merge(workItemUpdates)
const savedWorkItem = await foundWorkItem.save()
res.json(savedWorkItem.toClient())
}
async getWorkItem(req, res, next) {
try {
const WorkItem = this.db.WorkItem
const _id = req.params._id
const workItem = await WorkItem.findById(_id)
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())
} catch (err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
if (!workItem) {
throw createError.NotFound(`WorkItem with _id ${_id} not found`)
}
res.json(workItem.toClient())
}
async deleteWorkItem(req, res, next) {
try {
const isAdmin = req.user.administrator
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({})
} catch (err) {
if (err instanceof createError.HttpError) {
next(err)
} else {
next(createError.InternalServerError(err.message))
}
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({})
}
}