2017-11-30 10:51:13 +01:00
|
|
|
import * as express from 'express'
|
2018-01-25 15:05:18 +01:00
|
|
|
import { ResultList } from '../../../shared'
|
|
|
|
import { Job, JobType, JobState } from '../../../shared/models'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { UserRight } from '../../../shared/models/users'
|
2018-01-25 15:05:18 +01:00
|
|
|
import { JobQueue } from '../../lib/job-queue'
|
2018-01-18 10:53:54 +01:00
|
|
|
import {
|
2018-01-25 15:05:18 +01:00
|
|
|
asyncMiddleware,
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight,
|
|
|
|
jobsSortValidator,
|
|
|
|
setDefaultPagination,
|
2018-01-18 10:53:54 +01:00
|
|
|
setDefaultSort
|
|
|
|
} from '../../middlewares'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { paginationValidator } from '../../middlewares/validators'
|
2018-01-25 15:05:18 +01:00
|
|
|
import { listJobsValidator } from '../../middlewares/validators/jobs'
|
2017-11-30 10:51:13 +01:00
|
|
|
|
|
|
|
const jobsRouter = express.Router()
|
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
jobsRouter.get('/:state',
|
2017-11-30 10:51:13 +01:00
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_JOBS),
|
|
|
|
paginationValidator,
|
|
|
|
jobsSortValidator,
|
2018-01-17 10:50:33 +01:00
|
|
|
setDefaultSort,
|
2018-01-18 10:53:54 +01:00
|
|
|
setDefaultPagination,
|
2018-01-25 15:05:18 +01:00
|
|
|
asyncMiddleware(listJobsValidator),
|
2017-11-30 10:51:13 +01:00
|
|
|
asyncMiddleware(listJobs)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
jobsRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function listJobs (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2018-02-27 16:57:53 +01:00
|
|
|
const sort = req.query.sort === 'createdAt' ? 'ASC' : 'DESC'
|
2018-01-25 15:05:18 +01:00
|
|
|
|
|
|
|
const jobs = await JobQueue.Instance.listForApi(req.params.state, req.query.start, req.query.count, sort)
|
|
|
|
const total = await JobQueue.Instance.count(req.params.state)
|
|
|
|
|
|
|
|
const result: ResultList<any> = {
|
|
|
|
total,
|
|
|
|
data: jobs.map(j => formatJob(j.toJSON()))
|
|
|
|
}
|
|
|
|
return res.json(result)
|
|
|
|
}
|
2017-11-30 10:51:13 +01:00
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
function formatJob (job: any): Job {
|
|
|
|
return {
|
|
|
|
id: job.id,
|
|
|
|
state: job.state as JobState,
|
|
|
|
type: job.type as JobType,
|
|
|
|
data: job.data,
|
|
|
|
error: job.error,
|
|
|
|
createdAt: new Date(parseInt(job.created_at, 10)),
|
|
|
|
updatedAt: new Date(parseInt(job.updated_at, 10))
|
|
|
|
}
|
2017-11-30 10:51:13 +01:00
|
|
|
}
|