49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
import passport from "passport"
|
|
import createError from "http-errors"
|
|
import autobind from "autobind-decorator"
|
|
import zlib from "zlib"
|
|
import { Readable } from "stream"
|
|
import { catchAll, BaseRoutes } from "."
|
|
|
|
@autobind
|
|
export class TeamRoutes extends BaseRoutes {
|
|
constructor(container) {
|
|
super(container, container.db.Team)
|
|
|
|
container.app
|
|
.route("/teams/status")
|
|
.get(
|
|
passport.authenticate("bearer", { session: false }),
|
|
catchAll(this.getTeamStatus)
|
|
)
|
|
}
|
|
|
|
async getTeamStatus(req, res, next) {
|
|
const Team = this.db.Team
|
|
const Activity = this.db.Activity
|
|
let teams = await Team.find({}).exec()
|
|
|
|
teams = teams.map((doc) => doc.toObject({ versionKey: false }))
|
|
|
|
for (let team of teams) {
|
|
let activities = await Activity.find({ team: team._id }).exec()
|
|
|
|
team.activities = activities.map((doc) =>
|
|
doc.toObject({ versionKey: false })
|
|
)
|
|
}
|
|
|
|
const gzip = zlib.createGzip()
|
|
let readable = new Readable()
|
|
|
|
readable.push(JSON.stringify(teams, null, " "))
|
|
readable.push(null)
|
|
|
|
res.writeHead(200, {
|
|
"Content-Type": "application/gzip",
|
|
"Content-Disposition": 'attachment; filename="team-scores.json.gzip"',
|
|
})
|
|
readable.pipe(gzip).pipe(res)
|
|
}
|
|
}
|