aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/runners/runners.ts
blob: 71a1275d264233b40e1b14e643fc2d81e8fff07c (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import express from 'express'
import { body, param } from 'express-validator'
import { isIdValid } from '@server/helpers/custom-validators/misc'
import {
  isRunnerDescriptionValid,
  isRunnerNameValid,
  isRunnerRegistrationTokenValid,
  isRunnerTokenValid
} from '@server/helpers/custom-validators/runners/runners'
import { RunnerModel } from '@server/models/runner/runner'
import { RunnerRegistrationTokenModel } from '@server/models/runner/runner-registration-token'
import { forceNumber } from '@shared/core-utils'
import { HttpStatusCode, RegisterRunnerBody, ServerErrorCode } from '@shared/models'
import { areValidationErrors } from '../shared/utils'

const tags = [ 'runner' ]

const registerRunnerValidator = [
  body('registrationToken').custom(isRunnerRegistrationTokenValid),
  body('name').custom(isRunnerNameValid),
  body('description').optional().custom(isRunnerDescriptionValid),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (areValidationErrors(req, res, { tags })) return

    const body: RegisterRunnerBody = req.body

    const runnerRegistrationToken = await RunnerRegistrationTokenModel.loadByRegistrationToken(body.registrationToken)

    if (!runnerRegistrationToken) {
      return res.fail({
        status: HttpStatusCode.NOT_FOUND_404,
        message: 'Registration token is invalid',
        tags
      })
    }

    res.locals.runnerRegistrationToken = runnerRegistrationToken

    return next()
  }
]

const deleteRunnerValidator = [
  param('runnerId').custom(isIdValid),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (areValidationErrors(req, res, { tags })) return

    const runner = await RunnerModel.load(forceNumber(req.params.runnerId))

    if (!runner) {
      return res.fail({
        status: HttpStatusCode.NOT_FOUND_404,
        message: 'Runner not found',
        tags
      })
    }

    res.locals.runner = runner

    return next()
  }
]

const getRunnerFromTokenValidator = [
  body('runnerToken').custom(isRunnerTokenValid),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (areValidationErrors(req, res, { tags })) return

    const runner = await RunnerModel.loadByToken(req.body.runnerToken)

    if (!runner) {
      return res.fail({
        status: HttpStatusCode.NOT_FOUND_404,
        message: 'Unknown runner token',
        type: ServerErrorCode.UNKNOWN_RUNNER_TOKEN,
        tags
      })
    }

    res.locals.runner = runner

    return next()
  }
]

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

export {
  registerRunnerValidator,
  deleteRunnerValidator,
  getRunnerFromTokenValidator
}