aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/jobs.ts
blob: aa58a9144ea4472db750337219939cdb7f78b5ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as express from 'express'
import { ResultList } from '../../../shared'
import { Job, JobType, JobState } from '../../../shared/models'
import { UserRight } from '../../../shared/models/users'
import { JobQueue } from '../../lib/job-queue'
import {
  asyncMiddleware,
  authenticate,
  ensureUserHasRight,
  jobsSortValidator,
  setDefaultPagination,
  setDefaultSort
} from '../../middlewares'
import { paginationValidator } from '../../middlewares/validators'
import { listJobsValidator } from '../../middlewares/validators/jobs'

const jobsRouter = express.Router()

jobsRouter.get('/:state',
  authenticate,
  ensureUserHasRight(UserRight.MANAGE_JOBS),
  paginationValidator,
  jobsSortValidator,
  setDefaultSort,
  setDefaultPagination,
  asyncMiddleware(listJobsValidator),
  asyncMiddleware(listJobs)
)

// ---------------------------------------------------------------------------

export {
  jobsRouter
}

// ---------------------------------------------------------------------------

async function listJobs (req: express.Request, res: express.Response, next: express.NextFunction) {
  const sort = req.query.sort === 'createdAt' ? 'ASC' : 'DESC'

  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)
}

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))
  }
}