aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/controllers/api/jobs.ts
blob: de37dea39f0be43eb5f63fe6776fadb30df890f8 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                  
                                                        
                                                         



                                                                                             

                                                                  







                                            
                 
                       











                                                                                                   
                                                                                                


                                                                         
import * as express from 'express'
import { UserRight } from '../../../shared/models/users'
import { getFormattedObjects } from '../../helpers/utils'
import {
  asyncMiddleware, authenticate, ensureUserHasRight, jobsSortValidator, setDefaultPagination,
  setDefaultSort
} from '../../middlewares'
import { paginationValidator } from '../../middlewares/validators'
import { JobModel } from '../../models/job/job'

const jobsRouter = express.Router()

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

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

export {
  jobsRouter
}

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

async function listJobs (req: express.Request, res: express.Response, next: express.NextFunction) {
  const resultList = await JobModel.listForApi(req.query.start, req.query.count, req.query.sort)

  return res.json(getFormattedObjects(resultList.data, resultList.total))
}