aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/jobs.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/jobs.ts')
-rw-r--r--server/controllers/api/jobs.ts38
1 files changed, 32 insertions, 6 deletions
diff --git a/server/controllers/api/jobs.ts b/server/controllers/api/jobs.ts
index de37dea39..132d110ad 100644
--- a/server/controllers/api/jobs.ts
+++ b/server/controllers/api/jobs.ts
@@ -1,22 +1,29 @@
1import * as express from 'express' 1import * as express from 'express'
2import { ResultList } from '../../../shared'
3import { Job, JobType, JobState } from '../../../shared/models'
2import { UserRight } from '../../../shared/models/users' 4import { UserRight } from '../../../shared/models/users'
3import { getFormattedObjects } from '../../helpers/utils' 5import { JobQueue } from '../../lib/job-queue'
4import { 6import {
5 asyncMiddleware, authenticate, ensureUserHasRight, jobsSortValidator, setDefaultPagination, 7 asyncMiddleware,
8 authenticate,
9 ensureUserHasRight,
10 jobsSortValidator,
11 setDefaultPagination,
6 setDefaultSort 12 setDefaultSort
7} from '../../middlewares' 13} from '../../middlewares'
8import { paginationValidator } from '../../middlewares/validators' 14import { paginationValidator } from '../../middlewares/validators'
9import { JobModel } from '../../models/job/job' 15import { listJobsValidator } from '../../middlewares/validators/jobs'
10 16
11const jobsRouter = express.Router() 17const jobsRouter = express.Router()
12 18
13jobsRouter.get('/', 19jobsRouter.get('/:state',
14 authenticate, 20 authenticate,
15 ensureUserHasRight(UserRight.MANAGE_JOBS), 21 ensureUserHasRight(UserRight.MANAGE_JOBS),
16 paginationValidator, 22 paginationValidator,
17 jobsSortValidator, 23 jobsSortValidator,
18 setDefaultSort, 24 setDefaultSort,
19 setDefaultPagination, 25 setDefaultPagination,
26 asyncMiddleware(listJobsValidator),
20 asyncMiddleware(listJobs) 27 asyncMiddleware(listJobs)
21) 28)
22 29
@@ -29,7 +36,26 @@ export {
29// --------------------------------------------------------------------------- 36// ---------------------------------------------------------------------------
30 37
31async function listJobs (req: express.Request, res: express.Response, next: express.NextFunction) { 38async function listJobs (req: express.Request, res: express.Response, next: express.NextFunction) {
32 const resultList = await JobModel.listForApi(req.query.start, req.query.count, req.query.sort) 39 const sort = req.query.sort === 'createdAt' ? 'asc' : 'desc'
40
41 const jobs = await JobQueue.Instance.listForApi(req.params.state, req.query.start, req.query.count, sort)
42 const total = await JobQueue.Instance.count(req.params.state)
43
44 const result: ResultList<any> = {
45 total,
46 data: jobs.map(j => formatJob(j.toJSON()))
47 }
48 return res.json(result)
49}
33 50
34 return res.json(getFormattedObjects(resultList.data, resultList.total)) 51function formatJob (job: any): Job {
52 return {
53 id: job.id,
54 state: job.state as JobState,
55 type: job.type as JobType,
56 data: job.data,
57 error: job.error,
58 createdAt: new Date(parseInt(job.created_at, 10)),
59 updatedAt: new Date(parseInt(job.updated_at, 10))
60 }
35} 61}