Integrated master/detail, refactor Icon, add base router

This commit is contained in:
John Lyon-Smith
2018-05-12 12:36:39 -07:00
parent 84babf0e4b
commit 6fae5ef5d6
61 changed files with 1203 additions and 1620 deletions

View File

@@ -1,44 +1,15 @@
import passport from "passport"
import createError from "http-errors"
import autobind from "autobind-decorator"
import { catchAll, TeamRoutes } from "."
import { catchAll, TeamRoutes, BaseRoutes } from "."
@autobind
export class ActivityRoutes {
export class ActivityRoutes extends BaseRoutes {
constructor(container) {
super(container, container.db.Activity)
const app = container.app
this.log = container.log
this.db = container.db
this.mq = container.mq
this.ws = container.ws
app
.route("/activities")
.get(
passport.authenticate("bearer", { session: false }),
catchAll(this.listActivities)
)
.post(
passport.authenticate("bearer", { session: false }),
catchAll(this.createActivity)
)
.put(
passport.authenticate("bearer", { session: false }),
catchAll(this.updateActivity)
)
app
.route("/activities/:_id([a-f0-9]{24})")
.get(
passport.authenticate("bearer", { session: false }),
catchAll(this.getActivity)
)
.delete(
passport.authenticate("bearer", { session: false }),
catchAll(this.deleteActivity)
)
app
.route("/activities/all")
.delete(
@@ -47,117 +18,6 @@ export class ActivityRoutes {
)
}
async listActivities(req, res, next) {
const Activity = this.db.Activity
const limit = req.query.limit || 20
const skip = req.query.skip || 0
const partial = !!req.query.partial
let query = {}
const total = await Activity.count({})
let Activities = []
let cursor = Activity.find(query)
.limit(limit)
.skip(skip)
.cursor()
.map((doc) => {
return doc.toClient(partial)
})
cursor.on("data", (doc) => {
Activities.push(doc)
})
cursor.on("end", () => {
res.json({
total: total,
offset: skip,
count: activities.length,
items: activities,
})
})
cursor.on("error", (err) => {
throw createError.InternalServerError(err.message)
})
}
async createActivity(req, res, next) {
const isAdmin = req.user.administrator
if (!isAdmin) {
return new createError.Forbidden()
}
// Create a new Activity template then assign it to a value in the req.body
const Activity = this.db.Activity
let activity = new Activity(req.body)
// Save the activity (with promise) - If it doesnt, catch and throw error
const newActivity = await activity.save()
res.json(newActivity.toClient())
}
async updateActivity(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 Activity = this.db.Activity
let activity = await Activity.findById(req.body._id)
if (!activity) {
return next(
createError.NotFound(`Activity with _id ${req.body_id} was not found`)
)
}
let activityUpdates = new Activity(req.body)
// Strip off all BSON types
activity.merge(activityUpdates)
const savedActivity = await activity.save()
res.json(savedActivity.toClient())
}
async getActivity(req, res, next) {
const Activity = this.db.Activity
const _id = req.params._id
const activity = await Activity.findById(_id)
if (!activity) {
throw createError.NotFound(`Activity with _id ${_id} not found`)
}
res.json(activity.toClient())
}
async deleteActivity(req, res, next) {
const isAdmin = req.user.administrator
if (!isAdmin) {
return new createError.Forbidden()
}
const Activity = this.db.Activity
const _id = req.params._id
const activity = await Activity.remove({ _id })
if (!activity) {
throw createError.NotFound(`Activity with _id ${_id} not found`)
}
res.json({})
}
async deleteAllActivities(req, res, next) {
const Activity = this.db.Activity
const Team = this.db.Team

View File

@@ -11,19 +11,19 @@ import B64 from "b64"
import { PassThrough } from "stream"
import { catchAll } from "."
function pipeToGridFS(readable, gfsWriteable, decoder) {
function pipeToGridFS(readable, writable, decoder) {
const promise = new Promise((resolve, reject) => {
readable.on("error", (error) => {
reject(error)
})
gfsWriteable.on("error", (error) => {
writeable.on("error", (error) => {
reject(error)
})
gfsWriteable.on("close", (file) => {
writeable.on("finish", (file) => {
resolve(file)
})
})
readable.pipe(decoder).pipe(gfsWriteable)
readable.pipe(decoder).pipe(writeable)
return promise
}
@@ -73,12 +73,13 @@ export class AssetRoutes {
assetId = assetId.slice(0, extIndex)
}
const file = await this.db.gridfs.findOneAsync({ _id: assetId })
const cursor = await this.db.gridfs.findOne({ _id: assetId })
if (!file) {
if (!cursor) {
throw createError.NotFound(`Asset ${assetId} was not found`)
}
const file = cursor.next()
const ifNoneMatch = req.get("If-None-Match")
if (ifNoneMatch && ifNoneMatch === file.md5) {
@@ -98,13 +99,13 @@ export class AssetRoutes {
ETag: file.md5,
})
this.db.gridfs.createReadStream({ _id: file._id }).pipe(res)
this.db.gridfs.openDownloadStream(file._id).pipe(res)
}
async deleteAsset(req, res, next) {
const assetId = req.params._id
await this.db.gridfs.removeAsync({ _id: assetId })
await this.db.gridfs.delete(assetId)
res.json({})
}
@@ -235,11 +236,11 @@ export class AssetRoutes {
if (uploadedChunks >= uploadData.numberOfChunks) {
let readable = redisReadStream(this.rs.client, uploadDataId)
let writeable = this.db.gridfs.createWriteStream({
_id: uploadId,
filename: uploadData.fileName,
content_type: uploadData.contentType,
})
let writeable = this.db.gridfs.openUploadStreamWithId(
uploadId,
uploadData.fileName,
{ contentType: uploadData.contentType }
)
const decoder =
uploadData.chunkContentType === "application/base64"

View File

@@ -0,0 +1,147 @@
import passport from "passport"
import createError from "http-errors"
import autobind from "autobind-decorator"
import { catchAll } from "."
@autobind
export class BaseRoutes {
constructor(container, model) {
this.model = model
this.log = container.log
this.db = container.db
const basePath = "/" + model.collection.collectionName
const app = container.app
app
.route(basePath)
.get(
passport.authenticate("bearer", { session: false }),
catchAll(this.listItems)
)
.post(
passport.authenticate("bearer", { session: false }),
catchAll(this.createItem)
)
.put(
passport.authenticate("bearer", { session: false }),
catchAll(this.updateItem)
)
app
.route(basePath + "/:_id([a-f0-9]{24})")
.get(
passport.authenticate("bearer", { session: false }),
catchAll(this.getItem)
)
.delete(
passport.authenticate("bearer", { session: false }),
catchAll(this.deleteItem)
)
}
async listItems(req, res, next) {
const ItemModel = this.model
const limit = req.query.limit || 20
const skip = req.query.skip || 0
const partial = !!req.query.partial
let query = {}
const total = await ItemModel.count({})
let items = []
let cursor = ItemModel.find(query)
.limit(limit)
.skip(skip)
.cursor()
.map((doc) => {
return doc.toClient(partial)
})
cursor.on("data", (doc) => {
items.push(doc)
})
cursor.on("end", () => {
res.json({
total: total,
offset: skip,
count: items.length,
items: items,
})
})
cursor.on("error", (err) => {
throw createError.InternalServerError(err.message)
})
}
async createItem(req, res, next) {
const isAdmin = req.user.administrator
if (!isAdmin) {
return new createError.Forbidden()
}
const ItemModel = this.model
let item = new ItemModel(req.body)
const newItem = await item.save()
res.json(newItem.toClient())
}
async updateItem(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 ItemModel = this.model
let item = await ItemModel.findById(req.body._id)
if (!item) {
return next(createError.NotFound(`Item with _id ${_id} was not found`))
}
item.merge(new ItemModel(req.body))
const savedItem = await item.save()
res.json(savedItem.toClient())
}
async getItem(req, res, next) {
const ItemModel = this.model
const _id = req.params._id
const item = await ItemModel.findById(_id)
if (!item) {
throw createError.NotFound(`Item with _id ${_id} not found`)
}
res.json(item.toClient())
}
async deleteItem(req, res, next) {
const isAdmin = req.user.administrator
if (!isAdmin) {
return new createError.Forbidden()
}
const ItemModel = this.model
const _id = req.params._id
const item = await ItemModel.remove({ _id })
if (!item) {
throw createError.NotFound(`Item with _id ${_id} not found`)
}
res.json({})
}
}

View File

@@ -3,45 +3,14 @@ import createError from "http-errors"
import autobind from "autobind-decorator"
import zlib from "zlib"
import { Readable } from "stream"
import { catchAll } from "."
import { catchAll, BaseRoutes } from "."
@autobind
export class TeamRoutes {
export class TeamRoutes extends BaseRoutes {
constructor(container) {
const app = container.app
super(container, container.db.Team)
this.log = container.log
this.db = container.db
this.mq = container.mq
this.ws = container.ws
app
.route("/teams")
.get(
passport.authenticate("bearer", { session: false }),
catchAll(this.listTeams)
)
.post(
passport.authenticate("bearer", { session: false }),
catchAll(this.createTeam)
)
.put(
passport.authenticate("bearer", { session: false }),
catchAll(this.updateTeam)
)
app
.route("/teams/:_id([a-f0-9]{24})")
.get(
passport.authenticate("bearer", { session: false }),
catchAll(this.getTeam)
)
.delete(
passport.authenticate("bearer", { session: false }),
catchAll(this.deleteTeam)
)
app
container.app
.route("/teams/status")
.get(
passport.authenticate("bearer", { session: false }),
@@ -49,109 +18,6 @@ export class TeamRoutes {
)
}
async listTeams(req, res, next) {
const Team = this.db.Team
let limit = req.query.limit || 20
let skip = req.query.skip || 0
let partial = !!req.query.partial
let query = {}
const total = await Team.count({})
let teams = []
let cursor = Team.find(query)
.limit(limit)
.skip(skip)
.cursor()
.map((doc) => {
return doc.toClient(partial)
})
cursor.on("data", (doc) => {
teams.push(doc)
})
cursor.on("end", () => {
res.json({
total: total,
offset: skip,
count: teams.length,
items: teams,
})
})
cursor.on("error", (err) => {
throw err
})
}
async createTeam(req, res, next) {
if (!req.user.administrator) {
throw createError.Forbidden()
}
// Create a new Team template then assign it to a value in the req.body
const Team = this.db.Team
let team = new Team(req.body)
const newTeam = await team.save()
res.json(newTeam.toClient())
}
async updateTeam(req, res, next) {
if (!req.user.administrator) {
throw 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 Team = this.db.Team
let team = await Team.findById(req.body._id)
if (!team) {
throw createError.NotFound(`Team with _id ${req.body_id} was not found`)
}
let teamUpdates = new Team(req.body)
team.merge(teamUpdates)
const savedTeam = await team.save()
res.json(savedTeam.toClient())
}
async getTeam(req, res, next) {
const Team = this.db.Team
const _id = req.params._id
const team = await Team.findById(_id)
if (!team) {
throw createError.NotFound(`Team with _id ${_id} not found`)
}
res.json(team.toClient())
}
async deleteTeam(req, res, next) {
if (!req.user.administrator) {
throw createError.Forbidden()
}
const Team = this.db.Team
const _id = req.params._id
const removedTeam = await Team.remove({ _id })
if (!removedTeam) {
throw createError.NotFound(`Team with _id ${_id} not found`)
}
res.json({})
}
async getTeamStatus(req, res, next) {
const Team = this.db.Team
const Activity = this.db.Activity

View File

@@ -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

View File

@@ -1,3 +1,4 @@
export { BaseRoutes } from "./BaseRoutes"
export { AuthRoutes } from "./AuthRoutes"
export { AssetRoutes } from "./AssetRoutes"
export { UserRoutes } from "./UserRoutes"

View File

@@ -1,6 +1,6 @@
import mongoose from "mongoose"
import mongodb from "mongodb"
import Grid from "gridfs-stream"
import { GridFSBucket } from "mongodb"
import mergePlugin from "mongoose-doc-merge"
import autobind from "autobind-decorator"
import * as Schemas from "./schemas"
@@ -18,9 +18,7 @@ export class DB {
autoIndex: !isProduction,
})
this.gridfs = Grid(connection.db, mongoose.mongo)
this.gridfs.findOneAsync = util.promisify(this.gridfs.findOne)
this.gridfs.removeAsync = util.promisify(this.gridfs.remove)
this.gridfs = new GridFSBucket(connection.db)
this.User = connection.model("User", Schemas.userSchema)
this.WorkItem = connection.model("WorkItem", Schemas.workItemSchema)