Hook up team dropdowns and lists
This commit is contained in:
@@ -22,19 +22,15 @@ export class TeamRoutes {
|
||||
.delete(passport.authenticate('bearer', { session: false }), this.deleteTeam)
|
||||
}
|
||||
|
||||
listTeams(req, res, next) {
|
||||
async listTeams(req, res, next) {
|
||||
const Team = this.db.Team
|
||||
let limit = req.params.limit || 20
|
||||
let skip = req.params.skip || 0
|
||||
let partial = !!req.params.partial
|
||||
let branch = req.params.branch
|
||||
let limit = req.query.limit || 20
|
||||
let skip = req.query.skip || 0
|
||||
let partial = !!req.query.partial
|
||||
let query = {}
|
||||
|
||||
if (branch) {
|
||||
query.branch = branch
|
||||
}
|
||||
|
||||
Team.count({}).then((total) => {
|
||||
try {
|
||||
const total = await Team.count({})
|
||||
let teams = []
|
||||
let cursor = Team.find(query).limit(limit).skip(skip).cursor().map((doc) => {
|
||||
return doc.toClient(partial)
|
||||
@@ -52,93 +48,120 @@ export class TeamRoutes {
|
||||
})
|
||||
})
|
||||
cursor.on('error', (err) => {
|
||||
next(createError.InternalServerError(err.message))
|
||||
throw err
|
||||
})
|
||||
}).catch((err) => {
|
||||
next(createError.InternalServerError(err.message))
|
||||
})
|
||||
} catch(err) {
|
||||
if (err instanceof createError.HttpError) {
|
||||
next(err)
|
||||
} else {
|
||||
next(createError.InternalServerError(err.message))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
createTeam(req, res, next) {
|
||||
if (!req.user.administrator) {
|
||||
return next(new 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)
|
||||
|
||||
// Save the team (with promise) - If it doesnt, catch and throw error
|
||||
team.save().then((newTeam) => {
|
||||
res.json(newTeam.toClient())
|
||||
}).catch((err) => {
|
||||
next(createError.InternalServerError(err.message))
|
||||
})
|
||||
}
|
||||
|
||||
updateTeam(req, res, next) {
|
||||
if (!req.user.administrator) {
|
||||
return next(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 Team = this.db.Team
|
||||
let teamUpdates = null
|
||||
|
||||
async createTeam(req, res, next) {
|
||||
try {
|
||||
teamUpdates = new Team(req.body)
|
||||
} catch (err) {
|
||||
return next(createError.BadRequest('Invalid data'))
|
||||
}
|
||||
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())
|
||||
} catch(err) {
|
||||
if (err instanceof createError.HttpError) {
|
||||
next(err)
|
||||
} else {
|
||||
next(createError.InternalServerError(err.message))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async updateTeam(req, res, next) {
|
||||
try {
|
||||
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 teamUpdates = null
|
||||
|
||||
try {
|
||||
teamUpdates = new Team(req.body)
|
||||
} catch (err) {
|
||||
throw createError.BadRequest('Invalid data')
|
||||
}
|
||||
|
||||
const foundTeam = await Team.findById(teamUpdates._id)
|
||||
|
||||
Team.findById(teamUpdates._id).then((foundTeam) => {
|
||||
if (!foundTeam) {
|
||||
return next(createError.NotFound(`Team with _id ${_id} was not found`))
|
||||
throw createError.NotFound(`Team with _id ${_id} was not found`)
|
||||
}
|
||||
foundTeam.merge(teamUpdates)
|
||||
return foundTeam.save()
|
||||
}).then((savedTeam) => {
|
||||
const savedTeam = await foundTeam.save()
|
||||
|
||||
res.json(savedTeam.toClient())
|
||||
}).catch((err) => {
|
||||
next(createError.InternalServerError(err.message))
|
||||
})
|
||||
} catch (err) {
|
||||
if (err instanceof createError.HttpError) {
|
||||
next(err)
|
||||
} else {
|
||||
next(createError.InternalServerError(err.message))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getTeam(req, res, next) {
|
||||
async getTeam(req, res, next) {
|
||||
const Team = this.db.Team
|
||||
const _id = req.params._id
|
||||
|
||||
Team.findById(_id).then((team) => {
|
||||
try {
|
||||
const team = await Team.findById(_id)
|
||||
|
||||
if (!team) {
|
||||
return next(createError.NotFound(`Team with _id ${_id} not found`))
|
||||
throw createError.NotFound(`Team with _id ${_id} not found`)
|
||||
}
|
||||
|
||||
res.json(team.toClient())
|
||||
}).catch((err) => {
|
||||
next(createError.InternalServerError(err.message))
|
||||
})
|
||||
} catch (err) {
|
||||
if (err instanceof createError.HttpError) {
|
||||
next(err)
|
||||
} else {
|
||||
next(createError.InternalServerError(err.message))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deleteTeam(req, res, next) {
|
||||
if (!req.user.administrator) {
|
||||
return next(new createError.Forbidden())
|
||||
}
|
||||
async deleteTeam(req, res, next) {
|
||||
try {
|
||||
if (!req.user.administrator) {
|
||||
throw createError.Forbidden()
|
||||
}
|
||||
|
||||
const Team = this.db.Team
|
||||
const _id = req.params._id
|
||||
const Team = this.db.Team
|
||||
const _id = req.params._id
|
||||
|
||||
Team.remove({ _id }).then((team) => {
|
||||
if (!project) {
|
||||
return next(createError.NotFound(`Team with _id ${_id} not found`))
|
||||
const removedTeam = await Team.remove({ _id })
|
||||
|
||||
if (!removedTeam) {
|
||||
throw createError.NotFound(`Team 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user