aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/runners/registration-token.ts
blob: cc31d4a7e34129757dc3aae8d5086eb7b9103c24 (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
import express from 'express'
import { param } from 'express-validator'
import { isIdValid } from '@server/helpers/custom-validators/misc'
import { RunnerRegistrationTokenModel } from '@server/models/runner/runner-registration-token'
import { forceNumber } from '@shared/core-utils'
import { HttpStatusCode } from '@shared/models'
import { areValidationErrors } from '../shared/utils'

const tags = [ 'runner' ]

const deleteRegistrationTokenValidator = [
  param('id').custom(isIdValid),

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

    const registrationToken = await RunnerRegistrationTokenModel.load(forceNumber(req.params.id))

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

    res.locals.runnerRegistrationToken = registrationToken

    return next()
  }
]

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

export {
  deleteRegistrationTokenValidator
}