aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/remote/pods.ts
blob: f917b61ee451cb1253c749d5c4bec6f3ddedd8bd (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
import { body } from 'express-validator/check'
import * as express from 'express'

import { database as db } from '../../../initializers'
import { isHostValid, logger } from '../../../helpers'
import { checkErrors } from '../utils'

const remotePodsAddValidator = [
  body('host').custom(isHostValid).withMessage('Should have a host'),
  body('email').isEmail().withMessage('Should have an email'),
  body('publicKey').not().isEmpty().withMessage('Should have a public key'),

  (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking podsAdd parameters', { parameters: req.body })

    checkErrors(req, res, () => {
      db.Pod.loadByHost(req.body.host)
        .then(pod => {
          // Pod with this host already exists
          if (pod) {
            return res.sendStatus(409)
          }

          return next()
        })
        .catch(err => {
          logger.error('Cannot load pod by host.', err)
          res.sendStatus(500)
        })
    })
  }
]

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

export {
  remotePodsAddValidator
}