Integrated master/detail, refactor Icon, add base router
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user