diff options
Diffstat (limited to 'server/controllers/api/jobs.ts')
-rw-r--r-- | server/controllers/api/jobs.ts | 38 |
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 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { ResultList } from '../../../shared' | ||
3 | import { Job, JobType, JobState } from '../../../shared/models' | ||
2 | import { UserRight } from '../../../shared/models/users' | 4 | import { UserRight } from '../../../shared/models/users' |
3 | import { getFormattedObjects } from '../../helpers/utils' | 5 | import { JobQueue } from '../../lib/job-queue' |
4 | import { | 6 | import { |
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' |
8 | import { paginationValidator } from '../../middlewares/validators' | 14 | import { paginationValidator } from '../../middlewares/validators' |
9 | import { JobModel } from '../../models/job/job' | 15 | import { listJobsValidator } from '../../middlewares/validators/jobs' |
10 | 16 | ||
11 | const jobsRouter = express.Router() | 17 | const jobsRouter = express.Router() |
12 | 18 | ||
13 | jobsRouter.get('/', | 19 | jobsRouter.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 | ||
31 | async function listJobs (req: express.Request, res: express.Response, next: express.NextFunction) { | 38 | async 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)) | 51 | function 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 | } |