87 lines
2.1 KiB
JavaScript
87 lines
2.1 KiB
JavaScript
import passport from "passport"
|
|
import createError from "http-errors"
|
|
import autobind from "autobind-decorator"
|
|
import merge from "deepmerge"
|
|
import { catchAll, BaseRoutes } from "."
|
|
|
|
@autobind
|
|
export class WorkItemRoutes extends BaseRoutes {
|
|
constructor(container) {
|
|
super({
|
|
container,
|
|
model: container.db.WorkItem,
|
|
nonAdmin: { listItems: true, getItem: true },
|
|
})
|
|
const app = container.app
|
|
|
|
app
|
|
.route("/workitems/activities")
|
|
.get(
|
|
passport.authenticate("bearer", { session: false }),
|
|
catchAll(this.listWorkItemActivities)
|
|
)
|
|
|
|
app
|
|
.route("/workitems/all")
|
|
.delete(
|
|
passport.authenticate("bearer", { session: false }),
|
|
catchAll(this.deleteAllWorkItems)
|
|
)
|
|
}
|
|
|
|
async deleteItem(req, res, next) {
|
|
super.deleteItem(req, res, next)
|
|
|
|
const id = req.params._id
|
|
const Activity = this.db.Activity
|
|
|
|
await Activity.remove({ workItem: id })
|
|
}
|
|
|
|
async listWorkItemActivities(req, res, next) {
|
|
const WorkItem = this.db.WorkItem
|
|
const aggregate = WorkItem.aggregate()
|
|
.sort({ ticketNumber: 1 })
|
|
.lookup({
|
|
from: "activities",
|
|
localField: "_id",
|
|
foreignField: "workItem",
|
|
as: "data",
|
|
})
|
|
.project({
|
|
workItemType: 1,
|
|
ticketNumber: 1,
|
|
address: 1,
|
|
"coordinate.longitude": { $arrayElemAt: ["$location.coordinates", 0] },
|
|
"coordinate.latitude": { $arrayElemAt: ["$location.coordinates", 1] },
|
|
"data._id": 1,
|
|
"data.resolution": 1,
|
|
"data.status": 1,
|
|
"data.createdAt": 1,
|
|
"data.team": 1,
|
|
})
|
|
|
|
const items = await aggregate.exec()
|
|
|
|
res.json({ items })
|
|
}
|
|
|
|
async deleteAllWorkItems(req, res, next) {
|
|
const isAdmin = !!req.user.administrator
|
|
|
|
if (!isAdmin) {
|
|
throw createError.Forbidden()
|
|
}
|
|
|
|
const Activity = this.db.Activity
|
|
const WorkItem = this.db.WorkItem
|
|
const Team = this.db.Team
|
|
|
|
await Activity.remove({})
|
|
await Team.updateMany({}, { $set: { start: null } })
|
|
await WorkItem.remove({})
|
|
|
|
res.json({})
|
|
}
|
|
}
|