diff options
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/jobs.ts | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/server/controllers/api/jobs.ts b/server/controllers/api/jobs.ts index ed6c94533..1131a44d6 100644 --- a/server/controllers/api/jobs.ts +++ b/server/controllers/api/jobs.ts | |||
@@ -12,11 +12,23 @@ import { | |||
12 | setDefaultSort | 12 | setDefaultSort |
13 | } from '../../middlewares' | 13 | } from '../../middlewares' |
14 | import { paginationValidator } from '../../middlewares/validators' | 14 | import { paginationValidator } from '../../middlewares/validators' |
15 | import { listJobsValidator } from '../../middlewares/validators/jobs' | 15 | import { listJobsStateValidator, listJobsValidator } from '../../middlewares/validators/jobs' |
16 | import { isArray } from '../../helpers/custom-validators/misc' | 16 | import { isArray } from '../../helpers/custom-validators/misc' |
17 | import { jobStates } from '@server/helpers/custom-validators/jobs' | ||
17 | 18 | ||
18 | const jobsRouter = express.Router() | 19 | const jobsRouter = express.Router() |
19 | 20 | ||
21 | jobsRouter.get('/', | ||
22 | authenticate, | ||
23 | ensureUserHasRight(UserRight.MANAGE_JOBS), | ||
24 | paginationValidator, | ||
25 | jobsSortValidator, | ||
26 | setDefaultSort, | ||
27 | setDefaultPagination, | ||
28 | listJobsValidator, | ||
29 | asyncMiddleware(listJobs) | ||
30 | ) | ||
31 | |||
20 | jobsRouter.get('/:state', | 32 | jobsRouter.get('/:state', |
21 | authenticate, | 33 | authenticate, |
22 | ensureUserHasRight(UserRight.MANAGE_JOBS), | 34 | ensureUserHasRight(UserRight.MANAGE_JOBS), |
@@ -25,6 +37,7 @@ jobsRouter.get('/:state', | |||
25 | setDefaultSort, | 37 | setDefaultSort, |
26 | setDefaultPagination, | 38 | setDefaultPagination, |
27 | listJobsValidator, | 39 | listJobsValidator, |
40 | listJobsStateValidator, | ||
28 | asyncMiddleware(listJobs) | 41 | asyncMiddleware(listJobs) |
29 | ) | 42 | ) |
30 | 43 | ||
@@ -37,7 +50,7 @@ export { | |||
37 | // --------------------------------------------------------------------------- | 50 | // --------------------------------------------------------------------------- |
38 | 51 | ||
39 | async function listJobs (req: express.Request, res: express.Response) { | 52 | async function listJobs (req: express.Request, res: express.Response) { |
40 | const state = req.params.state as JobState | 53 | const state = req.params.state as JobState || jobStates |
41 | const asc = req.query.sort === 'createdAt' | 54 | const asc = req.query.sort === 'createdAt' |
42 | const jobType = req.query.jobType | 55 | const jobType = req.query.jobType |
43 | 56 | ||
@@ -52,7 +65,11 @@ async function listJobs (req: express.Request, res: express.Response) { | |||
52 | 65 | ||
53 | const result: ResultList<Job> = { | 66 | const result: ResultList<Job> = { |
54 | total, | 67 | total, |
55 | data: jobs.map(j => formatJob(j, state)) | 68 | data: Array.isArray(state) |
69 | ? await Promise.all( | ||
70 | jobs.map(async j => formatJob(j, await j.getState() as JobState)) | ||
71 | ) | ||
72 | : jobs.map(j => formatJob(j, state)) | ||
56 | } | 73 | } |
57 | return res.json(result) | 74 | return res.json(result) |
58 | } | 75 | } |