Add gzipped download endpoint

This commit is contained in:
John Lyon-Smith
2018-04-26 18:00:16 -07:00
parent 5e5daa2e78
commit 96e2599cdb
9 changed files with 390 additions and 164 deletions

View File

@@ -38,6 +38,13 @@ export class ActivityRoutes {
passport.authenticate("bearer", { session: false }),
catchAll(this.deleteActivity)
)
app
.route("/activities/all")
.delete(
passport.authenticate("bearer", { session: false }),
catchAll(this.deleteAllActivities)
)
}
async listActivities(req, res, next) {
@@ -156,4 +163,12 @@ export class ActivityRoutes {
res.json({})
}
async deleteAllActivities(req, res, next) {
const Activity = this.db.Activity
await Activity.remove({})
res.json({})
}
}

View File

@@ -1,6 +1,8 @@
import passport from "passport"
import createError from "http-errors"
import autobind from "autobind-decorator"
import zlib from "zlib"
import { Readable } from "stream"
import { catchAll } from "."
@autobind
@@ -38,6 +40,13 @@ export class TeamRoutes {
passport.authenticate("bearer", { session: false }),
catchAll(this.deleteTeam)
)
app
.route("/teams/status")
.get(
passport.authenticate("bearer", { session: false }),
catchAll(this.getTeamStatus)
)
}
async listTeams(req, res, next) {
@@ -146,4 +155,32 @@ export class TeamRoutes {
res.json({})
}
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": "text/html",
"Content-Encoding": "gzip",
})
readable.pipe(gzip).pipe(res)
}
}

View File

@@ -45,6 +45,13 @@ export class WorkItemRoutes {
passport.authenticate("bearer", { session: false }),
catchAll(this.deleteWorkItem)
)
app
.route("/workitems/all")
.delete(
passport.authenticate("bearer", { session: false }),
catchAll(this.deleteAllWorkItems)
)
}
async listWorkItems(req, res, next) {
@@ -189,4 +196,14 @@ export class WorkItemRoutes {
res.json({})
}
async deleteAllWorkItems(req, res, next) {
const Activity = this.db.Activity
const WorkItem = this.db.WorkItem
await Activity.remove({})
await WorkItem.remove({})
res.json({})
}
}

View File

@@ -16,7 +16,6 @@ export let activitySchema = new Schema(
required: true,
},
notes: { type: String, required: true },
when: { type: Date, required: true },
fromStreetNumber: Number,
toStreetNumber: Number,
photos: [Schema.Types.ObjectId],
@@ -25,5 +24,5 @@ export let activitySchema = new Schema(
)
activitySchema.methods.toClient = function() {
return this.toObject()
return this.toObject({ versionKey: false })
}

View File

@@ -1,9 +1,14 @@
import { Schema } from 'mongoose'
import { Schema } from "mongoose"
export let teamSchema = new Schema({
name: { type: String },
}, { timestamps: true, id: false })
export let teamSchema = new Schema(
{
name: { type: String },
start: { type: Date },
stop: { type: Date },
},
{ timestamps: true, id: false }
)
teamSchema.methods.toClient = function() {
return this.toObject()
return this.toObject({ versionKey: false })
}