1 import * as express from 'express'
2 import { param } from 'express-validator/check'
7 isAccountNameWithHostExist,
8 isLocalAccountNameExist
9 } from '../../helpers/custom-validators/accounts'
10 import { logger } from '../../helpers/logger'
11 import { areValidationErrors } from './utils'
12 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
14 const localAccountValidator = [
15 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
17 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
18 logger.debug('Checking localAccountValidator parameters', { parameters: req.params })
20 if (areValidationErrors(req, res)) return
21 if (!await isLocalAccountNameExist(req.params.name, res)) return
27 const accountsGetValidator = [
28 param('id').custom(isAccountIdValid).withMessage('Should have a valid id/uuid/name/name with host'),
30 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
31 logger.debug('Checking accountsGetValidator parameters', { parameters: req.params })
33 if (areValidationErrors(req, res)) return
35 let accountFetched = false
36 if (isIdOrUUIDValid(req.params.id)) accountFetched = await isAccountIdExist(req.params.id, res, false)
37 if (!accountFetched) accountFetched = await isAccountNameWithHostExist(req.params.id, res, true)
39 if (!accountFetched) return
45 const accountsNameWithHostGetValidator = [
46 param('nameWithHost').exists().withMessage('Should have an account name with host'),
48 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
49 logger.debug('Checking accountsNameWithHostGetValidator parameters', { parameters: req.params })
51 if (areValidationErrors(req, res)) return
52 if (!await isAccountNameWithHostExist(req.params.nameWithHost, res)) return
58 // ---------------------------------------------------------------------------
61 localAccountValidator,
63 accountsNameWithHostGetValidator