1 import express from 'express'
2 import { body, param } from 'express-validator'
3 import { isIdValid } from '@server/helpers/custom-validators/misc'
5 isRunnerDescriptionValid,
7 isRunnerRegistrationTokenValid,
9 } from '@server/helpers/custom-validators/runners/runners'
10 import { RunnerModel } from '@server/models/runner/runner'
11 import { RunnerRegistrationTokenModel } from '@server/models/runner/runner-registration-token'
12 import { forceNumber } from '@shared/core-utils'
13 import { HttpStatusCode, RegisterRunnerBody, ServerErrorCode } from '@shared/models'
14 import { areValidationErrors } from '../shared/utils'
16 const tags = [ 'runner' ]
18 const registerRunnerValidator = [
19 body('registrationToken').custom(isRunnerRegistrationTokenValid),
20 body('name').custom(isRunnerNameValid),
21 body('description').optional().custom(isRunnerDescriptionValid),
23 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
24 if (areValidationErrors(req, res, { tags })) return
26 const body: RegisterRunnerBody = req.body
28 const runnerRegistrationToken = await RunnerRegistrationTokenModel.loadByRegistrationToken(body.registrationToken)
30 if (!runnerRegistrationToken) {
32 status: HttpStatusCode.NOT_FOUND_404,
33 message: 'Registration token is invalid',
38 res.locals.runnerRegistrationToken = runnerRegistrationToken
44 const deleteRunnerValidator = [
45 param('runnerId').custom(isIdValid),
47 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
48 if (areValidationErrors(req, res, { tags })) return
50 const runner = await RunnerModel.load(forceNumber(req.params.runnerId))
54 status: HttpStatusCode.NOT_FOUND_404,
55 message: 'Runner not found',
60 res.locals.runner = runner
66 const getRunnerFromTokenValidator = [
67 body('runnerToken').custom(isRunnerTokenValid),
69 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
70 if (areValidationErrors(req, res, { tags })) return
72 const runner = await RunnerModel.loadByToken(req.body.runnerToken)
76 status: HttpStatusCode.NOT_FOUND_404,
77 message: 'Unknown runner token',
78 type: ServerErrorCode.UNKNOWN_RUNNER_TOKEN,
83 res.locals.runner = runner
89 // ---------------------------------------------------------------------------
92 registerRunnerValidator,
93 deleteRunnerValidator,
94 getRunnerFromTokenValidator