2018-01-03 16:38:50 +01:00
|
|
|
import * as express from 'express'
|
|
|
|
import { getFormattedObjects } from '../../helpers/utils'
|
2018-01-18 10:53:54 +01:00
|
|
|
import { asyncMiddleware, paginationValidator, setDefaultSort, setDefaultPagination } from '../../middlewares'
|
2018-01-03 16:38:50 +01:00
|
|
|
import { accountsGetValidator, accountsSortValidator } from '../../middlewares/validators'
|
|
|
|
import { AccountModel } from '../../models/account/account'
|
|
|
|
|
|
|
|
const accountsRouter = express.Router()
|
|
|
|
|
|
|
|
accountsRouter.get('/',
|
|
|
|
paginationValidator,
|
|
|
|
accountsSortValidator,
|
2018-01-17 10:50:33 +01:00
|
|
|
setDefaultSort,
|
2018-01-18 10:53:54 +01:00
|
|
|
setDefaultPagination,
|
2018-01-03 16:38:50 +01:00
|
|
|
asyncMiddleware(listAccounts)
|
|
|
|
)
|
|
|
|
|
|
|
|
accountsRouter.get('/:id',
|
|
|
|
asyncMiddleware(accountsGetValidator),
|
|
|
|
getAccount
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
accountsRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
return res.json(res.locals.account.toFormattedJSON())
|
|
|
|
}
|
|
|
|
|
|
|
|
async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
|
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|